@@ -16,6 +16,7 @@ import (
16
16
"sort"
17
17
"testing"
18
18
19
+ "github.com/google/osv-scalibr/extractor"
19
20
"github.com/google/osv-scanner/v2/internal/clients/clientimpl/localmatcher"
20
21
"github.com/google/osv-scanner/v2/internal/testutility"
21
22
"github.com/google/osv-scanner/v2/internal/version"
@@ -146,7 +147,7 @@ func TestNewZippedDB_Offline_WithoutCache(t *testing.T) {
146
147
t .Errorf ("a server request was made when running offline" )
147
148
})
148
149
149
- _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , true )
150
+ _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , true , nil )
150
151
151
152
if ! errors .Is (err , localmatcher .ErrOfflineDatabaseNotFound ) {
152
153
t .Errorf ("expected \" %v\" error but got \" %v\" " , localmatcher .ErrOfflineDatabaseNotFound , err )
@@ -178,7 +179,7 @@ func TestNewZippedDB_Offline_WithCache(t *testing.T) {
178
179
"GHSA-5.json" : {ID : "GHSA-5" },
179
180
}))
180
181
181
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , true )
182
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , true , nil )
182
183
183
184
if err != nil {
184
185
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -196,7 +197,7 @@ func TestNewZippedDB_BadZip(t *testing.T) {
196
197
_ , _ = w .Write ([]byte ("this is not a zip" ))
197
198
})
198
199
199
- _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
200
+ _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
200
201
201
202
if err == nil {
202
203
t .Errorf ("expected an error but did not get one" )
@@ -208,7 +209,7 @@ func TestNewZippedDB_UnsupportedProtocol(t *testing.T) {
208
209
209
210
testDir := testutility .CreateTestDir (t )
210
211
211
- _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , "file://hello-world" , userAgent , false )
212
+ _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , "file://hello-world" , userAgent , false , nil )
212
213
213
214
if err == nil {
214
215
t .Errorf ("expected an error but did not get one" )
@@ -238,7 +239,7 @@ func TestNewZippedDB_Online_WithoutCache(t *testing.T) {
238
239
})
239
240
})
240
241
241
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
242
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
242
243
243
244
if err != nil {
244
245
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -270,7 +271,7 @@ func TestNewZippedDB_Online_WithoutCacheAndNoHashHeader(t *testing.T) {
270
271
}))
271
272
})
272
273
273
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
274
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
274
275
275
276
if err != nil {
276
277
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -308,7 +309,7 @@ func TestNewZippedDB_Online_WithSameCache(t *testing.T) {
308
309
309
310
cacheWrite (t , determineStoredAtPath (testDir , "my-db" ), cache )
310
311
311
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
312
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
312
313
313
314
if err != nil {
314
315
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -346,7 +347,7 @@ func TestNewZippedDB_Online_WithDifferentCache(t *testing.T) {
346
347
"GHSA-3.json" : {ID : "GHSA-3" },
347
348
}))
348
349
349
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
350
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
350
351
351
352
if err != nil {
352
353
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -376,7 +377,7 @@ func TestNewZippedDB_Online_WithCacheButNoHashHeader(t *testing.T) {
376
377
"GHSA-3.json" : {ID : "GHSA-3" },
377
378
}))
378
379
379
- _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
380
+ _ , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
380
381
381
382
if err == nil {
382
383
t .Errorf ("expected an error but did not get one" )
@@ -404,7 +405,7 @@ func TestNewZippedDB_Online_WithBadCache(t *testing.T) {
404
405
405
406
cacheWriteBad (t , determineStoredAtPath (testDir , "my-db" ), "this is not json!" )
406
407
407
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
408
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
408
409
409
410
if err != nil {
410
411
t .Fatalf ("unexpected error \" %v\" " , err )
@@ -430,11 +431,92 @@ func TestNewZippedDB_FileChecks(t *testing.T) {
430
431
})
431
432
})
432
433
433
- db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false )
434
+ db , err := localmatcher .NewZippedDB (t .Context (), testDir , "my-db" , ts .URL , userAgent , false , nil )
434
435
435
436
if err != nil {
436
437
t .Fatalf ("unexpected error \" %v\" " , err )
437
438
}
438
439
439
440
expectDBToHaveOSVs (t , db , osvs )
440
441
}
442
+
443
+ func TestNewZippedDB_WithSpecificPackages (t * testing.T ) {
444
+ t .Parallel ()
445
+
446
+ testDir := testutility .CreateTestDir (t )
447
+
448
+ ts := createZipServer (t , func (w http.ResponseWriter , _ * http.Request ) {
449
+ _ , _ = writeOSVsZip (t , w , map [string ]osvschema.Vulnerability {
450
+ "GHSA-1.json" : {
451
+ ID : "GHSA-1" ,
452
+ Affected : []osvschema.Affected {},
453
+ },
454
+ "GHSA-2.json" : {
455
+ ID : "GHSA-2" ,
456
+ Affected : []osvschema.Affected {
457
+ {Package : osvschema.Package {Name : "pkg-1" }},
458
+ },
459
+ },
460
+ "GHSA-3.json" : {
461
+ ID : "GHSA-3" ,
462
+ },
463
+ "GHSA-4.json" : {
464
+ ID : "GHSA-4" ,
465
+ Affected : []osvschema.Affected {
466
+ {Package : osvschema.Package {Name : "pkg-2" }},
467
+ },
468
+ },
469
+ "GHSA-5.json" : {
470
+ ID : "GHSA-5" ,
471
+ Affected : []osvschema.Affected {
472
+ {Package : osvschema.Package {Name : "pkg-2" }},
473
+ {Package : osvschema.Package {Name : "pkg-1" }},
474
+ },
475
+ },
476
+ "GHSA-6.json" : {
477
+ ID : "GHSA-6" ,
478
+ Affected : []osvschema.Affected {
479
+ {Package : osvschema.Package {Name : "pkg-3" }},
480
+ {Package : osvschema.Package {Name : "pkg-2" }},
481
+ },
482
+ },
483
+ })
484
+ })
485
+
486
+ db , err := localmatcher .NewZippedDB (
487
+ t .Context (),
488
+ testDir ,
489
+ "my-db" ,
490
+ ts .URL ,
491
+ userAgent ,
492
+ false ,
493
+ []* extractor.Package {{Name : "pkg-1" }, {Name : "pkg-3" }},
494
+ )
495
+
496
+ if err != nil {
497
+ t .Fatalf ("unexpected error \" %v\" " , err )
498
+ }
499
+
500
+ expectDBToHaveOSVs (t , db , []osvschema.Vulnerability {
501
+ {
502
+ ID : "GHSA-2" ,
503
+ Affected : []osvschema.Affected {
504
+ {Package : osvschema.Package {Name : "pkg-1" }},
505
+ },
506
+ },
507
+ {
508
+ ID : "GHSA-5" ,
509
+ Affected : []osvschema.Affected {
510
+ {Package : osvschema.Package {Name : "pkg-2" }},
511
+ {Package : osvschema.Package {Name : "pkg-1" }},
512
+ },
513
+ },
514
+ {
515
+ ID : "GHSA-6" ,
516
+ Affected : []osvschema.Affected {
517
+ {Package : osvschema.Package {Name : "pkg-3" }},
518
+ {Package : osvschema.Package {Name : "pkg-2" }},
519
+ },
520
+ },
521
+ })
522
+ }
0 commit comments