forked from thought-machine/please
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_test.go
More file actions
90 lines (78 loc) · 2.85 KB
/
Copy pathverify_test.go
File metadata and controls
90 lines (78 loc) · 2.85 KB
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
package update
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
)
// readFile reads a file into an io.Reader
// It uses ioutil.ReadFile because that more closely mimics how we would do this
// for real (i.e. we would read to a buffer over HTTP then verify that, because we
// need to reuse the reader again afterwards and don't want to parse the tarball
// until we're sure it's OK).
func readFile(filename string) io.Reader {
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("%s", err)
}
return bytes.NewReader(b)
}
func TestVerifyGoodSignature(t *testing.T) {
signed := readFile("src/update/test_data/test.txt")
signature := readFile("src/update/test_data/test.txt.asc")
assert.True(t, verifySignature(signed, signature))
}
func TestVerifyBadSignature(t *testing.T) {
signed := readFile("src/update/test_data/test.txt")
signature := readFile("src/update/test_data/bad.txt.asc")
assert.False(t, verifySignature(signed, signature))
}
func TestVerifyBadFile(t *testing.T) {
signed := readFile("src/update/test_data/bad.txt")
signature := readFile("src/update/test_data/test.txt.asc")
assert.False(t, verifySignature(signed, signature))
}
func TestMustVerifyGoodSignature(t *testing.T) {
signed := readFile("src/update/test_data/test.txt")
signature := readFile("src/update/test_data/test.txt.asc")
r := mustVerifySignature(signed, signature)
b, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.EqualValues(t, []byte("Test file for verifying release signatures.\n"), b)
}
func TestMustVerifyBadSignature(t *testing.T) {
signed := readFile("src/update/test_data/test.txt")
signature := readFile("src/update/test_data/bad.txt.asc")
assert.Panics(t, func() { mustVerifySignature(signed, signature) })
}
func TestMustVerifyBadFile(t *testing.T) {
signed := readFile("src/update/test_data/bad.txt")
signature := readFile("src/update/test_data/test.txt.asc")
assert.Panics(t, func() { mustVerifySignature(signed, signature) })
}
func TestMustVerifyHash(t *testing.T) {
r := readFile("src/update/test_data/test.txt")
r = mustVerifyHash(r, []string{
"d5ddcfb56bee0bf465da6d8e0ab0db5b4635061b45be18c231a558cf1d86c2e0",
})
b, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.EqualValues(t, []byte("Test file for verifying release signatures.\n"), b)
}
func TestMustVerifyHashMultiple(t *testing.T) {
r := readFile("src/update/test_data/test.txt")
mustVerifyHash(r, []string{
"510dc30e9c55d5da05d971bed8568534667640b70295f78082967207745afec0",
"d5ddcfb56bee0bf465da6d8e0ab0db5b4635061b45be18c231a558cf1d86c2e0",
})
}
func TestMustVerifyHashBad(t *testing.T) {
r := readFile("src/update/test_data/test.txt")
assert.Panics(t, func() {
mustVerifyHash(r, []string{
"510dc30e9c55d5da05d971bed8568534667640b70295f78082967207745afec0",
"877265724bc9ba415dc1774569384fcfde80c6694d70f8f29405a917bbdf09db",
})
})
}