]> Sergey Matveev's repositories - btrtrc.git/blob - mse/mse_test.go
Support initial payload, and improve tests
[btrtrc.git] / mse / mse_test.go
1 package mse
2
3 import (
4         "bytes"
5         "io"
6         "net"
7         "sync"
8
9         "github.com/bradfitz/iter"
10
11         "testing"
12 )
13
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 {
19                         t.Fatal(err)
20                 }
21                 if r.Len() != leftover {
22                         t.Fatal(r.Len())
23                 }
24         }
25         test("feakjfeafeafegbaabc00", "abc", 2, nil)
26         test("feakjfeafeafegbaadc00", "abc", 0, io.EOF)
27 }
28
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)
34                 }
35         }
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)
45 }
46
47 func handshakeTest(t testing.TB, ia []byte, aData, bData string) {
48         a, b := net.Pipe()
49         wg := sync.WaitGroup{}
50         wg.Add(2)
51         go func() {
52                 defer wg.Done()
53                 a, err := InitiateHandshake(a, []byte("yep"), ia)
54                 if err != nil {
55                         t.Fatal(err)
56                         return
57                 }
58                 go a.Write([]byte(aData))
59
60                 var msg [20]byte
61                 n, _ := a.Read(msg[:])
62                 if n != len(bData) {
63                         t.FailNow()
64                 }
65                 // t.Log(string(msg[:n]))
66         }()
67         go func() {
68                 defer wg.Done()
69                 b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")})
70                 if err != nil {
71                         t.Fatal(err)
72                         return
73                 }
74                 go b.Write([]byte(bData))
75                 // Need to be exact here, as there are several reads, and net.Pipe is
76                 // most synchronous.
77                 msg := make([]byte, len(ia)+len(aData))
78                 n, _ := io.ReadFull(b, msg[:])
79                 if n != len(msg) {
80                         t.FailNow()
81                 }
82                 // t.Log(string(msg[:n]))
83         }()
84         wg.Wait()
85         a.Close()
86         b.Close()
87 }
88
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")
93 }
94
95 func TestHandshake(t *testing.T) {
96         allHandshakeTests(t)
97         t.Logf("crypto provides encountered: %s", cryptoProvidesCount)
98 }
99
100 func BenchmarkHandshake(b *testing.B) {
101         for range iter.N(b.N) {
102                 allHandshakeTests(b)
103         }
104 }