-
Notifications
You must be signed in to change notification settings - Fork 0
/
sproto_test.go
85 lines (78 loc) · 1.65 KB
/
sproto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package sproto
import (
"bufio"
"bytes"
"errors"
"io"
"os"
"testing"
)
var interestingPacket [256]byte
func TestMain(m *testing.M) {
const plen = len(interestingPacket)
for i := 0; i < plen; i++ {
c := byte(i)
if c == sof || c == esc {
c = 0
}
interestingPacket[i] = c
}
// interestingPacket[0] = sof
// interestingPacket[1] = esc
// interestingPacket[19] = esc
// interestingPacket[20] = sof ^ escxor
// interestingPacket[plen-2] = esc
// interestingPacket[plen-1] = eof
os.Exit(m.Run())
}
func TestFrameLoopbackCopy(t *testing.T) {
data := interestingPacket
f := NewFrame(make([]byte, 300))
err := f.SetData(data[:])
if err != nil {
t.Fatal(err)
}
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
n, err := io.Copy(buffer, f)
if err != nil || n == 0 {
t.Fatal(n, err)
}
wire := buffer.Bytes()
_, err = f.ParseNext(bufio.NewReader(bytes.NewReader(wire)))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data[:], f.data) {
t.Errorf("got % x\nexpect % x", f.data, data)
}
}
func TestFrameMultiShotLoopback(t *testing.T) {
data := interestingPacket[:]
f := NewFrame(make([]byte, 300))
err := f.SetData(data[:])
if err != nil {
t.Fatal(err)
}
var buffer [64]byte
var wire []byte
for {
n, err := f.Read(buffer[:])
wire = append(wire, buffer[:n]...)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
t.Fatal(err)
break
}
}
n, err := f.ParseNext(bufio.NewReader(bytes.NewReader(wire)))
if err != nil {
t.Fatal(err)
}
if n != len(wire) {
t.Error("parsed bytes not equal to sent over wire", n, len(wire))
}
if !bytes.Equal(data, f.data) {
t.Errorf("got % x\nexpect % x", f.data, data)
}
}