12 "github.com/bradfitz/iter"
15 func TestReadUntil(t *testing.T) {
16 test := func(data, until string, leftover int, expectedErr error) {
17 r := bytes.NewReader([]byte(data))
18 err := readUntil(r, []byte(until))
19 if err != expectedErr {
22 if r.Len() != leftover {
26 test("feakjfeafeafegbaabc00", "abc", 2, nil)
27 test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
30 func TestSuffixMatchLen(t *testing.T) {
31 test := func(a, b string, expected int) {
32 actual := suffixMatchLen([]byte(a), []byte(b))
33 if actual != expected {
34 t.Fatalf("expected %d, got %d for %q and %q", expected, actual, a, b)
37 test("hello", "world", 0)
38 test("hello", "lo", 2)
39 test("hello", "llo", 3)
40 test("hello", "hell", 0)
41 test("hello", "helloooo!", 5)
42 test("hello", "lol!", 2)
43 test("hello", "mondo", 0)
44 test("mongo", "webscale", 0)
45 test("sup", "person", 1)
48 func handshakeTest(t testing.TB, ia []byte, aData, bData string) {
50 wg := sync.WaitGroup{}
54 a, err := InitiateHandshake(a, []byte("yep"), ia)
59 go a.Write([]byte(aData))
62 n, _ := a.Read(msg[:])
66 // t.Log(string(msg[:n]))
70 b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")})
75 go b.Write([]byte(bData))
76 // Need to be exact here, as there are several reads, and net.Pipe is
78 msg := make([]byte, len(ia)+len(aData))
79 n, _ := io.ReadFull(b, msg[:])
83 // t.Log(string(msg[:n]))
90 func allHandshakeTests(t testing.TB) {
91 handshakeTest(t, []byte("jump the gun, "), "hello world", "yo dawg")
92 handshakeTest(t, nil, "hello world", "yo dawg")
93 handshakeTest(t, []byte{}, "hello world", "yo dawg")
96 func TestHandshake(t *testing.T) {
98 t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
101 func BenchmarkHandshake(b *testing.B) {
102 for range iter.N(b.N) {
107 type trackReader struct {
112 func (me *trackReader) Read(b []byte) (n int, err error) {
113 n, err = me.r.Read(b)
118 func TestReceiveRandomData(t *testing.T) {
119 tr := trackReader{rand.Reader, 0}
120 ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil)