-
Notifications
You must be signed in to change notification settings - Fork 135
/
main_test.go
105 lines (84 loc) · 1.91 KB
/
main_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
package main
import (
"bytes"
"crypto"
"crypto/x509"
"errors"
"os"
"testing"
"github.com/github/smimesign/certstore"
"github.com/github/smimesign/fakeca"
"github.com/pborman/getopt/v2"
)
var (
ca = fakeca.New(fakeca.IsCA)
intermediate = ca.Issue(fakeca.IsCA)
leaf = intermediate.Issue()
aiaLeaf = intermediate.Issue(fakeca.IssuingCertificateURL("http://foo"))
wrappedLeaf = identity{leaf}
wrappedAIALeaf = identity{aiaLeaf}
)
// make *fakeca.Identity implement certstore.Identity
type identity struct {
*fakeca.Identity
}
func (i identity) Certificate() (*x509.Certificate, error) {
return i.Identity.Certificate, nil
}
func (i identity) CertificateChain() ([]*x509.Certificate, error) {
return i.Chain(), nil
}
func (i identity) Signer() (crypto.Signer, error) {
return i.PrivateKey, nil
}
func (i identity) Delete() error {
return errors.New("not implemented")
}
func (i identity) Close() {}
func TestMain(m *testing.M) {
resetIO()
os.Exit(m.Run())
}
type BufferCloser struct {
*bytes.Buffer
}
func (bc BufferCloser) Close() error {
return nil
}
var (
stdinBuf *bytes.Buffer
stdoutBuf *bytes.Buffer
stderrBuf *bytes.Buffer
)
func resetIO() {
stdinBuf = new(bytes.Buffer)
stdoutBuf = new(bytes.Buffer)
stderrBuf = new(bytes.Buffer)
stdin = BufferCloser{stdinBuf}
stdout = BufferCloser{stdoutBuf}
stderr = BufferCloser{stderrBuf}
}
// setup for a test
//
// - parses provided args
// - sets the failer to be a function that fails the test. returns a reset
// function that should be deferred.
//
// Example:
// func TestFoo(t *testing.T) {
// defer testSetup(t, "--sign")()
// ...
// }
func testSetup(t *testing.T, args ...string) func() {
t.Helper()
resetFunc := func() {
resetIO()
getopt.Reset()
}
getopt.CommandLine.Parse(append([]string{"smimesign"}, args...))
idents = []certstore.Identity{
wrappedLeaf,
wrappedAIALeaf,
}
return resetFunc
}