@@ -15,53 +15,85 @@ import (
1515)
1616
1717func TestVersionAssignment (t * testing.T ) {
18- for _ , c := range []struct {
19- name string
20- algorithms []digest.Algorithm
21- expectedVersion version
18+ for _ , contentType := range []struct {
19+ name string
20+ put func (* testing.T , types.ImageDestination , digest.Algorithm , int , types.BlobInfoCache )
2221 }{
2322 {
24- name : "SHA256 only gets version 1.1" ,
25- algorithms : []digest.Algorithm {digest .SHA256 },
26- expectedVersion : version1_1 ,
23+ name : "blob" ,
24+ put : func (t * testing.T , dest types.ImageDestination , algo digest.Algorithm , i int , cache types.BlobInfoCache ) {
25+ blobData := []byte ("test-blob-" + algo .String () + "-" + string (rune (i )))
26+ var blobDigest digest.Digest
27+ if algo == digest .SHA256 {
28+ blobDigest = ""
29+ } else {
30+ blobDigest = algo .FromBytes (blobData )
31+ }
32+ _ , err := dest .PutBlob (context .Background (), bytes .NewReader (blobData ), types.BlobInfo {Digest : blobDigest , Size : int64 (len (blobData ))}, cache , false )
33+ require .NoError (t , err )
34+ },
2735 },
2836 {
29- name : "SHA512 only gets version 1.2" ,
30- algorithms : []digest.Algorithm {digest .SHA512 },
31- expectedVersion : version1_2 ,
37+ name : "manifest" ,
38+ put : func (t * testing.T , dest types.ImageDestination , algo digest.Algorithm , i int , cache types.BlobInfoCache ) {
39+ manifestData := []byte ("test-manifest-" + algo .String () + "-" + string (rune (i )))
40+ instanceDigest := algo .FromBytes (manifestData )
41+ err := dest .PutManifest (context .Background (), manifestData , & instanceDigest )
42+ require .NoError (t , err )
43+ },
3244 },
3345 {
34- name : "Mixed SHA256 and SHA512 gets version 1.2" ,
35- algorithms : []digest.Algorithm {digest .SHA256 , digest .SHA512 },
36- expectedVersion : version1_2 ,
46+ name : "signature" ,
47+ put : func (t * testing.T , dest types.ImageDestination , algo digest.Algorithm , i int , cache types.BlobInfoCache ) {
48+ manifestData := []byte ("test-manifest-" + algo .String () + "-" + string (rune (i )))
49+ instanceDigest := algo .FromBytes (manifestData )
50+ // These signatures are completely invalid; start with 0xA3 just to be minimally plausible to signature.FromBlob.
51+ signatures := [][]byte {[]byte ("\xA3 sig" + algo .String () + "-" + string (rune (i )))}
52+ err := dest .PutSignatures (context .Background (), signatures , & instanceDigest )
53+ require .NoError (t , err )
54+ },
3755 },
3856 } {
39- t .Run (c .name , func (t * testing.T ) {
40- ref , tmpDir := refToTempDir (t )
41- cache := memory .New ()
57+ for _ , c := range []struct {
58+ name string
59+ algorithms []digest.Algorithm
60+ expectedVersion version
61+ }{
62+ {
63+ name : "SHA256 only gets version 1.1" ,
64+ algorithms : []digest.Algorithm {digest .SHA256 },
65+ expectedVersion : version1_1 ,
66+ },
67+ {
68+ name : "SHA512 only gets version 1.2" ,
69+ algorithms : []digest.Algorithm {digest .SHA512 },
70+ expectedVersion : version1_2 ,
71+ },
72+ {
73+ name : "Mixed SHA256 and SHA512 gets version 1.2" ,
74+ algorithms : []digest.Algorithm {digest .SHA256 , digest .SHA512 },
75+ expectedVersion : version1_2 ,
76+ },
77+ } {
78+ t .Run (contentType .name + ": " + c .name , func (t * testing.T ) {
79+ ref , tmpDir := refToTempDir (t )
80+ cache := memory .New ()
4281
43- dest , err := ref .NewImageDestination (context .Background (), nil )
44- require .NoError (t , err )
45- defer dest .Close ()
82+ dest , err := ref .NewImageDestination (context .Background (), nil )
83+ require .NoError (t , err )
84+ defer dest .Close ()
4685
47- for i , algo := range c .algorithms {
48- blobData := []byte ("test-blob-" + algo .String () + "-" + string (rune (i )))
49- var blobDigest digest.Digest
50- if algo == digest .SHA256 {
51- blobDigest = ""
52- } else {
53- blobDigest = algo .FromBytes (blobData )
86+ for i , algo := range c .algorithms {
87+ contentType .put (t , dest , algo , i , cache )
5488 }
55- _ , err = dest .PutBlob (context .Background (), bytes .NewReader (blobData ), types.BlobInfo {Digest : blobDigest , Size : int64 (len (blobData ))}, cache , false )
56- require .NoError (t , err )
57- }
5889
59- err = dest .Commit (context .Background (), nil )
60- require .NoError (t , err )
90+ err = dest .Commit (context .Background (), nil )
91+ require .NoError (t , err )
6192
62- versionBytes , err := os .ReadFile (filepath .Join (tmpDir , "version" ))
63- require .NoError (t , err )
64- assert .Equal (t , c .expectedVersion .String (), string (versionBytes ))
65- })
93+ versionBytes , err := os .ReadFile (filepath .Join (tmpDir , "version" ))
94+ require .NoError (t , err )
95+ assert .Equal (t , c .expectedVersion .String (), string (versionBytes ))
96+ })
97+ }
6698 }
6799}
0 commit comments