9 "github.com/bradfitz/iter"
14 func TestReadUntil(t *testing.T) {
15 test := func(data, until string, leftover int, expectedErr error) {
16 r := bytes.NewReader([]byte(data))
17 err := readUntil(r, []byte(until))
18 if err != expectedErr {
21 if r.Len() != leftover {
25 test("feakjfeafeafegbaabc00", "abc", 2, nil)
26 test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
29 func TestSuffixMatchLen(t *testing.T) {
30 test := func(a, b string, expected int) {
31 actual := suffixMatchLen([]byte(a), []byte(b))
32 if actual != expected {
33 t.Fatalf("expected %d, got %d for %q and %q", expected, actual, a, b)
36 test("hello", "world", 0)
37 test("hello", "lo", 2)
38 test("hello", "llo", 3)
39 test("hello", "hell", 0)
40 test("hello", "helloooo!", 5)
41 test("hello", "lol!", 2)
42 test("hello", "mondo", 0)
43 test("mongo", "webscale", 0)
44 test("sup", "person", 1)
47 func handshakeTest(t testing.TB, ia []byte, aData, bData string) {
49 wg := sync.WaitGroup{}
53 a, err := InitiateHandshake(a, []byte("yep"), ia)
58 go a.Write([]byte(aData))
61 n, _ := a.Read(msg[:])
65 // t.Log(string(msg[:n]))
69 b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")})
74 go b.Write([]byte(bData))
75 // Need to be exact here, as there are several reads, and net.Pipe is
77 msg := make([]byte, len(ia)+len(aData))
78 n, _ := io.ReadFull(b, msg[:])
82 // t.Log(string(msg[:n]))
89 func allHandshakeTests(t testing.TB) {
90 handshakeTest(t, []byte("jump the gun, "), "hello world", "yo dawg")
91 handshakeTest(t, nil, "hello world", "yo dawg")
92 handshakeTest(t, []byte{}, "hello world", "yo dawg")
95 func TestHandshake(t *testing.T) {
97 t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
100 func BenchmarkHandshake(b *testing.B) {
101 for range iter.N(b.N) {