@@ -10650,6 +10650,167 @@ func RunAuthorizationTests(t *testing.T, client *resty.Client, baseURL, user str
1065010650 })
1065110651}
1065210652
10653+ func TestSupportedDigestAlgorithms (t * testing.T ) {
10654+ port := test .GetFreePort ()
10655+ baseURL := test .GetBaseURL (port )
10656+
10657+ conf := config .New ()
10658+ conf .HTTP .Port = port
10659+
10660+ dir := t .TempDir ()
10661+
10662+ ctlr := api .NewController (conf )
10663+ ctlr .Config .Storage .RootDirectory = dir
10664+ ctlr .Config .Storage .Dedupe = false
10665+ ctlr .Config .Storage .GC = false
10666+
10667+ cm := test .NewControllerManager (ctlr )
10668+
10669+ cm .StartAndWait (port )
10670+ defer cm .StopServer ()
10671+
10672+ Convey ("Test SHA512 single-arch image" , t , func () {
10673+ image := CreateImageWithDigestAlgorithm (godigest .SHA512 ).
10674+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10675+
10676+ name := "algo-sha256"
10677+ tag := "singlearch"
10678+
10679+ err := UploadImage (image , baseURL , name , tag )
10680+ So (err , ShouldBeNil )
10681+
10682+ client := resty .New ()
10683+
10684+ // The server picks canonical digests when tags are pushed
10685+ // See https://github.com/opencontainers/distribution-spec/issues/494
10686+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10687+ // but there is no way to specify a client preference
10688+ // so all we can do is verify the correct algorithm is returned
10689+
10690+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10691+
10692+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10693+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10694+ })
10695+
10696+ Convey ("Test SHA384 single-arch image" , t , func () {
10697+ image := CreateImageWithDigestAlgorithm (godigest .SHA384 ).
10698+ RandomLayers (1 , 10 ).DefaultConfig ().Build ()
10699+
10700+ name := "algo-sha384"
10701+ tag := "singlearch"
10702+
10703+ err := UploadImage (image , baseURL , name , tag )
10704+ So (err , ShouldBeNil )
10705+
10706+ client := resty .New ()
10707+
10708+ // The server picks canonical digests when tags are pushed
10709+ // See https://github.com/opencontainers/distribution-spec/issues/494
10710+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10711+ // but there is no way to specify a client preference
10712+ // so all we can do is verify the correct algorithm is returned
10713+
10714+ expectedDigestStr := image .DigestForAlgorithm (godigest .Canonical ).String ()
10715+
10716+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10717+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10718+ })
10719+
10720+ Convey ("Test SHA512 multi-arch image" , t , func () {
10721+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10722+ DefaultConfig ().Build ()
10723+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA512 ).RandomLayers (1 , 10 ).
10724+ DefaultConfig ().Build ()
10725+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA512 ).
10726+ Images ([]Image {subImage1 , subImage2 }).Build ()
10727+
10728+ name := "algo-sha256"
10729+ tag := "multiarch"
10730+
10731+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10732+ So (err , ShouldBeNil )
10733+
10734+ client := resty .New ()
10735+
10736+ // The server picks canonical digests when tags are pushed
10737+ // See https://github.com/opencontainers/distribution-spec/issues/494
10738+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10739+ // but there is no way to specify a client preference
10740+ // so all we can do is verify the correct algorithm is returned
10741+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10742+
10743+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10744+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10745+
10746+ // While the expected multiarch manifest digest is always using the canonical algorithm
10747+ // the sub-imgage manifest digest can use any algorith
10748+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10749+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10750+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10751+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10752+ })
10753+
10754+ Convey ("Test SHA384 multi-arch image" , t , func () {
10755+ subImage1 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10756+ DefaultConfig ().Build ()
10757+ subImage2 := CreateImageWithDigestAlgorithm (godigest .SHA384 ).RandomLayers (1 , 10 ).
10758+ DefaultConfig ().Build ()
10759+ multiarch := CreateMultiarchWithDigestAlgorithm (godigest .SHA384 ).
10760+ Images ([]Image {subImage1 , subImage2 }).Build ()
10761+
10762+ name := "algo-sha384"
10763+ tag := "multiarch"
10764+
10765+ err := UploadMultiarchImage (multiarch , baseURL , name , tag )
10766+ So (err , ShouldBeNil )
10767+
10768+ client := resty .New ()
10769+
10770+ // The server picks canonical digests when tags are pushed
10771+ // See https://github.com/opencontainers/distribution-spec/issues/494
10772+ // It would be nice to be able to push tags with other digest algorithms and verify those are returned
10773+ // but there is no way to specify a client preference
10774+ // so all we can do is verify the correct algorithm is returned
10775+ expectedDigestStr := multiarch .DigestForAlgorithm (godigest .Canonical ).String ()
10776+
10777+ verifyReturnedManifestDigest (t , client , baseURL , name , tag , expectedDigestStr )
10778+ verifyReturnedManifestDigest (t , client , baseURL , name , expectedDigestStr , expectedDigestStr )
10779+
10780+ // While the expected multiarch manifest digest is always using the canonical algorithm
10781+ // the sub-imgage manifest digest can use any algorith
10782+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10783+ subImage1 .ManifestDescriptor .Digest .String (), subImage1 .ManifestDescriptor .Digest .String ())
10784+ verifyReturnedManifestDigest (t , client , baseURL , name ,
10785+ subImage2 .ManifestDescriptor .Digest .String (), subImage2 .ManifestDescriptor .Digest .String ())
10786+ })
10787+ }
10788+
10789+ func verifyReturnedManifestDigest (t * testing.T , client * resty.Client , baseURL , repoName ,
10790+ reference , expectedDigestStr string ,
10791+ ) {
10792+ t .Helper ()
10793+
10794+ t .Logf ("Verify Docker-Content-Digest returned for repo %s reference %s is %s" ,
10795+ repoName , reference , expectedDigestStr )
10796+
10797+ getResponse , err := client .R ().Get (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10798+ So (err , ShouldBeNil )
10799+ So (getResponse , ShouldNotBeNil )
10800+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10801+
10802+ contentDigestStr := getResponse .Header ().Get ("Docker-Content-Digest" )
10803+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10804+
10805+ getResponse , err = client .R ().Head (fmt .Sprintf ("%s/v2/%s/manifests/%s" , baseURL , repoName , reference ))
10806+ So (err , ShouldBeNil )
10807+ So (getResponse , ShouldNotBeNil )
10808+ So (getResponse .StatusCode (), ShouldEqual , http .StatusOK )
10809+
10810+ contentDigestStr = getResponse .Header ().Get ("Docker-Content-Digest" )
10811+ So (contentDigestStr , ShouldEqual , expectedDigestStr )
10812+ }
10813+
1065310814func getEmptyImageConfig () ([]byte , godigest.Digest ) {
1065410815 config := ispec.Image {}
1065510816
0 commit comments