44 "database/sql"
55 "os"
66 "path/filepath"
7+ "strings"
78 "testing"
89 "time"
910)
@@ -279,14 +280,20 @@ func TestGetCachedArtifact(t *testing.T) {
279280 if cached .StoragePath != "/cache/npm/" + filename {
280281 t .Errorf ("expected cached storage path, got %q" , cached .StoragePath )
281282 }
282- if cached .ContentHash . String != testContentHash {
283- t .Errorf ("expected cached content hash , got %q" , cached .ContentHash . String )
283+ if cached .Artifact . PURL != versionPURL {
284+ t .Errorf ("expected cached PURL %q , got %q" , versionPURL , cached .Artifact . PURL )
284285 }
285- if cached .Size . Int64 != 12345 {
286- t .Errorf ("expected cached size 12345 , got %d " , cached .Size . Int64 )
286+ if cached .Artifact . Digest . String () != "sha256:" + testContentHash {
287+ t .Errorf ("expected cached digest , got %q " , cached .Artifact . Digest )
287288 }
288- if cached .ContentType .String != "application/gzip" {
289- t .Errorf ("expected cached content type, got %q" , cached .ContentType .String )
289+ if cached .Artifact .Size != 12345 {
290+ t .Errorf ("expected cached size 12345, got %d" , cached .Artifact .Size )
291+ }
292+ if cached .Artifact .Filename != filename {
293+ t .Errorf ("expected cached filename %q, got %q" , filename , cached .Artifact .Filename )
294+ }
295+ if cached .Artifact .MediaType != "application/gzip" {
296+ t .Errorf ("expected cached content type, got %q" , cached .Artifact .MediaType )
290297 }
291298 if cached .Integrity .String != testIntegrity {
292299 t .Errorf ("expected cached integrity, got %q" , cached .Integrity .String )
@@ -302,6 +309,77 @@ func TestGetCachedArtifact(t *testing.T) {
302309 })
303310}
304311
312+ func TestCachedArtifactRowArtifact (t * testing.T ) {
313+ validRow := cachedArtifactRow {
314+ Ecosystem : "npm" ,
315+ StoragePath : "npm/example/1.0.0/example.tgz" ,
316+ ContentHash : sql.NullString {String : testContentHash , Valid : true },
317+ Size : sql.NullInt64 {Int64 : 0 , Valid : true },
318+ }
319+ tests := []struct {
320+ name string
321+ row cachedArtifactRow
322+ versionPURL string
323+ wantErr string
324+ }{
325+ {name : "zero byte" , row : validRow , versionPURL : "pkg:npm/example@1.0.0" },
326+ {
327+ name : "missing hash" ,
328+ row : cachedArtifactRow {Size : sql.NullInt64 {Int64 : 1 , Valid : true }},
329+ versionPURL : "pkg:npm/example@1.0.0" ,
330+ wantErr : "content hash is missing" ,
331+ },
332+ {
333+ name : "malformed hash" ,
334+ row : cachedArtifactRow {
335+ ContentHash : sql.NullString {String : "not-a-hash" , Valid : true },
336+ Size : sql.NullInt64 {Int64 : 1 , Valid : true },
337+ },
338+ versionPURL : "pkg:npm/example@1.0.0" ,
339+ wantErr : "digest" ,
340+ },
341+ {
342+ name : "missing size" ,
343+ row : cachedArtifactRow {
344+ ContentHash : sql.NullString {String : testContentHash , Valid : true },
345+ },
346+ versionPURL : "pkg:npm/example@1.0.0" ,
347+ wantErr : "size is missing" ,
348+ },
349+ {
350+ name : "negative size" ,
351+ row : cachedArtifactRow {
352+ ContentHash : sql.NullString {String : testContentHash , Valid : true },
353+ Size : sql.NullInt64 {Int64 : - 1 , Valid : true },
354+ },
355+ versionPURL : "pkg:npm/example@1.0.0" ,
356+ wantErr : "size" ,
357+ },
358+ {name : "malformed PURL" , row : validRow , versionPURL : "not-a-purl" , wantErr : "PURL" },
359+ }
360+
361+ for _ , test := range tests {
362+ t .Run (test .name , func (t * testing.T ) {
363+ cached , err := test .row .artifact (test .versionPURL , "example.tgz" )
364+ if test .wantErr != "" {
365+ if err == nil {
366+ t .Fatal ("artifact() error = nil" )
367+ }
368+ if ! strings .Contains (err .Error (), test .wantErr ) {
369+ t .Errorf ("error = %q, want %q" , err , test .wantErr )
370+ }
371+ return
372+ }
373+ if err != nil {
374+ t .Fatalf ("artifact() error = %v" , err )
375+ }
376+ if err := cached .Artifact .Validate (); err != nil {
377+ t .Errorf ("Artifact.Validate() error = %v" , err )
378+ }
379+ })
380+ }
381+ }
382+
305383func seedCachedArtifactTestData (t * testing.T , db * DB , packagePURL , versionPURL , filename string ) {
306384 t .Helper ()
307385
0 commit comments