1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
package manifest
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/containers/image/v5/pkg/compression"
compressiontypes "github.com/containers/image/v5/pkg/compression/types"
"github.com/containers/image/v5/types"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test that parser() rejects all of the provided manifest fixtures.
// Intended to help test manifest parsers' detection of schema mismatches.
func testManifestFixturesAreRejected(t *testing.T, parser func([]byte) error, fixtures []string) {
for _, fixture := range fixtures {
manifest, err := os.ReadFile(filepath.Join("fixtures", fixture))
require.NoError(t, err, fixture)
err = parser(manifest)
assert.Error(t, err, fixture)
}
}
// Test that parser() rejects validManifest with an added top-level field with any of the provided field names.
// Intended to help test callers of validateUnambiguousManifestFormat.
func testValidManifestWithExtraFieldsIsRejected(t *testing.T, parser func([]byte) error,
validManifest []byte, fields []string) {
for _, field := range fields {
// end (the final '}') is not always at len(validManifest)-1 because the manifest can end with
// white space.
end := bytes.LastIndexByte(validManifest, '}')
require.NotEqual(t, end, -1)
updatedManifest := []byte(string(validManifest[:end]) +
fmt.Sprintf(`,"%s":[]}`, field))
err := parser(updatedManifest)
// Make sure it is the error from validateUnambiguousManifestFormat, not something that
// went wrong with creating updatedManifest.
assert.ErrorContains(t, err, "rejecting ambiguous manifest", field)
}
}
func TestLayerInfosToStrings(t *testing.T) {
strings := layerInfosToStrings([]LayerInfo{})
assert.Equal(t, []string{}, strings)
strings = layerInfosToStrings([]LayerInfo{
{
BlobInfo: types.BlobInfo{
MediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip",
Digest: "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4",
Size: 32,
},
EmptyLayer: true,
},
{
BlobInfo: types.BlobInfo{
MediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip",
Digest: "sha256:bbd6b22eb11afce63cc76f6bc41042d99f10d6024c96b655dafba930b8d25909",
Size: 8841833,
},
EmptyLayer: false,
},
{
BlobInfo: types.BlobInfo{
MediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip",
Digest: "sha256:960e52ecf8200cbd84e70eb2ad8678f4367e50d14357021872c10fa3fc5935fa",
Size: 291,
},
EmptyLayer: false,
},
{
BlobInfo: types.BlobInfo{
MediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip",
Digest: "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4",
Size: 32,
},
EmptyLayer: true,
},
})
assert.Equal(t, []string{
"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4",
"sha256:bbd6b22eb11afce63cc76f6bc41042d99f10d6024c96b655dafba930b8d25909",
"sha256:960e52ecf8200cbd84e70eb2ad8678f4367e50d14357021872c10fa3fc5935fa",
"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4",
}, strings)
}
func TestCompressionVariantMIMEType(t *testing.T) {
sets := []compressionMIMETypeSet{
{mtsUncompressed: "AU", compressiontypes.GzipAlgorithmName: "AG" /* No zstd variant */},
{mtsUncompressed: "BU", compressiontypes.GzipAlgorithmName: "BG", compressiontypes.ZstdAlgorithmName: mtsUnsupportedMIMEType},
{ /* No uncompressed variant */ compressiontypes.GzipAlgorithmName: "CG", compressiontypes.ZstdAlgorithmName: "CZ"},
{mtsUncompressed: "", compressiontypes.GzipAlgorithmName: "DG"},
}
for _, c := range []struct {
input string
algo *compressiontypes.Algorithm
expected string
}{
{"AU", nil, "AU"}, {"AU", &compression.Gzip, "AG"}, {"AU", &compression.Zstd, ""},
{"AG", nil, "AU"}, {"AG", &compression.Gzip, "AG"}, {"AG", &compression.Zstd, ""},
{"BU", &compression.Zstd, ""},
{"BG", &compression.Zstd, ""},
{"CG", nil, ""}, {"CG", &compression.Zstd, "CZ"},
{"CZ", nil, ""}, {"CZ", &compression.Gzip, "CG"},
{"DG", nil, ""},
{"unknown", nil, ""}, {"unknown", &compression.Gzip, ""},
{"", nil, ""}, {"", &compression.Gzip, ""},
} {
res, err := compressionVariantMIMEType(sets, c.input, c.algo)
if c.expected == "" {
assert.Error(t, err, c.input)
} else {
require.NoError(t, err, c.input)
assert.Equal(t, c.expected, res, c.input)
}
}
}
func TestUpdatedMIMEType(t *testing.T) {
// all known types, PreserveOriginal
preserve := []struct {
compression []compressionMIMETypeSet
mimeType string
}{
{schema2CompressionMIMETypeSets, DockerV2Schema1MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema1SignedMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ConfigMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2LayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2SchemaLayerMediaTypeUncompressed},
{schema2CompressionMIMETypeSets, DockerV2ListMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaTypeGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeDescriptor},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeLayoutHeader},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageManifest},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageIndex},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayer},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerZstd},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributable}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableGzip}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableZstd}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageConfig},
}
for i, c := range preserve {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.PreserveOriginal,
}
updatedType, err := updatedMIMEType(c.compression, c.mimeType, update)
require.NoErrorf(t, err, "%d: updatedMIMEType(%q, %+v) failed unexpectedly", i, c.mimeType, update)
assert.Equalf(t, c.mimeType, updatedType, "%d: updatedMIMEType(%q, %+v)", i, c.mimeType, update)
}
// known types where Decompress is expected to succeed
decompressSuccess := []struct {
compression []compressionMIMETypeSet
mimeType string
updatedType string
}{
{schema2CompressionMIMETypeSets, DockerV2SchemaLayerMediaTypeUncompressed, DockerV2SchemaLayerMediaTypeUncompressed},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaTypeGzip, DockerV2Schema2ForeignLayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2LayerMediaType, DockerV2SchemaLayerMediaTypeUncompressed},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaType, DockerV2Schema2ForeignLayerMediaType},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayer, imgspecv1.MediaTypeImageLayer},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerGzip, imgspecv1.MediaTypeImageLayer},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerZstd, imgspecv1.MediaTypeImageLayer},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributable, imgspecv1.MediaTypeImageLayerNonDistributable}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableGzip, imgspecv1.MediaTypeImageLayerNonDistributable}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableZstd, imgspecv1.MediaTypeImageLayerNonDistributable}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
}
for i, c := range decompressSuccess {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Decompress,
}
updatedType, err := updatedMIMEType(c.compression, c.mimeType, update)
require.NoErrorf(t, err, "%d: updatedMIMEType(%q, %+v) failed unexpectedly", i, c.mimeType, update)
assert.Equalf(t, c.updatedType, updatedType, "%d: updatedMIMEType(%q, %+v)", i, c.mimeType, update)
}
// known types where Decompress is expected to fail
decompressFailure := []struct {
compression []compressionMIMETypeSet
mimeType string
}{
{schema2CompressionMIMETypeSets, DockerV2Schema1MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema1SignedMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ConfigMediaType},
{schema2CompressionMIMETypeSets, DockerV2ListMediaType},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeDescriptor},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeLayoutHeader},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageManifest},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageIndex},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageConfig},
}
for i, c := range decompressFailure {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Decompress,
}
_, err := updatedMIMEType(c.compression, c.mimeType, update)
require.Errorf(t, err, "%d: updatedMIMEType(%q, %+v) should have failed", i, c.mimeType, update)
}
require.Equalf(t, len(preserve), len(decompressSuccess)+len(decompressFailure), "missing some decompression tests")
// all known types where Compress with gzip should succeed
compressGzipSuccess := []struct {
compression []compressionMIMETypeSet
mimeType string
updatedType string
}{
{schema2CompressionMIMETypeSets, DockerV2Schema2LayerMediaType, DockerV2Schema2LayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2SchemaLayerMediaTypeUncompressed, DockerV2Schema2LayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaType, DockerV2Schema2ForeignLayerMediaTypeGzip},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaTypeGzip, DockerV2Schema2ForeignLayerMediaTypeGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayer, imgspecv1.MediaTypeImageLayerGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerGzip, imgspecv1.MediaTypeImageLayerGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerZstd, imgspecv1.MediaTypeImageLayerGzip},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributable, imgspecv1.MediaTypeImageLayerNonDistributableGzip}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableGzip, imgspecv1.MediaTypeImageLayerNonDistributableGzip}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableZstd, imgspecv1.MediaTypeImageLayerNonDistributableGzip}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
}
for i, c := range compressGzipSuccess {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Compress,
CompressionAlgorithm: &compression.Gzip,
}
updatedType, err := updatedMIMEType(c.compression, c.mimeType, update)
require.NoErrorf(t, err, "%d: updatedMIMEType(%q, %+v) failed unexpectedly", i, c.mimeType, update)
assert.Equalf(t, c.updatedType, updatedType, "%d: updatedMIMEType(%q, %+v)", i, c.mimeType, update)
}
// known types where Compress with gzip is expected to fail
compressGzipFailure := []struct {
compression []compressionMIMETypeSet
mimeType string
}{
{schema2CompressionMIMETypeSets, DockerV2Schema1MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema1SignedMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ConfigMediaType},
{schema2CompressionMIMETypeSets, DockerV2ListMediaType},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeDescriptor},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeLayoutHeader},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageManifest},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageIndex},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageConfig},
}
for i, c := range compressGzipFailure {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Compress,
CompressionAlgorithm: &compression.Gzip,
}
_, err := updatedMIMEType(c.compression, c.mimeType, update)
require.Errorf(t, err, "%d: updatedMIMEType(%q, %+v) should have failed", i, c.mimeType, update)
}
require.Equalf(t, len(preserve), len(compressGzipSuccess)+len(compressGzipFailure), "missing some gzip compression tests")
// known types where Compress with zstd is expected to succeed
compressZstdSuccess := []struct {
compression []compressionMIMETypeSet
mimeType string
updatedType string
}{
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayer, imgspecv1.MediaTypeImageLayerZstd},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerGzip, imgspecv1.MediaTypeImageLayerZstd},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerZstd, imgspecv1.MediaTypeImageLayerZstd},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributable, imgspecv1.MediaTypeImageLayerNonDistributableZstd}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableGzip, imgspecv1.MediaTypeImageLayerNonDistributableZstd}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageLayerNonDistributableZstd, imgspecv1.MediaTypeImageLayerNonDistributableZstd}, //nolint:staticcheck // NonDistributable layers are deprecated, but we want to continue to support manipulating pre-existing images.
}
for i, c := range compressZstdSuccess {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Compress,
CompressionAlgorithm: &compression.Zstd,
}
updatedType, err := updatedMIMEType(c.compression, c.mimeType, update)
require.NoErrorf(t, err, "%d: updatedMIMEType(%q, %+v) failed unexpectedly", i, c.mimeType, update)
assert.Equalf(t, c.updatedType, updatedType, "%d: updatedMIMEType(%q, %+v)", i, c.mimeType, update)
}
// known types where Compress with zstd is expected to fail
compressZstdFailure := []struct {
compression []compressionMIMETypeSet
mimeType string
}{
{schema2CompressionMIMETypeSets, DockerV2SchemaLayerMediaTypeUncompressed},
{schema2CompressionMIMETypeSets, DockerV2Schema2LayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ForeignLayerMediaTypeGzip},
{schema2CompressionMIMETypeSets, DockerV2Schema1MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema1SignedMediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2MediaType},
{schema2CompressionMIMETypeSets, DockerV2Schema2ConfigMediaType},
{schema2CompressionMIMETypeSets, DockerV2ListMediaType},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeDescriptor},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeLayoutHeader},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageManifest},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageIndex},
{oci1CompressionMIMETypeSets, imgspecv1.MediaTypeImageConfig},
}
for i, c := range compressZstdFailure {
update := types.BlobInfo{
MediaType: c.mimeType,
CompressionOperation: types.Compress,
CompressionAlgorithm: &compression.Zstd,
}
_, err := updatedMIMEType(c.compression, c.mimeType, update)
require.Errorf(t, err, "%d: updatedMIMEType(%q, %+v) should have failed", i, c.mimeType, update)
}
require.Equalf(t, len(preserve), len(compressZstdSuccess)+len(compressZstdFailure), "missing some zstd compression tests")
}
|