Debian Codegraph @

main_test.go

raw symbols SCIP
1 package main
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/x509"
7 "errors"
8 "os"
9 "testing"
10
11 "github.com/github/smimesign/certstore"
12 "github.com/github/smimesign/fakeca"
13 "github.com/pborman/getopt/v2"
14 )
15
16 var (
17 ca = fakeca.New(fakeca.IsCA)
18 intermediate = ca.Issue(fakeca.IsCA)
19 leaf = intermediate.Issue()
20 aiaLeaf = intermediate.Issue(fakeca.IssuingCertificateURL("http://foo"))
21
22 wrappedLeaf = identity{leaf}
23 wrappedAIALeaf = identity{aiaLeaf}
24 )
25
26 // make *fakeca.Identity implement certstore.Identity
27 type identity struct {
28 *fakeca.Identity
29 }
30
31 func (i identity) Certificate() (*x509.Certificate, error) {
32 return i.Identity.Certificate, nil
33 }
34
35 func (i identity) CertificateChain() ([]*x509.Certificate, error) {
36 return i.Chain(), nil
37 }
38
39 func (i identity) Signer() (crypto.Signer, error) {
40 return i.PrivateKey, nil
41 }
42
43 func (i identity) Delete() error {
44 return errors.New("not implemented")
45 }
46
47 func (i identity) Close() {}
48
49 func TestMain(m *testing.M) {
50 resetIO()
51 os.Exit(m.Run())
52 }
53
54 type BufferCloser struct {
55 *bytes.Buffer
56 }
57
58 func (bc BufferCloser) Close() error {
59 return nil
60 }
61
62 var (
63 stdinBuf *bytes.Buffer
64 stdoutBuf *bytes.Buffer
65 stderrBuf *bytes.Buffer
66 )
67
68 func resetIO() {
69 stdinBuf = new(bytes.Buffer)
70 stdoutBuf = new(bytes.Buffer)
71 stderrBuf = new(bytes.Buffer)
72
73 stdin = BufferCloser{stdinBuf}
74 stdout = BufferCloser{stdoutBuf}
75 stderr = BufferCloser{stderrBuf}
76 }
77
78 // setup for a test
79 //
80 // - parses provided args
81 // - sets the failer to be a function that fails the test. returns a reset
82 // function that should be deferred.
83 //
84 // Example:
85 // func TestFoo(t *testing.T) {
86 // defer testSetup(t, "--sign")()
87 // ...
88 // }
89 func testSetup(t *testing.T, args ...string) func() {
90 t.Helper()
91
92 resetFunc := func() {
93 resetIO()
94 getopt.Reset()
95 }
96
97 getopt.CommandLine.Parse(append([]string{"smimesign"}, args...))
98
99 idents = []certstore.Identity{
100 wrappedLeaf,
101 wrappedAIALeaf,
102 }
103
104 return resetFunc
105 }