-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
485 lines (393 loc) · 13.4 KB
/
set_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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package cpe
import (
"fmt"
"strings"
"testing"
)
func createTestCPE(cpe23 string, part Part, vendor string, product string, version string) *CPE {
return &CPE{
Cpe23: cpe23,
Part: part,
Vendor: Vendor(vendor),
ProductName: Product(product),
Version: Version(version),
}
}
// TestNewCPESet 测试创建CPE集合
func TestNewCPESet(t *testing.T) {
name := "TestSet"
desc := "Test Description"
set := NewCPESet(name, desc)
if set.Name != name {
t.Errorf("NewCPESet() name = %v, want %v", set.Name, name)
}
if set.Description != desc {
t.Errorf("NewCPESet() description = %v, want %v", set.Description, desc)
}
if set.Size() != 0 {
t.Errorf("NewCPESet() should create empty set, got size %v", set.Size())
}
}
// TestCPESet_Add 测试添加CPE到集合
func TestCPESet_Add(t *testing.T) {
set := NewCPESet("Test", "Test")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
// Test adding a CPE
set.Add(cpe1)
if !set.Contains(cpe1) {
t.Errorf("Set should contain added CPE")
}
// Test adding duplicate CPE
initialSize := set.Size()
set.Add(cpe1)
if set.Size() != initialSize {
t.Errorf("Adding duplicate CPE should not increase set size")
}
// Test adding another CPE
set.Add(cpe2)
if !set.Contains(cpe2) {
t.Errorf("Set should contain second added CPE")
}
// Test contains with non-present CPE
cpe3 := createTestCPE("cpe:2.3:a:vendor:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "other", "1.0")
if set.Contains(cpe3) {
t.Errorf("Set should not contain non-added CPE")
}
}
// TestCPESet_Remove 测试从集合中删除CPE
func TestCPESet_Remove(t *testing.T) {
set := NewCPESet("Test", "Test")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
set.Add(cpe1)
set.Add(cpe2)
// Test removing existing CPE
removed := set.Remove(cpe1)
if !removed || set.Contains(cpe1) {
t.Errorf("Remove() failed to remove existing CPE")
}
// Test removing non-existing CPE
removed = set.Remove(cpe1)
if removed {
t.Errorf("Remove() should return false for non-existing CPE")
}
}
// TestCPESet_Contains 测试检查集合是否包含CPE
func TestCPESet_Contains(t *testing.T) {
set := NewCPESet("Test", "Test")
// Test contains with non-present CPE
cpe3 := createTestCPE("cpe:2.3:a:vendor:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "other", "1.0")
if set.Contains(cpe3) {
t.Errorf("Set should not contain non-added CPE")
}
}
// TestCPESet_Size 测试获取集合大小
func TestCPESet_Size(t *testing.T) {
set := NewCPESet("Test", "Test")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
// Test initial size
if set.Size() != 0 {
t.Errorf("Initial set size should be 0")
}
// Test size after adding
set.Add(cpe1)
set.Add(cpe2)
if set.Size() != 2 {
t.Errorf("Set size should be 2 after adding 2 CPEs")
}
}
// TestCPESet_Clear 测试清空集合
func TestCPESet_Clear(t *testing.T) {
set := NewCPESet("Test", "Test")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
// Test initial size
if set.Size() != 0 {
t.Errorf("Initial set size should be 0")
}
// Test size after adding
set.Add(cpe1)
set.Add(cpe2)
if set.Size() != 2 {
t.Errorf("Set size should be 2 after adding 2 CPEs")
}
// Test clear
set.Clear()
if set.Size() != 0 {
t.Errorf("Set size should be 0 after clear")
}
}
// TestCPESet_Union 测试集合并集操作
func TestCPESet_Union(t *testing.T) {
set1 := NewCPESet("Set1", "First set")
set2 := NewCPESet("Set2", "Second set")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
cpe3 := createTestCPE("cpe:2.3:a:vendor:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "other", "1.0")
set1.Add(cpe1)
set1.Add(cpe2)
set2.Add(cpe2)
set2.Add(cpe3)
union := set1.Union(set2)
if union.Size() != 3 {
t.Errorf("Union size should be 3, got %d", union.Size())
}
if !union.Contains(cpe1) || !union.Contains(cpe2) || !union.Contains(cpe3) {
t.Errorf("Union should contain all CPEs from both sets")
}
}
// TestCPESet_Intersection 测试集合交集操作
func TestCPESet_Intersection(t *testing.T) {
set1 := NewCPESet("Set1", "First set")
set2 := NewCPESet("Set2", "Second set")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
cpe3 := createTestCPE("cpe:2.3:a:vendor:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "other", "1.0")
set1.Add(cpe1)
set1.Add(cpe2)
set2.Add(cpe2)
set2.Add(cpe3)
intersection := set1.Intersection(set2)
if intersection.Size() != 1 {
t.Errorf("Intersection size should be 1, got %d", intersection.Size())
}
if !intersection.Contains(cpe2) {
t.Errorf("Intersection should contain CPE2")
}
if intersection.Contains(cpe1) || intersection.Contains(cpe3) {
t.Errorf("Intersection should not contain CPE1 or CPE3")
}
}
// TestCPESet_Difference 测试集合差集操作
func TestCPESet_Difference(t *testing.T) {
set1 := NewCPESet("Set1", "First set")
set2 := NewCPESet("Set2", "Second set")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
cpe3 := createTestCPE("cpe:2.3:a:vendor:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "other", "1.0")
set1.Add(cpe1)
set1.Add(cpe2)
set2.Add(cpe2)
set2.Add(cpe3)
difference := set1.Difference(set2)
if difference.Size() != 1 {
t.Errorf("Difference size should be 1, got %d", difference.Size())
}
if !difference.Contains(cpe1) {
t.Errorf("Difference should contain CPE1")
}
if difference.Contains(cpe2) || difference.Contains(cpe3) {
t.Errorf("Difference should not contain CPE2 or CPE3")
}
}
// TestCPESet_Filter 测试过滤集合
func TestCPESet_Filter(t *testing.T) {
set := NewCPESet("TestSet", "Test set")
cpe1 := createTestCPE("cpe:2.3:a:vendor1:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor1", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor2:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor2", "product", "2.0")
cpe3 := createTestCPE("cpe:2.3:a:vendor3:other:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor3", "other", "1.0")
set.Add(cpe1)
set.Add(cpe2)
set.Add(cpe3)
// Filter by product
criteria := &CPE{
ProductName: "product",
}
filtered := set.Filter(criteria, nil)
if filtered.Size() != 2 {
t.Errorf("Filter by product should return 2 CPEs, got %d", filtered.Size())
}
// Filter by vendor
criteria = &CPE{
Vendor: "vendor1",
}
filtered = set.Filter(criteria, nil)
if filtered.Size() != 1 || !filtered.Contains(cpe1) {
t.Errorf("Filter by vendor should return 1 CPE (cpe1)")
}
}
// TestFromArray 测试从数组创建CPE集合
func TestFromArray(t *testing.T) {
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
cpes := []*CPE{cpe1, cpe2}
set := FromArray(cpes, "TestSet", "Created from array")
if set.Size() != 2 {
t.Errorf("FromArray() set should have 2 CPEs, got %d", set.Size())
}
if !set.Contains(cpe1) || !set.Contains(cpe2) {
t.Errorf("FromArray() set should contain all CPEs from array")
}
if set.Name != "TestSet" || set.Description != "Created from array" {
t.Errorf("FromArray() set has incorrect name or description")
}
}
// TestFindRelated 测试查找相关CPE
func TestFindRelated(t *testing.T) {
set := NewCPESet("TestSet", "Test set")
cpe1 := createTestCPE("cpe:2.3:a:vendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "1.0")
cpe2 := createTestCPE("cpe:2.3:a:vendor:product:2.0:*:*:*:*:*:*:*", *PartApplication, "vendor", "product", "2.0")
cpe3 := createTestCPE("cpe:2.3:a:othervendor:product:1.0:*:*:*:*:*:*:*", *PartApplication, "othervendor", "product", "1.0")
set.Add(cpe1)
set.Add(cpe2)
set.Add(cpe3)
// Find related by vendor and product
criteria := &CPE{
Vendor: "vendor",
ProductName: "product",
}
related := set.FindRelated(criteria, nil)
if related.Size() != 2 {
t.Errorf("FindRelated should return 2 CPEs, got %d", related.Size())
}
if !related.Contains(cpe1) || !related.Contains(cpe2) {
t.Errorf("FindRelated should find cpe1 and cpe2")
}
}
// Utility function to check if a string contains a substring
func contains(s, substr string) bool {
return s != "" && substr != "" && len(s) >= len(substr) && s != substr && strings.Contains(s, substr)
}
// TestCPESetBasicOperations 测试CPESet的基本操作
func TestCPESetBasicOperations(t *testing.T) {
set := NewCPESet("Test Set", "A test set for testing")
// 测试初始状态
if set.Size() != 0 {
t.Errorf("新集合的大小应为0,实际为%d", set.Size())
}
// 测试添加
cpe1, _ := ParseCpe23("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
cpe2, _ := ParseCpe23("cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*")
cpe3, _ := ParseCpe23("cpe:2.3:a:apple:macos:11:*:*:*:*:*:*:*")
set.Add(cpe1)
set.Add(cpe2)
set.Add(cpe3)
if set.Size() != 3 {
t.Errorf("添加3个CPE后大小应为3,实际为%d", set.Size())
}
// 测试重复添加
set.Add(cpe1)
if set.Size() != 3 {
t.Errorf("添加重复CPE后大小应为3,实际为%d", set.Size())
}
// 测试包含
if !set.Contains(cpe1) {
t.Errorf("集合应包含已添加的CPE")
}
// 测试移除
removed := set.Remove(cpe2)
if !removed || set.Size() != 2 {
t.Errorf("移除后大小应为2,实际为%d,移除状态:%v", set.Size(), removed)
}
// 测试移除不存在的CPE
cpe4, _ := ParseCpe23("cpe:2.3:a:google:chrome:90:*:*:*:*:*:*:*")
removed = set.Remove(cpe4)
if removed {
t.Errorf("移除不存在的CPE应返回false")
}
// 测试清空
set.Clear()
if set.Size() != 0 {
t.Errorf("清空后大小应为0,实际为%d", set.Size())
}
}
// TestCPESetSetOperations 测试CPESet的集合操作
func TestCPESetSetOperations(t *testing.T) {
set1 := NewCPESet("Set1", "First set")
set2 := NewCPESet("Set2", "Second set")
// 添加CPE到set1
cpe1, _ := ParseCpe23("cpe:2.3:a:microsoft:windows:10:*:*:*:*:*:*:*")
cpe2, _ := ParseCpe23("cpe:2.3:a:microsoft:office:2019:*:*:*:*:*:*:*")
set1.Add(cpe1)
set1.Add(cpe2)
// 添加CPE到set2
cpe3, _ := ParseCpe23("cpe:2.3:a:apple:macos:11:*:*:*:*:*:*:*")
set2.Add(cpe2) // 与set1有一个重叠
set2.Add(cpe3)
// 测试并集
union := set1.Union(set2)
if union.Size() != 3 {
t.Errorf("并集大小应为3,实际为%d", union.Size())
}
// 测试交集
intersection := set1.Intersection(set2)
if intersection.Size() != 1 {
t.Errorf("交集大小应为1,实际为%d", intersection.Size())
}
// 测试差集
diff1 := set1.Difference(set2)
if diff1.Size() != 1 {
t.Errorf("set1-set2差集大小应为1,实际为%d", diff1.Size())
}
diff2 := set2.Difference(set1)
if diff2.Size() != 1 {
t.Errorf("set2-set1差集大小应为1,实际为%d", diff2.Size())
}
}
// BenchmarkCPESetOperations 性能测试CPESet的基本操作
func BenchmarkCPESetOperations(b *testing.B) {
// 预生成大量CPE以用于测试
cpes := make([]*CPE, 1000)
for i := 0; i < 1000; i++ {
// 创建不同的CPE
cpe := &CPE{
Part: *PartApplication,
Vendor: Vendor(fmt.Sprintf("vendor%d", i%100)),
ProductName: Product(fmt.Sprintf("product%d", i%200)),
Version: Version(fmt.Sprintf("%d.0", i%50)),
}
cpe.Cpe23 = FormatCpe23(cpe) // 生成URI
cpes[i] = cpe
}
// 基准测试:添加
b.Run("Add", func(b *testing.B) {
set := NewCPESet("Benchmark", "Benchmark set")
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Add(cpes[i%1000])
}
})
// 基准测试:包含检查
b.Run("Contains", func(b *testing.B) {
set := NewCPESet("Benchmark", "Benchmark set")
// 先添加一半的CPE
for i := 0; i < 500; i++ {
set.Add(cpes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Contains(cpes[i%1000]) // 一半存在,一半不存在
}
})
// 基准测试:移除
b.Run("Remove", func(b *testing.B) {
set := NewCPESet("Benchmark", "Benchmark set")
b.ResetTimer()
for i := 0; i < b.N; i++ {
// 确保有足够的元素可以移除
if i%2 == 0 {
set.Add(cpes[i%1000])
} else {
set.Remove(cpes[i%1000])
}
}
})
// 基准测试:交集操作
b.Run("Intersection", func(b *testing.B) {
set1 := NewCPESet("Set1", "First set")
set2 := NewCPESet("Set2", "Second set")
// 添加CPE,两个集合有50%的重叠
for i := 0; i < 500; i++ {
set1.Add(cpes[i])
}
for i := 250; i < 750; i++ {
set2.Add(cpes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set1.Intersection(set2)
}
})
}