11 "github.com/bradfitz/iter"
16 func TestReadUntil(t *testing.T) {
17 test := func(data, until string, leftover int, expectedErr error) {
18 r := bytes.NewReader([]byte(data))
19 err := readUntil(r, []byte(until))
20 if err != expectedErr {
23 if r.Len() != leftover {
27 test("feakjfeafeafegbaabc00", "abc", 2, nil)
28 test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
31 func TestSuffixMatchLen(t *testing.T) {
32 test := func(a, b string, expected int) {
33 actual := suffixMatchLen([]byte(a), []byte(b))
34 if actual != expected {
35 t.Fatalf("expected %d, got %d for %q and %q", expected, actual, a, b)
38 test("hello", "world", 0)
39 test("hello", "lo", 2)
40 test("hello", "llo", 3)
41 test("hello", "hell", 0)
42 test("hello", "helloooo!", 5)
43 test("hello", "lol!", 2)
44 test("hello", "mondo", 0)
45 test("mongo", "webscale", 0)
46 test("sup", "person", 1)
49 func handshakeTest(t testing.TB, ia []byte, aData, bData string) {
51 wg := sync.WaitGroup{}
55 a, err := InitiateHandshake(a, []byte("yep"), ia)
60 go a.Write([]byte(aData))
63 n, _ := a.Read(msg[:])
67 // t.Log(string(msg[:n]))
71 b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")})
76 go b.Write([]byte(bData))
77 // Need to be exact here, as there are several reads, and net.Pipe is
79 msg := make([]byte, len(ia)+len(aData))
80 n, _ := io.ReadFull(b, msg[:])
84 // t.Log(string(msg[:n]))
91 func allHandshakeTests(t testing.TB) {
92 handshakeTest(t, []byte("jump the gun, "), "hello world", "yo dawg")
93 handshakeTest(t, nil, "hello world", "yo dawg")
94 handshakeTest(t, []byte{}, "hello world", "yo dawg")
97 func TestHandshake(t *testing.T) {
99 t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
102 func BenchmarkHandshake(b *testing.B) {
103 for range iter.N(b.N) {
108 type trackReader struct {
113 func (me *trackReader) Read(b []byte) (n int, err error) {
114 n, err = me.r.Read(b)
119 func TestReceiveRandomData(t *testing.T) {
120 tr := trackReader{rand.Reader, 0}
121 ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil)