summaryrefslogtreecommitdiff
path: root/binproto_test.go
blob: 1a44a3d08dfc673e47c2dbabf9543e10010d85a3 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package binproto

import (
	"bytes"
	"io/ioutil"
	"testing"
)

var data = []byte{
	0x01, 0x2a, 0x00, // Request(42)
	0x08,       // IdKVMap
	0x09, 0x10, // UKey (16)
	0x04, 0x02, 0x00, 0x00, 0x00, 'h', 'i', // Bin(hi)
	0x09, 0x01, // UKey(1)
	0x0a,                                            // BinStream
	0x05, 0x00, 0x00, 0x00, 'h', 'e', 'l', 'l', 'o', // BinStream chunk (hello)
	0x02, 0x00, 0x00, 0x00, ',', ' ', // BinStream chunk (, )
	0x06, 0x00, 0x00, 0x00, 'w', 'o', 'r', 'l', 'd', '!', // BinStream chunk (world!)
	0xff, 0xff, 0xff, 0xff, // Terminating BinStream
	0x09, 0x02, // UKey(2)
	0x06,                                                 // List
	0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(1)
	0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(2)
	0x0b,       // Term
	0x09, 0x03, // UKey(3)
	0x07,                                        // TextKVMap
	0x04, 0x03, 0x00, 0x00, 0x00, 'f', 'o', 'o', // Bin(foo)
	0x04, 0x03, 0x00, 0x00, 0x00, 'b', 'a', 'r', // Bin(bar)
	0x0b,       // Term
	0x09, 0x04, // UKey(4)
	0x0c, 0x00, // UBool(false)
	0x09, 0x05, // UKey(5)
	0x0c, 0x01, // UBool(true)
	0x09, 0x06, // UKey(6)
	0x0d, 0x0a, // UByte(10)
	0x0b} // Term

func chkUnitType(t *testing.T, recv, expected UnitType) {
	if recv != expected {
		t.Fatalf("Unit has type %s, %s expected.", recv, expected)
	}
}

func readExpect2(t *testing.T, ur UnitReader, expected UnitType) interface{} {
	data, err := ReadExpect(ur, expected)
	if err != nil {
		t.Fatalf("Error from ReadExpect: %s", err)
	}

	return data
}

func TestReading(t *testing.T) {
	r := bytes.NewReader(data)
	ur := NewSimpleUnitReader(r)

	data := readExpect2(t, ur, UTRequest)
	if code := data.(uint16); code != 42 {
		t.Errorf("Request had code %d, not 42.", code)
	}

	readExpect2(t, ur, UTIdKVMap)

	idkvp, err := ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 16) || (idkvp.ValueType != UTBin) || (!bytes.Equal(idkvp.ValuePayload.([]byte), []byte("hi"))) {
		t.Errorf("Wrong idkvp content: %v", idkvp)
	}

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 1) || (idkvp.ValueType != UTBinStream) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}
	d, err := ioutil.ReadAll(idkvp.ValuePayload.(*BinstreamReader))
	if err != nil {
		t.Fatalf("BinstreamReader failed: %s", err)
	}
	if !bytes.Equal(d, []byte("hello, world!")) {
		t.Errorf("Wrong Binstream data: %v", d)
	}

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 2) || (idkvp.ValueType != UTList) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}

	if item := readExpect2(t, ur, UTNumber); item.(int64) != 1 {
		t.Errorf("Wrong number in list. Want: 1. Got: %d", item.(int64))
	}

	if item := readExpect2(t, ur, UTNumber); item.(int64) != 2 {
		t.Errorf("Wrong number in list. Want: 2. Got: %d", item.(int64))
	}

	readExpect2(t, ur, UTTerm)

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 3) || (idkvp.ValueType != UTTextKVMap) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}

	textkvp, err := ReadTextKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read TextKVPair: %s", err)
	}
	if (textkvp.Key != "foo") || (textkvp.ValueType != UTBin) || (!bytes.Equal(textkvp.ValuePayload.([]byte), []byte("bar"))) {
		t.Errorf("Wrong textkvp content: %v", textkvp)
	}

	if _, err = ReadTextKVPair(ur); err != Terminated {
		t.Fatal("TextKVMap not terminated?")
	}

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 4) || (idkvp.ValueType != UTBool) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}
	if idkvp.ValuePayload.(bool) != false {
		t.Error("Got true, want false")
	}

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 5) || (idkvp.ValueType != UTBool) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}
	if idkvp.ValuePayload.(bool) != true {
		t.Error("Got false, want true")
	}

	idkvp, err = ReadIdKVPair(ur)
	if err != nil {
		t.Fatalf("Could not read IdKVPair: %s", err)
	}
	if (idkvp.Key != 6) || (idkvp.ValueType != UTByte) {
		t.Fatalf("Wrong key or value type: %d, %s", idkvp.Key, idkvp.ValueType)
	}
	if val := idkvp.ValuePayload.(byte); val != 10 {
		t.Errorf("Got byte %d, want 10.", val)
	}

	if _, err = ReadIdKVPair(ur); err != Terminated {
		t.Fatal("IdKVMap not terminated?")
	}
}

func chkerr(t *testing.T, err error, whatfailed string) {
	if err != nil {
		t.Fatalf("%s failed: %s", whatfailed, err)
	}
}

func TestWriting(t *testing.T) {
	w := new(bytes.Buffer)

	chkerr(t, InitRequest(w, 42), "InitRequest")
	chkerr(t, InitIdKVMap(w), "InitIdKVMap")

	chkerr(t, SendUKey(w, 16), "SendUKey")
	chkerr(t, SendBin(w, []byte("hi")), "SendBin")

	chkerr(t, SendUKey(w, 1), "SendUKey")

	bsw, err := InitBinStream(w)
	if err != nil {
		t.Fatalf("Could not init a BinstremWriter: %s", err)
	}
	if _, err := bsw.Write([]byte("hello")); err != nil {
		t.Fatalf("Could not write chunk to bsw: %s", err)
	}
	if _, err := bsw.Write([]byte(", ")); err != nil {
		t.Fatalf("Could not write chunk to bsw: %s", err)
	}
	if _, err := bsw.Write([]byte("world!")); err != nil {
		t.Fatalf("Could not write chunk to bsw: %s", err)
	}
	if err := bsw.Close(); err != nil {
		t.Fatalf("Could not close bsw: %s", err)
	}

	chkerr(t, SendUKey(w, 2), "SendUKey")
	chkerr(t, InitList(w), "InitList")
	chkerr(t, SendNumber(w, 1), "SendNumber")
	chkerr(t, SendNumber(w, 2), "SendNumber")
	chkerr(t, SendTerm(w), "SendTerm")

	chkerr(t, SendUKey(w, 3), "SendUKey")
	chkerr(t, InitTextKVMap(w), "InitTextKVMap")
	chkerr(t, SendTextKey(w, "foo"), "SendTextKey")
	chkerr(t, SendBin(w, []byte("bar")), "SendBin")

	chkerr(t, SendTerm(w), "SendTerm")

	chkerr(t, SendUKey(w, 4), "SendUKey")
	chkerr(t, SendBool(w, false), "SendBool")

	chkerr(t, SendUKey(w, 5), "SendUKey")
	chkerr(t, SendBool(w, true), "SendBool")

	chkerr(t, SendUKey(w, 6), "SendUKey")
	chkerr(t, SendByte(w, 10), "SendByte")

	chkerr(t, SendTerm(w), "SendTerm")

	if !bytes.Equal(w.Bytes(), data) {
		t.Errorf("Wrong data constructed, got: %v", w.Bytes())
	}
}

func TestSkipping(t *testing.T) {
	skipdata := []byte{
		0x08,       // IdKVMap
		0x09, 0x01, // UKey(1)
		0x06,                                                 // List
		0x06,                                                 // List
		0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(1)
		0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(2)
		0x0b,                                                 // Term
		0x06,                                                 // List
		0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(3)
		0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Number(4)
		0x0b,       // Term
		0x0b,       // Term
		0x09, 0x02, // UKey(2)
		0x07,                                        // TextKVMap
		0x04, 0x03, 0x00, 0x00, 0x00, 'f', 'o', 'o', // Bin(foo)
		0x0a,                                            // BinStream
		0x05, 0x00, 0x00, 0x00, 'h', 'e', 'l', 'l', 'o', // BinStream chunk (hello)
		0x02, 0x00, 0x00, 0x00, ',', ' ', // BinStream chunk (, )
		0x06, 0x00, 0x00, 0x00, 'w', 'o', 'r', 'l', 'd', '!', // BinStream chunk (world!)
		0xff, 0xff, 0xff, 0xff, // Terminating BinStream
		0x0b,                                                 // Term
		0x0b,                                                 // Term
		0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // Number(8)

	r := bytes.NewReader(skipdata)
	ur := NewSimpleUnitReader(r)

	if err := SkipNext(ur); err != nil {
		t.Fatalf("Skipping failed: %s", err)
	}

	if ut, data, err := ur.ReadUnit(); (err != nil) || (ut != UTNumber) || (data.(int64) != 8) {
		t.Errorf("ReadUnit returned with unexpected data: %s, %v, %s", ut, data, err)
	}
}