]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package thrift | |
21 | ||
22 | import ( | |
23 | "context" | |
24 | "encoding/base64" | |
25 | "encoding/json" | |
26 | "fmt" | |
27 | "math" | |
28 | "strconv" | |
29 | "strings" | |
30 | "testing" | |
31 | ) | |
32 | ||
33 | func TestWriteSimpleJSONProtocolBool(t *testing.T) { | |
34 | thetype := "boolean" | |
35 | trans := NewTMemoryBuffer() | |
36 | p := NewTSimpleJSONProtocol(trans) | |
37 | for _, value := range BOOL_VALUES { | |
38 | if e := p.WriteBool(value); e != nil { | |
39 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
40 | } | |
41 | if e := p.Flush(context.Background()); e != nil { | |
42 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
43 | } | |
44 | s := trans.String() | |
45 | if s != fmt.Sprint(value) { | |
46 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
47 | } | |
48 | v := false | |
49 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
50 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
51 | } | |
52 | trans.Reset() | |
53 | } | |
54 | trans.Close() | |
55 | } | |
56 | ||
57 | func TestReadSimpleJSONProtocolBool(t *testing.T) { | |
58 | thetype := "boolean" | |
59 | for _, value := range BOOL_VALUES { | |
60 | trans := NewTMemoryBuffer() | |
61 | p := NewTSimpleJSONProtocol(trans) | |
62 | if value { | |
63 | trans.Write(JSON_TRUE) | |
64 | } else { | |
65 | trans.Write(JSON_FALSE) | |
66 | } | |
67 | trans.Flush(context.Background()) | |
68 | s := trans.String() | |
69 | v, e := p.ReadBool() | |
70 | if e != nil { | |
71 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
72 | } | |
73 | if v != value { | |
74 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
75 | } | |
76 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
77 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
78 | } | |
79 | trans.Reset() | |
80 | trans.Close() | |
81 | } | |
82 | } | |
83 | ||
84 | func TestWriteSimpleJSONProtocolByte(t *testing.T) { | |
85 | thetype := "byte" | |
86 | trans := NewTMemoryBuffer() | |
87 | p := NewTSimpleJSONProtocol(trans) | |
88 | for _, value := range BYTE_VALUES { | |
89 | if e := p.WriteByte(value); e != nil { | |
90 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
91 | } | |
92 | if e := p.Flush(context.Background()); e != nil { | |
93 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
94 | } | |
95 | s := trans.String() | |
96 | if s != fmt.Sprint(value) { | |
97 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
98 | } | |
99 | v := int8(0) | |
100 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
101 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
102 | } | |
103 | trans.Reset() | |
104 | } | |
105 | trans.Close() | |
106 | } | |
107 | ||
108 | func TestReadSimpleJSONProtocolByte(t *testing.T) { | |
109 | thetype := "byte" | |
110 | for _, value := range BYTE_VALUES { | |
111 | trans := NewTMemoryBuffer() | |
112 | p := NewTSimpleJSONProtocol(trans) | |
113 | trans.WriteString(strconv.Itoa(int(value))) | |
114 | trans.Flush(context.Background()) | |
115 | s := trans.String() | |
116 | v, e := p.ReadByte() | |
117 | if e != nil { | |
118 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
119 | } | |
120 | if v != value { | |
121 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
122 | } | |
123 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
124 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
125 | } | |
126 | trans.Reset() | |
127 | trans.Close() | |
128 | } | |
129 | } | |
130 | ||
131 | func TestWriteSimpleJSONProtocolI16(t *testing.T) { | |
132 | thetype := "int16" | |
133 | trans := NewTMemoryBuffer() | |
134 | p := NewTSimpleJSONProtocol(trans) | |
135 | for _, value := range INT16_VALUES { | |
136 | if e := p.WriteI16(value); e != nil { | |
137 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
138 | } | |
139 | if e := p.Flush(context.Background()); e != nil { | |
140 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
141 | } | |
142 | s := trans.String() | |
143 | if s != fmt.Sprint(value) { | |
144 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
145 | } | |
146 | v := int16(0) | |
147 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
148 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
149 | } | |
150 | trans.Reset() | |
151 | } | |
152 | trans.Close() | |
153 | } | |
154 | ||
155 | func TestReadSimpleJSONProtocolI16(t *testing.T) { | |
156 | thetype := "int16" | |
157 | for _, value := range INT16_VALUES { | |
158 | trans := NewTMemoryBuffer() | |
159 | p := NewTSimpleJSONProtocol(trans) | |
160 | trans.WriteString(strconv.Itoa(int(value))) | |
161 | trans.Flush(context.Background()) | |
162 | s := trans.String() | |
163 | v, e := p.ReadI16() | |
164 | if e != nil { | |
165 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
166 | } | |
167 | if v != value { | |
168 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
169 | } | |
170 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
171 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
172 | } | |
173 | trans.Reset() | |
174 | trans.Close() | |
175 | } | |
176 | } | |
177 | ||
178 | func TestWriteSimpleJSONProtocolI32(t *testing.T) { | |
179 | thetype := "int32" | |
180 | trans := NewTMemoryBuffer() | |
181 | p := NewTSimpleJSONProtocol(trans) | |
182 | for _, value := range INT32_VALUES { | |
183 | if e := p.WriteI32(value); e != nil { | |
184 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
185 | } | |
186 | if e := p.Flush(context.Background()); e != nil { | |
187 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
188 | } | |
189 | s := trans.String() | |
190 | if s != fmt.Sprint(value) { | |
191 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
192 | } | |
193 | v := int32(0) | |
194 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
195 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
196 | } | |
197 | trans.Reset() | |
198 | } | |
199 | trans.Close() | |
200 | } | |
201 | ||
202 | func TestReadSimpleJSONProtocolI32(t *testing.T) { | |
203 | thetype := "int32" | |
204 | for _, value := range INT32_VALUES { | |
205 | trans := NewTMemoryBuffer() | |
206 | p := NewTSimpleJSONProtocol(trans) | |
207 | trans.WriteString(strconv.Itoa(int(value))) | |
208 | trans.Flush(context.Background()) | |
209 | s := trans.String() | |
210 | v, e := p.ReadI32() | |
211 | if e != nil { | |
212 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
213 | } | |
214 | if v != value { | |
215 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
216 | } | |
217 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
218 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
219 | } | |
220 | trans.Reset() | |
221 | trans.Close() | |
222 | } | |
223 | } | |
224 | ||
225 | func TestReadSimpleJSONProtocolI32Null(t *testing.T) { | |
226 | thetype := "int32" | |
227 | value := "null" | |
228 | ||
229 | trans := NewTMemoryBuffer() | |
230 | p := NewTSimpleJSONProtocol(trans) | |
231 | trans.WriteString(value) | |
232 | trans.Flush(context.Background()) | |
233 | s := trans.String() | |
234 | v, e := p.ReadI32() | |
235 | ||
236 | if e != nil { | |
237 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
238 | } | |
239 | if v != 0 { | |
240 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
241 | } | |
242 | trans.Reset() | |
243 | trans.Close() | |
244 | } | |
245 | ||
246 | func TestWriteSimpleJSONProtocolI64(t *testing.T) { | |
247 | thetype := "int64" | |
248 | trans := NewTMemoryBuffer() | |
249 | p := NewTSimpleJSONProtocol(trans) | |
250 | for _, value := range INT64_VALUES { | |
251 | if e := p.WriteI64(value); e != nil { | |
252 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
253 | } | |
254 | if e := p.Flush(context.Background()); e != nil { | |
255 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
256 | } | |
257 | s := trans.String() | |
258 | if s != fmt.Sprint(value) { | |
259 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
260 | } | |
261 | v := int64(0) | |
262 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
263 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
264 | } | |
265 | trans.Reset() | |
266 | } | |
267 | trans.Close() | |
268 | } | |
269 | ||
270 | func TestReadSimpleJSONProtocolI64(t *testing.T) { | |
271 | thetype := "int64" | |
272 | for _, value := range INT64_VALUES { | |
273 | trans := NewTMemoryBuffer() | |
274 | p := NewTSimpleJSONProtocol(trans) | |
275 | trans.WriteString(strconv.FormatInt(value, 10)) | |
276 | trans.Flush(context.Background()) | |
277 | s := trans.String() | |
278 | v, e := p.ReadI64() | |
279 | if e != nil { | |
280 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
281 | } | |
282 | if v != value { | |
283 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
284 | } | |
285 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
286 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
287 | } | |
288 | trans.Reset() | |
289 | trans.Close() | |
290 | } | |
291 | } | |
292 | ||
293 | func TestReadSimpleJSONProtocolI64Null(t *testing.T) { | |
294 | thetype := "int32" | |
295 | value := "null" | |
296 | ||
297 | trans := NewTMemoryBuffer() | |
298 | p := NewTSimpleJSONProtocol(trans) | |
299 | trans.WriteString(value) | |
300 | trans.Flush(context.Background()) | |
301 | s := trans.String() | |
302 | v, e := p.ReadI64() | |
303 | ||
304 | if e != nil { | |
305 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
306 | } | |
307 | if v != 0 { | |
308 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
309 | } | |
310 | trans.Reset() | |
311 | trans.Close() | |
312 | } | |
313 | ||
314 | func TestWriteSimpleJSONProtocolDouble(t *testing.T) { | |
315 | thetype := "double" | |
316 | trans := NewTMemoryBuffer() | |
317 | p := NewTSimpleJSONProtocol(trans) | |
318 | for _, value := range DOUBLE_VALUES { | |
319 | if e := p.WriteDouble(value); e != nil { | |
320 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
321 | } | |
322 | if e := p.Flush(context.Background()); e != nil { | |
323 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
324 | } | |
325 | s := trans.String() | |
326 | if math.IsInf(value, 1) { | |
327 | if s != jsonQuote(JSON_INFINITY) { | |
328 | t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_INFINITY)) | |
329 | } | |
330 | } else if math.IsInf(value, -1) { | |
331 | if s != jsonQuote(JSON_NEGATIVE_INFINITY) { | |
332 | t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NEGATIVE_INFINITY)) | |
333 | } | |
334 | } else if math.IsNaN(value) { | |
335 | if s != jsonQuote(JSON_NAN) { | |
336 | t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NAN)) | |
337 | } | |
338 | } else { | |
339 | if s != fmt.Sprint(value) { | |
340 | t.Fatalf("Bad value for %s %v: %s", thetype, value, s) | |
341 | } | |
342 | v := float64(0) | |
343 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
344 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
345 | } | |
346 | } | |
347 | trans.Reset() | |
348 | } | |
349 | trans.Close() | |
350 | } | |
351 | ||
352 | func TestReadSimpleJSONProtocolDouble(t *testing.T) { | |
353 | thetype := "double" | |
354 | for _, value := range DOUBLE_VALUES { | |
355 | trans := NewTMemoryBuffer() | |
356 | p := NewTSimpleJSONProtocol(trans) | |
357 | n := NewNumericFromDouble(value) | |
358 | trans.WriteString(n.String()) | |
359 | trans.Flush(context.Background()) | |
360 | s := trans.String() | |
361 | v, e := p.ReadDouble() | |
362 | if e != nil { | |
363 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
364 | } | |
365 | if math.IsInf(value, 1) { | |
366 | if !math.IsInf(v, 1) { | |
367 | t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) | |
368 | } | |
369 | } else if math.IsInf(value, -1) { | |
370 | if !math.IsInf(v, -1) { | |
371 | t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) | |
372 | } | |
373 | } else if math.IsNaN(value) { | |
374 | if !math.IsNaN(v) { | |
375 | t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v) | |
376 | } | |
377 | } else { | |
378 | if v != value { | |
379 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
380 | } | |
381 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
382 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
383 | } | |
384 | } | |
385 | trans.Reset() | |
386 | trans.Close() | |
387 | } | |
388 | } | |
389 | ||
390 | func TestWriteSimpleJSONProtocolString(t *testing.T) { | |
391 | thetype := "string" | |
392 | trans := NewTMemoryBuffer() | |
393 | p := NewTSimpleJSONProtocol(trans) | |
394 | for _, value := range STRING_VALUES { | |
395 | if e := p.WriteString(value); e != nil { | |
396 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
397 | } | |
398 | if e := p.Flush(context.Background()); e != nil { | |
399 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
400 | } | |
401 | s := trans.String() | |
402 | if s[0] != '"' || s[len(s)-1] != '"' { | |
403 | t.Fatalf("Bad value for %s '%v', wrote '%v', expected: %v", thetype, value, s, fmt.Sprint("\"", value, "\"")) | |
404 | } | |
405 | v := new(string) | |
406 | if err := json.Unmarshal([]byte(s), v); err != nil || *v != value { | |
407 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v) | |
408 | } | |
409 | trans.Reset() | |
410 | } | |
411 | trans.Close() | |
412 | } | |
413 | ||
414 | func TestReadSimpleJSONProtocolString(t *testing.T) { | |
415 | thetype := "string" | |
416 | for _, value := range STRING_VALUES { | |
417 | trans := NewTMemoryBuffer() | |
418 | p := NewTSimpleJSONProtocol(trans) | |
419 | trans.WriteString(jsonQuote(value)) | |
420 | trans.Flush(context.Background()) | |
421 | s := trans.String() | |
422 | v, e := p.ReadString() | |
423 | if e != nil { | |
424 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
425 | } | |
426 | if v != value { | |
427 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
428 | } | |
429 | v1 := new(string) | |
430 | if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != value { | |
431 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1) | |
432 | } | |
433 | trans.Reset() | |
434 | trans.Close() | |
435 | } | |
436 | } | |
437 | func TestReadSimpleJSONProtocolStringNull(t *testing.T) { | |
438 | thetype := "string" | |
439 | value := "null" | |
440 | ||
441 | trans := NewTMemoryBuffer() | |
442 | p := NewTSimpleJSONProtocol(trans) | |
443 | trans.WriteString(value) | |
444 | trans.Flush(context.Background()) | |
445 | s := trans.String() | |
446 | v, e := p.ReadString() | |
447 | if e != nil { | |
448 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
449 | } | |
450 | if v != "" { | |
451 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
452 | } | |
453 | trans.Reset() | |
454 | trans.Close() | |
455 | } | |
456 | ||
457 | func TestWriteSimpleJSONProtocolBinary(t *testing.T) { | |
458 | thetype := "binary" | |
459 | value := protocol_bdata | |
460 | b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata))) | |
461 | base64.StdEncoding.Encode(b64value, value) | |
462 | b64String := string(b64value) | |
463 | trans := NewTMemoryBuffer() | |
464 | p := NewTSimpleJSONProtocol(trans) | |
465 | if e := p.WriteBinary(value); e != nil { | |
466 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
467 | } | |
468 | if e := p.Flush(context.Background()); e != nil { | |
469 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) | |
470 | } | |
471 | s := trans.String() | |
472 | if s != fmt.Sprint("\"", b64String, "\"") { | |
473 | t.Fatalf("Bad value for %s %v\n wrote: %v\nexpected: %v", thetype, value, s, "\""+b64String+"\"") | |
474 | } | |
475 | v1 := new(string) | |
476 | if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String { | |
477 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1) | |
478 | } | |
479 | trans.Close() | |
480 | } | |
481 | ||
482 | func TestReadSimpleJSONProtocolBinary(t *testing.T) { | |
483 | thetype := "binary" | |
484 | value := protocol_bdata | |
485 | b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata))) | |
486 | base64.StdEncoding.Encode(b64value, value) | |
487 | b64String := string(b64value) | |
488 | trans := NewTMemoryBuffer() | |
489 | p := NewTSimpleJSONProtocol(trans) | |
490 | trans.WriteString(jsonQuote(b64String)) | |
491 | trans.Flush(context.Background()) | |
492 | s := trans.String() | |
493 | v, e := p.ReadBinary() | |
494 | if e != nil { | |
495 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
496 | } | |
497 | if len(v) != len(value) { | |
498 | t.Fatalf("Bad value for %s value length %v, wrote: %v, received length: %v", thetype, len(value), s, len(v)) | |
499 | } | |
500 | for i := 0; i < len(v); i++ { | |
501 | if v[i] != value[i] { | |
502 | t.Fatalf("Bad value for %s at index %d value %v, wrote: %v, received: %v", thetype, i, value[i], s, v[i]) | |
503 | } | |
504 | } | |
505 | v1 := new(string) | |
506 | if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String { | |
507 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1) | |
508 | } | |
509 | trans.Reset() | |
510 | trans.Close() | |
511 | } | |
512 | ||
513 | func TestReadSimpleJSONProtocolBinaryNull(t *testing.T) { | |
514 | thetype := "binary" | |
515 | value := "null" | |
516 | ||
517 | trans := NewTMemoryBuffer() | |
518 | p := NewTSimpleJSONProtocol(trans) | |
519 | trans.WriteString(value) | |
520 | trans.Flush(context.Background()) | |
521 | s := trans.String() | |
522 | b, e := p.ReadBinary() | |
523 | v := string(b) | |
524 | ||
525 | if e != nil { | |
526 | t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error()) | |
527 | } | |
528 | if v != "" { | |
529 | t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v) | |
530 | } | |
531 | trans.Reset() | |
532 | trans.Close() | |
533 | } | |
534 | ||
535 | func TestWriteSimpleJSONProtocolList(t *testing.T) { | |
536 | thetype := "list" | |
537 | trans := NewTMemoryBuffer() | |
538 | p := NewTSimpleJSONProtocol(trans) | |
539 | p.WriteListBegin(TType(DOUBLE), len(DOUBLE_VALUES)) | |
540 | for _, value := range DOUBLE_VALUES { | |
541 | if e := p.WriteDouble(value); e != nil { | |
542 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
543 | } | |
544 | } | |
545 | p.WriteListEnd() | |
546 | if e := p.Flush(context.Background()); e != nil { | |
547 | t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) | |
548 | } | |
549 | str := trans.String() | |
550 | str1 := new([]interface{}) | |
551 | err := json.Unmarshal([]byte(str), str1) | |
552 | if err != nil { | |
553 | t.Fatalf("Unable to decode %s, wrote: %s", thetype, str) | |
554 | } | |
555 | l := *str1 | |
556 | if len(l) < 2 { | |
557 | t.Fatalf("List must be at least of length two to include metadata") | |
558 | } | |
559 | if int(l[0].(float64)) != DOUBLE { | |
560 | t.Fatal("Invalid type for list, expected: ", DOUBLE, ", but was: ", l[0]) | |
561 | } | |
562 | if int(l[1].(float64)) != len(DOUBLE_VALUES) { | |
563 | t.Fatal("Invalid length for list, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1]) | |
564 | } | |
565 | for k, value := range DOUBLE_VALUES { | |
566 | s := l[k+2] | |
567 | if math.IsInf(value, 1) { | |
568 | if s.(string) != JSON_INFINITY { | |
569 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str) | |
570 | } | |
571 | } else if math.IsInf(value, 0) { | |
572 | if s.(string) != JSON_NEGATIVE_INFINITY { | |
573 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str) | |
574 | } | |
575 | } else if math.IsNaN(value) { | |
576 | if s.(string) != JSON_NAN { | |
577 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str) | |
578 | } | |
579 | } else { | |
580 | if s.(float64) != value { | |
581 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s) | |
582 | } | |
583 | } | |
584 | trans.Reset() | |
585 | } | |
586 | trans.Close() | |
587 | } | |
588 | ||
589 | func TestWriteSimpleJSONProtocolSet(t *testing.T) { | |
590 | thetype := "set" | |
591 | trans := NewTMemoryBuffer() | |
592 | p := NewTSimpleJSONProtocol(trans) | |
593 | p.WriteSetBegin(TType(DOUBLE), len(DOUBLE_VALUES)) | |
594 | for _, value := range DOUBLE_VALUES { | |
595 | if e := p.WriteDouble(value); e != nil { | |
596 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) | |
597 | } | |
598 | } | |
599 | p.WriteSetEnd() | |
600 | if e := p.Flush(context.Background()); e != nil { | |
601 | t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) | |
602 | } | |
603 | str := trans.String() | |
604 | str1 := new([]interface{}) | |
605 | err := json.Unmarshal([]byte(str), str1) | |
606 | if err != nil { | |
607 | t.Fatalf("Unable to decode %s, wrote: %s", thetype, str) | |
608 | } | |
609 | l := *str1 | |
610 | if len(l) < 2 { | |
611 | t.Fatalf("Set must be at least of length two to include metadata") | |
612 | } | |
613 | if int(l[0].(float64)) != DOUBLE { | |
614 | t.Fatal("Invalid type for set, expected: ", DOUBLE, ", but was: ", l[0]) | |
615 | } | |
616 | if int(l[1].(float64)) != len(DOUBLE_VALUES) { | |
617 | t.Fatal("Invalid length for set, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1]) | |
618 | } | |
619 | for k, value := range DOUBLE_VALUES { | |
620 | s := l[k+2] | |
621 | if math.IsInf(value, 1) { | |
622 | if s.(string) != JSON_INFINITY { | |
623 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_INFINITY), str) | |
624 | } | |
625 | } else if math.IsInf(value, 0) { | |
626 | if s.(string) != JSON_NEGATIVE_INFINITY { | |
627 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY), str) | |
628 | } | |
629 | } else if math.IsNaN(value) { | |
630 | if s.(string) != JSON_NAN { | |
631 | t.Fatalf("Bad value for %s at index %v %v, wrote: %q, expected: %q, originally wrote: %q", thetype, k, value, s, jsonQuote(JSON_NAN), str) | |
632 | } | |
633 | } else { | |
634 | if s.(float64) != value { | |
635 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s) | |
636 | } | |
637 | } | |
638 | trans.Reset() | |
639 | } | |
640 | trans.Close() | |
641 | } | |
642 | ||
643 | func TestWriteSimpleJSONProtocolMap(t *testing.T) { | |
644 | thetype := "map" | |
645 | trans := NewTMemoryBuffer() | |
646 | p := NewTSimpleJSONProtocol(trans) | |
647 | p.WriteMapBegin(TType(I32), TType(DOUBLE), len(DOUBLE_VALUES)) | |
648 | for k, value := range DOUBLE_VALUES { | |
649 | if e := p.WriteI32(int32(k)); e != nil { | |
650 | t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error()) | |
651 | } | |
652 | if e := p.WriteDouble(value); e != nil { | |
653 | t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error()) | |
654 | } | |
655 | } | |
656 | p.WriteMapEnd() | |
657 | if e := p.Flush(context.Background()); e != nil { | |
658 | t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error()) | |
659 | } | |
660 | str := trans.String() | |
661 | if str[0] != '[' || str[len(str)-1] != ']' { | |
662 | t.Fatalf("Bad value for %s, wrote: %v, in go: %v", thetype, str, DOUBLE_VALUES) | |
663 | } | |
664 | l := strings.Split(str[1:len(str)-1], ",") | |
665 | if len(l) < 3 { | |
666 | t.Fatal("Expected list of at least length 3 for map for metadata, but was of length ", len(l)) | |
667 | } | |
668 | expectedKeyType, _ := strconv.Atoi(l[0]) | |
669 | expectedValueType, _ := strconv.Atoi(l[1]) | |
670 | expectedSize, _ := strconv.Atoi(l[2]) | |
671 | if expectedKeyType != I32 { | |
672 | t.Fatal("Expected map key type ", I32, ", but was ", l[0]) | |
673 | } | |
674 | if expectedValueType != DOUBLE { | |
675 | t.Fatal("Expected map value type ", DOUBLE, ", but was ", l[1]) | |
676 | } | |
677 | if expectedSize != len(DOUBLE_VALUES) { | |
678 | t.Fatal("Expected map size of ", len(DOUBLE_VALUES), ", but was ", l[2]) | |
679 | } | |
680 | for k, value := range DOUBLE_VALUES { | |
681 | strk := l[k*2+3] | |
682 | strv := l[k*2+4] | |
683 | ik, err := strconv.Atoi(strk) | |
684 | if err != nil { | |
685 | t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, strk, string(k), err.Error()) | |
686 | } | |
687 | if ik != k { | |
688 | t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v", thetype, k, strk, k) | |
689 | } | |
690 | s := strv | |
691 | if math.IsInf(value, 1) { | |
692 | if s != jsonQuote(JSON_INFINITY) { | |
693 | t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_INFINITY)) | |
694 | } | |
695 | } else if math.IsInf(value, 0) { | |
696 | if s != jsonQuote(JSON_NEGATIVE_INFINITY) { | |
697 | t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY)) | |
698 | } | |
699 | } else if math.IsNaN(value) { | |
700 | if s != jsonQuote(JSON_NAN) { | |
701 | t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NAN)) | |
702 | } | |
703 | } else { | |
704 | expected := strconv.FormatFloat(value, 'g', 10, 64) | |
705 | if s != expected { | |
706 | t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected %v", thetype, k, value, s, expected) | |
707 | } | |
708 | v := float64(0) | |
709 | if err := json.Unmarshal([]byte(s), &v); err != nil || v != value { | |
710 | t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v) | |
711 | } | |
712 | } | |
713 | trans.Reset() | |
714 | } | |
715 | trans.Close() | |
716 | } | |
717 | ||
718 | func TestWriteSimpleJSONProtocolSafePeek(t *testing.T) { | |
719 | trans := NewTMemoryBuffer() | |
720 | p := NewTSimpleJSONProtocol(trans) | |
721 | trans.Write([]byte{'a', 'b'}) | |
722 | trans.Flush(context.Background()) | |
723 | ||
724 | test1 := p.safePeekContains([]byte{'a', 'b'}) | |
725 | if !test1 { | |
726 | t.Fatalf("Should match at test 1") | |
727 | } | |
728 | ||
729 | test2 := p.safePeekContains([]byte{'a', 'b', 'c', 'd'}) | |
730 | if test2 { | |
731 | t.Fatalf("Should not match at test 2") | |
732 | } | |
733 | ||
734 | test3 := p.safePeekContains([]byte{'x', 'y'}) | |
735 | if test3 { | |
736 | t.Fatalf("Should not match at test 3") | |
737 | } | |
738 | } |