blob: 6d1cd8cd8ce3f335450760016a1bfba1375611d8 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
Jake Farrell5d02b802014-01-07 21:42:01 -050020#define _USE_MATH_DEFINES
David Reissdb0ea152008-02-18 01:49:37 +000021#include <cmath>
Konrad Grochowskia84e1392015-10-16 11:22:10 +020022#include <iomanip>
23#include <sstream>
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/protocol/TJSONProtocol.h>
ubuntu323f0322021-05-31 19:08:05 +053025#include <locale>
cyy316723a2019-01-05 16:35:14 +080026#include <memory>
James E. King, III82ae9572017-08-05 12:23:54 -040027#include <thrift/transport/TBufferTransports.h>
David Reissdb0ea152008-02-18 01:49:37 +000028#include "gen-cpp/DebugProtoTest_types.h"
29
Claudius Heine5ef662b2015-06-24 10:03:50 +020030#define BOOST_TEST_MODULE JSONProtoTest
31#include <boost/test/unit_test.hpp>
David Reissdb0ea152008-02-18 01:49:37 +000032
Claudius Heine5ef662b2015-06-24 10:03:50 +020033using namespace thrift::test::debug;
James E. King, III82ae9572017-08-05 12:23:54 -040034using namespace apache::thrift;
Claudius Heine5ef662b2015-06-24 10:03:50 +020035using apache::thrift::transport::TMemoryBuffer;
36using apache::thrift::protocol::TJSONProtocol;
David Reissdb0ea152008-02-18 01:49:37 +000037
cyy316723a2019-01-05 16:35:14 +080038static std::shared_ptr<OneOfEach> ooe;
David Reissdb0ea152008-02-18 01:49:37 +000039
Claudius Heine5ef662b2015-06-24 10:03:50 +020040void testCaseSetup_1() {
41 ooe.reset(new OneOfEach);
42 ooe->im_true = true;
43 ooe->im_false = false;
44 ooe->a_bite = 0x7f;
45 ooe->integer16 = 27000;
46 ooe->integer32 = 1 << 24;
47 ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
48 ooe->double_precision = M_PI;
49 ooe->some_characters = "JSON THIS! \"\1";
50 ooe->zomg_unicode = "\xd7\n\a\t";
51 ooe->base64 = "\1\2\3\255";
52}
David Reissdb0ea152008-02-18 01:49:37 +000053
Claudius Heine5ef662b2015-06-24 10:03:50 +020054BOOST_AUTO_TEST_CASE(test_json_proto_1) {
55 testCaseSetup_1();
David Reissdb0ea152008-02-18 01:49:37 +000056
Claudius Heine5ef662b2015-06-24 10:03:50 +020057 const std::string expected_result(
58 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
59 "\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +080060 "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
Claudius Heine5ef662b2015-06-24 10:03:50 +020061 "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
62 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
63 "\",3,1,2,3]}}");
64
65 const std::string result(apache::thrift::ThriftJSONString(*ooe));
66
67 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
68 "Expected:\n" << expected_result << "\nGotten:\n" << result);
69}
70
cyy316723a2019-01-05 16:35:14 +080071static std::shared_ptr<Nesting> n;
Claudius Heine5ef662b2015-06-24 10:03:50 +020072
73void testCaseSetup_2() {
74 testCaseSetup_1();
75
76 n.reset(new Nesting);
77 n->my_ooe = *ooe;
78 n->my_ooe.integer16 = 16;
79 n->my_ooe.integer32 = 32;
80 n->my_ooe.integer64 = 64;
81 n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
82 n->my_ooe.some_characters = ":R (me going \"rrrr\")";
83 n->my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
84 "\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
85 "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
86 "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
87 "\xbc";
88 n->my_bonk.type = 31337;
89 n->my_bonk.message = "I am a bonk... xor!";
90}
91
92BOOST_AUTO_TEST_CASE(test_json_proto_2) {
93 testCaseSetup_2();
94
95 const std::string expected_result(
96 "{\"1\":{\"rec\":{\"1\":{\"i32\":31337},\"2\":{\"str\":\"I am a bonk... xor"
97 "!\"}}},\"2\":{\"rec\":{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127"
98 "},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},\"7\":{\"dbl\":"
Wang Yaofu0433d172016-02-15 10:43:09 +080099 "1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\")\"},\"9\":{"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200100 "\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{\"str\":\""
101 "AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
102 ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]}}}}"
103 );
104
105 const std::string result(apache::thrift::ThriftJSONString(*n));
106
107 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
108 "Expected:\n" << expected_result << "\nGotten:\n" << result);
109}
110
cyy316723a2019-01-05 16:35:14 +0800111static std::shared_ptr<HolyMoley> hm;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200112
113void testCaseSetup_3() {
114 testCaseSetup_2();
115
116 hm.reset(new HolyMoley);
117
118 hm->big.push_back(*ooe);
119 hm->big.push_back(n->my_ooe);
120 hm->big[0].a_bite = 0x22;
121 hm->big[1].a_bite = 0x33;
David Reissdb0ea152008-02-18 01:49:37 +0000122
123 std::vector<std::string> stage1;
124 stage1.push_back("and a one");
125 stage1.push_back("and a two");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200126 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000127 stage1.clear();
128 stage1.push_back("then a one, two");
129 stage1.push_back("three!");
130 stage1.push_back("FOUR!!");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200131 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000132 stage1.clear();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200133 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000134
135 std::vector<Bonk> stage2;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200136 hm->bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000138 stage2.back().type = 1;
139 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100140 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000141 stage2.back().type = 2;
142 stage2.back().message = "What?";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200143 hm->bonks["something"] = stage2;
David Reissdb0ea152008-02-18 01:49:37 +0000144 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100145 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000146 stage2.back().type = 3;
147 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100148 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000149 stage2.back().type = 4;
150 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100151 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000152 stage2.back().type = 5;
153 stage2.back().message = "nevermore";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200154 hm->bonks["poe"] = stage2;
155}
David Reissdb0ea152008-02-18 01:49:37 +0000156
Claudius Heine5ef662b2015-06-24 10:03:50 +0200157BOOST_AUTO_TEST_CASE(test_json_proto_3) {
158 testCaseSetup_3();
159
160 const std::string expected_result(
161 "{\"1\":{\"lst\":[\"rec\",2,{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":"
162 "34},\"4\":{\"i16\":27000},\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000"
Wang Yaofu0433d172016-02-15 10:43:09 +0800163 "},\"7\":{\"dbl\":3.1415926535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200164 "\"},\"9\":{\"str\":\"\xd7\\n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":"
165 "\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
166 ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]}},{\"1\":{\"tf\":1},\"2\":{\"tf\":0},"
167 "\"3\":{\"i8\":51},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},"
Wang Yaofu0433d172016-02-15 10:43:09 +0800168 "\"7\":{\"dbl\":1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\""
Claudius Heine5ef662b2015-06-24 10:03:50 +0200169 ")\"},\"9\":{\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{"
170 "\"str\":\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16"
171 "\",3,1,2,3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]}}]},\"2\":{\"set\":[\"lst\",3"
172 ",[\"str\",0],[\"str\",2,\"and a one\",\"and a two\"],[\"str\",3,\"then a one"
173 ", two\",\"three!\",\"FOUR!!\"]]},\"3\":{\"map\":[\"str\",\"lst\",3,{\"nothin"
174 "g\":[\"rec\",0],\"poe\":[\"rec\",3,{\"1\":{\"i32\":3},\"2\":{\"str\":\"quoth"
175 "\"}},{\"1\":{\"i32\":4},\"2\":{\"str\":\"the raven\"}},{\"1\":{\"i32\":5},\""
176 "2\":{\"str\":\"nevermore\"}}],\"something\":[\"rec\",2,{\"1\":{\"i32\":1},\""
177 "2\":{\"str\":\"Wait.\"}},{\"1\":{\"i32\":2},\"2\":{\"str\":\"What?\"}}]}]}}"
178 );
179
180 const std::string result(apache::thrift::ThriftJSONString(*hm));
181
182 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
183 "Expected:\n" << expected_result << "\nGotten:\n" << result);
184}
185
186BOOST_AUTO_TEST_CASE(test_json_proto_4) {
187 testCaseSetup_1();
David Reissdb0ea152008-02-18 01:49:37 +0000188
cyy316723a2019-01-05 16:35:14 +0800189 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
190 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
David Reissdb0ea152008-02-18 01:49:37 +0000191
Claudius Heine5ef662b2015-06-24 10:03:50 +0200192 ooe->write(proto.get());
David Reissdb0ea152008-02-18 01:49:37 +0000193 OneOfEach ooe2;
194 ooe2.read(proto.get());
195
Claudius Heine5ef662b2015-06-24 10:03:50 +0200196 BOOST_CHECK(*ooe == ooe2);
197}
David Reissdb0ea152008-02-18 01:49:37 +0000198
Claudius Heine5ef662b2015-06-24 10:03:50 +0200199BOOST_AUTO_TEST_CASE(test_json_proto_5) {
200 testCaseSetup_3();
David Reissdb0ea152008-02-18 01:49:37 +0000201
cyy316723a2019-01-05 16:35:14 +0800202 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
203 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Claudius Heine5ef662b2015-06-24 10:03:50 +0200204
205 hm->write(proto.get());
David Reissdb0ea152008-02-18 01:49:37 +0000206 HolyMoley hm2;
207 hm2.read(proto.get());
208
Claudius Heine5ef662b2015-06-24 10:03:50 +0200209 BOOST_CHECK(*hm == hm2);
David Reissdb0ea152008-02-18 01:49:37 +0000210
Christian Lavoiecbf87cb2010-11-28 14:34:26 +0000211 hm2.big[0].a_bite = 0x00;
David Reiss4a8f46c2008-03-07 20:12:07 +0000212
Claudius Heine5ef662b2015-06-24 10:03:50 +0200213 BOOST_CHECK(*hm != hm2);
214}
David Reiss4a8f46c2008-03-07 20:12:07 +0000215
Claudius Heine5ef662b2015-06-24 10:03:50 +0200216BOOST_AUTO_TEST_CASE(test_json_proto_6) {
David Reissdb0ea152008-02-18 01:49:37 +0000217 Doubles dub;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100218 dub.nan = HUGE_VAL / HUGE_VAL;
David Reissdb0ea152008-02-18 01:49:37 +0000219 dub.inf = HUGE_VAL;
220 dub.neginf = -HUGE_VAL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100221 dub.repeating = 10.0 / 3.0;
David Reissdb0ea152008-02-18 01:49:37 +0000222 dub.big = 1E+305;
Roger Meier22888ce2014-02-09 11:31:02 +0100223 dub.tiny = 1E-305;
David Reissdb0ea152008-02-18 01:49:37 +0000224 dub.zero = 0.0;
225 dub.negzero = -0.0;
David Reissdb0ea152008-02-18 01:49:37 +0000226
Claudius Heine5ef662b2015-06-24 10:03:50 +0200227 const std::string expected_result(
228 "{\"1\":{\"dbl\":\"NaN\"},\"2\":{\"dbl\":\"Infinity\"},\"3\":{\"dbl\":\"-Infi"
Wang Yaofu0433d172016-02-15 10:43:09 +0800229 "nity\"},\"4\":{\"dbl\":3.3333333333333335},\"5\":{\"dbl\":9.9999999999999994e+"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200230 "304},\"6\":{\"dbl\":1e-305},\"7\":{\"dbl\":0},\"8\":{\"dbl\":-0}}"
231 );
232
zeshuai00786396c62020-03-31 14:38:20 +0800233 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
234 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
235 dub.write(proto.get());
236 Doubles dub_1;
237 dub_1.read(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200238
zeshuai00786396c62020-03-31 14:38:20 +0800239 const std::string result(apache::thrift::ThriftJSONString(dub));
240 const std::string result_1(apache::thrift::ThriftJSONString(dub_1));
241
Claudius Heine5ef662b2015-06-24 10:03:50 +0200242 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
243 "Expected:\n" << expected_result << "\nGotten:\n" << result);
zeshuai00786396c62020-03-31 14:38:20 +0800244 BOOST_CHECK_MESSAGE(!expected_result.compare(result_1),
245 "Expected:\n" << expected_result << "\nGotten:\n" << result_1);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200246}
247
248BOOST_AUTO_TEST_CASE(test_json_proto_7) {
cyy316723a2019-01-05 16:35:14 +0800249 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
250 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
David Reiss4a8f46c2008-03-07 20:12:07 +0000251
252 Base64 base;
253 base.a = 123;
254 base.b1 = "1";
255 base.b2 = "12";
256 base.b3 = "123";
257 base.b4 = "1234";
258 base.b5 = "12345";
259 base.b6 = "123456";
260
261 base.write(proto.get());
262 Base64 base2;
263 base2.read(proto.get());
264
Claudius Heine5ef662b2015-06-24 10:03:50 +0200265 BOOST_CHECK(base == base2);
David Reissdb0ea152008-02-18 01:49:37 +0000266}
Claudius Heine8f11f522015-07-01 10:35:38 +0200267
268BOOST_AUTO_TEST_CASE(test_json_proto_8) {
269 const char* json_string =
270 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
271 "\"5\":{\"i32\":16.77216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800272 "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
Claudius Heine8f11f522015-07-01 10:35:38 +0200273 "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
274 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
275 "\",3,1,2,3]}}";
276
James E. King, III7edc8fa2017-01-20 10:11:41 -0500277 const std::size_t bufSiz = strlen(json_string) * sizeof(char);
cyy316723a2019-01-05 16:35:14 +0800278 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
James E. King, III7edc8fa2017-01-20 10:11:41 -0500279 (uint8_t*)(json_string), static_cast<uint32_t>(bufSiz)));
cyy316723a2019-01-05 16:35:14 +0800280 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Claudius Heine8f11f522015-07-01 10:35:38 +0200281
282 OneOfEach ooe2;
283
284 BOOST_CHECK_THROW(ooe2.read(proto.get()),
285 apache::thrift::protocol::TProtocolException);
286}
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200287
288static std::string toHexSequence(const std::string& str) {
289 std::stringstream ss;
290 ss << std::hex << std::setfill('0');
291 for (std::size_t i = 0; i < str.size(); i++) {
292 ss << "\\x" << int(uint8_t(str[i]));
293 }
294 return ss.str();
295}
296
297BOOST_AUTO_TEST_CASE(test_json_unicode_escaped) {
298 const char json_string[] =
299 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
300 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800301 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\u0e01 \\ud835\\udd3e\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200302 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
303 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
304 "\",3,1,2,3]}}";
305 const char* expected_zomg_unicode = "\xe0\xb8\x81 \xf0\x9d\x94\xbe";
306
cyy316723a2019-01-05 16:35:14 +0800307 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200308 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800309 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200310
311 OneOfEach ooe2;
312 ooe2.read(proto.get());
313 BOOST_CHECK_MESSAGE(!ooe2.zomg_unicode.compare(expected_zomg_unicode),
314 "Expected:\n" << toHexSequence(expected_zomg_unicode) << "\nGotten:\n"
315 << toHexSequence(ooe2.zomg_unicode));
316
317}
318
319BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_low_surrogate) {
320 const char json_string[] =
321 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
322 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800323 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\ud835\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200324 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
325 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
326 "\",3,1,2,3]}}";
327
cyy316723a2019-01-05 16:35:14 +0800328 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200329 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800330 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200331
332 OneOfEach ooe2;
333 BOOST_CHECK_THROW(ooe2.read(proto.get()),
334 apache::thrift::protocol::TProtocolException);
335}
336
337BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_hi_surrogate) {
338 const char json_string[] =
339 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
340 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800341 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\udd3e\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200342 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
343 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
344 "\",3,1,2,3]}}";
345
cyy316723a2019-01-05 16:35:14 +0800346 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200347 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800348 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200349
350 OneOfEach ooe2;
351 BOOST_CHECK_THROW(ooe2.read(proto.get()),
352 apache::thrift::protocol::TProtocolException);
353}
ubuntu323f0322021-05-31 19:08:05 +0530354
355BOOST_AUTO_TEST_CASE(test_json_locale_with_thousands_separator_comma) {
356 testCaseSetup_1();
357
358 const std::string expected_result(
359 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
360 "\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
361 "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
362 "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
363 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
364 "\",3,1,2,3]}}");
365
366#if _WIN32
367 std::locale::global(std::locale("en-US.UTF-8"));
368#else
369 std::locale::global(std::locale("en_US.UTF-8"));
370#endif
371
372 const std::string result(apache::thrift::ThriftJSONString(*ooe));
373
374 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
375 "Expected:\n" << expected_result << "\nGotten:\n" << result);
376}