forked from dlclark/regexp2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinexp_test.go
223 lines (186 loc) · 5.56 KB
/
binexp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package binexp
import (
"github.com/polyverse/binexp/syntax"
"testing"
)
func TestRegexBinaryMatchBasic(t *testing.T) {
// Ensure this is a non-UTF-8 compliant string
opcode, err := Compile("\x65\xff\x15", None)
if err != nil {
t.Error(err)
}
data := string([]byte{0x65, 0xff, 0x15, 0xef, 0x65, 0x15, 0xcd, 0x50, 0x65, 0xff, 0x15, 0x25})
match, err := opcode.FindStringMatchStartingAt(data, 0)
if err != nil {
t.Error(err)
}
if match.Index != 0 {
t.Fatalf("First match expected at index 0. Instead found it at: %d", match.Index)
}
if match.Length != 3 {
t.Fatalf("First match length expected to be 3. Instead found it at: %d", match.Length)
}
match, err = opcode.FindStringMatchStartingAt(data, 1)
if err != nil {
t.Error(err)
}
if match.Index != 8 {
t.Fatalf("Second match (starting from 1) expected at index 8. Instead found it at: %d", match.Index)
}
if match.Length != 3 {
t.Fatalf("Second match (starting from 1) length expected to be 3. Instead found it at: %d", match.Length)
}
match, err = opcode.FindStringMatchStartingAt(data, 8)
if err != nil {
t.Error(err)
}
if match.Index != 8 {
t.Fatalf("Second match (starting from 8) expected at index 8. Instead found it at: %d", match.Index)
}
if match.Length != 3 {
t.Fatalf("First match (starting from 8) length expected to be 3. Instead found it at: %d", match.Length)
}
match, err = opcode.FindStringMatchStartingAt(data, 9)
if err != nil {
t.Error(err)
}
if match != nil {
t.Fatalf("No matches were expected after index 9. Found a match.")
}
}
func TestEnsureRegexStringMatchFail(t *testing.T) {
// Ensure this is a non-UTF-8 compliant string
opcode, err := Compile("\xca[\x00-\xff]{2}", None)
if err != nil {
t.Error(err)
}
rawdata := []byte{0x65, 0xca, 0x05, 0xf4, 0x65, 0xca, 0xaf, 0xca, 0x65, 0xff, 0x15, 0x25}
data := string(rawdata)
match, err := opcode.FindStringMatchStartingAt(data, 1)
if err != nil {
t.Error(err)
}
match, err = opcode.FindBytesMatchStartingAt(rawdata, 1)
if err != nil {
t.Error(err)
}
if match != nil {
t.Errorf("Did NOT expect a match")
}
}
func TestRegexByteMatchSuccess(t *testing.T) {
// Ensure this is a non-UTF-8 compliant string
opcode, err := Compile("\xca[\x00-\xff]{2}", syntax.ByteRunes)
if err != nil {
t.Error(err)
}
rawdata := []byte{0x65, 0xca, 0x05, 0xf4, 0x65, 0xca, 0xaf, 0xca, 0x65, 0xff, 0x15, 0x25}
data := string(rawdata)
match, err := opcode.FindStringMatchStartingAt(data, 1)
if err != nil {
t.Error(err)
}
if match != nil {
t.Errorf("Did NOT expect a match")
}
match, err = opcode.FindBytesMatchStartingAt(rawdata, 1)
if err != nil {
t.Error(err)
}
if match.Index != 1 {
t.Errorf("First match expected at index 1. Instead got %d.", match.Index)
}
if match.Length != 3 {
t.Errorf("First match expected length 3. Instead got %d.", match.Length)
}
match, err = opcode.FindBytesMatchStartingAt(rawdata, 2)
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
if match.Index != 5 {
t.Errorf("Second match expected at index 5. Instead got %d.", match.Index)
}
if match.Length != 3 {
t.Errorf("second match expected length 3. Instead got %d.", match.Length)
}
match, err = opcode.FindBytesMatchStartingAt(rawdata, 5)
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
if match.Index != 5 {
t.Errorf("Second match expected at index 5. Instead got %d.", match.Index)
}
if match.Length != 3 {
t.Errorf("second match expected length 3. Instead got %d.", match.Length)
}
match, err = opcode.FindBytesMatchStartingAt(rawdata, 6)
if err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
if match.Index != 7 {
t.Errorf("Third match expected at index 7. Instead got %d.", match.Index)
}
if match.Length != 3 {
t.Errorf("second match expected length 3. Instead got %d.", match.Length)
}
}
func TestRegexByteMatchNext(t *testing.T) {
// Ensure this is a non-UTF-8 compliant string
opcode, err := Compile("\xca[\x00-\xff]{2}", syntax.ByteRunes)
if err != nil {
t.Error(err)
}
matches := []int{}
rawdata := []byte{0x65, 0xca, 0x05, 0xf4, 0x65, 0xca, 0xaf, 0xca, 0x65, 0xff, 0x15, 0x25}
for match, err := opcode.FindBytesMatchStartingAt(rawdata, 0); match != nil; match, err = opcode.FindNextMatch(match) {
if err != nil {
t.Error(err)
}
matches = append(matches, match.Index)
}
if len(matches) != 2 {
t.Fatalf("Expected two non-overlapping matches, but found %d", len(matches))
}
if matches[0] != 1 {
t.Fatalf("First non-overlapping match should be at index 1, found %d", matches[0])
}
if matches[1] != 5 {
t.Fatalf("Second non-overlapping match should be at index 5, found %d", matches[1])
}
}
func TestRegexByteMatchNextOverlapping(t *testing.T) {
// Ensure this is a non-UTF-8 compliant string
opcode, err := Compile("\xca[\x00-\xff]{2}", syntax.ByteRunes)
if err != nil {
t.Error(err)
}
matches := []int{}
rawdata := []byte{0x65, 0xca, 0x05, 0xf4, 0x65, 0xca, 0xaf, 0xca, 0x65, 0xff, 0x15, 0x25}
for match, err := opcode.FindBytesMatchStartingAt(rawdata, 0); match != nil; match, err = opcode.FindNextOverlappingMatch(match) {
if err != nil {
t.Error(err)
}
matches = append(matches, match.Index)
}
if len(matches) != 3 {
t.Fatalf("Expected three overlapping matches, but found %d", len(matches))
}
if matches[0] != 1 {
t.Fatalf("First overlapping match should be at index 1, found %d", matches[0])
}
if matches[1] != 5 {
t.Fatalf("Second overlapping match should be at index 5, found %d", matches[1])
}
if matches[2] != 7 {
t.Fatalf("Third overlapping match should be at index 7, found %d", matches[2])
}
}