forked from theupdateframework/go-tuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
197 lines (179 loc) · 5.84 KB
/
util_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
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
package util
import (
"bytes"
"encoding/hex"
"testing"
"github.com/theupdateframework/go-tuf/data"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type UtilSuite struct{}
var _ = Suite(&UtilSuite{})
func (UtilSuite) TestGenerateTargetFileMetaDefault(c *C) {
// default is sha512
r := bytes.NewReader([]byte("foo"))
meta, err := GenerateTargetFileMeta(r)
c.Assert(err, IsNil)
c.Assert(meta.Length, Equals, int64(3))
hashes := meta.Hashes
c.Assert(hashes, HasLen, 1)
hash, ok := hashes["sha512"]
if !ok {
c.Fatal("missing sha512 hash")
}
c.Assert(hash.String(), DeepEquals, "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7")
}
func (UtilSuite) TestGenerateTargetFileMetaExplicit(c *C) {
r := bytes.NewReader([]byte("foo"))
meta, err := GenerateTargetFileMeta(r, "sha256", "sha512")
c.Assert(err, IsNil)
c.Assert(meta.Length, Equals, int64(3))
hashes := meta.Hashes
c.Assert(hashes, HasLen, 2)
for name, val := range map[string]string{
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
"sha512": "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7",
} {
hash, ok := hashes[name]
if !ok {
c.Fatalf("missing %s hash", name)
}
c.Assert(hash.String(), DeepEquals, val)
}
}
func makeHashes(c *C, hashes map[string]string) data.Hashes {
h := make(map[string]data.HexBytes, len(hashes))
for typ, hash := range hashes {
v, err := hex.DecodeString(hash)
c.Assert(err, IsNil)
h[typ] = v
}
return h
}
type testMetaFile struct {
name string
actual data.FileMeta
expected data.FileMeta
err func(testMetaFile) error
}
func testMetaFileCases(c *C) []testMetaFile {
fileMeta := func(c *C, length int64, hashes map[string]string) data.FileMeta {
return data.FileMeta{
Length: length,
Hashes: makeHashes(c, hashes),
}
}
return []testMetaFile{
{
name: "wrong length",
actual: data.FileMeta{Length: 1},
expected: data.FileMeta{Length: 2},
err: func(testMetaFile) error { return ErrWrongLength{Actual: 1, Expected: 2} },
},
{
name: "wrong sha512 hash",
actual: fileMeta(c, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(c, 10, map[string]string{"sha512": "222222"}),
err: func(t testMetaFile) error {
return ErrWrongHash{"sha512", t.expected.Hashes["sha512"], t.actual.Hashes["sha512"]}
},
},
{
name: "intersecting hashes",
actual: fileMeta(c, 10, map[string]string{"sha512": "111111", "md5": "222222"}),
expected: fileMeta(c, 10, map[string]string{"sha512": "111111", "sha256": "333333"}),
err: func(testMetaFile) error { return nil },
},
{
name: "no common hashes",
actual: fileMeta(c, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(c, 10, map[string]string{"sha256": "222222", "md5": "333333"}),
err: func(t testMetaFile) error { return ErrNoCommonHash{t.expected.Hashes, t.actual.Hashes} },
},
}
}
func (UtilSuite) TestSnapshotFileMetaEqual(c *C) {
type test struct {
name string
actual data.SnapshotFileMeta
expected data.SnapshotFileMeta
err func(test) error
}
fileMeta := func(version int, length int64, hashes map[string]string) data.SnapshotFileMeta {
return data.SnapshotFileMeta{
data.FileMeta{
Length: length,
Hashes: makeHashes(c, hashes),
},
version,
}
}
tests := []test{
{
name: "same version",
actual: fileMeta(1, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(1, 10, map[string]string{"sha512": "111111"}),
err: func(test) error { return nil },
},
{
name: "wrong version",
actual: fileMeta(0, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(1, 10, map[string]string{"sha512": "111111"}),
err: func(test) error { return ErrWrongVersion{Expected: 1, Actual: 0} },
},
{
name: "wrong version",
actual: fileMeta(1, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(0, 10, map[string]string{"sha512": "111111"}),
err: func(test) error { return ErrWrongVersion{Expected: 0, Actual: 1} },
},
{
name: "wrong version",
actual: fileMeta(1, 10, map[string]string{"sha512": "111111"}),
expected: fileMeta(2, 10, map[string]string{"sha512": "111111"}),
err: func(test) error { return ErrWrongVersion{Expected: 2, Actual: 1} },
},
}
for _, t := range tests {
c.Assert(SnapshotFileMetaEqual(t.actual, t.expected), DeepEquals, t.err(t), Commentf("name = %s", t.name))
}
for _, t := range testMetaFileCases(c) {
actual := data.SnapshotFileMeta{FileMeta: t.actual}
expected := data.SnapshotFileMeta{FileMeta: t.expected}
c.Assert(SnapshotFileMetaEqual(actual, expected), DeepEquals, t.err(t), Commentf("name = %s", t.name))
}
}
func (UtilSuite) TestNormalizeTarget(c *C) {
for before, after := range map[string]string{
"": "",
"foo.txt": "foo.txt",
"/bar.txt": "bar.txt",
"foo//bar.txt": "foo/bar.txt",
"/with/./a/dot": "with/a/dot",
"/with/double/../dot": "with/dot",
} {
c.Assert(NormalizeTarget(before), Equals, after)
}
}
func (UtilSuite) TestHashedPaths(c *C) {
hexBytes := func(s string) data.HexBytes {
v, err := hex.DecodeString(s)
c.Assert(err, IsNil)
return v
}
hashes := data.Hashes{
"sha512": hexBytes("abc123"),
"sha256": hexBytes("def456"),
}
paths := HashedPaths("foo/bar.txt", hashes)
// cannot use DeepEquals as the returned order is non-deterministic
c.Assert(paths, HasLen, 2)
expected := map[string]struct{}{"foo/abc123.bar.txt": {}, "foo/def456.bar.txt": {}}
for _, path := range paths {
if _, ok := expected[path]; !ok {
c.Fatalf("unexpected path: %s", path)
}
delete(expected, path)
}
}