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
20package thrift
21
22import (
23	"context"
24	"encoding/base64"
25	"encoding/json"
26	"fmt"
27	"math"
28	"strconv"
29	"testing"
30)
31
32func TestWriteJSONProtocolBool(t *testing.T) {
33	thetype := "boolean"
34	trans := NewTMemoryBuffer()
35	p := NewTJSONProtocol(trans)
36	for _, value := range BOOL_VALUES {
37		if e := p.WriteBool(context.Background(), value); e != nil {
38			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
39		}
40		if e := p.Flush(context.Background()); e != nil {
41			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
42		}
43		s := trans.String()
44		expected := ""
45		if value {
46			expected = "1"
47		} else {
48			expected = "0"
49		}
50		if s != expected {
51			t.Fatalf("Bad value for %s %v: %s expected", thetype, value, s)
52		}
53		v := -1
54		if err := json.Unmarshal([]byte(s), &v); err != nil || (v != 0) != value {
55			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
56		}
57		trans.Reset()
58	}
59	trans.Close()
60}
61
62func TestReadJSONProtocolBool(t *testing.T) {
63	thetype := "boolean"
64	for _, value := range BOOL_VALUES {
65		trans := NewTMemoryBuffer()
66		p := NewTJSONProtocol(trans)
67		if value {
68			trans.Write([]byte{'1'}) // not JSON_TRUE
69		} else {
70			trans.Write([]byte{'0'}) // not JSON_FALSE
71		}
72		trans.Flush(context.Background())
73		s := trans.String()
74		v, e := p.ReadBool(context.Background())
75		if e != nil {
76			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
77		}
78		if v != value {
79			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
80		}
81		vv := -1
82		if err := json.Unmarshal([]byte(s), &vv); err != nil || (vv != 0) != value {
83			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, vv)
84		}
85		trans.Reset()
86		trans.Close()
87	}
88}
89
90func TestWriteJSONProtocolByte(t *testing.T) {
91	thetype := "byte"
92	trans := NewTMemoryBuffer()
93	p := NewTJSONProtocol(trans)
94	for _, value := range BYTE_VALUES {
95		if e := p.WriteByte(context.Background(), value); e != nil {
96			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
97		}
98		if e := p.Flush(context.Background()); e != nil {
99			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
100		}
101		s := trans.String()
102		if s != fmt.Sprint(value) {
103			t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
104		}
105		v := int8(0)
106		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
107			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
108		}
109		trans.Reset()
110	}
111	trans.Close()
112}
113
114func TestReadJSONProtocolByte(t *testing.T) {
115	thetype := "byte"
116	for _, value := range BYTE_VALUES {
117		trans := NewTMemoryBuffer()
118		p := NewTJSONProtocol(trans)
119		trans.WriteString(strconv.Itoa(int(value)))
120		trans.Flush(context.Background())
121		s := trans.String()
122		v, e := p.ReadByte(context.Background())
123		if e != nil {
124			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
125		}
126		if v != value {
127			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
128		}
129		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
130			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
131		}
132		trans.Reset()
133		trans.Close()
134	}
135}
136
137func TestWriteJSONProtocolI16(t *testing.T) {
138	thetype := "int16"
139	trans := NewTMemoryBuffer()
140	p := NewTJSONProtocol(trans)
141	for _, value := range INT16_VALUES {
142		if e := p.WriteI16(context.Background(), value); e != nil {
143			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
144		}
145		if e := p.Flush(context.Background()); e != nil {
146			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
147		}
148		s := trans.String()
149		if s != fmt.Sprint(value) {
150			t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
151		}
152		v := int16(0)
153		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
154			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
155		}
156		trans.Reset()
157	}
158	trans.Close()
159}
160
161func TestReadJSONProtocolI16(t *testing.T) {
162	thetype := "int16"
163	for _, value := range INT16_VALUES {
164		trans := NewTMemoryBuffer()
165		p := NewTJSONProtocol(trans)
166		trans.WriteString(strconv.Itoa(int(value)))
167		trans.Flush(context.Background())
168		s := trans.String()
169		v, e := p.ReadI16(context.Background())
170		if e != nil {
171			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
172		}
173		if v != value {
174			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
175		}
176		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
177			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
178		}
179		trans.Reset()
180		trans.Close()
181	}
182}
183
184func TestWriteJSONProtocolI32(t *testing.T) {
185	thetype := "int32"
186	trans := NewTMemoryBuffer()
187	p := NewTJSONProtocol(trans)
188	for _, value := range INT32_VALUES {
189		if e := p.WriteI32(context.Background(), value); e != nil {
190			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
191		}
192		if e := p.Flush(context.Background()); e != nil {
193			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
194		}
195		s := trans.String()
196		if s != fmt.Sprint(value) {
197			t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
198		}
199		v := int32(0)
200		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
201			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
202		}
203		trans.Reset()
204	}
205	trans.Close()
206}
207
208func TestReadJSONProtocolI32(t *testing.T) {
209	thetype := "int32"
210	for _, value := range INT32_VALUES {
211		trans := NewTMemoryBuffer()
212		p := NewTJSONProtocol(trans)
213		trans.WriteString(strconv.Itoa(int(value)))
214		trans.Flush(context.Background())
215		s := trans.String()
216		v, e := p.ReadI32(context.Background())
217		if e != nil {
218			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
219		}
220		if v != value {
221			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
222		}
223		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
224			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
225		}
226		trans.Reset()
227		trans.Close()
228	}
229}
230
231func TestWriteJSONProtocolI64(t *testing.T) {
232	thetype := "int64"
233	trans := NewTMemoryBuffer()
234	p := NewTJSONProtocol(trans)
235	for _, value := range INT64_VALUES {
236		if e := p.WriteI64(context.Background(), value); e != nil {
237			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
238		}
239		if e := p.Flush(context.Background()); e != nil {
240			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
241		}
242		s := trans.String()
243		if s != fmt.Sprint(value) {
244			t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
245		}
246		v := int64(0)
247		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
248			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
249		}
250		trans.Reset()
251	}
252	trans.Close()
253}
254
255func TestReadJSONProtocolI64(t *testing.T) {
256	thetype := "int64"
257	for _, value := range INT64_VALUES {
258		trans := NewTMemoryBuffer()
259		p := NewTJSONProtocol(trans)
260		trans.WriteString(strconv.FormatInt(value, 10))
261		trans.Flush(context.Background())
262		s := trans.String()
263		v, e := p.ReadI64(context.Background())
264		if e != nil {
265			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
266		}
267		if v != value {
268			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
269		}
270		if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
271			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
272		}
273		trans.Reset()
274		trans.Close()
275	}
276}
277
278func TestWriteJSONProtocolDouble(t *testing.T) {
279	thetype := "double"
280	trans := NewTMemoryBuffer()
281	p := NewTJSONProtocol(trans)
282	for _, value := range DOUBLE_VALUES {
283		if e := p.WriteDouble(context.Background(), value); e != nil {
284			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
285		}
286		if e := p.Flush(context.Background()); e != nil {
287			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
288		}
289		s := trans.String()
290		if math.IsInf(value, 1) {
291			if s != jsonQuote(JSON_INFINITY) {
292				t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_INFINITY))
293			}
294		} else if math.IsInf(value, -1) {
295			if s != jsonQuote(JSON_NEGATIVE_INFINITY) {
296				t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NEGATIVE_INFINITY))
297			}
298		} else if math.IsNaN(value) {
299			if s != jsonQuote(JSON_NAN) {
300				t.Fatalf("Bad value for %s %v, wrote: %v, expected: %v", thetype, value, s, jsonQuote(JSON_NAN))
301			}
302		} else {
303			if s != fmt.Sprint(value) {
304				t.Fatalf("Bad value for %s %v: %s", thetype, value, s)
305			}
306			v := float64(0)
307			if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
308				t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
309			}
310		}
311		trans.Reset()
312	}
313	trans.Close()
314}
315
316func TestReadJSONProtocolDouble(t *testing.T) {
317	thetype := "double"
318	for _, value := range DOUBLE_VALUES {
319		trans := NewTMemoryBuffer()
320		p := NewTJSONProtocol(trans)
321		n := NewNumericFromDouble(value)
322		trans.WriteString(n.String())
323		trans.Flush(context.Background())
324		s := trans.String()
325		v, e := p.ReadDouble(context.Background())
326		if e != nil {
327			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
328		}
329		if math.IsInf(value, 1) {
330			if !math.IsInf(v, 1) {
331				t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
332			}
333		} else if math.IsInf(value, -1) {
334			if !math.IsInf(v, -1) {
335				t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
336			}
337		} else if math.IsNaN(value) {
338			if !math.IsNaN(v) {
339				t.Fatalf("Bad value for %s %v, wrote: %v, received: %v", thetype, value, s, v)
340			}
341		} else {
342			if v != value {
343				t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
344			}
345			if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
346				t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
347			}
348		}
349		trans.Reset()
350		trans.Close()
351	}
352}
353
354func TestWriteJSONProtocolString(t *testing.T) {
355	thetype := "string"
356	trans := NewTMemoryBuffer()
357	p := NewTJSONProtocol(trans)
358	for _, value := range STRING_VALUES {
359		if e := p.WriteString(context.Background(), value); e != nil {
360			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
361		}
362		if e := p.Flush(context.Background()); e != nil {
363			t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
364		}
365		s := trans.String()
366		if s[0] != '"' || s[len(s)-1] != '"' {
367			t.Fatalf("Bad value for %s '%v', wrote '%v', expected: %v", thetype, value, s, fmt.Sprint("\"", value, "\""))
368		}
369		v := new(string)
370		if err := json.Unmarshal([]byte(s), v); err != nil || *v != value {
371			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v)
372		}
373		trans.Reset()
374	}
375	trans.Close()
376}
377
378func TestReadJSONProtocolString(t *testing.T) {
379	thetype := "string"
380	for _, value := range STRING_VALUES {
381		trans := NewTMemoryBuffer()
382		p := NewTJSONProtocol(trans)
383		trans.WriteString(jsonQuote(value))
384		trans.Flush(context.Background())
385		s := trans.String()
386		v, e := p.ReadString(context.Background())
387		if e != nil {
388			t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
389		}
390		if v != value {
391			t.Fatalf("Bad value for %s value %v, wrote: %v, received: %v", thetype, value, s, v)
392		}
393		v1 := new(string)
394		if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != value {
395			t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1)
396		}
397		trans.Reset()
398		trans.Close()
399	}
400}
401
402func TestWriteJSONProtocolBinary(t *testing.T) {
403	thetype := "binary"
404	value := protocol_bdata
405	b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata)))
406	base64.StdEncoding.Encode(b64value, value)
407	b64String := string(b64value)
408	trans := NewTMemoryBuffer()
409	p := NewTJSONProtocol(trans)
410	if e := p.WriteBinary(context.Background(), value); e != nil {
411		t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
412	}
413	if e := p.Flush(context.Background()); e != nil {
414		t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
415	}
416	s := trans.String()
417	expectedString := fmt.Sprint("\"", b64String, "\"")
418	if s != expectedString {
419		t.Fatalf("Bad value for %s %v\n  wrote:  \"%v\"\nexpected: \"%v\"", thetype, value, s, expectedString)
420	}
421	v1, err := p.ReadBinary(context.Background())
422	if err != nil {
423		t.Fatalf("Unable to read binary: %s", err.Error())
424	}
425	if len(v1) != len(value) {
426		t.Fatalf("Invalid value for binary\nexpected: \"%v\"\n   read: \"%v\"", value, v1)
427	}
428	for k, v := range value {
429		if v1[k] != v {
430			t.Fatalf("Invalid value for binary at %v\nexpected: \"%v\"\n   read: \"%v\"", k, v, v1[k])
431		}
432	}
433	trans.Close()
434}
435
436func TestReadJSONProtocolBinary(t *testing.T) {
437	thetype := "binary"
438	value := protocol_bdata
439	b64value := make([]byte, base64.StdEncoding.EncodedLen(len(protocol_bdata)))
440	base64.StdEncoding.Encode(b64value, value)
441	b64String := string(b64value)
442	trans := NewTMemoryBuffer()
443	p := NewTJSONProtocol(trans)
444	trans.WriteString(jsonQuote(b64String))
445	trans.Flush(context.Background())
446	s := trans.String()
447	v, e := p.ReadBinary(context.Background())
448	if e != nil {
449		t.Fatalf("Unable to read %s value %v due to error: %s", thetype, value, e.Error())
450	}
451	if len(v) != len(value) {
452		t.Fatalf("Bad value for %s value length %v, wrote: %v, received length: %v", thetype, len(value), s, len(v))
453	}
454	for i := 0; i < len(v); i++ {
455		if v[i] != value[i] {
456			t.Fatalf("Bad value for %s at index %d value %v, wrote: %v, received: %v", thetype, i, value[i], s, v[i])
457		}
458	}
459	v1 := new(string)
460	if err := json.Unmarshal([]byte(s), v1); err != nil || *v1 != b64String {
461		t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, *v1)
462	}
463	trans.Reset()
464	trans.Close()
465}
466
467func TestWriteJSONProtocolList(t *testing.T) {
468	thetype := "list"
469	trans := NewTMemoryBuffer()
470	p := NewTJSONProtocol(trans)
471	p.WriteListBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES))
472	for _, value := range DOUBLE_VALUES {
473		if e := p.WriteDouble(context.Background(), value); e != nil {
474			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
475		}
476	}
477	p.WriteListEnd(context.Background())
478	if e := p.Flush(context.Background()); e != nil {
479		t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
480	}
481	str := trans.String()
482	str1 := new([]interface{})
483	err := json.Unmarshal([]byte(str), str1)
484	if err != nil {
485		t.Fatalf("Unable to decode %s, wrote: %s", thetype, str)
486	}
487	l := *str1
488	if len(l) < 2 {
489		t.Fatalf("List must be at least of length two to include metadata")
490	}
491	if l[0] != "dbl" {
492		t.Fatal("Invalid type for list, expected: ", STRING, ", but was: ", l[0])
493	}
494	if int(l[1].(float64)) != len(DOUBLE_VALUES) {
495		t.Fatal("Invalid length for list, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1])
496	}
497	for k, value := range DOUBLE_VALUES {
498		s := l[k+2]
499		if math.IsInf(value, 1) {
500			if s.(string) != JSON_INFINITY {
501				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)
502			}
503		} else if math.IsInf(value, 0) {
504			if s.(string) != JSON_NEGATIVE_INFINITY {
505				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)
506			}
507		} else if math.IsNaN(value) {
508			if s.(string) != JSON_NAN {
509				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)
510			}
511		} else {
512			if s.(float64) != value {
513				t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s)
514			}
515		}
516		trans.Reset()
517	}
518	trans.Close()
519}
520
521func TestWriteJSONProtocolSet(t *testing.T) {
522	thetype := "set"
523	trans := NewTMemoryBuffer()
524	p := NewTJSONProtocol(trans)
525	p.WriteSetBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES))
526	for _, value := range DOUBLE_VALUES {
527		if e := p.WriteDouble(context.Background(), value); e != nil {
528			t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
529		}
530	}
531	p.WriteSetEnd(context.Background())
532	if e := p.Flush(context.Background()); e != nil {
533		t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
534	}
535	str := trans.String()
536	str1 := new([]interface{})
537	err := json.Unmarshal([]byte(str), str1)
538	if err != nil {
539		t.Fatalf("Unable to decode %s, wrote: %s", thetype, str)
540	}
541	l := *str1
542	if len(l) < 2 {
543		t.Fatalf("Set must be at least of length two to include metadata")
544	}
545	if l[0] != "dbl" {
546		t.Fatal("Invalid type for set, expected: ", DOUBLE, ", but was: ", l[0])
547	}
548	if int(l[1].(float64)) != len(DOUBLE_VALUES) {
549		t.Fatal("Invalid length for set, expected: ", len(DOUBLE_VALUES), ", but was: ", l[1])
550	}
551	for k, value := range DOUBLE_VALUES {
552		s := l[k+2]
553		if math.IsInf(value, 1) {
554			if s.(string) != JSON_INFINITY {
555				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)
556			}
557		} else if math.IsInf(value, 0) {
558			if s.(string) != JSON_NEGATIVE_INFINITY {
559				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)
560			}
561		} else if math.IsNaN(value) {
562			if s.(string) != JSON_NAN {
563				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)
564			}
565		} else {
566			if s.(float64) != value {
567				t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s'", thetype, value, s)
568			}
569		}
570		trans.Reset()
571	}
572	trans.Close()
573}
574
575func TestWriteJSONProtocolMap(t *testing.T) {
576	thetype := "map"
577	trans := NewTMemoryBuffer()
578	p := NewTJSONProtocol(trans)
579	p.WriteMapBegin(context.Background(), TType(I32), TType(DOUBLE), len(DOUBLE_VALUES))
580	for k, value := range DOUBLE_VALUES {
581		if e := p.WriteI32(context.Background(), int32(k)); e != nil {
582			t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error())
583		}
584		if e := p.WriteDouble(context.Background(), value); e != nil {
585			t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error())
586		}
587	}
588	p.WriteMapEnd(context.Background())
589	if e := p.Flush(context.Background()); e != nil {
590		t.Fatalf("Unable to write %s due to error flushing: %s", thetype, e.Error())
591	}
592	str := trans.String()
593	if str[0] != '[' || str[len(str)-1] != ']' {
594		t.Fatalf("Bad value for %s, wrote: %v, in go: %v", thetype, str, DOUBLE_VALUES)
595	}
596	expectedKeyType, expectedValueType, expectedSize, err := p.ReadMapBegin(context.Background())
597	if err != nil {
598		t.Fatalf("Error while reading map begin: %s", err.Error())
599	}
600	if expectedKeyType != I32 {
601		t.Fatal("Expected map key type ", I32, ", but was ", expectedKeyType)
602	}
603	if expectedValueType != DOUBLE {
604		t.Fatal("Expected map value type ", DOUBLE, ", but was ", expectedValueType)
605	}
606	if expectedSize != len(DOUBLE_VALUES) {
607		t.Fatal("Expected map size of ", len(DOUBLE_VALUES), ", but was ", expectedSize)
608	}
609	for k, value := range DOUBLE_VALUES {
610		ik, err := p.ReadI32(context.Background())
611		if err != nil {
612			t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, ik, k, err.Error())
613		}
614		if int(ik) != k {
615			t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v", thetype, k, ik, k)
616		}
617		dv, err := p.ReadDouble(context.Background())
618		if err != nil {
619			t.Fatalf("Bad value for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, dv, value, err.Error())
620		}
621		s := strconv.FormatFloat(dv, 'g', 10, 64)
622		if math.IsInf(value, 1) {
623			if !math.IsInf(dv, 1) {
624				t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_INFINITY))
625			}
626		} else if math.IsInf(value, 0) {
627			if !math.IsInf(dv, 0) {
628				t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NEGATIVE_INFINITY))
629			}
630		} else if math.IsNaN(value) {
631			if !math.IsNaN(dv) {
632				t.Fatalf("Bad value for %s at index %v  %v, wrote: %v, expected: %v", thetype, k, value, s, jsonQuote(JSON_NAN))
633			}
634		} else {
635			expected := strconv.FormatFloat(value, 'g', 10, 64)
636			if s != expected {
637				t.Fatalf("Bad value for %s at index %v %v, wrote: %v, expected %v", thetype, k, value, s, expected)
638			}
639			v := float64(0)
640			if err := json.Unmarshal([]byte(s), &v); err != nil || v != value {
641				t.Fatalf("Bad json-decoded value for %s %v, wrote: '%s', expected: '%v'", thetype, value, s, v)
642			}
643		}
644	}
645	err = p.ReadMapEnd(context.Background())
646	if err != nil {
647		t.Fatalf("Error while reading map end: %s", err.Error())
648	}
649	trans.Close()
650}
651
652func TestTJSONProtocolUnmatchedBeginEnd(t *testing.T) {
653	UnmatchedBeginEndProtocolTest(t, NewTJSONProtocolFactory())
654}
655