github.com/containerd/Containerd@v1.4.13/images/image_test.go (about) 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 ) 25 26 func TestValidateMediaType(t *testing.T) { 27 docTests := []struct { 28 mt string 29 index bool 30 }{ 31 {MediaTypeDockerSchema2Manifest, false}, 32 {ocispec.MediaTypeImageManifest, false}, 33 {MediaTypeDockerSchema2ManifestList, true}, 34 {ocispec.MediaTypeImageIndex, true}, 35 } 36 for _, tc := range docTests { 37 t.Run("manifest-"+tc.mt, func(t *testing.T) { 38 manifest := ocispec.Manifest{ 39 Config: ocispec.Descriptor{Size: 1}, 40 Layers: []ocispec.Descriptor{{Size: 2}}, 41 } 42 b, err := json.Marshal(manifest) 43 if err != nil { 44 t.Fatal("failed to marshal manifest", err) 45 } 46 47 err = validateMediaType(b, tc.mt) 48 if tc.index { 49 if err == nil { 50 t.Error("manifest should not be a valid index") 51 } 52 } else { 53 if err != nil { 54 t.Error("manifest should be valid") 55 } 56 } 57 }) 58 t.Run("index-"+tc.mt, func(t *testing.T) { 59 index := ocispec.Index{ 60 Manifests: []ocispec.Descriptor{{Size: 1}}, 61 } 62 b, err := json.Marshal(index) 63 if err != nil { 64 t.Fatal("failed to marshal index", err) 65 } 66 67 err = validateMediaType(b, tc.mt) 68 if tc.index { 69 if err != nil { 70 t.Error("index should be valid") 71 } 72 } else { 73 if err == nil { 74 t.Error("index should not be a valid manifest") 75 } 76 } 77 }) 78 } 79 80 mtTests := []struct { 81 mt string 82 valid []string 83 invalid []string 84 }{{ 85 MediaTypeDockerSchema2Manifest, 86 []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, 87 []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, 88 }, { 89 ocispec.MediaTypeImageManifest, 90 []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, 91 []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, 92 }, { 93 MediaTypeDockerSchema2ManifestList, 94 []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, 95 []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, 96 }, { 97 ocispec.MediaTypeImageIndex, 98 []string{MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex}, 99 []string{MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest}, 100 }} 101 for _, tc := range mtTests { 102 for _, v := range tc.valid { 103 t.Run("valid-"+tc.mt+"-"+v, func(t *testing.T) { 104 doc := struct { 105 MediaType string `json:"mediaType"` 106 }{MediaType: v} 107 b, err := json.Marshal(doc) 108 if err != nil { 109 t.Fatal("failed to marshal document", err) 110 } 111 112 err = validateMediaType(b, tc.mt) 113 if err != nil { 114 t.Error("document should be valid", err) 115 } 116 }) 117 } 118 for _, iv := range tc.invalid { 119 t.Run("invalid-"+tc.mt+"-"+iv, func(t *testing.T) { 120 doc := struct { 121 MediaType string `json:"mediaType"` 122 }{MediaType: iv} 123 b, err := json.Marshal(doc) 124 if err != nil { 125 t.Fatal("failed to marshal document", err) 126 } 127 128 err = validateMediaType(b, tc.mt) 129 if err == nil { 130 t.Error("document should not be valid") 131 } 132 }) 133 } 134 } 135 t.Run("schema1", func(t *testing.T) { 136 doc := struct { 137 FSLayers []string `json:"fsLayers"` 138 }{FSLayers: []string{"1"}} 139 b, err := json.Marshal(doc) 140 if err != nil { 141 t.Fatal("failed to marshal document", err) 142 } 143 144 err = validateMediaType(b, "") 145 if err == nil { 146 t.Error("document should not be valid") 147 } 148 149 }) 150 }