Debian Codegraph @

naming/cpe_name_unbinder_test.go

raw
1 package naming
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/knqyf263/go-cpe/common"
8 "github.com/pkg/errors"
9 )
10
11 func TestUnbindURI(t *testing.T) {
12 vectors := []struct {
13 s string
14 expected common.WellFormedName
15 wantErr error
16 }{{
17 s: "cpe:/a:microsoft:internet_explorer%01%01%01%01:?:beta",
18 expected: common.WellFormedName{
19 "part": "a",
20 "vendor": "microsoft",
21 "product": "internet_explorer????",
22 "version": "?",
23 "update": "beta",
24 "edition": any,
25 "sw_edition": any,
26 "target_sw": any,
27 "target_hw": any,
28 "other": any,
29 "language": any,
30 },
31 }, {
32 s: "cpe:/a:microsoft:internet_explorer:8.%2a:sp%3f",
33 expected: common.WellFormedName{
34 "part": "a",
35 "vendor": "microsoft",
36 "product": "internet_explorer",
37 "version": `8\.\*`,
38 "update": `sp\?`,
39 "edition": any,
40 "sw_edition": any,
41 "target_sw": any,
42 "target_hw": any,
43 "other": any,
44 "language": any,
45 },
46 }, {
47 s: "cpe:/a:microsoft:internet_explorer:8.%02:sp%01",
48 expected: common.WellFormedName{
49 "part": "a",
50 "vendor": "microsoft",
51 "product": "internet_explorer",
52 "version": `8\.*`,
53 "update": `sp?`,
54 "edition": any,
55 "sw_edition": any,
56 "target_sw": any,
57 "target_hw": any,
58 "other": any,
59 "language": any,
60 },
61 }, {
62 s: "cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~",
63 expected: common.WellFormedName{
64 "part": "a",
65 "vendor": "hp",
66 "product": "insight_diagnostics",
67 "version": `7\.4\.0\.1570`,
68 "update": any,
69 "edition": any,
70 "sw_edition": "online",
71 "target_sw": "win2003",
72 "target_hw": "x64",
73 "other": any,
74 "language": any,
75 },
76 }, {
77 s: "cpe:/a:%01%01microsoft",
78 expected: common.WellFormedName{
79 "part": "a",
80 "vendor": "??microsoft",
81 },
82 }, {
83 s: "cpe:2.3:a:foo\\$bar:insight:7.4.0.1570:-:*:*:online:win2003:x64:*",
84 wantErr: common.ErrParse,
85 }, {
86 s: "cpe:/a:microsoft:internet_explorer:8.%02:s%01p",
87 wantErr: common.ErrParse,
88 }, {
89 s: "cpe:/a:micro%02soft",
90 wantErr: common.ErrParse,
91 }, {
92 s: "cpe:/a:micro%01%01:internet%02:%21%22%23%24%25%26%27%28%29%2a%2b%2c:beta-.::online%2f%3a%3b%3c%3d%3e%3f%40%5b%5c%5d%5e",
93 expected: common.WellFormedName{
94 "part": "a",
95 "vendor": "micro??",
96 "product": "internet*",
97 "version": `\!\"\#\$\%\&\'\(\)\*\+\,`,
98 "update": `beta\-\.`,
99 "language": `online\/\:\;\<\=\>\?\@\[\\\]\^`,
100 },
101 }, {
102 s: "cpe:/a:%60:%7b%7c%7d%7ea",
103 expected: common.WellFormedName{
104 "part": "a",
105 "vendor": "\\`",
106 "product": `\{\|\}\~a`,
107 },
108 }, {
109 s: "cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003",
110 wantErr: common.ErrParse,
111 }, {
112 s: "cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~%99~",
113 wantErr: common.ErrParse,
114 }, {
115 s: `cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~a*b~`,
116 wantErr: common.ErrParse,
117 }, {
118 s: "cpe:/a:%99",
119 wantErr: common.ErrParse,
120 }, {
121 s: "cpe:/a:b*c",
122 wantErr: common.ErrParse,
123 },
124 }
125
126 for i, v := range vectors {
127 actual, err := UnbindURI(v.s)
128 if errors.Cause(err) != v.wantErr {
129 t.Errorf("test %d, Error: got %v, want %v", i, errors.Cause(err), v.wantErr)
130 }
131 if err != nil {
132 continue
133 }
134 if actual.Get("part") != v.expected.Get("part") {
135 t.Errorf("test %d, part: got %v, want %v", i, actual.Get("part"), v.expected.Get("part"))
136 }
137 if actual.Get("vendor") != v.expected.Get("vendor") {
138 t.Errorf("test %d, vendor: got %v, want %v", i, actual.Get("vendor"), v.expected.Get("vendor"))
139 }
140 if actual.Get("product") != v.expected.Get("product") {
141 t.Errorf("test %d, product: got %v, want %v", i, actual.Get("product"), v.expected.Get("product"))
142 }
143 if actual.Get("version") != v.expected.Get("version") {
144 t.Errorf("test %d, version: got %v, want %v", i, actual.Get("version"), v.expected.Get("version"))
145 }
146 if actual.Get("update") != v.expected.Get("update") {
147 t.Errorf("test %d, update: got %v, want %v", i, actual.Get("update"), v.expected.Get("update"))
148 }
149 if actual.Get("edition") != v.expected.Get("edition") {
150 t.Errorf("test %d, edition: got %v, want %v", i, actual.Get("edition"), v.expected.Get("edition"))
151 }
152 if actual.Get("sw_edition") != v.expected.Get("sw_edition") {
153 t.Errorf("test %d, sw_edition: got %v, want %v", i, actual.Get("sw_edition"), v.expected.Get("sw_edition"))
154 }
155 if actual.Get("target_sw") != v.expected.Get("target_sw") {
156 t.Errorf("test %d, target_sw: got %v, want %v", i, actual.Get("target_sw"), v.expected.Get("target_sw"))
157 }
158 if actual.Get("target_hw") != v.expected.Get("target_hw") {
159 t.Errorf("test %d, target_hw: got %v, want %v", i, actual.Get("target_hw"), v.expected.Get("target_hw"))
160 }
161 if actual.Get("other") != v.expected.Get("other") {
162 t.Errorf("test %d, other: got %v, want %v", i, actual.Get("other"), v.expected.Get("other"))
163 }
164 if actual.Get("language") != v.expected.Get("language") {
165 t.Errorf("test %d, language: got %v, want %v", i, actual.Get("language"), v.expected.Get("language"))
166 }
167 }
168 }
169
170 func TestUnbindFS(t *testing.T) {
171 vectors := []struct {
172 s string
173 expected common.WellFormedName
174 wantErr error
175 }{{
176 s: "cpe:2.3:a:micr\\?osoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*",
177 expected: common.WellFormedName{
178 "part": "a",
179 "vendor": `micr\?osoft`,
180 "product": "internet_explorer",
181 "version": `8\.0\.6001`,
182 "update": "beta",
183 "edition": any,
184 "sw_edition": any,
185 "target_sw": any,
186 "target_hw": any,
187 "other": any,
188 "language": any,
189 },
190 }, {
191 s: `cpe:2.3:a:\$0.99_kindle_books_project:\$0.99_kindle_books:6:*:*:*:*:android:*:*`,
192 expected: common.WellFormedName{
193 "part": "a",
194 "vendor": `\$0\.99_kindle_books_project`,
195 "product": `\$0\.99_kindle_books`,
196 "version": `6`,
197 "update": any,
198 "edition": any,
199 "sw_edition": any,
200 "target_sw": `android`,
201 "target_hw": any,
202 "other": any,
203 "language": any,
204 },
205 }, {
206 s: `cpe:2.3:a:2glux*:??com_sexypolling??:0.9.1:-:-:*:-:joomla\!:*:*`,
207 expected: common.WellFormedName{
208 "part": "a",
209 "vendor": `2glux*`,
210 "product": `??com_sexypolling??`,
211 "version": `0\.9\.1`,
212 "update": na,
213 "edition": na,
214 "sw_edition": na,
215 "target_sw": `joomla\!`,
216 "target_hw": any,
217 "other": any,
218 "language": any,
219 },
220 }, {
221 // invalid prefix
222 s: `cpe:2.4:a:2glux:com_sexypolling:0.9.1:-:-:*:-:joomla\!:*:*`,
223 wantErr: common.ErrParse,
224 }, {
225 // embedded unquoted *
226 s: `cpe:2.3:a:2g*lux:com_sexypolling:0.9.1:-:-:*:-:joomla\!:*:*`,
227 wantErr: common.ErrParse,
228 }, {
229 // embedded unquoted ?
230 s: `cpe:2.3:a:2g?lux:com_sexypolling:0.9.1:-:-:*:-:joomla\!:*:*`,
231 wantErr: common.ErrParse,
232 },
233 }
234
235 for i, v := range vectors {
236 actual, err := UnbindFS(v.s)
237 if errors.Cause(err) != v.wantErr {
238 fmt.Println(err)
239 t.Errorf("test %d, Error: got %v, want %v", i, errors.Cause(err), v.wantErr)
240 }
241 if err != nil {
242 continue
243 }
244 if actual.Get("part") != v.expected.Get("part") {
245 t.Errorf("test %d, part: got %v, want %v", i, actual.Get("part"), v.expected.Get("part"))
246 }
247 if actual.Get("vendor") != v.expected.Get("vendor") {
248 t.Errorf("test %d, vendor: got %v, want %v", i, actual.Get("vendor"), v.expected.Get("vendor"))
249 }
250 if actual.Get("product") != v.expected.Get("product") {
251 t.Errorf("test %d, product: got %v, want %v", i, actual.Get("product"), v.expected.Get("product"))
252 }
253 if actual.Get("version") != v.expected.Get("version") {
254 t.Errorf("test %d, version: got %v, want %v", i, actual.Get("version"), v.expected.Get("version"))
255 }
256 if actual.Get("update") != v.expected.Get("update") {
257 t.Errorf("test %d, update: got %v, want %v", i, actual.Get("update"), v.expected.Get("update"))
258 }
259 if actual.Get("edition") != v.expected.Get("edition") {
260 t.Errorf("test %d, edition: got %v, want %v", i, actual.Get("edition"), v.expected.Get("edition"))
261 }
262 if actual.Get("sw_edition") != v.expected.Get("sw_edition") {
263 t.Errorf("test %d, sw_edition: got %v, want %v", i, actual.Get("sw_edition"), v.expected.Get("sw_edition"))
264 }
265 if actual.Get("target_sw") != v.expected.Get("target_sw") {
266 t.Errorf("test %d, target_sw: got %v, want %v", i, actual.Get("target_sw"), v.expected.Get("target_sw"))
267 }
268 if actual.Get("target_hw") != v.expected.Get("target_hw") {
269 t.Errorf("test %d, target_hw: got %v, want %v", i, actual.Get("target_hw"), v.expected.Get("target_hw"))
270 }
271 if actual.Get("other") != v.expected.Get("other") {
272 t.Errorf("test %d, other: got %v, want %v", i, actual.Get("other"), v.expected.Get("other"))
273 }
274 if actual.Get("language") != v.expected.Get("language") {
275 t.Errorf("test %d, language: got %v, want %v", i, actual.Get("language"), v.expected.Get("language"))
276 }
277 }
278
279 }
280
281 func TestGetCompURI(t *testing.T) {
282 vectors := []struct {
283 uri string
284 index int
285 expected string
286 }{{
287 uri: "cpe:/a:microsoft:internet_explorer%01%01%01%01:?:beta",
288 index: 0,
289 expected: "cpe:",
290 }, {
291 uri: "cpe:/a:microsoft:internet_explorer%01%01%01%01:?:beta",
292 index: 1,
293 expected: "a",
294 }, {
295 uri: "cpe:/a:microsoft:internet_explorer%01%01%01%01:?:beta",
296 index: 2,
297 expected: "microsoft",
298 }, {
299 uri: "cpe:/a:microsoft:internet_explorer%01%01%01%01:?:beta",
300 index: 100,
301 expected: "",
302 },
303 }
304
305 for i, v := range vectors {
306 actual := getCompURI(v.uri, v.index)
307 if actual != v.expected {
308 t.Errorf("test %d, Result: got %v, want %v", i, actual, v.expected)
309 }
310 }
311 }
312
313 func TestGetCompFS(t *testing.T) {
314 vectors := []struct {
315 fs string
316 index int
317 expected string
318 }{{
319 fs: "cpe:2.3:a:microsoft:internet_explorer%01%01%01%01:?:beta",
320 index: 0,
321 expected: "cpe",
322 }, {
323 fs: "cpe:2.3:a:microsoft:internet_explorer%01%01%01%01:?:beta",
324 index: 1,
325 expected: "2.3",
326 }, {
327 fs: "cpe:2.3:a:microsoft:internet_explorer%01%01%01%01:?:beta",
328 index: 2,
329 expected: "a",
330 }, {
331 fs: "cpe:2.3:a:microsoft:internet_explorer%01%01%01%01:?:beta",
332 index: 100,
333 expected: "",
334 }, {
335 fs: "cpe:2.3:a:microsoft:internet_explorer%01%01%01%01:?:beta",
336 index: -1,
337 expected: "",
338 }, {
339 fs: `a\:b\:c:d\:e\:f:g`,
340 index: 0,
341 expected: `a\:b\:c`,
342 }, {
343 fs: `a\:b\:c:d\:e\:f:g`,
344 index: 1,
345 expected: `d\:e\:f`,
346 }, {
347 fs: `a\:b\:c:d\:e\:f:g`,
348 index: 2,
349 expected: "g",
350 }, {
351 fs: `a\:b\:c:d\:e\:f:g`,
352 index: 3,
353 expected: "",
354 },
355 }
356
357 for i, v := range vectors {
358 actual := getCompFS(v.fs, v.index)
359 if actual != v.expected {
360 t.Errorf("test %d, Result: got %v, want %v", i, actual, v.expected)
361 }
362 }
363 }
364
365 func TestUnpack(t *testing.T) {
366 vectors := []struct {
367 s string
368 expected common.WellFormedName
369 wantErr error
370 }{{
371 s: "~-~-~wordpress~~",
372 expected: common.WellFormedName{
373 "edition": na,
374 "sw_edition": na,
375 "target_sw": "wordpress",
376 "target_hw": any,
377 "other": any,
378 },
379 }, {
380 s: "~~~~~standalone",
381 expected: common.WellFormedName{
382 "other": "standalone",
383 },
384 }, {
385 s: "~-~portal~sw~x86~a~b~c",
386 expected: common.WellFormedName{
387 "edition": na,
388 "sw_edition": "portal",
389 "target_sw": "sw",
390 "target_hw": "x86",
391 "other": `a\~b\~c`,
392 },
393 },
394 }
395
396 for i, v := range vectors {
397 wfn := common.WellFormedName{}
398 actual, err := unpack(v.s, wfn)
399 if errors.Cause(err) != v.wantErr {
400 fmt.Println(err)
401 t.Errorf("test %d, Error: got %v, want %v", i, errors.Cause(err), v.wantErr)
402 }
403 if err != nil {
404 continue
405 }
406 if actual.Get("edition") != v.expected.Get("edition") {
407 t.Errorf("test %d, edition: got %v, want %v", i, actual.Get("edition"), v.expected.Get("edition"))
408 }
409 if actual.Get("sw_edition") != v.expected.Get("sw_edition") {
410 t.Errorf("test %d, sw_edition: got %v, want %v", i, actual.Get("sw_edition"), v.expected.Get("sw_edition"))
411 }
412 if actual.Get("target_sw") != v.expected.Get("target_sw") {
413 t.Errorf("test %d, target_sw: got %v, want %v", i, actual.Get("target_sw"), v.expected.Get("target_sw"))
414 }
415 if actual.Get("target_hw") != v.expected.Get("target_hw") {
416 t.Errorf("test %d, target_hw: got %v, want %v", i, actual.Get("target_hw"), v.expected.Get("target_hw"))
417 }
418 if actual.Get("other") != v.expected.Get("other") {
419 t.Errorf("test %d, other: got %v, want %v", i, actual.Get("other"), v.expected.Get("other"))
420 }
421 }
422
423 }