|
| 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 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/opencontainers/image-spec/schema" |
| 22 | +) |
| 23 | + |
| 24 | +func TestDescriptor(t *testing.T) { |
| 25 | + for i, tt := range []struct { |
| 26 | + descriptor string |
| 27 | + fail bool |
| 28 | + }{ |
| 29 | + // valid descriptor |
| 30 | + { |
| 31 | + descriptor: ` |
| 32 | +{ |
| 33 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 34 | + "size": 7682, |
| 35 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 36 | +} |
| 37 | +`, |
| 38 | + fail: false, |
| 39 | + }, |
| 40 | + |
| 41 | + // expected failure: mediaType missing |
| 42 | + { |
| 43 | + descriptor: ` |
| 44 | +{ |
| 45 | + "size": 7682, |
| 46 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 47 | +} |
| 48 | +`, |
| 49 | + fail: true, |
| 50 | + }, |
| 51 | + |
| 52 | + // expected failure: mediaType does not match pattern (no subtype) |
| 53 | + { |
| 54 | + descriptor: ` |
| 55 | +{ |
| 56 | + "mediaType": "application", |
| 57 | + "size": 7682, |
| 58 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 59 | +} |
| 60 | +`, |
| 61 | + fail: true, |
| 62 | + }, |
| 63 | + |
| 64 | + // expected failure: mediaType does not match pattern (invalid first type character) |
| 65 | + { |
| 66 | + descriptor: ` |
| 67 | +{ |
| 68 | + "mediaType": ".foo/bar", |
| 69 | + "size": 7682, |
| 70 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 71 | +} |
| 72 | +`, |
| 73 | + fail: true, |
| 74 | + }, |
| 75 | + |
| 76 | + // expected failure: mediaType does not match pattern (invalid first subtype character) |
| 77 | + { |
| 78 | + descriptor: ` |
| 79 | +{ |
| 80 | + "mediaType": "foo/.bar", |
| 81 | + "size": 7682, |
| 82 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 83 | +} |
| 84 | +`, |
| 85 | + fail: true, |
| 86 | + }, |
| 87 | + |
| 88 | + // expected success: mediaType has type and subtype as long as possible |
| 89 | + { |
| 90 | + descriptor: ` |
| 91 | +{ |
| 92 | + "mediaType": "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567", |
| 93 | + "size": 7682, |
| 94 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 95 | +} |
| 96 | +`, |
| 97 | + fail: false, |
| 98 | + }, |
| 99 | + |
| 100 | + // expected success: mediaType does not match pattern (type too long) |
| 101 | + { |
| 102 | + descriptor: ` |
| 103 | +{ |
| 104 | + "mediaType": "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678/bar", |
| 105 | + "size": 7682, |
| 106 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 107 | +} |
| 108 | +`, |
| 109 | + fail: true, |
| 110 | + }, |
| 111 | + |
| 112 | + // expected success: mediaType does not match pattern (subtype too long) |
| 113 | + { |
| 114 | + descriptor: ` |
| 115 | +{ |
| 116 | + "mediaType": "foo/12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678", |
| 117 | + "size": 7682, |
| 118 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 119 | +} |
| 120 | +`, |
| 121 | + fail: true, |
| 122 | + }, |
| 123 | + |
| 124 | + // expected failure: size missing |
| 125 | + { |
| 126 | + descriptor: ` |
| 127 | +{ |
| 128 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 129 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 130 | +} |
| 131 | +`, |
| 132 | + fail: true, |
| 133 | + }, |
| 134 | + |
| 135 | + // expected failure: size is a string, expected integer |
| 136 | + { |
| 137 | + descriptor: ` |
| 138 | +{ |
| 139 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 140 | + "size": "7682", |
| 141 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 142 | +} |
| 143 | +`, |
| 144 | + fail: true, |
| 145 | + }, |
| 146 | + |
| 147 | + // expected failure: digest missing |
| 148 | + { |
| 149 | + descriptor: ` |
| 150 | +{ |
| 151 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 152 | + "size": 7682 |
| 153 | +} |
| 154 | +`, |
| 155 | + fail: true, |
| 156 | + }, |
| 157 | + |
| 158 | + // expected failure: digest does not match pattern (no algorithm) |
| 159 | + { |
| 160 | + descriptor: ` |
| 161 | +{ |
| 162 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 163 | + "size": 7682, |
| 164 | + "digest": ":5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 165 | +} |
| 166 | +`, |
| 167 | + fail: true, |
| 168 | + }, |
| 169 | + |
| 170 | + // expected failure: digest does not match pattern (no hash) |
| 171 | + { |
| 172 | + descriptor: ` |
| 173 | +{ |
| 174 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 175 | + "size": 7682, |
| 176 | + "digest": "sha256" |
| 177 | +} |
| 178 | +`, |
| 179 | + fail: true, |
| 180 | + }, |
| 181 | + |
| 182 | + // expected failure: digest does not match pattern (invalid aglorithm characters) |
| 183 | + { |
| 184 | + descriptor: ` |
| 185 | +{ |
| 186 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 187 | + "size": 7682, |
| 188 | + "digest": "SHA256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 189 | +} |
| 190 | +`, |
| 191 | + fail: true, |
| 192 | + }, |
| 193 | + |
| 194 | + // expected failure: digest does not match pattern (invalid hash characters) |
| 195 | + { |
| 196 | + descriptor: ` |
| 197 | +{ |
| 198 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 199 | + "size": 7682, |
| 200 | + "digest": "sha256:5B0BCABD1ED22E9FB1310CF6C2DEC7CDEF19F0AD69EFA1F392E94A4333501270" |
| 201 | +} |
| 202 | +`, |
| 203 | + fail: true, |
| 204 | + }, |
| 205 | + } { |
| 206 | + r := strings.NewReader(tt.descriptor) |
| 207 | + err := schema.MediaTypeDescriptor.Validate(r) |
| 208 | + |
| 209 | + if got := err != nil; tt.fail != got { |
| 210 | + t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err) |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments