|
| 1 | +// Copyright 2019, 2020 OCI Contributors |
| 2 | +// Copyright 2017 Docker, Inc. |
| 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 | +// https://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 | +package digest |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestParseDigest(t *testing.T) { |
| 23 | + for _, testcase := range []struct { |
| 24 | + input string |
| 25 | + err error |
| 26 | + algorithm Algorithm |
| 27 | + encoded string |
| 28 | + }{ |
| 29 | + { |
| 30 | + input: "sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b", |
| 31 | + algorithm: "sha256", |
| 32 | + encoded: "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b", |
| 33 | + }, |
| 34 | + { |
| 35 | + input: "sha384:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 36 | + algorithm: "sha384", |
| 37 | + encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 38 | + }, |
| 39 | + { |
| 40 | + input: "blake3:af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262", |
| 41 | + algorithm: "blake3", |
| 42 | + encoded: "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262", |
| 43 | + }, |
| 44 | + { |
| 45 | + // empty |
| 46 | + input: "", |
| 47 | + err: ErrDigestInvalidFormat, |
| 48 | + }, |
| 49 | + { |
| 50 | + // whitespace only |
| 51 | + input: " ", |
| 52 | + err: ErrDigestInvalidFormat, |
| 53 | + }, |
| 54 | + { |
| 55 | + // empty hex |
| 56 | + input: "sha256:", |
| 57 | + err: ErrDigestInvalidFormat, |
| 58 | + }, |
| 59 | + { |
| 60 | + // hex with correct length, but whitespace only |
| 61 | + input: "sha256: ", |
| 62 | + err: ErrDigestInvalidFormat, |
| 63 | + }, |
| 64 | + { |
| 65 | + // empty hex |
| 66 | + input: ":", |
| 67 | + err: ErrDigestInvalidFormat, |
| 68 | + }, |
| 69 | + { |
| 70 | + // just hex |
| 71 | + input: "d41d8cd98f00b204e9800998ecf8427e", |
| 72 | + err: ErrDigestInvalidFormat, |
| 73 | + }, |
| 74 | + { |
| 75 | + // not hex |
| 76 | + input: "sha256:d41d8cd98f00b204e9800m98ecf8427e", |
| 77 | + err: ErrDigestInvalidLength, |
| 78 | + }, |
| 79 | + { |
| 80 | + // too short |
| 81 | + input: "sha256:abcdef0123456789", |
| 82 | + err: ErrDigestInvalidLength, |
| 83 | + }, |
| 84 | + { |
| 85 | + // too short (from different algorithm) |
| 86 | + input: "sha512:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789", |
| 87 | + err: ErrDigestInvalidLength, |
| 88 | + }, |
| 89 | + { |
| 90 | + input: "foo:d41d8cd98f00b204e9800998ecf8427e", |
| 91 | + err: ErrDigestUnsupported, |
| 92 | + }, |
| 93 | + { |
| 94 | + // repeated separators |
| 95 | + input: "sha384__foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 96 | + err: ErrDigestInvalidFormat, |
| 97 | + }, |
| 98 | + { |
| 99 | + // ensure that we parse, but we don't have support for the algorithm |
| 100 | + input: "sha384.foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 101 | + algorithm: "sha384.foo+bar", |
| 102 | + encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 103 | + err: ErrDigestUnsupported, |
| 104 | + }, |
| 105 | + { |
| 106 | + input: "sha384_foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 107 | + algorithm: "sha384_foo+bar", |
| 108 | + encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d", |
| 109 | + err: ErrDigestUnsupported, |
| 110 | + }, |
| 111 | + { |
| 112 | + input: "sha256+b64:LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564", |
| 113 | + algorithm: "sha256+b64", |
| 114 | + encoded: "LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564", |
| 115 | + err: ErrDigestUnsupported, |
| 116 | + }, |
| 117 | + { |
| 118 | + input: "sha256:E58FCF7418D4390DEC8E8FB69D88C06EC07039D651FEDD3AA72AF9972E7D046B", |
| 119 | + err: ErrDigestInvalidFormat, |
| 120 | + }, |
| 121 | + } { |
| 122 | + t.Run(testcase.input, func(t *testing.T) { |
| 123 | + digest, err := Parse(testcase.input) |
| 124 | + if err != testcase.err { |
| 125 | + t.Fatalf("error differed from expected while parsing %q: %v != %v", testcase.input, err, testcase.err) |
| 126 | + } |
| 127 | + |
| 128 | + if testcase.err != nil { |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + if digest.Algorithm() != testcase.algorithm { |
| 133 | + t.Fatalf("incorrect algorithm for parsed digest: %q != %q", digest.Algorithm(), testcase.algorithm) |
| 134 | + } |
| 135 | + |
| 136 | + if digest.Encoded() != testcase.encoded { |
| 137 | + t.Fatalf("incorrect hex for parsed digest: %q != %q", digest.Encoded(), testcase.encoded) |
| 138 | + } |
| 139 | + |
| 140 | + // Parse string return value and check equality |
| 141 | + newParsed, err := Parse(digest.String()) |
| 142 | + |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unexpected error parsing input %q: %v", testcase.input, err) |
| 145 | + } |
| 146 | + |
| 147 | + if newParsed != digest { |
| 148 | + t.Fatalf("expected equal: %q != %q", newParsed, digest) |
| 149 | + } |
| 150 | + |
| 151 | + newFromHex := NewDigestFromEncoded(newParsed.Algorithm(), newParsed.Encoded()) |
| 152 | + if newFromHex != digest { |
| 153 | + t.Fatalf("%v != %v", newFromHex, digest) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments