|
| 1 | +// Copyright 2016 The Linux Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package schema_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/sha256" |
| 19 | + "encoding/hex" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/opencontainers/image-spec/schema" |
| 25 | +) |
| 26 | + |
| 27 | +var compatMap = map[string]string{ |
| 28 | + "application/vnd.docker.distribution.manifest.list.v2+json": "application/vnd.oci.image.manifest.list.v1+json", |
| 29 | + "application/vnd.docker.distribution.manifest.v2+json": "application/vnd.oci.image.manifest.v1+json", |
| 30 | + "application/vnd.docker.image.rootfs.diff.tar.gzip": "application/vnd.oci.image.rootfs.tar.gzip", |
| 31 | + "application/vnd.docker.container.image.v1+json": "application/vnd.oci.image.serialization.config.v1+json", |
| 32 | +} |
| 33 | + |
| 34 | +// convertFormats converts Docker v2.2 image format JSON documents to OCI |
| 35 | +// format by simply replacing instances of the strings found in the compatMap |
| 36 | +// found in the input string. |
| 37 | +func convertFormats(input string) string { |
| 38 | + out := input |
| 39 | + for k, v := range compatMap { |
| 40 | + out = strings.Replace(out, v, k, -1) |
| 41 | + } |
| 42 | + return out |
| 43 | +} |
| 44 | + |
| 45 | +func TestBackwardsCompatibilityManifest(t *testing.T) { |
| 46 | + for i, tt := range []struct { |
| 47 | + manifest string |
| 48 | + digest string |
| 49 | + fail bool |
| 50 | + }{ |
| 51 | + // manifest pulled from docker hub using hash value |
| 52 | + // |
| 53 | + // curl -L -H "Authorization: Bearer ..." -H \ |
| 54 | + // "Accept: application/vnd.docker.distribution.manifest.v2+json" \ |
| 55 | + // https://registry-1.docker.io/v2/library/docker/manifests/sha256:888206c77cd2811ec47e752ba291e5b7734e3ef137dfd222daadaca39a9f17bc |
| 56 | + { |
| 57 | + digest: "sha256:888206c77cd2811ec47e752ba291e5b7734e3ef137dfd222daadaca39a9f17bc", |
| 58 | + manifest: `{ |
| 59 | + "schemaVersion": 2, |
| 60 | + "mediaType": "application/vnd.docker.distribution.manifest.v2+json", |
| 61 | + "config": { |
| 62 | + "mediaType": "application/octet-stream", |
| 63 | + "size": 3210, |
| 64 | + "digest": "sha256:5359a4f250650c20227055957e353e8f8a74152f35fe36f00b6b1f9fc19c8861" |
| 65 | + }, |
| 66 | + "layers": [ |
| 67 | + { |
| 68 | + "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 69 | + "size": 2310272, |
| 70 | + "digest": "sha256:fae91920dcd4542f97c9350b3157139a5d901362c2abec284de5ebd1b45b4957" |
| 71 | + }, |
| 72 | + { |
| 73 | + "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 74 | + "size": 913022, |
| 75 | + "digest": "sha256:f384f6ab36adad485192f09379c0b58dc612a3cde82c551e082a7c29a87c95da" |
| 76 | + }, |
| 77 | + { |
| 78 | + "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 79 | + "size": 9861668, |
| 80 | + "digest": "sha256:ed0d2dd5e1a0e5e650a330a864c8a122e9aa91fa6ba9ac6f0bd1882e59df55e7" |
| 81 | + }, |
| 82 | + { |
| 83 | + "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 84 | + "size": 465, |
| 85 | + "digest": "sha256:ec4d00b58417c45f7ddcfde7bcad8c9d62a7d6d5d17cdc1f7d79bcb2e22c1491" |
| 86 | + } |
| 87 | + ] |
| 88 | +}`, |
| 89 | + fail: false, |
| 90 | + }, |
| 91 | + } { |
| 92 | + sum := sha256.Sum256([]byte(tt.manifest)) |
| 93 | + got := fmt.Sprintf("sha256:%s", hex.EncodeToString(sum[:])) |
| 94 | + if tt.digest != got { |
| 95 | + t.Errorf("test %d: expected digest %s but got %s", i, tt.digest, got) |
| 96 | + } |
| 97 | + |
| 98 | + manifest := convertFormats(tt.manifest) |
| 99 | + r := strings.NewReader(manifest) |
| 100 | + err := schema.MediaTypeManifest.Validate(r) |
| 101 | + |
| 102 | + if got := err != nil; tt.fail != got { |
| 103 | + t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments