@@ -11167,6 +11167,167 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL, user str
1116711167 })
1116811168}
1116911169
11170+ func TestSupportedDigestAlgorithms (t * testing.T ) {
11171+ port := test .GetFreePort ()
11172+ baseURL := test .GetBaseURL (port )
11173+
11174+ conf := config .New ()
11175+ conf .HTTP .Port = port
11176+
11177+ dir := t .TempDir ()
11178+
11179+ ctlr := api .NewController (conf )
11180+ ctlr .Config .Storage .RootDirectory = dir
11181+ ctlr .Config .Storage .Dedupe = false
11182+ ctlr .Config .Storage .GC = false
11183+
11184+ cm := test .NewControllerManager (ctlr )
11185+
11186+ cm .StartAndWait (port )
11187+ defer cm .StopServer ()
11188+
11189+ Convey ("Test SHA512 single-arch image" , t , func () {
11190+ image := CreateImageWithDigestAlgorithm (godigest .SHA512 ).
11191+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
11192+
11193+ name := "algo-sha256"
11194+ tag := "singlearch"
11195+
11196+ err := UploadImage (image , baseURL , name , tag )
11197+ So (err , ShouldBeNil )
11198+
11199+ client := resty .New ()
11200+
11201+ // The server picks canonical digests when tags are pushed
11202+ // See https://github.com/opencontainers/distribution-spec/issues/494
11203+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
11204+ // but there is no way to specify a client preference
11205+ // so all we can do is verify the correct algorithm is returned
11206+
11207+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
11208+
11209+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
11210+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
11211+ })
11212+
11213+ Convey ("Test SHA384 single-arch image" , t , func () {
11214+ image := CreateImageWithDigestAlgorithm (godigest .SHA384 ).
11215+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
11216+
11217+ name := "algo-sha384"
11218+ tag := "singlearch"
11219+
11220+ err := UploadImage (image , baseURL , name , tag )
11221+ So (err , ShouldBeNil )
11222+
11223+ client := resty .New ()
11224+
11225+ // The server picks canonical digests when tags are pushed
11226+ // See https://github.com/opencontainers/distribution-spec/issues/494
11227+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
11228+ // but there is no way to specify a client preference
11229+ // so all we can do is verify the correct algorithm is returned
11230+
11231+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
11232+
11233+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
11234+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
11235+ })
11236+
11237+ Convey ("Test SHA512 multi-arch image" , t , func () {
11238+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
11239+ DefaultConfig ().Build ()
11240+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
11241+ DefaultConfig ().Build ()
11242+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA512 ).
11243+ Images ([]Image {subImage1 , subImage2 }).Build ()
11244+
11245+ name := "algo-sha256"
11246+ tag := "multiarch"
11247+
11248+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
11249+ So (err , ShouldBeNil )
11250+
11251+ client := resty .New ()
11252+
11253+ // The server picks canonical digests when tags are pushed
11254+ // See https://github.com/opencontainers/distribution-spec/issues/494
11255+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
11256+ // but there is no way to specify a client preference
11257+ // so all we can do is verify the correct algorithm is returned
11258+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
11259+
11260+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
11261+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
11262+
11263+ // While the expected multiarch manifest digest is always using the canonical algorithm
11264+ // the sub-imgage manifest digest can use any algorith
11265+ verifyReturnedManifestDigest (t , client , baseURL , name ,
11266+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
11267+ verifyReturnedManifestDigest (t , client , baseURL , name ,
11268+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
11269+ })
11270+
11271+ Convey ("Test SHA384 multi-arch image" , t , func () {
11272+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
11273+ DefaultConfig ().Build ()
11274+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
11275+ DefaultConfig ().Build ()
11276+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA384 ).
11277+ Images ([]Image {subImage1 , subImage2 }).Build ()
11278+
11279+ name := "algo-sha384"
11280+ tag := "multiarch"
11281+
11282+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
11283+ So (err , ShouldBeNil )
11284+
11285+ client := resty .New ()
11286+
11287+ // The server picks canonical digests when tags are pushed
11288+ // See https://github.com/opencontainers/distribution-spec/issues/494
11289+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
11290+ // but there is no way to specify a client preference
11291+ // so all we can do is verify the correct algorithm is returned
11292+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
11293+
11294+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
11295+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
11296+
11297+ // While the expected multiarch manifest digest is always using the canonical algorithm
11298+ // the sub-imgage manifest digest can use any algorith
11299+ verifyReturnedManifestDigest (t , client , baseURL , name ,
11300+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
11301+ verifyReturnedManifestDigest (t , client , baseURL , name ,
11302+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
11303+ })
11304+ }
11305+
11306+ func verifyReturnedManifestDigest (t * testing.T , client * resty.Client , baseURL , repoName ,
11307+ reference , expectedDigestStr string ,
11308+ ) {
11309+ t .Helper ()
11310+
11311+ t .Logf ("Verify Docker-Content-Digest returned for repo %s reference %s is %s" ,
11312+ repoName , reference , expectedDigestStr )
11313+
11314+ getResponse , err := client .R ().Get (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
11315+ So (err , ShouldBeNil )
11316+ So (getResponse , ShouldNotBeNil )
11317+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
11318+
11319+ contentDigestStr := getResponse .Header ().Get ("Docker-Content-Digest" )
11320+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
11321+
11322+ getResponse , err = client .R ().Head (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
11323+ So (err , ShouldBeNil )
11324+ So (getResponse , ShouldNotBeNil )
11325+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
11326+
11327+ contentDigestStr = getResponse .Header ().Get ("Docker-Content-Digest" )
11328+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
11329+ }
11330+
1117011331func getEmptyImageConfig () ([]byte , godigest.Digest ) {
1117111332 config := ispec.Image {}
1117211333
0 commit comments