Debian Codegraph @

matching/cpe_name_matcher_test.go

raw
1 package matching
2
3 import (
4 "testing"
5
6 "github.com/knqyf263/go-cpe/common"
7 )
8
9 func TestMatcher(t *testing.T) {
10 any, _ := common.NewLogicalValue("ANY")
11 na, _ := common.NewLogicalValue("NA")
12 vectors := []struct {
13 wfn common.WellFormedName
14 wfn2 common.WellFormedName
15 expectedIsDisjoint bool
16 expectedIsEqual bool
17 expectedIsSubset bool
18 expectedIsSuperset bool
19 }{{
20 wfn: common.WellFormedName{
21 "part": "a",
22 "vendor": `microsoft`,
23 "product": "internet_explorer",
24 "version": `8\.0\.6001`,
25 "update": "beta",
26 "edition": any,
27 "sw_edition": any,
28 "target_sw": any,
29 "target_hw": any,
30 "other": any,
31 "language": "sp2",
32 },
33 wfn2: common.WellFormedName{
34 "part": "a",
35 "vendor": `microsoft`,
36 "product": "internet_explorer",
37 "version": any,
38 "update": "beta",
39 "edition": any,
40 "sw_edition": any,
41 "target_sw": any,
42 "target_hw": any,
43 "other": any,
44 "language": any,
45 },
46 expectedIsDisjoint: false,
47 expectedIsEqual: false,
48 expectedIsSubset: true,
49 expectedIsSuperset: false,
50 }, {
51 wfn: common.WellFormedName{
52 "part": "a",
53 "vendor": `adobe`,
54 "product": any,
55 "version": `9\.*`,
56 "update": any,
57 "edition": "PalmOS",
58 "sw_edition": any,
59 "target_sw": any,
60 "target_hw": any,
61 "other": any,
62 "language": any,
63 },
64 wfn2: common.WellFormedName{
65 "part": "a",
66 "vendor": any,
67 "product": "reader",
68 "version": `9\.3\.2`,
69 "update": na,
70 "edition": na,
71 "sw_edition": any,
72 "target_sw": any,
73 "target_hw": any,
74 "other": any,
75 "language": any,
76 },
77 expectedIsDisjoint: true,
78 expectedIsEqual: false,
79 expectedIsSubset: false,
80 expectedIsSuperset: false,
81 }, {
82 wfn: common.WellFormedName{
83 "part": "a",
84 "vendor": `adobe`,
85 "product": "reader",
86 "version": `9\.*`,
87 "update": na,
88 "edition": na,
89 "sw_edition": any,
90 "target_sw": any,
91 "target_hw": any,
92 "other": any,
93 "language": any,
94 },
95 wfn2: common.WellFormedName{
96 "part": "a",
97 "vendor": `adobe`,
98 "product": "reader",
99 "version": `8\.3\.2`,
100 "update": na,
101 "edition": na,
102 "sw_edition": any,
103 "target_sw": any,
104 "target_hw": any,
105 "other": any,
106 "language": any,
107 },
108 expectedIsDisjoint: true,
109 expectedIsEqual: false,
110 expectedIsSubset: false,
111 expectedIsSuperset: false,
112 }, {
113 wfn: common.WellFormedName{
114 "part": "o",
115 "vendor": `microsoft`,
116 "product": `windows_?`,
117 "version": any,
118 "update": any,
119 "edition": any,
120 "sw_edition": `home*`,
121 "target_sw": na,
122 "target_hw": `x64`,
123 "other": na,
124 "language": `en\-us`,
125 },
126 wfn2: common.WellFormedName{
127 "part": "o",
128 "vendor": `microsoft`,
129 "product": `windows_7`,
130 "version": `6\.1`,
131 "update": "sp1",
132 "edition": any,
133 "sw_edition": `home_basic`,
134 "target_sw": na,
135 "target_hw": `x32`,
136 "other": any,
137 "language": `en\-us`,
138 },
139 expectedIsDisjoint: true,
140 expectedIsEqual: false,
141 expectedIsSubset: false,
142 expectedIsSuperset: false,
143 }, {
144 wfn: common.WellFormedName{
145 "part": "o",
146 "vendor": `microsoft`,
147 "product": `windows_?`,
148 "version": any,
149 "update": any,
150 "edition": any,
151 "sw_edition": `home*`,
152 "target_sw": na,
153 "target_hw": `x64`,
154 "other": na,
155 "language": `en\-us`,
156 },
157 wfn2: common.WellFormedName{
158 "part": "o",
159 "vendor": `microsoft`,
160 "product": `windows_7`,
161 "version": `6\.1`,
162 "update": "sp1",
163 "edition": any,
164 "sw_edition": `home_basic`,
165 "target_sw": na,
166 "target_hw": `x64`,
167 "other": na,
168 "language": `en\-us`,
169 },
170 expectedIsDisjoint: false,
171 expectedIsEqual: false,
172 expectedIsSubset: false,
173 expectedIsSuperset: true,
174 }, {
175 wfn: common.WellFormedName{
176 "part": "o",
177 "vendor": `microsoft`,
178 "product": `windows_7`,
179 "version": any,
180 "update": any,
181 "edition": any,
182 "sw_edition": `home_basic`,
183 "target_sw": na,
184 "target_hw": `x64`,
185 "other": na,
186 "language": `en\-us`,
187 },
188 wfn2: common.WellFormedName{
189 "part": "o",
190 "vendor": `microsoft`,
191 "product": `windows_7`,
192 "version": any,
193 "update": any,
194 "edition": any,
195 "sw_edition": `home_basic`,
196 "target_sw": na,
197 "target_hw": `x64`,
198 "other": na,
199 "language": `en\-us`,
200 },
201 expectedIsDisjoint: false,
202 expectedIsEqual: true,
203 expectedIsSubset: true,
204 expectedIsSuperset: true,
205 }, {
206 wfn: common.WellFormedName{
207 "part": "o",
208 "vendor": `microsoft`,
209 },
210 wfn2: common.WellFormedName{
211 "part": "o",
212 "vendor": `microsoft*`,
213 },
214 expectedIsDisjoint: false,
215 expectedIsEqual: false,
216 expectedIsSubset: false,
217 expectedIsSuperset: false,
218 }, {
219 wfn: common.WellFormedName{
220 "part": "o",
221 "vendor": na,
222 },
223 wfn2: common.WellFormedName{
224 "part": "o",
225 "vendor": `microsoft`,
226 },
227 expectedIsDisjoint: true,
228 expectedIsEqual: false,
229 expectedIsSubset: false,
230 expectedIsSuperset: false,
231 }, {
232 wfn: common.WellFormedName{
233 "part": "o",
234 "vendor": "*soft",
235 },
236 wfn2: common.WellFormedName{
237 "part": "o",
238 "vendor": `microsoft`,
239 },
240 expectedIsDisjoint: false,
241 expectedIsEqual: false,
242 expectedIsSubset: false,
243 expectedIsSuperset: true,
244 }, {
245 wfn: common.WellFormedName{
246 "part": "o",
247 "vendor": "?icrosoft",
248 },
249 wfn2: common.WellFormedName{
250 "part": "o",
251 "vendor": `microsoft`,
252 },
253 expectedIsDisjoint: false,
254 expectedIsEqual: false,
255 expectedIsSubset: false,
256 expectedIsSuperset: true,
257 }, {
258 wfn: common.WellFormedName{
259 "part": "o",
260 "vendor": "?crosoft",
261 },
262 wfn2: common.WellFormedName{
263 "part": "o",
264 "vendor": `microsoft`,
265 },
266 expectedIsDisjoint: true,
267 expectedIsEqual: false,
268 expectedIsSubset: false,
269 expectedIsSuperset: false,
270 }, {
271 wfn: common.WellFormedName{
272 "part": "o",
273 "vendor": "??icrosoft",
274 },
275 wfn2: common.WellFormedName{
276 "part": "o",
277 "vendor": `\!\#icrosoft`,
278 },
279 expectedIsDisjoint: false,
280 expectedIsEqual: false,
281 expectedIsSubset: false,
282 expectedIsSuperset: true,
283 }, {
284 wfn: common.WellFormedName{
285 "part": "o",
286 "vendor": "microso??",
287 },
288 wfn2: common.WellFormedName{
289 "part": "o",
290 "vendor": `microso\!\#`,
291 },
292 expectedIsDisjoint: false,
293 expectedIsEqual: false,
294 expectedIsSubset: false,
295 expectedIsSuperset: true,
296 }, {
297 wfn: common.WellFormedName{
298 "part": "o",
299 "vendor": "microso??",
300 },
301 wfn2: common.WellFormedName{
302 "part": "o",
303 "vendor": `microso\!\#ft`,
304 },
305 expectedIsDisjoint: true,
306 expectedIsEqual: false,
307 expectedIsSubset: false,
308 expectedIsSuperset: false,
309 }, {
310 wfn: common.WellFormedName{
311 "part": "o",
312 "vendor": "?",
313 },
314 wfn2: common.WellFormedName{
315 "part": "o",
316 "vendor": `a`,
317 },
318 expectedIsDisjoint: false,
319 expectedIsEqual: false,
320 expectedIsSubset: false,
321 expectedIsSuperset: true,
322 }, {
323 wfn: common.WellFormedName{
324 "part": "o",
325 "vendor": "???",
326 },
327 wfn2: common.WellFormedName{
328 "part": "o",
329 "vendor": `ab`,
330 },
331 expectedIsDisjoint: false,
332 expectedIsEqual: false,
333 expectedIsSubset: false,
334 expectedIsSuperset: true,
335 }, {
336 wfn: common.WellFormedName{
337 "part": "o",
338 "vendor": "?",
339 },
340 wfn2: common.WellFormedName{
341 "part": "o",
342 "vendor": `ab`,
343 },
344 expectedIsDisjoint: true,
345 expectedIsEqual: false,
346 expectedIsSubset: false,
347 expectedIsSuperset: false,
348 }, {
349 wfn: common.WellFormedName{
350 "part": "a",
351 "version": "3",
352 },
353 wfn2: common.WellFormedName{
354 "part": "a",
355 "version": "33.0.1750.126",
356 },
357 expectedIsDisjoint: true,
358 expectedIsEqual: false,
359 expectedIsSubset: false,
360 expectedIsSuperset: false,
361 },
362 }
363
364 var actual bool
365 for i, v := range vectors {
366 actual = IsDisjoint(v.wfn, v.wfn2)
367 if actual != v.expectedIsDisjoint {
368 t.Errorf("test %d, IsDisJoint: got %v, want %v", i, actual, v.expectedIsDisjoint)
369 }
370 actual = IsEqual(v.wfn, v.wfn2)
371 if actual != v.expectedIsEqual {
372 t.Errorf("test %d, IsEqual: got %v, want %v", i, actual, v.expectedIsEqual)
373 }
374 actual = IsSubset(v.wfn, v.wfn2)
375 if actual != v.expectedIsSubset {
376 t.Errorf("test %d, IsSubset: got %v, want %v", i, actual, v.expectedIsSubset)
377 }
378 actual = IsSuperset(v.wfn, v.wfn2)
379 if actual != v.expectedIsSuperset {
380 t.Errorf("test %d, IsSuperset: got %v, want %v", i, actual, v.expectedIsSuperset)
381 }
382 }
383 }
384
385 func TestIsEvenWildcards(t *testing.T) {
386 vectors := []struct {
387 str string
388 index int
389 expected bool
390 }{{
391 str: `abc`,
392 index: 2,
393 expected: true,
394 }, {
395 str: `abc*`,
396 index: 3,
397 expected: true,
398 }, {
399 str: `abc\*`,
400 index: 4,
401 expected: false,
402 }, {
403 str: `abc\\*`,
404 index: 5,
405 expected: true,
406 }, {
407 str: `abc\\\*`,
408 index: 6,
409 expected: false,
410 }, {
411 str: `abc\\def\*`,
412 index: 9,
413 expected: false,
414 },
415 }
416
417 for i, v := range vectors {
418 actual := IsEvenWildcards(v.str, v.index)
419 if actual != v.expected {
420 t.Errorf("test %d, Result: got %v, want %v", i, actual, v.expected)
421 }
422 }
423 }