-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpe_test.go
454 lines (435 loc) · 11.1 KB
/
cpe_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package cpe
import (
"testing"
)
// TestCPEMatch 测试CPE匹配功能
func TestCPEMatch(t *testing.T) {
tests := []struct {
name string
cpe1 *CPE
cpe2 *CPE
expected bool
}{
{
name: "完全相同的CPE应匹配",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
expected: true,
},
{
name: "通配符匹配",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:*:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "*",
},
expected: true,
},
{
name: "不匹配的版本",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:11:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "11",
},
expected: false,
},
{
name: "不匹配的产品",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:a:microsoft:office:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "office",
Version: "10",
},
expected: false,
},
{
name: "不匹配的Part",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:o:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartOperationSystem,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
expected: false,
},
{
name: "NA 值匹配",
cpe1: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:-:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
Update: "-",
},
cpe2: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:-:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
Update: "-",
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cpe1.Match(tt.cpe2); got != tt.expected {
t.Errorf("CPE.Match() = %v, want %v", got, tt.expected)
}
})
}
}
// TestGetURI 测试GetURI方法
func TestGetURI(t *testing.T) {
tests := []struct {
name string
cpe *CPE
expected string
}{
{
name: "通过字段生成URI",
cpe: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
Update: "*",
Edition: "*",
Language: "*",
SoftwareEdition: "*",
TargetSoftware: "*",
TargetHardware: "*",
Other: "*",
},
expected: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
},
{
name: "直接使用已有的Cpe23",
cpe: &CPE{
Cpe23: "cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*",
},
expected: "cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*",
},
{
name: "nil CPE",
cpe: nil,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cpe.GetURI(); got != tt.expected {
t.Errorf("CPE.GetURI() = %v, want %v", got, tt.expected)
}
})
}
}
// TestParseCpe23 测试解析CPE 2.3格式字符串
func TestParseCpe23(t *testing.T) {
tests := []struct {
name string
cpeStr string
wantErr bool
expected *CPE
}{
{
name: "有效的CPE 2.3字符串",
cpeStr: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
wantErr: false,
expected: &CPE{
Cpe23: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
Update: "*",
Edition: "*",
Language: "*",
},
},
{
name: "无效的CPE 2.3格式",
cpeStr: "cpe:2.3:a:microsoft:windows:10",
wantErr: true,
},
{
name: "无效的Part值",
cpeStr: "cpe:2.3:x:microsoft:windows:10:*:*:*:*:*:*:*",
wantErr: true,
},
{
name: "非CPE字符串",
cpeStr: "not-a-cpe-string",
wantErr: true,
},
{
name: "操作系统CPE",
cpeStr: "cpe:2.3:o:microsoft:windows:11:*:*:*:*:*:*:*",
wantErr: false,
expected: &CPE{
Cpe23: "cpe:2.3:o:microsoft:windows:11:*:*:*:*:*:*:*",
Part: *PartOperationSystem,
Vendor: "microsoft",
ProductName: "windows",
Version: "11",
},
},
{
name: "硬件CPE",
cpeStr: "cpe:2.3:h:intel:core_i7:1068g7:*:*:*:*:*:*:*",
wantErr: false,
expected: &CPE{
Cpe23: "cpe:2.3:h:intel:core_i7:1068g7:*:*:*:*:*:*:*",
Part: *PartHardware,
Vendor: "intel",
ProductName: "core_i7",
Version: "1068g7",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseCpe23(tt.cpeStr)
if (err != nil) != tt.wantErr {
t.Errorf("ParseCpe23() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
if got.Cpe23 != tt.expected.Cpe23 {
t.Errorf("ParseCpe23() Cpe23 = %v, want %v", got.Cpe23, tt.expected.Cpe23)
}
if got.Part.ShortName != tt.expected.Part.ShortName {
t.Errorf("ParseCpe23() Part = %v, want %v", got.Part.ShortName, tt.expected.Part.ShortName)
}
if got.Vendor != tt.expected.Vendor {
t.Errorf("ParseCpe23() Vendor = %v, want %v", got.Vendor, tt.expected.Vendor)
}
if got.ProductName != tt.expected.ProductName {
t.Errorf("ParseCpe23() ProductName = %v, want %v", got.ProductName, tt.expected.ProductName)
}
if got.Version != tt.expected.Version {
t.Errorf("ParseCpe23() Version = %v, want %v", got.Version, tt.expected.Version)
}
}
})
}
}
// TestFormatCpe23 测试格式化CPE为2.3格式字符串
func TestFormatCpe23(t *testing.T) {
tests := []struct {
name string
cpe *CPE
expected string
}{
{
name: "标准格式化",
cpe: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
Update: "*",
Edition: "*",
Language: "*",
},
expected: "cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*",
},
{
name: "已有Cpe23值",
cpe: &CPE{
Cpe23: "cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*",
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "office",
Version: "2019",
},
expected: "cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*",
},
{
name: "特殊字符转义",
cpe: &CPE{
Part: *PartApplication,
Vendor: "example.com",
ProductName: "product:name",
Version: "1.0",
},
expected: "cpe:2.3:a:example\\.com:product\\:name:1.0:*:*:*:*:*:*:*",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatCpe23(tt.cpe); got != tt.expected {
t.Errorf("FormatCpe23() = %v, want %v", got, tt.expected)
}
})
}
}
// TestMatchCPE 测试MatchCPE函数
func TestMatchCPE(t *testing.T) {
tests := []struct {
name string
criteria *CPE
target *CPE
options *MatchOptions
expected bool
}{
{
name: "基本匹配",
criteria: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
target: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
options: DefaultMatchOptions(),
expected: true,
},
{
name: "忽略版本匹配",
criteria: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
target: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "11",
},
options: &MatchOptions{
IgnoreVersion: true,
},
expected: true,
},
{
name: "不匹配 - 不同产品",
criteria: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "windows",
Version: "10",
},
target: &CPE{
Part: *PartApplication,
Vendor: "microsoft",
ProductName: "office",
Version: "10",
},
options: DefaultMatchOptions(),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MatchCPE(tt.criteria, tt.target, tt.options); got != tt.expected {
t.Errorf("MatchCPE() = %v, want %v", got, tt.expected)
}
})
}
}
// TestDefaultMatchOptions 测试DefaultMatchOptions函数
func TestDefaultMatchOptions(t *testing.T) {
options := DefaultMatchOptions()
if options.IgnoreVersion {
t.Errorf("DefaultMatchOptions().IgnoreVersion = %v, want %v", options.IgnoreVersion, false)
}
if !options.AllowSubVersions {
t.Errorf("DefaultMatchOptions().AllowSubVersions = %v, want %v", options.AllowSubVersions, true)
}
if options.UseRegex {
t.Errorf("DefaultMatchOptions().UseRegex = %v, want %v", options.UseRegex, false)
}
if options.VersionRange {
t.Errorf("DefaultMatchOptions().VersionRange = %v, want %v", options.VersionRange, false)
}
}
// TestCompareVersionsString 测试版本比较功能
func TestCompareVersionsString(t *testing.T) {
// 测试 compareVersionStrings 函数功能
testCases := []struct {
name string
v1 string
v2 string
expected int
}{
{"相等版本", "1.0.0", "1.0.0", 0},
{"v1小于v2 - 主版本号", "1.0.0", "2.0.0", -1},
{"v1大于v2 - 主版本号", "2.0.0", "1.0.0", 1},
{"v1小于v2 - 次版本号", "1.0.0", "1.1.0", -1},
{"v1大于v2 - 次版本号", "1.1.0", "1.0.0", 1},
{"v1小于v2 - 修订号", "1.0.0", "1.0.1", -1},
{"v1大于v2 - 修订号", "1.0.1", "1.0.0", 1},
{"v1小于v2 - 长度不同", "1.0", "1.0.1", -1},
{"v1大于v2 - 长度不同", "1.0.1", "1.0", 1},
{"字母版本比较", "1.0a", "1.0b", -1},
{"数字与字母混合", "1.0.0", "1.0.0a", -1},
{"复杂版本格式", "1.0-alpha", "1.0-beta", -1},
{"带下划线版本", "1.0_alpha", "1.0_beta", -1},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := compareVersionStrings(tc.v1, tc.v2)
if result != tc.expected {
t.Errorf("compareVersionStrings(%q, %q) = %d, 期望 %d", tc.v1, tc.v2, result, tc.expected)
}
})
}
}