OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package tls | 5 package tls |
6 | 6 |
7 import ( | 7 import ( |
8 » "bytes"; | 8 » "bytes" |
9 » "testing"; | 9 » "testing" |
10 » "testing/iotest"; | 10 » "testing/iotest" |
11 ) | 11 ) |
12 | 12 |
13 func matchRecord(r1, r2 *record) bool { | 13 func matchRecord(r1, r2 *record) bool { |
14 if (r1 == nil) != (r2 == nil) { | 14 if (r1 == nil) != (r2 == nil) { |
15 return false | 15 return false |
16 } | 16 } |
17 if r1 == nil { | 17 if r1 == nil { |
18 return true | 18 return true |
19 } | 19 } |
20 return r1.contentType == r2.contentType && | 20 return r1.contentType == r2.contentType && |
21 r1.major == r2.major && | 21 r1.major == r2.major && |
22 r1.minor == r2.minor && | 22 r1.minor == r2.minor && |
23 » » bytes.Compare(r1.payload, r2.payload) == 0; | 23 » » bytes.Compare(r1.payload, r2.payload) == 0 |
24 } | 24 } |
25 | 25 |
26 type recordReaderTest struct { | 26 type recordReaderTest struct { |
27 » in» []byte; | 27 » in []byte |
28 » out» []*record; | 28 » out []*record |
29 } | 29 } |
30 | 30 |
31 var recordReaderTests = []recordReaderTest{ | 31 var recordReaderTests = []recordReaderTest{ |
32 recordReaderTest{nil, nil}, | 32 recordReaderTest{nil, nil}, |
33 recordReaderTest{fromHex("01"), nil}, | 33 recordReaderTest{fromHex("01"), nil}, |
34 recordReaderTest{fromHex("0102"), nil}, | 34 recordReaderTest{fromHex("0102"), nil}, |
35 recordReaderTest{fromHex("010203"), nil}, | 35 recordReaderTest{fromHex("010203"), nil}, |
36 recordReaderTest{fromHex("01020300"), nil}, | 36 recordReaderTest{fromHex("01020300"), nil}, |
37 recordReaderTest{fromHex("0102030000"), []*record{&record{1, 2, 3, nil}}
}, | 37 recordReaderTest{fromHex("0102030000"), []*record{&record{1, 2, 3, nil}}
}, |
38 recordReaderTest{fromHex("01020300000102030000"), []*record{&record{1, 2
, 3, nil}, &record{1, 2, 3, nil}}}, | 38 recordReaderTest{fromHex("01020300000102030000"), []*record{&record{1, 2
, 3, nil}, &record{1, 2, 3, nil}}}, |
39 recordReaderTest{fromHex("0102030001fe0102030002feff"), []*record{&recor
d{1, 2, 3, []byte{0xfe}}, &record{1, 2, 3, []byte{0xfe, 0xff}}}}, | 39 recordReaderTest{fromHex("0102030001fe0102030002feff"), []*record{&recor
d{1, 2, 3, []byte{0xfe}}, &record{1, 2, 3, []byte{0xfe, 0xff}}}}, |
40 recordReaderTest{fromHex("010203000001020300"), []*record{&record{1, 2,
3, nil}}}, | 40 recordReaderTest{fromHex("010203000001020300"), []*record{&record{1, 2,
3, nil}}}, |
41 } | 41 } |
42 | 42 |
43 func TestRecordReader(t *testing.T) { | 43 func TestRecordReader(t *testing.T) { |
44 for i, test := range recordReaderTests { | 44 for i, test := range recordReaderTests { |
45 » » buf := bytes.NewBuffer(test.in); | 45 » » buf := bytes.NewBuffer(test.in) |
46 » » c := make(chan *record); | 46 » » c := make(chan *record) |
47 » » go recordReader(c, buf); | 47 » » go recordReader(c, buf) |
48 » » matchRecordReaderOutput(t, i, test, c); | 48 » » matchRecordReaderOutput(t, i, test, c) |
49 | 49 |
50 » » buf = bytes.NewBuffer(test.in); | 50 » » buf = bytes.NewBuffer(test.in) |
51 » » buf2 := iotest.OneByteReader(buf); | 51 » » buf2 := iotest.OneByteReader(buf) |
52 » » c = make(chan *record); | 52 » » c = make(chan *record) |
53 » » go recordReader(c, buf2); | 53 » » go recordReader(c, buf2) |
54 » » matchRecordReaderOutput(t, i*2, test, c); | 54 » » matchRecordReaderOutput(t, i*2, test, c) |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 func matchRecordReaderOutput(t *testing.T, i int, test recordReaderTest, c <-cha
n *record) { | 58 func matchRecordReaderOutput(t *testing.T, i int, test recordReaderTest, c <-cha
n *record) { |
59 for j, r1 := range test.out { | 59 for j, r1 := range test.out { |
60 » » r2 := <-c; | 60 » » r2 := <-c |
61 if r2 == nil { | 61 if r2 == nil { |
62 » » » t.Errorf("#%d truncated after %d values", i, j); | 62 » » » t.Errorf("#%d truncated after %d values", i, j) |
63 » » » break; | 63 » » » break |
64 } | 64 } |
65 if !matchRecord(r1, r2) { | 65 if !matchRecord(r1, r2) { |
66 t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1) | 66 t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1) |
67 } | 67 } |
68 } | 68 } |
69 » <-c; | 69 » <-c |
70 if !closed(c) { | 70 if !closed(c) { |
71 t.Errorf("#%d: channel didn't close", i) | 71 t.Errorf("#%d: channel didn't close", i) |
72 } | 72 } |
73 } | 73 } |
OLD | NEW |