forked from dropbox/godropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_client_test.go
346 lines (299 loc) · 11 KB
/
raw_client_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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package memcache
import (
"bytes"
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into go test runner
func Test(t *testing.T) {
TestingT(t)
}
type RawClientSuite struct {
channel *bytes.Buffer
client *RawClient
}
var _ = Suite(&RawClientSuite{})
func (s *RawClientSuite) SetUpTest(c *C) {
s.channel = &bytes.Buffer{}
s.client = NewRawClient(0, s.channel).(*RawClient)
}
func (s *RawClientSuite) TestSendRequest(c *C) {
err := s.client.sendRequest(
opAdd,
0xdecafbad, // CAS
[]byte("Hello"), // key
[]byte("World"), // value
uint32(0xdeadbeef), // flags
uint32(0xe10)) // expiry
c.Assert(err, IsNil)
/*
Byte/ 0 | 1 | 2 | 3 |
/ | | | |
|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
+---------------+---------------+---------------+---------------+
0| 0x80 | 0x02 | 0x00 | 0x05 |
+---------------+---------------+---------------+---------------+
4| 0x08 | 0x00 | 0x00 | 0x00 |
+---------------+---------------+---------------+---------------+
8| 0x00 | 0x00 | 0x00 | 0x12 |
+---------------+---------------+---------------+---------------+
12| 0x00 | 0x00 | 0x00 | 0x00 |
+---------------+---------------+---------------+---------------+
16| 0x00 | 0x00 | 0x00 | 0x00 |
+---------------+---------------+---------------+---------------+
20| 0xde | 0xca | 0xfb | 0xad |
+---------------+---------------+---------------+---------------+
24| 0xde | 0xad | 0xbe | 0xef |
+---------------+---------------+---------------+---------------+
28| 0x00 | 0x00 | 0x0e | 0x10 |
+---------------+---------------+---------------+---------------+
32| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
+---------------+---------------+---------------+---------------+
36| 0x6f ('o') | 0x57 ('W') | 0x6f ('o') | 0x72 ('r') |
+---------------+---------------+---------------+---------------+
40| 0x6c ('l') | 0x64 ('d') |
+---------------+---------------+
Total 42 bytes (24 byte header, 8 byte extras, 5 byte key and
5 byte value)
Field (offset) (value)
Magic (0) : 0x80
Opcode (1) : 0x02
Key length (2,3) : 0x0005
Extra length (4) : 0x08
Data type (5) : 0x00
VBucket (6,7) : 0x0000
Total body (8-11) : 0x00000012
Opaque (12-15): 0x00000000
CAS (16-23): 0x00000000decafbad
Extras :
Flags (24-27): 0xdeadbeef
Expiry (28-31): 0x00000e10
Key (32-36): The textual string "Hello"
Value (37-41): The textual string "World"
*/
var serializedRequestMessage = []byte{
0x80, // magic
0x02, // op code
0x00, 0x05, // key length
0x08, // extra length
0x00, // data type
0x00, 0x00, // v bucket id
0x00, 0x00, 0x00, 0x12, // total body length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0xde, 0xca, 0xfb, 0xad, // cas
0xde, 0xad, 0xbe, 0xef, // flags
0x00, 0x00, 0x0e, 0x10, // expiry
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
c.Assert(s.channel.Bytes(), DeepEquals, serializedRequestMessage)
}
func (s *RawClientSuite) TestRecvResponse(c *C) {
/*
Byte/ 0 | 1 | 2 | 3 |
/ | | | |
|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
+---------------+---------------+---------------+---------------+
0| 0x81 | 0x09 | 0x00 | 0x05 |
+---------------+---------------+---------------+---------------+
4| 0x04 | 0x00 | 0x00 | 0x02 |
+---------------+---------------+---------------+---------------+
8| 0x00 | 0x00 | 0x00 | 0x0e |
+---------------+---------------+---------------+---------------+
12| 0x00 | 0x00 | 0x00 | 0x00 |
+---------------+---------------+---------------+---------------+
16| 0x00 | 0x00 | 0x00 | 0x00 |
+---------------+---------------+---------------+---------------+
20| 0x00 | 0x00 | 0x00 | 0x01 |
+---------------+---------------+---------------+---------------+
24| 0xde | 0xad | 0xbe | 0xef |
+---------------+---------------+---------------+---------------+
28| 0x48 ('H') | 0x65 ('e') | 0x6c ('l') | 0x6c ('l') |
+---------------+---------------+---------------+---------------+
32| 0x6f ('o') | 0x57 ('W') | 0x6f ('o') | 0x72 ('r') |
+---------------+---------------+---------------+---------------+
36| 0x6c ('l') | 0x64 ('d') |
+---------------+---------------+
Total 38 bytes (24 byte header, 4 byte extras, 5 byte key
and 5 byte value)
Field (offset) (value)
Magic (0) : 0x81
Opcode (1) : 0x09
Key length (2,3) : 0x0005
Extra length (4) : 0x04
Data type (5) : 0x00
Status (6,7) : 0x0002
Total body (8-11) : 0x0000000e
Opaque (12-15): 0x00000000
CAS (16-23): 0x0000000000000001
Extras :
Flags (24-27): 0xdeadbeef
Key (28-32): The textual string: "Hello"
Value (33-37): The textual string: "World"
*/
var serializedResponseMessage = []byte{
0x81, // magic
0x09, // op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
status, cas, key, val, err := s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, IsNil)
c.Assert(status, Equals, StatusKeyExists)
c.Assert(cas, Equals, uint64(1))
c.Assert(key, DeepEquals, []byte("Hello"))
c.Assert(val, DeepEquals, []byte("World"))
c.Assert(flags, Equals, uint32(0xdeadbeef))
}
func (s *RawClientSuite) TestRecvResponseNoExtrasByteWithExtrasArg(c *C) {
var serializedResponseMessage = []byte{
0x81, // magic
0x09, // op code
0x00, 0x05, // key length
0x00, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, NotNil)
}
func (s *RawClientSuite) TestRecvResponseNotEnoughExtrasBytes(c *C) {
var serializedResponseMessage = []byte{
0x81, // magic
0x09, // op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
var otherExtra uint32 // unexpected extra field
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags,
&otherExtra)
c.Assert(err, NotNil)
}
func (s *RawClientSuite) TestRecvResponseTooManExtrasBytes(c *C) {
var serializedResponseMessage = []byte{
0x81, // magic
0x09, // op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint16 // normally should be 32
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, NotNil)
}
func (s *RawClientSuite) TestRecvResponseBadTotalLength(c *C) {
var serializedResponseMessage = []byte{
0x81, // magic
0x09, // op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x01, // (bad) total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, NotNil)
}
func (s *RawClientSuite) TestRecvResponseBadMagic(c *C) {
var serializedResponseMessage = []byte{
0x82, // (bad) magic
0x09, // op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, NotNil)
}
func (s *RawClientSuite) TestRecvResponseBadOpCode(c *C) {
var serializedResponseMessage = []byte{
0x81, // magic
0x03, // (bad) op code
0x00, 0x05, // key length
0x04, // extras length
0x0, // data type
0x00, 0x02, // status
0x00, 0x00, 0x00, 0x0e, // total length
0x00, 0x00, 0x00, 0x00, // opaque
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // cas
0xde, 0xad, 0xbe, 0xef, // flags
'H', 'e', 'l', 'l', 'o', // key
'W', 'o', 'r', 'l', 'd', // value
}
_, err := s.channel.Write(serializedResponseMessage)
c.Assert(err, IsNil)
var flags uint32
_, _, _, _, err = s.client.receiveResponse(
opGetQ,
&flags)
c.Assert(err, NotNil)
}