1 |
747 |
jeremybenn |
// Copyright 2010 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
package tls
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"testing"
|
9 |
|
|
)
|
10 |
|
|
|
11 |
|
|
func TestRoundUp(t *testing.T) {
|
12 |
|
|
if roundUp(0, 16) != 0 ||
|
13 |
|
|
roundUp(1, 16) != 16 ||
|
14 |
|
|
roundUp(15, 16) != 16 ||
|
15 |
|
|
roundUp(16, 16) != 16 ||
|
16 |
|
|
roundUp(17, 16) != 32 {
|
17 |
|
|
t.Error("roundUp broken")
|
18 |
|
|
}
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
var paddingTests = []struct {
|
22 |
|
|
in []byte
|
23 |
|
|
good bool
|
24 |
|
|
expectedLen int
|
25 |
|
|
}{
|
26 |
|
|
{[]byte{1, 2, 3, 4, 0}, true, 4},
|
27 |
|
|
{[]byte{1, 2, 3, 4, 0, 1}, false, 0},
|
28 |
|
|
{[]byte{1, 2, 3, 4, 99, 99}, false, 0},
|
29 |
|
|
{[]byte{1, 2, 3, 4, 1, 1}, true, 4},
|
30 |
|
|
{[]byte{1, 2, 3, 2, 2, 2}, true, 3},
|
31 |
|
|
{[]byte{1, 2, 3, 3, 3, 3}, true, 2},
|
32 |
|
|
{[]byte{1, 2, 3, 4, 3, 3}, false, 0},
|
33 |
|
|
{[]byte{1, 4, 4, 4, 4, 4}, true, 1},
|
34 |
|
|
{[]byte{5, 5, 5, 5, 5, 5}, true, 0},
|
35 |
|
|
{[]byte{6, 6, 6, 6, 6, 6}, false, 0},
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
func TestRemovePadding(t *testing.T) {
|
39 |
|
|
for i, test := range paddingTests {
|
40 |
|
|
payload, good := removePadding(test.in)
|
41 |
|
|
expectedGood := byte(255)
|
42 |
|
|
if !test.good {
|
43 |
|
|
expectedGood = 0
|
44 |
|
|
}
|
45 |
|
|
if good != expectedGood {
|
46 |
|
|
t.Errorf("#%d: wrong validity, want:%d got:%d", i, expectedGood, good)
|
47 |
|
|
}
|
48 |
|
|
if good == 255 && len(payload) != test.expectedLen {
|
49 |
|
|
t.Errorf("#%d: got %d, want %d", i, len(payload), test.expectedLen)
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
var certExampleCom = `308201403081eda003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313138353835325a170d3132303933303138353835325a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31a301830160603551d11040f300d820b6578616d706c652e636f6d300b06092a864886f70d0101050341001a0b419d2c74474c6450654e5f10b32bf426ffdf55cad1c52602e7a9151513a3424c70f5960dcd682db0c33769cc1daa3fcdd3db10809d2392ed4a1bf50ced18`
|
55 |
|
|
|
56 |
|
|
var certWildcardExampleCom = `308201423081efa003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303034365a170d3132303933303139303034365a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31c301a30180603551d110411300f820d2a2e6578616d706c652e636f6d300b06092a864886f70d0101050341001676f0c9e7c33c1b656ed5a6476c4e2ee9ec8e62df7407accb1875272b2edd0a22096cb2c22598d11604104d604f810eb4b5987ca6bb319c7e6ce48725c54059`
|
57 |
|
|
|
58 |
|
|
var certFooExampleCom = `308201443081f1a003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303131345a170d3132303933303139303131345a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31e301c301a0603551d1104133011820f666f6f2e6578616d706c652e636f6d300b06092a864886f70d010105034100646a2a51f2aa2477add854b462cf5207ba16d3213ffb5d3d0eed473fbf09935019192d1d5b8ca6a2407b424cf04d97c4cd9197c83ecf81f0eab9464a1109d09f`
|
59 |
|
|
|
60 |
|
|
var certDoubleWildcardExampleCom = `308201443081f1a003020102020101300b06092a864886f70d010105301e311c301a060355040a131354657374696e67204365727469666963617465301e170d3131313030313139303134315a170d3132303933303139303134315a301e311c301a060355040a131354657374696e67204365727469666963617465305a300b06092a864886f70d010101034b003048024100bced6e32368599eeddf18796bfd03958a154f87e5b084f96e85136a56b886733592f493f0fc68b0d6b3551781cb95e13c5de458b28d6fb60d20a9129313261410203010001a31e301c301a0603551d1104133011820f2a2e2a2e6578616d706c652e636f6d300b06092a864886f70d0101050341001c3de267975f56ef57771c6218ef95ecc65102e57bd1defe6f7efea90d9b26cf40de5bd7ad75e46201c7f2a92aaa3e907451e9409f65e28ddb6db80d726290f6`
|
61 |
|
|
|
62 |
|
|
func TestCertificateSelection(t *testing.T) {
|
63 |
|
|
config := Config{
|
64 |
|
|
Certificates: []Certificate{
|
65 |
|
|
{
|
66 |
|
|
Certificate: [][]byte{fromHex(certExampleCom)},
|
67 |
|
|
},
|
68 |
|
|
{
|
69 |
|
|
Certificate: [][]byte{fromHex(certWildcardExampleCom)},
|
70 |
|
|
},
|
71 |
|
|
{
|
72 |
|
|
Certificate: [][]byte{fromHex(certFooExampleCom)},
|
73 |
|
|
},
|
74 |
|
|
{
|
75 |
|
|
Certificate: [][]byte{fromHex(certDoubleWildcardExampleCom)},
|
76 |
|
|
},
|
77 |
|
|
},
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
config.BuildNameToCertificate()
|
81 |
|
|
|
82 |
|
|
pointerToIndex := func(c *Certificate) int {
|
83 |
|
|
for i := range config.Certificates {
|
84 |
|
|
if c == &config.Certificates[i] {
|
85 |
|
|
return i
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
return -1
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
if n := pointerToIndex(config.getCertificateForName("example.com")); n != 0 {
|
92 |
|
|
t.Errorf("example.com returned certificate %d, not 0", n)
|
93 |
|
|
}
|
94 |
|
|
if n := pointerToIndex(config.getCertificateForName("bar.example.com")); n != 1 {
|
95 |
|
|
t.Errorf("bar.example.com returned certificate %d, not 1", n)
|
96 |
|
|
}
|
97 |
|
|
if n := pointerToIndex(config.getCertificateForName("foo.example.com")); n != 2 {
|
98 |
|
|
t.Errorf("foo.example.com returned certificate %d, not 2", n)
|
99 |
|
|
}
|
100 |
|
|
if n := pointerToIndex(config.getCertificateForName("foo.bar.example.com")); n != 3 {
|
101 |
|
|
t.Errorf("foo.bar.example.com returned certificate %d, not 3", n)
|
102 |
|
|
}
|
103 |
|
|
if n := pointerToIndex(config.getCertificateForName("foo.bar.baz.example.com")); n != 0 {
|
104 |
|
|
t.Errorf("foo.bar.baz.example.com returned certificate %d, not 0", n)
|
105 |
|
|
}
|
106 |
|
|
}
|