2009-11-04 01:25:13 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2009-12-15 23:33:31 +00:00
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
"testing/iotest"
|
2009-11-04 01:25:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func matchRecord(r1, r2 *record) bool {
|
|
|
|
if (r1 == nil) != (r2 == nil) {
|
2009-11-09 20:07:39 +00:00
|
|
|
return false
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
if r1 == nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
return true
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
return r1.contentType == r2.contentType &&
|
|
|
|
r1.major == r2.major &&
|
|
|
|
r1.minor == r2.minor &&
|
2009-12-15 23:33:31 +00:00
|
|
|
bytes.Compare(r1.payload, r2.payload) == 0
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type recordReaderTest struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
in []byte
|
|
|
|
out []*record
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var recordReaderTests = []recordReaderTest{
|
|
|
|
recordReaderTest{nil, nil},
|
|
|
|
recordReaderTest{fromHex("01"), nil},
|
|
|
|
recordReaderTest{fromHex("0102"), nil},
|
|
|
|
recordReaderTest{fromHex("010203"), nil},
|
|
|
|
recordReaderTest{fromHex("01020300"), nil},
|
|
|
|
recordReaderTest{fromHex("0102030000"), []*record{&record{1, 2, 3, nil}}},
|
|
|
|
recordReaderTest{fromHex("01020300000102030000"), []*record{&record{1, 2, 3, nil}, &record{1, 2, 3, nil}}},
|
|
|
|
recordReaderTest{fromHex("0102030001fe0102030002feff"), []*record{&record{1, 2, 3, []byte{0xfe}}, &record{1, 2, 3, []byte{0xfe, 0xff}}}},
|
|
|
|
recordReaderTest{fromHex("010203000001020300"), []*record{&record{1, 2, 3, nil}}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordReader(t *testing.T) {
|
|
|
|
for i, test := range recordReaderTests {
|
2009-12-15 23:33:31 +00:00
|
|
|
buf := bytes.NewBuffer(test.in)
|
|
|
|
c := make(chan *record)
|
|
|
|
go recordReader(c, buf)
|
|
|
|
matchRecordReaderOutput(t, i, test, c)
|
2009-11-04 01:25:13 +00:00
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
buf = bytes.NewBuffer(test.in)
|
|
|
|
buf2 := iotest.OneByteReader(buf)
|
|
|
|
c = make(chan *record)
|
|
|
|
go recordReader(c, buf2)
|
|
|
|
matchRecordReaderOutput(t, i*2, test, c)
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchRecordReaderOutput(t *testing.T, i int, test recordReaderTest, c <-chan *record) {
|
|
|
|
for j, r1 := range test.out {
|
2009-12-15 23:33:31 +00:00
|
|
|
r2 := <-c
|
2009-11-04 01:25:13 +00:00
|
|
|
if r2 == nil {
|
2009-12-15 23:33:31 +00:00
|
|
|
t.Errorf("#%d truncated after %d values", i, j)
|
|
|
|
break
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
if !matchRecord(r1, r2) {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1)
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
<-c
|
2009-11-04 01:25:13 +00:00
|
|
|
if !closed(c) {
|
2009-11-09 20:07:39 +00:00
|
|
|
t.Errorf("#%d: channel didn't close", i)
|
2009-11-04 01:25:13 +00:00
|
|
|
}
|
|
|
|
}
|