1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package pki
17
18 import (
19 "os"
20 "testing"
21
22 "go.uber.org/goleak"
23 )
24
25 func TestMain(m *testing.M) {
26 goleak.VerifyTestMain(m)
27 }
28
29 func TestFactoryNewKey(t *testing.T) {
30 type TestCase struct {
31 name string
32 format string
33 keyFile string
34 sigFile string
35 expectSuccess bool
36 expectValidFormat bool
37 }
38
39 testCases := []TestCase{
40 {
41 name: "valid pgp",
42 format: "pgp",
43 keyFile: "pgp/testdata/valid_armored_public.pgp",
44 sigFile: "pgp/testdata/hello_world.txt.asc.sig",
45 expectSuccess: true,
46 expectValidFormat: true,
47 },
48 {
49 name: "valid minisign",
50 format: "minisign",
51 keyFile: "minisign/testdata/minisign.pub",
52 sigFile: "minisign/testdata/hello_world.txt.minisig",
53 expectSuccess: true,
54 expectValidFormat: true,
55 },
56 {
57 name: "valid x509",
58 format: "x509",
59 keyFile: "x509/testdata/ec.pub",
60 sigFile: "x509/testdata/hello_world.txt.sig",
61 expectSuccess: true,
62 expectValidFormat: true,
63 },
64 {
65 name: "valid ssh",
66 format: "ssh",
67 keyFile: "ssh/testdata/id_rsa.pub",
68 sigFile: "ssh/testdata/hello_world.txt.sig",
69 expectSuccess: true,
70 expectValidFormat: true,
71 },
72 {
73 name: "invalid ssh signature",
74 format: "ssh",
75 keyFile: "ssh/testdata/id_rsa.pub",
76 sigFile: "ssh/testdata/hello_world.txt",
77 expectSuccess: false,
78 expectValidFormat: true,
79 },
80 {
81 name: "invalid ssh key",
82 format: "ssh",
83 keyFile: "ssh/testdata/hello_world.txt",
84 sigFile: "ssh/testdata/hello_world.txt.sig",
85 expectSuccess: false,
86 expectValidFormat: true,
87 },
88 {
89 format: "bogus",
90 expectSuccess: false,
91 expectValidFormat: false,
92 },
93 }
94
95 for _, tc := range testCases {
96 tc := tc
97 t.Run(tc.name, func(t *testing.T) {
98 factory, err := NewArtifactFactory(Format(tc.format))
99 if tc.expectValidFormat != (err == nil) {
100 t.Fatalf("unexpected error initializing factory for %v", tc.format)
101 }
102 if factory != nil {
103 keyFile, _ := os.Open(tc.keyFile)
104 _, newKeyErr := factory.NewPublicKey(keyFile)
105
106 sigFile, _ := os.Open(tc.sigFile)
107 _, newSigErr := factory.NewSignature(sigFile)
108
109 if tc.expectSuccess {
110 if newKeyErr != nil || newSigErr != nil {
111 t.Errorf("unexpected error generating public key %v or signature %v", newKeyErr, newSigErr)
112 }
113 } else {
114 if newKeyErr == nil && newSigErr == nil {
115 t.Error("expected error generating public key and signature. got none")
116 }
117 }
118 }
119 })
120 }
121 }
122
View as plain text