@@ -10567,6 +10567,167 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL, user str
1056710567 })
1056810568}
1056910569
10570+ func TestSupportedDigestAlgorithms (t * testing.T ) {
10571+ port := test .GetFreePort ()
10572+ baseURL := test .GetBaseURL (port )
10573+
10574+ conf := config .New ()
10575+ conf .HTTP .Port = port
10576+
10577+ dir := t .TempDir ()
10578+
10579+ ctlr := api .NewController (conf )
10580+ ctlr .Config .Storage .RootDirectory = dir
10581+ ctlr .Config .Storage .Dedupe = false
10582+ ctlr .Config .Storage .GC = false
10583+
10584+ cm := test .NewControllerManager (ctlr )
10585+
10586+ cm .StartAndWait (port )
10587+ defer cm .StopServer ()
10588+
10589+ Convey ("Test SHA512 single-arch image" , t , func () {
10590+ image := CreateImageWithDigestAlgorithm (godigest .SHA512 ).
10591+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10592+
10593+ name := "algo-sha256"
10594+ tag := "singlearch"
10595+
10596+ err := UploadImage (image , baseURL , name , tag )
10597+ So (err , ShouldBeNil )
10598+
10599+ client := resty .New ()
10600+
10601+ // The server picks canonical digests when tags are pushed
10602+ // See https://github.com/opencontainers/distribution-spec/issues/494
10603+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10604+ // but there is no way to specify a client preference
10605+ // so all we can do is verify the correct algorithm is returned
10606+
10607+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10608+
10609+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10610+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10611+ })
10612+
10613+ Convey ("Test SHA384 single-arch image" , t , func () {
10614+ image := CreateImageWithDigestAlgorithm (godigest .SHA384 ).
10615+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10616+
10617+ name := "algo-sha384"
10618+ tag := "singlearch"
10619+
10620+ err := UploadImage (image , baseURL , name , tag )
10621+ So (err , ShouldBeNil )
10622+
10623+ client := resty .New ()
10624+
10625+ // The server picks canonical digests when tags are pushed
10626+ // See https://github.com/opencontainers/distribution-spec/issues/494
10627+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10628+ // but there is no way to specify a client preference
10629+ // so all we can do is verify the correct algorithm is returned
10630+
10631+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10632+
10633+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10634+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10635+ })
10636+
10637+ Convey ("Test SHA512 multi-arch image" , t , func () {
10638+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10639+ DefaultConfig ().Build ()
10640+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10641+ DefaultConfig ().Build ()
10642+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA512 ).
10643+ Images ([]Image {subImage1 , subImage2 }).Build ()
10644+
10645+ name := "algo-sha256"
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+ Convey ("Test SHA384 multi-arch image" , t , func () {
10672+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10673+ DefaultConfig ().Build ()
10674+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10675+ DefaultConfig ().Build ()
10676+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA384 ).
10677+ Images ([]Image {subImage1 , subImage2 }).Build ()
10678+
10679+ name := "algo-sha384"
10680+ tag := "multiarch"
10681+
10682+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10683+ So (err , ShouldBeNil )
10684+
10685+ client := resty .New ()
10686+
10687+ // The server picks canonical digests when tags are pushed
10688+ // See https://github.com/opencontainers/distribution-spec/issues/494
10689+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10690+ // but there is no way to specify a client preference
10691+ // so all we can do is verify the correct algorithm is returned
10692+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10693+
10694+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10695+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10696+
10697+ // While the expected multiarch manifest digest is always using the canonical algorithm
10698+ // the sub-imgage manifest digest can use any algorith
10699+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10700+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10701+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10702+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10703+ })
10704+ }
10705+
10706+ func verifyReturnedManifestDigest (t * testing.T , client * resty.Client , baseURL , repoName ,
10707+ reference , expectedDigestStr string ,
10708+ ) {
10709+ t .Helper ()
10710+
10711+ t .Logf ("Verify Docker-Content-Digest returned for repo %s reference %s is %s" ,
10712+ repoName , reference , expectedDigestStr )
10713+
10714+ getResponse , err := client .R ().Get (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10715+ So (err , ShouldBeNil )
10716+ So (getResponse , ShouldNotBeNil )
10717+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10718+
10719+ contentDigestStr := getResponse .Header ().Get ("Docker-Content-Digest" )
10720+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10721+
10722+ getResponse , err = client .R ().Head (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10723+ So (err , ShouldBeNil )
10724+ So (getResponse , ShouldNotBeNil )
10725+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10726+
10727+ contentDigestStr = getResponse .Header ().Get ("Docker-Content-Digest" )
10728+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10729+ }
10730+
1057010731func getEmptyImageConfig () ([]byte , godigest.Digest ) {
1057110732 config := ispec.Image {}
1057210733
0 commit comments