blob: 93976ed594abb22884cc3c44d4197bae19c5ce47 [file] [log] [blame]
Jan Schärb48174d2025-04-14 10:13:02 +00001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
4package oci
5
6import (
7 "fmt"
8 "strings"
9 "testing"
10)
11
12func TestEmbeddedContent(t *testing.T) {
13 manifest := `{
14 "schemaVersion": 2,
15 "mediaType": "application/vnd.oci.image.manifest.v1+json",
16 "config": {
17 "mediaType": "application/vnd.oci.empty.v1+json",
18 "digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
19 "size": 2,
20 "data": "e30="
21 },
22 "layers": [
23 {
24 "digest": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
25 "size": 0
26 },
27 {
28 "digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff80",
29 "size": 2,
30 "data": "e30="
31 }
32 ]
33}`
34 // Pass nil for blobs, which means reading can only work if it uses the
35 // embedded content.
36 image, err := NewImage([]byte(manifest), "", nil)
37 if err != nil {
38 t.Fatal(err)
39 }
40 configBytes, err := image.ReadBlobVerified(&image.Manifest.Config)
41 if err != nil {
42 t.Fatal(err)
43 }
44 if got, want := string(configBytes), "{}"; got != want {
45 t.Errorf("Got config %q, expected %q", got, want)
46 }
47 layerBytes, err := image.ReadBlobVerified(&image.Manifest.Layers[0])
48 if err != nil {
49 t.Fatal(err)
50 }
51 if len(layerBytes) != 0 {
52 t.Errorf("Got layer %q, expected to be empty", layerBytes)
53 }
54 // Layer 1 has a wrong digest.
55 _, err = image.ReadBlobVerified(&image.Manifest.Layers[1])
56 if !strings.Contains(fmt.Sprintf("%v", err), "failed verification") {
57 t.Errorf("Expected failed verification, got %v", err)
58 }
59}
60
61func TestParseDigest(t *testing.T) {
62 testCases := []struct {
63 input string
64 algorithm string
65 encoded string
66 err string
67 }{
68 {input: "", err: `invalid digest`},
69 {input: "1234", err: `invalid digest`},
70 {input: "x:y", err: `unknown digest algorithm "x"`},
71 {input: "sha256:1234", err: `invalid sha256 digest length`},
72 {input: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550", err: `invalid sha256 digest length`},
73 {input: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85x", err: `invalid character in sha256 digest`},
74 {
75 input: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
76 algorithm: "sha256",
77 encoded: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
78 },
79 }
80 for _, tC := range testCases {
81 algorithm, encoded, err := ParseDigest(tC.input)
82 if algorithm != tC.algorithm {
83 t.Errorf("ParseDigest(%q): algorithm = %q, expected %q", tC.input, algorithm, tC.algorithm)
84 }
85 if encoded != tC.encoded {
86 t.Errorf("ParseDigest(%q): encoded = %q, expected %q", tC.input, encoded, tC.encoded)
87 }
88 errStr := ""
89 if err != nil {
90 errStr = err.Error()
91 }
92 if errStr != tC.err {
93 t.Errorf("ParseDigest(%q): err = %q, expected %q", tC.input, errStr, tC.err)
94 }
95 }
96}