|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package images |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "testing" |
| 22 | + |
| 23 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestValidateMediaType(t *testing.T) { |
| 29 | + docTests := []struct { |
| 30 | + mt string |
| 31 | + index bool |
| 32 | + }{ |
| 33 | + {MediaTypeDockerSchema2Manifest, false}, |
| 34 | + {ocispec.MediaTypeImageManifest, false}, |
| 35 | + {MediaTypeDockerSchema2ManifestList, true}, |
| 36 | + {ocispec.MediaTypeImageIndex, true}, |
| 37 | + } |
| 38 | + for _, tc := range docTests { |
| 39 | + t.Run("manifest-"+tc.mt, func(t *testing.T) { |
| 40 | + manifest := ocispec.Manifest{ |
| 41 | + Config: ocispec.Descriptor{Size: 1}, |
| 42 | + Layers: []ocispec.Descriptor{{Size: 2}}, |
| 43 | + } |
| 44 | + b, err := json.Marshal(manifest) |
| 45 | + require.NoError(t, err, "failed to marshal manifest") |
| 46 | + |
| 47 | + err = validateMediaType(b, tc.mt) |
| 48 | + if tc.index { |
| 49 | + assert.Error(t, err, "manifest should not be a valid index") |
| 50 | + } else { |
| 51 | + assert.NoError(t, err, "manifest should be valid") |
| 52 | + } |
| 53 | + }) |
| 54 | + t.Run("index-"+tc.mt, func(t *testing.T) { |
| 55 | + index := ocispec.Index{ |
| 56 | + Manifests: []ocispec.Descriptor{{Size: 1}}, |
| 57 | + } |
| 58 | + b, err := json.Marshal(index) |
| 59 | + require.NoError(t, err, "failed to marshal index") |
| 60 | + |
| 61 | + err = validateMediaType(b, tc.mt) |
| 62 | + if tc.index { |
| 63 | + assert.NoError(t, err, "index should be valid") |
| 64 | + } else { |
| 65 | + assert.Error(t, err, "index should not be a valid manifest") |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + mtTests := []struct { |
| 71 | + mt string |
| 72 | + valid []string |
| 73 | + invalid []string |
| 74 | + }{{ |
| 75 | + MediaTypeDockerSchema2Manifest, |
| 76 | + []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, |
| 77 | + []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, |
| 78 | + }, { |
| 79 | + ocispec.MediaTypeImageManifest, |
| 80 | + []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, |
| 81 | + []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, |
| 82 | + }, { |
| 83 | + MediaTypeDockerSchema2ManifestList, |
| 84 | + []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, |
| 85 | + []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, |
| 86 | + }, { |
| 87 | + ocispec.MediaTypeImageIndex, |
| 88 | + []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, |
| 89 | + []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, |
| 90 | + }} |
| 91 | + for _, tc := range mtTests { |
| 92 | + for _, v := range tc.valid { |
| 93 | + t.Run("valid-"+tc.mt+"-"+v, func(t *testing.T) { |
| 94 | + doc := struct { |
| 95 | + MediaType string `json:"mediaType"` |
| 96 | + }{MediaType: v} |
| 97 | + b, err := json.Marshal(doc) |
| 98 | + require.NoError(t, err, "failed to marshal document") |
| 99 | + |
| 100 | + err = validateMediaType(b, tc.mt) |
| 101 | + assert.NoError(t, err, "document should be valid") |
| 102 | + }) |
| 103 | + } |
| 104 | + for _, iv := range tc.invalid { |
| 105 | + t.Run("invalid-"+tc.mt+"-"+iv, func(t *testing.T) { |
| 106 | + doc := struct { |
| 107 | + MediaType string `json:"mediaType"` |
| 108 | + }{MediaType: iv} |
| 109 | + b, err := json.Marshal(doc) |
| 110 | + require.NoError(t, err, "failed to marshal document") |
| 111 | + |
| 112 | + err = validateMediaType(b, tc.mt) |
| 113 | + assert.Error(t, err, "document should not be valid") |
| 114 | + }) |
| 115 | + } |
| 116 | + } |
| 117 | + t.Run("schema1", func(t *testing.T) { |
| 118 | + doc := struct { |
| 119 | + FSLayers []string `json:"fsLayers"` |
| 120 | + }{FSLayers: []string{"1"}} |
| 121 | + b, err := json.Marshal(doc) |
| 122 | + require.NoError(t, err, "failed to marshal document") |
| 123 | + |
| 124 | + err = validateMediaType(b, "") |
| 125 | + assert.Error(t, err, "document should not be valid") |
| 126 | + }) |
| 127 | +} |
0 commit comments