File: util_test.go

package info (click to toggle)
golang-github-theupdateframework-go-tuf 0.5.2-5~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 7,596 kB
  • sloc: python: 163; sh: 37; makefile: 12
file content (282 lines) | stat: -rw-r--r-- 8,080 bytes parent folder | download | duplicates (2)
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
package util

import (
	"bytes"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	"hash"
	"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 int64, length int64, hashes map[string]string) data.SnapshotFileMeta {
		return data.SnapshotFileMeta{
			Length:  length,
			Hashes:  makeHashes(c, hashes),
			Version: 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{Length: t.actual.Length, Hashes: t.actual.Hashes}
		expected := data.SnapshotFileMeta{Length: t.expected.Length, Hashes: t.expected.Hashes}
		c.Assert(SnapshotFileMetaEqual(actual, expected), DeepEquals, t.err(t), Commentf("name = %s %d %d", t.name, t.actual.Length, t.expected.Length))
		c.Assert(FileMetaEqual(t.actual, t.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)
	}
}

func (UtilSuite) TestVersionEqual(c *C) {
	c.Assert(VersionEqual(1, 1), IsNil)
	c.Assert(VersionEqual(1, 3), Equals, ErrWrongVersion{3, 1})
}

func makeHash(b []byte, alg string) []byte {
	var h hash.Hash

	switch alg {
	case "sha256":
		h = sha256.New()
	case "sha512":
		h = sha512.New()
	}
	h.Write(b)
	return h.Sum(nil)
}

func (UtilSuite) TestBytesMatchLenAndHashes(c *C) {
	type test struct {
		name   string
		bytes  []byte
		length int64
		hashes data.Hashes
		err    func(test) error
	}

	b := []byte{82, 253, 252, 7, 33, 130, 101, 79, 22, 63, 95, 15, 154, 98, 29, 114}
	bhashes := data.Hashes{
		"sha512": makeHash(b, "sha512"),
		"sha256": makeHash(b, "sha256"),
	}

	tests := []test{
		{
			name:   "correct len and hashes",
			bytes:  b,
			length: 16,
			hashes: bhashes,
			err:    func(test) error { return nil },
		},
		{
			name:   "incorrect len",
			bytes:  b,
			length: 32,
			hashes: bhashes,
			err:    func(test) error { return ErrWrongLength{32, 16} },
		},
		{
			name:   "incorrect hashes sha512",
			bytes:  b,
			length: 16,
			hashes: data.Hashes{
				"sha512": makeHash(b, "sha256"),
			},
			err: func(test) error { return ErrWrongHash{"sha512", bhashes["sha256"], bhashes["sha512"]} },
		},
		{
			name:   "incorrect hashes sha256",
			bytes:  b,
			length: 16,
			hashes: data.Hashes{
				"sha256": makeHash(b, "sha512"),
			},
			err: func(test) error { return ErrWrongHash{"sha256", bhashes["sha512"], bhashes["sha256"]} },
		},
		{
			name:   "incorrect len and hashes",
			bytes:  b,
			length: 32,
			hashes: data.Hashes{
				"sha512": makeHash(b, "sha256"),
				"sha256": makeHash(b, "sha512"),
			},
			err: func(test) error { return ErrWrongLength{32, 16} },
		},
	}

	for _, t := range tests {
		c.Assert(BytesMatchLenAndHashes(t.bytes, t.length, t.hashes), DeepEquals, t.err(t), Commentf("name = %s", t.name))
	}
}