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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
package fixchain
import (
"net/http"
"sync"
"testing"
"github.com/google/certificate-transparency/go/x509"
)
// NewFixer() test
func TestNewFixer(t *testing.T) {
chains := make(chan []*x509.Certificate)
errors := make(chan *FixError)
var expectedChains [][]string
var expectedErrs []errorType
for _, test := range handleChainTests {
expectedChains = append(expectedChains, test.expectedChains...)
expectedErrs = append(expectedErrs, test.expectedErrs...)
}
var wg sync.WaitGroup
wg.Add(2)
go testChains(t, 0, expectedChains, chains, &wg)
go testErrors(t, 0, expectedErrs, errors, &wg)
f := NewFixer(10, chains, errors, &http.Client{Transport: &testRoundTripper{}}, false)
for _, test := range handleChainTests {
f.QueueChain(GetTestCertificateFromPEM(t, test.cert),
extractTestChain(t, 0, test.chain), extractTestRoots(t, 0, test.roots))
}
f.Wait()
close(chains)
close(errors)
wg.Wait()
}
// Fixer.fixServer() test
func TestFixServer(t *testing.T) {
cache := &urlCache{cache: newLockedCache(), client: &http.Client{Transport: &testRoundTripper{}}}
f := &Fixer{cache: cache}
var wg sync.WaitGroup
fixServerTests := handleChainTests
// Pass chains to be fixed one at a time to fixServer and check the chain
// and errors produced are correct.
for i, fst := range fixServerTests {
chains := make(chan []*x509.Certificate)
errors := make(chan *FixError)
f.toFix = make(chan *toFix)
f.chains = chains
f.errors = errors
wg.Add(2)
go testChains(t, i, fst.expectedChains, chains, &wg)
go testErrors(t, i, fst.expectedErrs, errors, &wg)
f.wg.Add(1)
go f.fixServer()
f.QueueChain(GetTestCertificateFromPEM(t, fst.cert),
extractTestChain(t, i, fst.chain), extractTestRoots(t, i, fst.roots))
f.Wait()
close(chains)
close(errors)
wg.Wait()
}
// Pass multiple chains to be fixed to fixServer and check the chain and
// errors produced are correct.
chains := make(chan []*x509.Certificate)
errors := make(chan *FixError)
f.toFix = make(chan *toFix)
f.chains = chains
f.errors = errors
var expectedChains [][]string
var expectedErrs []errorType
for _, fst := range fixServerTests {
expectedChains = append(expectedChains, fst.expectedChains...)
expectedErrs = append(expectedErrs, fst.expectedErrs...)
}
i := len(fixServerTests)
wg.Add(2)
go testChains(t, i, expectedChains, chains, &wg)
go testErrors(t, i, expectedErrs, errors, &wg)
f.wg.Add(1)
go f.fixServer()
for _, fst := range fixServerTests {
f.QueueChain(GetTestCertificateFromPEM(t, fst.cert),
extractTestChain(t, i, fst.chain), extractTestRoots(t, i, fst.roots))
}
f.Wait()
close(chains)
close(errors)
wg.Wait()
}
func TestRemoveSuperChains(t *testing.T) {
superChainsTests := []struct {
chains [][]string
expectedChains [][]string
}{
{
chains: [][]string{
[]string{googleLeaf, thawteIntermediate},
[]string{googleLeaf},
},
expectedChains: [][]string{
[]string{"Google"},
},
},
{
chains: [][]string{
[]string{googleLeaf, verisignRoot},
[]string{googleLeaf, thawteIntermediate},
[]string{googleLeaf},
},
expectedChains: [][]string{
[]string{"Google"},
},
},
{
chains: [][]string{
[]string{googleLeaf, thawteIntermediate, verisignRoot},
[]string{googleLeaf, thawteIntermediate},
[]string{googleLeaf},
},
expectedChains: [][]string{
[]string{"Google"},
},
},
{
chains: [][]string{
[]string{googleLeaf, thawteIntermediate, verisignRoot},
[]string{googleLeaf},
},
expectedChains: [][]string{
[]string{"Google"},
},
},
{
chains: [][]string{
[]string{googleLeaf, thawteIntermediate, verisignRoot},
[]string{googleLeaf, verisignRoot},
[]string{googleLeaf, thawteIntermediate},
},
expectedChains: [][]string{
[]string{"Google", "Thawte"},
[]string{"Google", "VeriSign"},
},
},
{
chains: [][]string{
[]string{testLeaf, testIntermediate2},
[]string{googleLeaf, thawteIntermediate, verisignRoot},
[]string{testLeaf, testIntermediate2, testIntermediate1, testRoot},
[]string{googleLeaf, verisignRoot},
[]string{testLeaf, testIntermediate2, testIntermediate1},
[]string{googleLeaf, thawteIntermediate},
[]string{testLeaf, googleLeaf, thawteIntermediate, verisignRoot},
},
expectedChains: [][]string{
[]string{"Google", "Thawte"},
[]string{"Google", "VeriSign"},
[]string{"Leaf", "Intermediate2"},
[]string{"Leaf", "Google", "Thawte", "VeriSign"},
},
},
}
for i, test := range superChainsTests {
var chains [][]*x509.Certificate
for _, chain := range test.chains {
chains = append(chains, extractTestChain(t, i, chain))
}
matchTestChainList(t, i, test.expectedChains, removeSuperChains(chains))
}
}
// Fixer.updateCounters() tests
func TestUpdateCounters(t *testing.T) {
counterTests := []struct {
errors []errorType
reconstructed uint32
notReconstructed uint32
fixed uint32
notFixed uint32
}{
{[]errorType{}, 1, 0, 0, 0},
{[]errorType{VerifyFailed}, 0, 1, 1, 0},
{[]errorType{VerifyFailed, FixFailed}, 0, 1, 0, 1},
{[]errorType{ParseFailure}, 1, 0, 0, 0},
{[]errorType{ParseFailure, VerifyFailed}, 0, 1, 1, 0},
{[]errorType{ParseFailure, VerifyFailed, FixFailed}, 0, 1, 0, 1},
}
for i, test := range counterTests {
f := &Fixer{}
var ferrs []*FixError
for _, err := range test.errors {
ferrs = append(ferrs, &FixError{Type: err})
}
f.updateCounters(nil, ferrs)
if f.reconstructed != test.reconstructed {
t.Errorf("#%d: Incorrect value for reconstructed, wanted %d, got %d", i, test.reconstructed, f.reconstructed)
}
if f.notReconstructed != test.notReconstructed {
t.Errorf("#%d: Incorrect value for notReconstructed, wanted %d, got %d", i, test.notReconstructed, f.notReconstructed)
}
if f.fixed != test.fixed {
t.Errorf("#%d: Incorrect value for fixed, wanted %d, got %d", i, test.fixed, f.fixed)
}
if f.notFixed != test.notFixed {
t.Errorf("#%d: Incorrect value for notFixed, wanted %d, got %d", i, test.notFixed, f.notFixed)
}
}
}
// Fixer.QueueChain() tests
type fixerQueueTest struct {
cert string
chain []string
roots []string
dchain []string
}
var fixerQueueTests = []fixerQueueTest{
{
cert: googleLeaf,
chain: []string{verisignRoot, thawteIntermediate},
roots: []string{verisignRoot},
dchain: []string{"VeriSign", "Thawte"},
},
{
cert: googleLeaf,
chain: []string{verisignRoot, verisignRoot, thawteIntermediate},
roots: []string{verisignRoot},
dchain: []string{"VeriSign", "Thawte"},
},
{
cert: googleLeaf,
roots: []string{verisignRoot},
dchain: []string{},
},
}
func testFixerQueueChain(t *testing.T, i int, qt *fixerQueueTest, f *Fixer) {
defer f.wg.Done()
fix := <-f.toFix
// Check the deduped chain
matchTestChain(t, i, qt.dchain, fix.chain.certs)
}
func TestFixerQueueChain(t *testing.T) {
ch := make(chan *toFix)
defer close(ch)
f := &Fixer{toFix: ch}
for i, qt := range fixerQueueTests {
f.wg.Add(1)
go testFixerQueueChain(t, i, &qt, f)
chain := extractTestChain(t, i, qt.chain)
roots := extractTestRoots(t, i, qt.roots)
f.QueueChain(GetTestCertificateFromPEM(t, qt.cert), chain, roots)
f.wg.Wait()
}
}
|