@@ -10,7 +10,6 @@ import (
10
10
"fmt"
11
11
"net/http"
12
12
"net/http/httptest"
13
- "net/url"
14
13
"os"
15
14
"path/filepath"
16
15
"reflect"
@@ -19,68 +18,9 @@ import (
19
18
"testing"
20
19
"time"
21
20
22
- "github.com/google/go-cmp/cmp"
23
- "golang.org/x/vuln/internal"
24
- "golang.org/x/vuln/internal/osv"
25
21
"golang.org/x/vuln/internal/web"
26
22
)
27
23
28
- // testCache for testing purposes
29
- type testCache struct {
30
- indexMap map [string ]DBIndex
31
- indexStamp map [string ]time.Time
32
- vulnMap map [string ]map [string ][]* osv.Entry
33
- }
34
-
35
- func newTestCache () * testCache {
36
- return & testCache {
37
- indexMap : make (map [string ]DBIndex ),
38
- indexStamp : make (map [string ]time.Time ),
39
- vulnMap : make (map [string ]map [string ][]* osv.Entry ),
40
- }
41
- }
42
-
43
- func (tc * testCache ) ReadIndex (db string ) (DBIndex , time.Time , error ) {
44
- index , ok := tc .indexMap [db ]
45
- if ! ok {
46
- return nil , time.Time {}, nil
47
- }
48
- stamp , ok := tc .indexStamp [db ]
49
- if ! ok {
50
- return nil , time.Time {}, nil
51
- }
52
- return index , stamp , nil
53
- }
54
-
55
- func (tc * testCache ) WriteIndex (db string , index DBIndex , stamp time.Time ) error {
56
- tc .indexMap [db ] = index
57
- tc .indexStamp [db ] = stamp
58
- return nil
59
- }
60
-
61
- func (tc * testCache ) ReadEntries (db , module string ) ([]* osv.Entry , error ) {
62
- mMap , ok := tc .vulnMap [db ]
63
- if ! ok {
64
- return nil , nil
65
- }
66
- return mMap [module ], nil
67
- }
68
-
69
- func (tc * testCache ) WriteEntries (db , module string , entries []* osv.Entry ) error {
70
- mMap , ok := tc .vulnMap [db ]
71
- if ! ok {
72
- mMap = make (map [string ][]* osv.Entry )
73
- tc .vulnMap [db ] = mMap
74
- }
75
- mMap [module ] = nil
76
- for _ , e := range entries {
77
- e2 := * e
78
- e2 .Details = "cached: " + e2 .Details
79
- mMap [module ] = append (mMap [module ], & e2 )
80
- }
81
- return nil
82
- }
83
-
84
24
func newTestServer () * httptest.Server {
85
25
mux := http .NewServeMux ()
86
26
mux .Handle ("/" , http .FileServer (http .Dir ("testdata/vulndb" )))
@@ -119,29 +59,16 @@ func TestByModule(t *testing.T) {
119
59
name string
120
60
source string
121
61
module string
122
- cache Cache
123
62
detailPrefix string
124
63
wantVulns int
125
64
}{
126
- // Test the http client without any cache.
127
- {name : "http-no-cache" , source : srv .URL , module : modulePath ,
128
- cache : nil , detailPrefix : detailStart , wantVulns : 3 },
129
- {name : "http-cache" , source : srv .URL , module : modulePath ,
130
- cache : newTestCache (), detailPrefix : "cached: Route" , wantVulns : 3 },
131
- // Repeat the same for local file client.
132
- {name : "file-no-cache" , source : localURL , module : modulePath ,
133
- cache : nil , detailPrefix : detailStart , wantVulns : 3 },
134
- // Cache does not play a role in local file databases.
135
- {name : "file-cache" , source : localURL , module : modulePath ,
136
- cache : newTestCache (), detailPrefix : detailStart , wantVulns : 3 },
137
- // Test all-lowercase module path.
138
- {name : "lower-http" , source : srv .URL , module : modulePathLowercase ,
139
- cache : nil , detailPrefix : detailStartLowercase , wantVulns : 4 },
140
- {name : "lower-file" , source : localURL , module : modulePathLowercase ,
141
- cache : nil , detailPrefix : detailStartLowercase , wantVulns : 4 },
65
+ {name : "http" , source : srv .URL , module : modulePath , detailPrefix : detailStart , wantVulns : 3 },
66
+ {name : "file" , source : localURL , module : modulePath , detailPrefix : detailStart , wantVulns : 3 },
67
+ {name : "lower-http" , source : srv .URL , module : modulePathLowercase , detailPrefix : detailStartLowercase , wantVulns : 4 },
68
+ {name : "lower-file" , source : localURL , module : modulePathLowercase , detailPrefix : detailStartLowercase , wantVulns : 4 },
142
69
} {
143
70
t .Run (test .name , func (t * testing.T ) {
144
- client , err := NewClient (test .source , Options {HTTPCache : test . cache })
71
+ client , err := NewClient (test .source , Options {})
145
72
if err != nil {
146
73
t .Fatal (err )
147
74
}
@@ -191,32 +118,30 @@ func TestMustUseIndex(t *testing.T) {
191
118
192
119
// List of modules to query, some are repeated to exercise cache hits.
193
120
modulePaths := []string {"github.com/BeeGo/beego" , "github.com/tidwall/gjson" , "net/http" , "abc.xyz" , "github.com/BeeGo/beego" }
194
- for _ , cache := range []Cache {newTestCache (), nil } {
195
- clt , err := NewClient (srv .URL , Options {HTTPCache : cache })
196
- if err != nil {
121
+ clt , err := NewClient (srv .URL , Options {})
122
+ if err != nil {
123
+ t .Fatal (err )
124
+ }
125
+ hs := clt .(* httpSource )
126
+ for _ , modulePath := range modulePaths {
127
+ indexCalls := hs .indexCalls
128
+ httpCalls := hs .httpCalls
129
+ if _ , err := clt .ByModule (ctx , modulePath ); err != nil {
197
130
t .Fatal (err )
198
131
}
199
- hs := clt .(* httpSource )
200
- for _ , modulePath := range modulePaths {
201
- indexCalls := hs .indexCalls
202
- httpCalls := hs .httpCalls
203
- if _ , err := clt .ByModule (ctx , modulePath ); err != nil {
132
+ // Number of index Calls should be increased.
133
+ if hs .indexCalls == indexCalls {
134
+ t .Errorf ("ByModule(ctx, %s) did not call Index(...)" , modulePath )
135
+ }
136
+ // If http request was made, then the modulePath must be in the index.
137
+ if hs .httpCalls > httpCalls {
138
+ index , err := hs .Index (ctx )
139
+ if err != nil {
204
140
t .Fatal (err )
205
141
}
206
- // Number of index Calls should be increased.
207
- if hs .indexCalls == indexCalls {
208
- t .Errorf ("ByModule(ctx, %s) [cache:%t] did not call Index(...)" , modulePath , cache != nil )
209
- }
210
- // If http request was made, then the modulePath must be in the index.
211
- if hs .httpCalls > httpCalls {
212
- index , err := hs .Index (ctx )
213
- if err != nil {
214
- t .Fatal (err )
215
- }
216
- _ , present := index [modulePath ]
217
- if ! present {
218
- t .Errorf ("ByModule(ctx, %s) [cache:%t] issued http request for module not in Index(...)" , modulePath , cache != nil )
219
- }
142
+ _ , present := index [modulePath ]
143
+ if ! present {
144
+ t .Errorf ("ByModule(ctx, %s) issued http request for module not in Index(...)" , modulePath )
220
145
}
221
146
}
222
147
}
@@ -282,64 +207,6 @@ func TestCorrectFetchesNoCache(t *testing.T) {
282
207
}
283
208
}
284
209
285
- // Make sure that a cached index is used in the case it is stale
286
- // but there were no changes to it at the server side.
287
- func TestCorrectFetchesNoChangeIndex (t * testing.T ) {
288
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
289
- if r .URL .Path == "/index.json" {
290
- w .WriteHeader (http .StatusNotModified )
291
- }
292
- }))
293
- defer ts .Close ()
294
- url , _ := url .Parse (ts .URL )
295
-
296
- // set timestamp so that cached index is stale,
297
- // i.e., more than two hours old.
298
- timeStamp := time .Now ().Add (time .Hour * (- 3 ))
299
- index := DBIndex {"a" : timeStamp }
300
- cache := newTestCache ()
301
- cache .WriteIndex (url .Hostname (), index , timeStamp )
302
-
303
- e := & osv.Entry {
304
- ID : "ID1" ,
305
- Details : "details" ,
306
- Modified : timeStamp ,
307
- }
308
- cache .WriteEntries (url .Hostname (), "a" , []* osv.Entry {e })
309
-
310
- client , err := NewClient (ts .URL , Options {HTTPCache : cache })
311
- if err != nil {
312
- t .Fatal (err )
313
- }
314
- gots , err := client .ByModule (context .Background (), "a" )
315
- if err != nil {
316
- t .Fatal (err )
317
- }
318
- if len (gots ) != 1 {
319
- t .Errorf ("got %d vulns, want 1" , len (gots ))
320
- } else {
321
- got := gots [0 ]
322
- want := * e
323
- want .Details = "cached: " + want .Details
324
- if ! cmp .Equal (got , & want ) {
325
- t .Errorf ("\n got %+v\n want %+v" , got , & want )
326
- }
327
- }
328
- }
329
-
330
- func mustReadEntry (t * testing.T , vulnID string ) * osv.Entry {
331
- t .Helper ()
332
- data , err := os .ReadFile (filepath .Join ("testdata" , "vulndb" , internal .IDDirectory , vulnID + ".json" ))
333
- if err != nil {
334
- t .Fatal (err )
335
- }
336
- var e * osv.Entry
337
- if err := json .Unmarshal (data , & e ); err != nil {
338
- t .Fatal (err )
339
- }
340
- return e
341
- }
342
-
343
210
func TestLastModifiedTime (t * testing.T ) {
344
211
if runtime .GOOS == "js" {
345
212
t .Skip ("skipping test: no network on js" )
0 commit comments