@@ -10466,6 +10466,167 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL, user str
1046610466 })
1046710467}
1046810468
10469+ func TestSupportedDigestAlgorithms (t * testing.T ) {
10470+ port := test .GetFreePort ()
10471+ baseURL := test .GetBaseURL (port )
10472+
10473+ conf := config .New ()
10474+ conf .HTTP .Port = port
10475+
10476+ dir := t .TempDir ()
10477+
10478+ ctlr := api .NewController (conf )
10479+ ctlr .Config .Storage .RootDirectory = dir
10480+ ctlr .Config .Storage .Dedupe = false
10481+ ctlr .Config .Storage .GC = false
10482+
10483+ cm := test .NewControllerManager (ctlr )
10484+
10485+ cm .StartAndWait (port )
10486+ defer cm .StopServer ()
10487+
10488+ Convey ("Test SHA512 single-arch image" , t , func () {
10489+ image := CreateImageWithDigestAlgorithm (godigest .SHA512 ).
10490+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10491+
10492+ name := "algo-sha256"
10493+ tag := "singlearch"
10494+
10495+ err := UploadImage (image , baseURL , name , tag )
10496+ So (err , ShouldBeNil )
10497+
10498+ client := resty .New ()
10499+
10500+ // The server picks canonical digests when tags are pushed
10501+ // See https://github.com/opencontainers/distribution-spec/issues/494
10502+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10503+ // but there is no way to specify a client preference
10504+ // so all we can do is verify the correct algorithm is returned
10505+
10506+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10507+
10508+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10509+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10510+ })
10511+
10512+ Convey ("Test SHA384 single-arch image" , t , func () {
10513+ image := CreateImageWithDigestAlgorithm (godigest .SHA384 ).
10514+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10515+
10516+ name := "algo-sha384"
10517+ tag := "singlearch"
10518+
10519+ err := UploadImage (image , baseURL , name , tag )
10520+ So (err , ShouldBeNil )
10521+
10522+ client := resty .New ()
10523+
10524+ // The server picks canonical digests when tags are pushed
10525+ // See https://github.com/opencontainers/distribution-spec/issues/494
10526+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10527+ // but there is no way to specify a client preference
10528+ // so all we can do is verify the correct algorithm is returned
10529+
10530+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10531+
10532+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10533+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10534+ })
10535+
10536+ Convey ("Test SHA512 multi-arch image" , t , func () {
10537+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10538+ DefaultConfig ().Build ()
10539+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10540+ DefaultConfig ().Build ()
10541+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA512 ).
10542+ Images ([]Image {subImage1 , subImage2 }).Build ()
10543+
10544+ name := "algo-sha256"
10545+ tag := "multiarch"
10546+
10547+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10548+ So (err , ShouldBeNil )
10549+
10550+ client := resty .New ()
10551+
10552+ // The server picks canonical digests when tags are pushed
10553+ // See https://github.com/opencontainers/distribution-spec/issues/494
10554+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10555+ // but there is no way to specify a client preference
10556+ // so all we can do is verify the correct algorithm is returned
10557+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10558+
10559+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10560+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10561+
10562+ // While the expected multiarch manifest digest is always using the canonical algorithm
10563+ // the sub-imgage manifest digest can use any algorith
10564+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10565+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10566+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10567+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10568+ })
10569+
10570+ Convey ("Test SHA384 multi-arch image" , t , func () {
10571+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10572+ DefaultConfig ().Build ()
10573+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10574+ DefaultConfig ().Build ()
10575+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA384 ).
10576+ Images ([]Image {subImage1 , subImage2 }).Build ()
10577+
10578+ name := "algo-sha384"
10579+ tag := "multiarch"
10580+
10581+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10582+ So (err , ShouldBeNil )
10583+
10584+ client := resty .New ()
10585+
10586+ // The server picks canonical digests when tags are pushed
10587+ // See https://github.com/opencontainers/distribution-spec/issues/494
10588+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10589+ // but there is no way to specify a client preference
10590+ // so all we can do is verify the correct algorithm is returned
10591+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10592+
10593+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10594+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10595+
10596+ // While the expected multiarch manifest digest is always using the canonical algorithm
10597+ // the sub-imgage manifest digest can use any algorith
10598+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10599+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10600+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10601+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10602+ })
10603+ }
10604+
10605+ func verifyReturnedManifestDigest (t * testing.T , client * resty.Client , baseURL , repoName ,
10606+ reference , expectedDigestStr string ,
10607+ ) {
10608+ t .Helper ()
10609+
10610+ t .Logf ("Verify Docker-Content-Digest returned for repo %s reference %s is %s" ,
10611+ repoName , reference , expectedDigestStr )
10612+
10613+ getResponse , err := client .R ().Get (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10614+ So (err , ShouldBeNil )
10615+ So (getResponse , ShouldNotBeNil )
10616+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10617+
10618+ contentDigestStr := getResponse .Header ().Get ("Docker-Content-Digest" )
10619+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10620+
10621+ getResponse , err = client .R ().Head (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10622+ So (err , ShouldBeNil )
10623+ So (getResponse , ShouldNotBeNil )
10624+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10625+
10626+ contentDigestStr = getResponse .Header ().Get ("Docker-Content-Digest" )
10627+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10628+ }
10629+
1046910630func getEmptyImageConfig () ([]byte , godigest.Digest ) {
1047010631 config := ispec.Image {}
1047110632
0 commit comments