This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
forked from facebookarchive/dvara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprotocol_test.go
171 lines (156 loc) · 3.69 KB
/
protocol_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package dvara
import (
"bytes"
"errors"
"io"
"testing"
)
type testReader struct {
read func([]byte) (int, error)
}
func (t testReader) Read(b []byte) (int, error) { return t.read(b) }
type testWriter struct {
write func([]byte) (int, error)
}
func (t testWriter) Write(b []byte) (int, error) { return t.write(b) }
func TestOpStrings(t *testing.T) {
t.Parallel()
cases := []struct {
OpCode OpCode
String string
}{
{OpCode(0), "UNKNOWN"},
{OpReply, "REPLY"},
{OpMessage, "MESSAGE"},
{OpUpdate, "UPDATE"},
{OpInsert, "INSERT"},
{Reserved, "RESERVED"},
{OpQuery, "QUERY"},
{OpGetMore, "GET_MORE"},
{OpDelete, "DELETE"},
{OpKillCursors, "KILL_CURSORS"},
}
for _, c := range cases {
if c.OpCode.String() != c.String {
t.Fatalf("for code %d expected %s but got %s", c.OpCode, c.String, c.OpCode)
}
}
}
func TestMsgHeaderString(t *testing.T) {
t.Parallel()
m := &messageHeader{
OpCode: OpQuery,
MessageLength: 10,
RequestID: 42,
ResponseTo: 43,
}
if m.String() != "opCode:QUERY (2004) msgLen:10 reqID:42 respID:43" {
t.Fatalf("did not find expected string, instead found: %s", m)
}
}
func TestCopyEmptyMessage(t *testing.T) {
t.Parallel()
msg := messageHeader{}
msgBytes := msg.ToWire()
r := bytes.NewReader(msgBytes)
var w bytes.Buffer
if err := copyMessage(&w, r); err != nil {
t.Fatal(err)
}
if !bytes.Equal(msgBytes, w.Bytes()) {
t.Fatalf("did not get expected bytes %v got %v", msgBytes, w.Bytes())
}
}
func TestCopyMessageFromReadError(t *testing.T) {
t.Parallel()
expectedErr := errors.New("foo")
r := testReader{
read: func(b []byte) (int, error) {
return 0, expectedErr
},
}
var w bytes.Buffer
if err := copyMessage(&w, r); err != expectedErr {
t.Fatalf("did not get expected error, instead got: %s", err)
}
}
func TestCopyMessageFromWriteError(t *testing.T) {
t.Parallel()
msg := messageHeader{}
r := bytes.NewReader(msg.ToWire())
expectedErr := errors.New("foo")
w := testWriter{
write: func(b []byte) (int, error) {
return 0, expectedErr
},
}
if err := copyMessage(w, r); err != expectedErr {
t.Fatalf("did not get expected error, instead got: %s", err)
}
}
func TestCopyMessageFromWriteLengthError(t *testing.T) {
t.Parallel()
msg := messageHeader{}
r := bytes.NewReader(msg.ToWire())
w := testWriter{
write: func(b []byte) (int, error) {
return 0, nil
},
}
if err := copyMessage(w, r); err != errWrite {
t.Fatalf("did not get expected error, instead got: %s", err)
}
}
func TestReadDocumentEmpty(t *testing.T) {
t.Parallel()
doc, err := readDocument(bytes.NewReader([]byte{}))
if err != io.EOF {
t.Fatal("did not find expected error")
}
if len(doc) != 0 {
t.Fatal("was expecting an empty document")
}
}
func TestReadDocumentPartial(t *testing.T) {
t.Parallel()
first := true
r := testReader{
read: func(b []byte) (int, error) {
if first {
first = false
setInt32(b, 0, 5)
return 4, nil
}
return 0, io.EOF
},
}
doc, err := readDocument(r)
if err != io.EOF {
t.Fatalf("did not find expected error, instead got %s %v", err, doc)
}
if len(doc) != 0 {
t.Fatal("was expecting an empty document")
}
}
func TestReadCString(t *testing.T) {
t.Parallel()
cases := []struct {
Data []byte
Expected []byte
Error error
}{
{nil, nil, io.EOF},
{[]byte{0}, []byte{0}, nil},
{[]byte{1, 2, 3, 0}, []byte{1, 2, 3, 0}, nil},
{[]byte{1, 0, 3}, []byte{1, 0}, nil},
}
for _, c := range cases {
cstring, err := readCString(bytes.NewReader(c.Data))
if err != c.Error {
t.Fatalf("did not find expected error, instead got %s %v", err, cstring)
}
if !bytes.Equal(c.Expected, cstring) {
t.Fatalf("did not find expected %v instead got %v", c.Expected, cstring)
}
}
}