| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 ( |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 23 | "context" |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 24 | "encoding/base64" |
| 25 | "encoding/json" |
| 26 | "fmt" |
| 27 | "math" |
| 28 | "strconv" |
| 29 | "testing" |
| 30 | ) |
| 31 | |
| 32 | func TestWriteJSONProtocolBool(t *testing.T) { |
| 33 | thetype := "boolean" |
| 34 | trans := NewTMemoryBuffer() |
| 35 | p := NewTJSONProtocol(trans) |
| 36 | for _, value := range BOOL_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 37 | if e := p.WriteBool(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 38 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 39 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 40 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 41 | t.Fatalf("Unable to write %s value %v due to error flushing: %s", thetype, value, e.Error()) |
| 42 | } |
| 43 | s := trans.String() |
| Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 44 | expected := "" |
| 45 | if value { |
| 46 | expected = "1" |
| 47 | } else { |
| 48 | expected = "0" |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 49 | } |
| Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 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 { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 62 | func TestReadJSONProtocolBool(t *testing.T) { |
| 63 | thetype := "boolean" |
| 64 | for _, value := range BOOL_VALUES { |
| 65 | trans := NewTMemoryBuffer() |
| 66 | p := NewTJSONProtocol(trans) |
| 67 | if value { |
| Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 68 | trans.Write([]byte{'1'}) // not JSON_TRUE |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 69 | } else { |
| Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 70 | trans.Write([]byte{'0'}) // not JSON_FALSE |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 71 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 72 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 73 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 74 | v, e := p.ReadBool(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | } |
| Jens Geyer | bf3a19d | 2013-12-17 21:39:30 +0100 | [diff] [blame] | 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) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 84 | } |
| 85 | trans.Reset() |
| 86 | trans.Close() |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestWriteJSONProtocolByte(t *testing.T) { |
| 91 | thetype := "byte" |
| 92 | trans := NewTMemoryBuffer() |
| 93 | p := NewTJSONProtocol(trans) |
| 94 | for _, value := range BYTE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 95 | if e := p.WriteByte(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 96 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 97 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 98 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | } |
| Jens Geyer | 5bc8b5a | 2015-09-05 12:50:24 +0200 | [diff] [blame] | 105 | v := int8(0) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 114 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 120 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 121 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 122 | v, e := p.ReadByte(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 137 | func TestWriteJSONProtocolI16(t *testing.T) { |
| 138 | thetype := "int16" |
| 139 | trans := NewTMemoryBuffer() |
| 140 | p := NewTJSONProtocol(trans) |
| 141 | for _, value := range INT16_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 142 | if e := p.WriteI16(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 143 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 144 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 145 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 161 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 167 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 168 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 169 | v, e := p.ReadI16(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 184 | func TestWriteJSONProtocolI32(t *testing.T) { |
| 185 | thetype := "int32" |
| 186 | trans := NewTMemoryBuffer() |
| 187 | p := NewTJSONProtocol(trans) |
| 188 | for _, value := range INT32_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 189 | if e := p.WriteI32(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 190 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 191 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 192 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 208 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 214 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 215 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 216 | v, e := p.ReadI32(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 231 | func TestWriteJSONProtocolI64(t *testing.T) { |
| 232 | thetype := "int64" |
| 233 | trans := NewTMemoryBuffer() |
| 234 | p := NewTJSONProtocol(trans) |
| 235 | for _, value := range INT64_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 236 | if e := p.WriteI64(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 237 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 238 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 239 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 255 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 261 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 262 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 263 | v, e := p.ReadI64(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 278 | func TestWriteJSONProtocolDouble(t *testing.T) { |
| 279 | thetype := "double" |
| 280 | trans := NewTMemoryBuffer() |
| 281 | p := NewTJSONProtocol(trans) |
| 282 | for _, value := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 283 | if e := p.WriteDouble(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 284 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 285 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 286 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 316 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 323 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 324 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 325 | v, e := p.ReadDouble(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 354 | func TestWriteJSONProtocolString(t *testing.T) { |
| 355 | thetype := "string" |
| 356 | trans := NewTMemoryBuffer() |
| 357 | p := NewTJSONProtocol(trans) |
| 358 | for _, value := range STRING_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 359 | if e := p.WriteString(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 360 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 361 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 362 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 378 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 384 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 385 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 386 | v, e := p.ReadString(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 402 | func 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' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 410 | if e := p.WriteBinary(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 411 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 412 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 413 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 421 | v1, err := p.ReadBinary(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 436 | func 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 Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 445 | trans.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 446 | s := trans.String() |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 447 | v, e := p.ReadBinary(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | } |
| Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 454 | for i := range v { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 467 | func TestWriteJSONProtocolList(t *testing.T) { |
| 468 | thetype := "list" |
| 469 | trans := NewTMemoryBuffer() |
| 470 | p := NewTJSONProtocol(trans) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 471 | p.WriteListBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES)) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 472 | for _, value := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 473 | if e := p.WriteDouble(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 474 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 475 | } |
| 476 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 477 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 478 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 521 | func TestWriteJSONProtocolSet(t *testing.T) { |
| 522 | thetype := "set" |
| 523 | trans := NewTMemoryBuffer() |
| 524 | p := NewTJSONProtocol(trans) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 525 | p.WriteSetBegin(context.Background(), TType(DOUBLE), len(DOUBLE_VALUES)) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 526 | for _, value := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 527 | if e := p.WriteDouble(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 528 | t.Fatalf("Unable to write %s value %v due to error: %s", thetype, value, e.Error()) |
| 529 | } |
| 530 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 531 | p.WriteSetEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 532 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | |
| 575 | func TestWriteJSONProtocolMap(t *testing.T) { |
| 576 | thetype := "map" |
| 577 | trans := NewTMemoryBuffer() |
| 578 | p := NewTJSONProtocol(trans) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 579 | p.WriteMapBegin(context.Background(), TType(I32), TType(DOUBLE), len(DOUBLE_VALUES)) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 580 | for k, value := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 581 | if e := p.WriteI32(context.Background(), int32(k)); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 582 | t.Fatalf("Unable to write %s key int32 value %v due to error: %s", thetype, k, e.Error()) |
| 583 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 584 | if e := p.WriteDouble(context.Background(), value); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 585 | t.Fatalf("Unable to write %s value float64 value %v due to error: %s", thetype, value, e.Error()) |
| 586 | } |
| 587 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 588 | p.WriteMapEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 589 | if e := p.Flush(context.Background()); e != nil { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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] != ']' { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 594 | t.Fatalf("Bad value for %s, wrote: %v, in go: %v", thetype, str, DOUBLE_VALUES) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 595 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 596 | expectedKeyType, expectedValueType, expectedSize, err := p.ReadMapBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 610 | ik, err := p.ReadI32(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 611 | if err != nil { |
| Yuxuan 'fishy' Wang | 22b6c0c | 2020-08-28 17:20:31 -0700 | [diff] [blame] | 612 | t.Fatalf("Bad key for %s index %v, wrote: %v, expected: %v, error: %s", thetype, k, ik, k, err.Error()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 613 | } |
| 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' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 617 | dv, err := p.ReadDouble(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 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 | } |
| Jens Geyer | 3bb34df | 2014-12-17 23:06:45 +0100 | [diff] [blame] | 644 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 645 | err = p.ReadMapEnd(context.Background()) |
| Jens Geyer | 3bb34df | 2014-12-17 23:06:45 +0100 | [diff] [blame] | 646 | if err != nil { |
| 647 | t.Fatalf("Error while reading map end: %s", err.Error()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 648 | } |
| 649 | trans.Close() |
| 650 | } |
| Yuxuan 'fishy' Wang | 64c2a4b | 2020-10-10 18:39:32 -0700 | [diff] [blame] | 651 | |
| 652 | func TestTJSONProtocolUnmatchedBeginEnd(t *testing.T) { |
| 653 | UnmatchedBeginEndProtocolTest(t, NewTJSONProtocolFactory()) |
| 654 | } |