blob: 1680532cfbd32ee10812ff9f29086d87016f2d38 [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001/*
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 (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -070037 if e := p.WriteBool(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020038 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
39 }
John Boiles57852792018-01-05 14:37:05 -080040 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020041 t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error())
42 }
43 s := trans.String()
Jens Geyerbf3a19d2013-12-17 21:39:30 +010044 expected := ""
45 if value {
46 expected = "1"
47 } else {
48 expected = "0"
Jens Geyer0e87c462013-06-18 22:25:07 +020049 }
Jens Geyerbf3a19d2013-12-17 21:39:30 +010050 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 {
Jens Geyer0e87c462013-06-18 22:25:07 +020055 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 {
Jens Geyerbf3a19d2013-12-17 21:39:30 +010068 trans.Write([]byte{'1'}) // not JSON_TRUE
Jens Geyer0e87c462013-06-18 22:25:07 +020069 } else {
Jens Geyerbf3a19d2013-12-17 21:39:30 +010070 trans.Write([]byte{'0'}) // not JSON_FALSE
Jens Geyer0e87c462013-06-18 22:25:07 +020071 }
John Boiles57852792018-01-05 14:37:05 -080072 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +020073 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -070074 v, e := p.ReadBool(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +020075 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 }
Jens Geyerbf3a19d2013-12-17 21:39:30 +010081 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)
Jens Geyer0e87c462013-06-18 22:25:07 +020084 }
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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -070095 if e := p.WriteByte(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020096 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
97 }
John Boiles57852792018-01-05 14:37:05 -080098 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +020099 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 }
Jens Geyer5bc8b5a2015-09-05 12:50:24 +0200105 v := int8(0)
Jens Geyer0e87c462013-06-18 22:25:07 +0200106 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)))
John Boiles57852792018-01-05 14:37:05 -0800120 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200121 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700122 v, e := p.ReadByte(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200123 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700142 if e := p.WriteI16(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200143 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
144 }
John Boiles57852792018-01-05 14:37:05 -0800145 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200146 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)))
John Boiles57852792018-01-05 14:37:05 -0800167 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200168 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700169 v, e := p.ReadI16(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200170 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700189 if e := p.WriteI32(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200190 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
191 }
John Boiles57852792018-01-05 14:37:05 -0800192 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200193 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)))
John Boiles57852792018-01-05 14:37:05 -0800214 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200215 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700216 v, e := p.ReadI32(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200217 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700236 if e := p.WriteI64(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200237 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
238 }
John Boiles57852792018-01-05 14:37:05 -0800239 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200240 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))
John Boiles57852792018-01-05 14:37:05 -0800261 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200262 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700263 v, e := p.ReadI64(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200264 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700283 if e := p.WriteDouble(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200284 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
285 }
John Boiles57852792018-01-05 14:37:05 -0800286 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200287 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())
John Boiles57852792018-01-05 14:37:05 -0800323 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200324 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700325 v, e := p.ReadDouble(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200326 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700359 if e := p.WriteString(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200360 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
361 }
John Boiles57852792018-01-05 14:37:05 -0800362 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200363 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))
John Boiles57852792018-01-05 14:37:05 -0800384 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200385 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700386 v, e := p.ReadString(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200387 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)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700410 if e := p.WriteBinary(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200411 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
412 }
John Boiles57852792018-01-05 14:37:05 -0800413 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200414 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 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700421 v1, err := p.ReadBinary(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200422 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))
John Boiles57852792018-01-05 14:37:05 -0800445 trans.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200446 s := trans.String()
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700447 v, e := p.ReadBinary(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200448 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 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700454 for i := range v {
Jens Geyer0e87c462013-06-18 22:25:07 +0200455 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)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700471 p.WriteListBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES))
Jens Geyer0e87c462013-06-18 22:25:07 +0200472 for _, value := range DOUBLE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700473 if e := p.WriteDouble(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200474 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
475 }
476 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700477 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800478 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200479 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)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700525 p.WriteSetBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES))
Jens Geyer0e87c462013-06-18 22:25:07 +0200526 for _, value := range DOUBLE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700527 if e := p.WriteDouble(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200528 t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error())
529 }
530 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700531 p.WriteSetEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800532 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200533 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)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700579 p.WriteMapBegin(context.Background(), TType(I32), TType(DOUBLE), len(DOUBLE_VALUES))
Jens Geyer0e87c462013-06-18 22:25:07 +0200580 for k, value := range DOUBLE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700581 if e := p.WriteI32(context.Background(), int32(k)); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200582 t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error())
583 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700584 if e := p.WriteDouble(context.Background(), value); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200585 t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error())
586 }
587 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700588 p.WriteMapEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800589 if e := p.Flush(context.Background()); e != nil {
Jens Geyer0e87c462013-06-18 22:25:07 +0200590 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] != ']' {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100594 t.Fatalf("Bad value for %s, wrote: %v, in go: %v", thetype, str, DOUBLE_VALUES)
Jens Geyer0e87c462013-06-18 22:25:07 +0200595 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700596 expectedKeyType, expectedValueType, expectedSize, err := p.ReadMapBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200597 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 {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700610 ik, err := p.ReadI32(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200611 if err != nil {
Yuxuan 'fishy' Wang22b6c0c2020-08-28 17:20:31 -0700612 t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, ik, k, err.Error())
Jens Geyer0e87c462013-06-18 22:25:07 +0200613 }
614 if int(ik) != k {
615 t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v", thetype, k, ik, k)
616 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700617 dv, err := p.ReadDouble(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200618 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 }
Jens Geyer3bb34df2014-12-17 23:06:45 +0100644 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700645 err = p.ReadMapEnd(context.Background())
Jens Geyer3bb34df2014-12-17 23:06:45 +0100646 if err != nil {
647 t.Fatalf("Error while reading map end: %s", err.Error())
Jens Geyer0e87c462013-06-18 22:25:07 +0200648 }
649 trans.Close()
650}
Yuxuan 'fishy' Wang64c2a4b2020-10-10 18:39:32 -0700651
652func TestTJSONProtocolUnmatchedBeginEnd(t *testing.T) {
653 UnmatchedBeginEndProtocolTest(t, NewTJSONProtocolFactory())
654}