forked from gruntwork-io/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpg_test.go
More file actions
48 lines (42 loc) · 1.79 KB
/
gpg_test.go
File metadata and controls
48 lines (42 loc) · 1.79 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
package main
import (
"fmt"
"testing"
)
func TestGpgVerify(t *testing.T) {
t.Parallel()
fixDir := "test-fixtures/gpg"
ascDir := fmt.Sprintf("%s/asc", fixDir)
assetsDir := fmt.Sprintf("%s/assets", fixDir)
usersDir := fmt.Sprintf("%s/users", fixDir)
cases := []struct {
user string // used to locate the gpg public key
asset string // file to verify
asc string // signature to use
res bool // should succeed?
}{
{"test-1-do-not-trust", "test-1", "test-1-by-test-1-do-not-trust.asc", true},
{"test-2-do-not-trust", "test-2", "test-2-by-test-2-do-not-trust.asc", true},
{"test-2-do-not-trust", "test-1", "test-2-by-test-2-do-not-trust.asc", false}, // wrong sig
{"test-1-do-not-trust", "test-2", "test-2-by-test-2-do-not-trust.asc", false}, // wrong key
{"test-2-do-not-trust", "test-1", "test-2-by-test-2-do-not-trust.asc", false}, // invalid sig
{"test-1-do-not-trust", "test-1", "malformed-signature.asc", false}, // invalid sig
{"test-1-do-not-trust", "test-1", "malformed-signature2.asc", false}, // invalid sig
{"test-1-do-not-trust", "test-1", "does-not-exist.asc", false}, // missing sig
{"test-1-do-not-trust", "test-1", "empty-signature.asc", false}, // empty sig
}
for _, tc := range cases {
gpgPubKey := fmt.Sprintf("%s/%s/gpg.pub", usersDir, tc.user)
gpgSigPath := fmt.Sprintf("%s/%s", ascDir, tc.asc)
assetPath := fmt.Sprintf("%s/%s", assetsDir, tc.asset)
err := gpgVerify(gpgPubKey, gpgSigPath, assetPath)
if tc.res && err != nil {
msg := "Failed to successfully verify %s with key %s and sig %s: %s"
t.Fatalf(msg, assetPath, gpgPubKey, gpgSigPath, err)
}
if !tc.res && err == nil {
msg := "Unexpectedly verified %s with key %s and sig %s!"
t.Fatalf(msg, assetPath, gpgPubKey, gpgSigPath)
}
}
}