@@ -10533,6 +10533,167 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL, user str
1053310533 })
1053410534}
1053510535
10536+ func TestSupportedDigestAlgorithms (t * testing.T ) {
10537+ port := test .GetFreePort ()
10538+ baseURL := test .GetBaseURL (port )
10539+
10540+ conf := config .New ()
10541+ conf .HTTP .Port = port
10542+
10543+ dir := t .TempDir ()
10544+
10545+ ctlr := api .NewController (conf )
10546+ ctlr .Config .Storage .RootDirectory = dir
10547+ ctlr .Config .Storage .Dedupe = false
10548+ ctlr .Config .Storage .GC = false
10549+
10550+ cm := test .NewControllerManager (ctlr )
10551+
10552+ cm .StartAndWait (port )
10553+ defer cm .StopServer ()
10554+
10555+ Convey ("Test SHA512 single-arch image" , t , func () {
10556+ image := CreateImageWithDigestAlgorithm (godigest .SHA512 ).
10557+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10558+
10559+ name := "algo-sha256"
10560+ tag := "singlearch"
10561+
10562+ err := UploadImage (image , baseURL , name , tag )
10563+ So (err , ShouldBeNil )
10564+
10565+ client := resty .New ()
10566+
10567+ // The server picks canonical digests when tags are pushed
10568+ // See https://github.com/opencontainers/distribution-spec/issues/494
10569+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10570+ // but there is no way to specify a client preference
10571+ // so all we can do is verify the correct algorithm is returned
10572+
10573+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10574+
10575+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10576+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10577+ })
10578+
10579+ Convey ("Test SHA384 single-arch image" , t , func () {
10580+ image := CreateImageWithDigestAlgorithm (godigest .SHA384 ).
10581+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10582+
10583+ name := "algo-sha384"
10584+ tag := "singlearch"
10585+
10586+ err := UploadImage (image , baseURL , name , tag )
10587+ So (err , ShouldBeNil )
10588+
10589+ client := resty .New ()
10590+
10591+ // The server picks canonical digests when tags are pushed
10592+ // See https://github.com/opencontainers/distribution-spec/issues/494
10593+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10594+ // but there is no way to specify a client preference
10595+ // so all we can do is verify the correct algorithm is returned
10596+
10597+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10598+
10599+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10600+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10601+ })
10602+
10603+ Convey ("Test SHA512 multi-arch image" , t , func () {
10604+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10605+ DefaultConfig ().Build ()
10606+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10607+ DefaultConfig ().Build ()
10608+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA512 ).
10609+ Images ([]Image {subImage1 , subImage2 }).Build ()
10610+
10611+ name := "algo-sha256"
10612+ tag := "multiarch"
10613+
10614+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10615+ So (err , ShouldBeNil )
10616+
10617+ client := resty .New ()
10618+
10619+ // The server picks canonical digests when tags are pushed
10620+ // See https://github.com/opencontainers/distribution-spec/issues/494
10621+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10622+ // but there is no way to specify a client preference
10623+ // so all we can do is verify the correct algorithm is returned
10624+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10625+
10626+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10627+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10628+
10629+ // While the expected multiarch manifest digest is always using the canonical algorithm
10630+ // the sub-imgage manifest digest can use any algorith
10631+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10632+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10633+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10634+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10635+ })
10636+
10637+ Convey ("Test SHA384 multi-arch image" , t , func () {
10638+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10639+ DefaultConfig ().Build ()
10640+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10641+ DefaultConfig ().Build ()
10642+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA384 ).
10643+ Images ([]Image {subImage1 , subImage2 }).Build ()
10644+
10645+ name := "algo-sha384"
10646+ tag := "multiarch"
10647+
10648+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10649+ So (err , ShouldBeNil )
10650+
10651+ client := resty .New ()
10652+
10653+ // The server picks canonical digests when tags are pushed
10654+ // See https://github.com/opencontainers/distribution-spec/issues/494
10655+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10656+ // but there is no way to specify a client preference
10657+ // so all we can do is verify the correct algorithm is returned
10658+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10659+
10660+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10661+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10662+
10663+ // While the expected multiarch manifest digest is always using the canonical algorithm
10664+ // the sub-imgage manifest digest can use any algorith
10665+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10666+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10667+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10668+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10669+ })
10670+ }
10671+
10672+ func verifyReturnedManifestDigest (t * testing.T , client * resty.Client , baseURL , repoName ,
10673+ reference , expectedDigestStr string ,
10674+ ) {
10675+ t .Helper ()
10676+
10677+ t .Logf ("Verify Docker-Content-Digest returned for repo %s reference %s is %s" ,
10678+ repoName , reference , expectedDigestStr )
10679+
10680+ getResponse , err := client .R ().Get (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10681+ So (err , ShouldBeNil )
10682+ So (getResponse , ShouldNotBeNil )
10683+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10684+
10685+ contentDigestStr := getResponse .Header ().Get ("Docker-Content-Digest" )
10686+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10687+
10688+ getResponse , err = client .R ().Head (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10689+ So (err , ShouldBeNil )
10690+ So (getResponse , ShouldNotBeNil )
10691+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10692+
10693+ contentDigestStr = getResponse .Header ().Get ("Docker-Content-Digest" )
10694+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10695+ }
10696+
1053610697func getEmptyImageConfig () ([]byte , godigest.Digest ) {
1053710698 config := ispec.Image {}
1053810699
0 commit comments