1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package fixchain
16
17 import (
18 "net/http"
19 "testing"
20
21 "github.com/google/certificate-transparency-go/x509"
22 )
23
24 var constructChainTests = []fixTest{
25
26 {
27 cert: googleLeaf,
28 chain: []string{thawteIntermediate, verisignRoot},
29 roots: []string{verisignRoot},
30
31 function: "constructChain",
32 expectedChains: [][]string{
33 {"Google", "Thawte", "VeriSign"},
34 },
35 },
36 {
37 cert: testLeaf,
38 chain: []string{testIntermediate2, testIntermediate1, testRoot},
39 roots: []string{testRoot},
40
41 function: "constructChain",
42 expectedChains: [][]string{
43 {"Leaf", "Intermediate2", "Intermediate1", "CA"},
44 },
45 },
46 {
47 cert: googleLeaf,
48 chain: []string{thawteIntermediate, verisignRoot},
49
50 function: "constructChain",
51 expectedErrs: []errorType{VerifyFailed},
52 },
53 {
54 cert: googleLeaf,
55 roots: []string{verisignRoot},
56
57 function: "constructChain",
58 expectedErrs: []errorType{VerifyFailed},
59 },
60 {
61 cert: megaLeaf,
62 chain: []string{thawteIntermediate, verisignRoot},
63 roots: []string{verisignRoot},
64
65 function: "constructChain",
66 expectedErrs: []errorType{VerifyFailed},
67 },
68 {
69 cert: megaLeaf,
70 chain: []string{comodoIntermediate, verisignRoot},
71 roots: []string{verisignRoot},
72
73 function: "constructChain",
74 expectedErrs: []errorType{VerifyFailed},
75 },
76 }
77
78 var fixChainTests = []fixTest{
79
80 {
81 cert: googleLeaf,
82 chain: []string{thawteIntermediate, verisignRoot},
83 roots: []string{verisignRoot},
84
85 function: "fixChain",
86 expectedChains: [][]string{
87 {"Google", "Thawte", "VeriSign"},
88 },
89 },
90 {
91 cert: googleLeaf,
92 chain: []string{thawteIntermediate, verisignRoot},
93
94 function: "fixChain",
95 expectedErrs: []errorType{FixFailed},
96 },
97 {
98 cert: testC,
99 chain: []string{testB, testA},
100
101 function: "fixChain",
102 expectedErrs: []errorType{FixFailed},
103 },
104 {
105 cert: googleLeaf,
106 roots: []string{verisignRoot},
107
108 function: "fixChain",
109 expectedChains: [][]string{
110 {"Google", "Thawte", "VeriSign"},
111 },
112 },
113 {
114 cert: testLeaf,
115 chain: []string{testIntermediate2},
116 roots: []string{testRoot},
117
118 function: "fixChain",
119 expectedChains: [][]string{
120 {"Leaf", "Intermediate2", "Intermediate1", "CA"},
121 },
122 },
123 {
124 cert: testLeaf,
125 chain: []string{testIntermediate1},
126 roots: []string{testRoot},
127
128 function: "fixChain",
129 expectedChains: [][]string{
130 {"Leaf", "Intermediate2", "Intermediate1", "CA"},
131 },
132 },
133 {
134 cert: testLeaf,
135 roots: []string{testRoot},
136
137 function: "fixChain",
138 expectedChains: [][]string{
139 {"Leaf", "Intermediate2", "Intermediate1", "CA"},
140 },
141 },
142 {
143 cert: megaLeaf,
144 chain: []string{thawteIntermediate, verisignRoot},
145 roots: []string{verisignRoot},
146
147 function: "fixChain",
148 expectedErrs: []errorType{FixFailed},
149 },
150 {
151 cert: megaLeaf,
152 chain: []string{comodoIntermediate, verisignRoot},
153 roots: []string{verisignRoot},
154
155 function: "fixChain",
156 expectedErrs: []errorType{FixFailed},
157 },
158
159 }
160
161 func setUpFix(t *testing.T, i int, ft *fixTest) *toFix {
162
163 fix := &toFix{
164 cert: GetTestCertificateFromPEM(t, ft.cert),
165 chain: newDedupedChain(extractTestChain(t, i, ft.chain)),
166 roots: extractTestRoots(t, i, ft.roots),
167 cache: newURLCache(&http.Client{Transport: &testRoundTripper{}}, false),
168 }
169
170 intermediates := x509.NewCertPool()
171 for j, cert := range ft.chain {
172 ok := intermediates.AppendCertsFromPEM([]byte(cert))
173 if !ok {
174 t.Errorf("#%d: Failed to parse intermediate #%d", i, j)
175 }
176 }
177
178 fix.opts = &x509.VerifyOptions{
179 Intermediates: intermediates,
180 Roots: fix.roots,
181 DisableTimeChecks: true,
182 KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny},
183 }
184
185 return fix
186 }
187
188 func testFixChainFunctions(t *testing.T, i int, ft *fixTest) {
189 fix := setUpFix(t, i, ft)
190
191 var chains [][]*x509.Certificate
192 var ferrs []*FixError
193 switch ft.function {
194 case "constructChain":
195 chains, ferrs = fix.constructChain()
196 case "fixChain":
197 chains, ferrs = fix.fixChain()
198 case "handleChain":
199 chains, ferrs = fix.handleChain()
200 }
201
202 matchTestChainList(t, i, ft.expectedChains, chains)
203 matchTestErrorList(t, i, ft.expectedErrs, ferrs)
204 }
205
206 func TestFixChainFunctions(t *testing.T) {
207 var allTests []fixTest
208 allTests = append(allTests, constructChainTests...)
209 allTests = append(allTests, fixChainTests...)
210 allTests = append(allTests, handleChainTests...)
211 for i, ft := range allTests {
212 testFixChainFunctions(t, i, &ft)
213 }
214 }
215
216 func TestFix(t *testing.T) {
217 for i, test := range handleChainTests {
218 chains, ferrs := Fix(GetTestCertificateFromPEM(t, test.cert),
219 extractTestChain(t, i, test.chain),
220 extractTestRoots(t, i, test.roots),
221 &http.Client{Transport: &testRoundTripper{}})
222
223 matchTestChainList(t, i, test.expectedChains, chains)
224 matchTestErrorList(t, i, test.expectedErrs, ferrs)
225 }
226 }
227
View as plain text