-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathdigest_test.go
143 lines (133 loc) · 4.26 KB
/
digest_test.go
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
// Copyright 2021 OCI Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package digest_test
import (
"crypto"
"crypto/sha256"
"testing"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/go-digest/testdigest"
)
func TestParseDigest(t *testing.T) {
// SHA-384 is not registered by default, but used in this test.
digest.RegisterAlgorithm(digest.SHA384, crypto.SHA384)
tests := []testdigest.TestCase{
{
Input: "sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b",
Algorithm: "sha256",
Encoded: "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b",
},
{
Input: "sha384:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Algorithm: "sha384",
Encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
},
{
// empty
Input: "",
Err: digest.ErrDigestInvalidFormat,
},
{
// whitespace only
Input: " ",
Err: digest.ErrDigestInvalidFormat,
},
{
// empty hex
Input: "sha256:",
Err: digest.ErrDigestInvalidFormat,
},
{
// hex with correct length, but whitespace only
Input: "sha256: ",
Err: digest.ErrDigestInvalidFormat,
},
{
// empty hex
Input: ":",
Err: digest.ErrDigestInvalidFormat,
},
{
// just hex
Input: "d41d8cd98f00b204e9800998ecf8427e",
Err: digest.ErrDigestInvalidFormat,
},
{
// not hex
Input: "sha256:d41d8cd98f00b204e9800m98ecf8427e",
Err: digest.ErrDigestInvalidLength,
},
{
// too short
Input: "sha256:abcdef0123456789",
Err: digest.ErrDigestInvalidLength,
},
{
// too short (from different Algorithm)
Input: "sha512:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
Err: digest.ErrDigestInvalidLength,
},
{
Input: "foo:d41d8cd98f00b204e9800998ecf8427e",
Err: digest.ErrDigestUnsupported,
},
{
// repeated separators
Input: "sha384__foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Err: digest.ErrDigestInvalidFormat,
},
{
// ensure that we parse, but we don't have support for the Algorithm
Input: "sha384.foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Algorithm: "sha384.foo+bar",
Encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Err: digest.ErrDigestUnsupported,
},
{
Input: "sha384_foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Algorithm: "sha384_foo+bar",
Encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
Err: digest.ErrDigestUnsupported,
},
{
Input: "sha256+b64:LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564",
Algorithm: "sha256+b64",
Encoded: "LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564",
Err: digest.ErrDigestUnsupported,
},
{
Input: "sha256:E58FCF7418D4390DEC8E8FB69D88C06EC07039D651FEDD3AA72AF9972E7D046B",
Err: digest.ErrDigestInvalidFormat,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.Input, func(t *testing.T) {
testdigest.RunTestCase(t, tc)
})
}
}
func BenchmarkNewDigestFromEncoded(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = digest.NewDigestFromEncoded("sha256", "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b")
}
}
func BenchmarkNewDigestFromBytes(b *testing.B) {
s := sha256.Sum256([]byte("hello world"))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = digest.NewDigestFromBytes("sha256", s[:])
}
}