blob: 1874ccdca3e82729cab5779662e5acf4b7014bd4 [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>
cyy316723a2019-01-05 16:35:14 +080025#include <memory>
James E. King, III82ae9572017-08-05 12:23:54 -040026#include <thrift/transport/TBufferTransports.h>
David Reissdb0ea152008-02-18 01:49:37 +000027#include "gen-cpp/DebugProtoTest_types.h"
28
Claudius Heine5ef662b2015-06-24 10:03:50 +020029#define BOOST_TEST_MODULE JSONProtoTest
30#include <boost/test/unit_test.hpp>
David Reissdb0ea152008-02-18 01:49:37 +000031
Claudius Heine5ef662b2015-06-24 10:03:50 +020032using namespace thrift::test::debug;
James E. King, III82ae9572017-08-05 12:23:54 -040033using namespace apache::thrift;
Claudius Heine5ef662b2015-06-24 10:03:50 +020034using apache::thrift::transport::TMemoryBuffer;
35using apache::thrift::protocol::TJSONProtocol;
David Reissdb0ea152008-02-18 01:49:37 +000036
cyy316723a2019-01-05 16:35:14 +080037static std::shared_ptr<OneOfEach> ooe;
David Reissdb0ea152008-02-18 01:49:37 +000038
Claudius Heine5ef662b2015-06-24 10:03:50 +020039void testCaseSetup_1() {
40 ooe.reset(new OneOfEach);
41 ooe->im_true = true;
42 ooe->im_false = false;
43 ooe->a_bite = 0x7f;
44 ooe->integer16 = 27000;
45 ooe->integer32 = 1 << 24;
46 ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
47 ooe->double_precision = M_PI;
48 ooe->some_characters = "JSON THIS! \"\1";
49 ooe->zomg_unicode = "\xd7\n\a\t";
50 ooe->base64 = "\1\2\3\255";
CJCombrink4b909092024-04-27 19:51:39 +020051 ooe->rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
Claudius Heine5ef662b2015-06-24 10:03:50 +020052}
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"
Carel Combrink4035ecc2025-05-14 08:39:55 +020063 "\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000-0000-000000000000\"},\"16\""
64 ":{\"lst\":[\"uid\",0]}}");
Claudius Heine5ef662b2015-06-24 10:03:50 +020065
66 const std::string result(apache::thrift::ThriftJSONString(*ooe));
67
68 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
69 "Expected:\n" << expected_result << "\nGotten:\n" << result);
70}
71
cyy316723a2019-01-05 16:35:14 +080072static std::shared_ptr<Nesting> n;
Claudius Heine5ef662b2015-06-24 10:03:50 +020073
74void testCaseSetup_2() {
75 testCaseSetup_1();
76
77 n.reset(new Nesting);
78 n->my_ooe = *ooe;
79 n->my_ooe.integer16 = 16;
80 n->my_ooe.integer32 = 32;
81 n->my_ooe.integer64 = 64;
82 n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
83 n->my_ooe.some_characters = ":R (me going \"rrrr\")";
84 n->my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
85 "\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
86 "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
87 "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
88 "\xbc";
CJCombrink4b909092024-04-27 19:51:39 +020089 n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
Carel Combrink4035ecc2025-05-14 08:39:55 +020090 std::vector<apache::thrift::TUuid> uuiid_list;
91 uuiid_list.push_back(apache::thrift::TUuid{"{fa1af5ec-fdc2-4355-844a-9f0dbfd00e50}"});
92 uuiid_list.push_back(apache::thrift::TUuid{"{1beece83-34f4-4fa3-b757-1ad1ac157fe3}"});
93 n->my_ooe.rfc4122_uuid_list = uuiid_list;
Claudius Heine5ef662b2015-06-24 10:03:50 +020094 n->my_bonk.type = 31337;
95 n->my_bonk.message = "I am a bonk... xor!";
96}
97
98BOOST_AUTO_TEST_CASE(test_json_proto_2) {
99 testCaseSetup_2();
100
101 const std::string expected_result(
102 "{\"1\":{\"rec\":{\"1\":{\"i32\":31337},\"2\":{\"str\":\"I am a bonk... xor"
103 "!\"}}},\"2\":{\"rec\":{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127"
104 "},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},\"7\":{\"dbl\":"
Wang Yaofu0433d172016-02-15 10:43:09 +0800105 "1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\")\"},\"9\":{"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200106 "\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{\"str\":\""
107 "AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
CJCombrink1d886ca2024-03-23 21:32:28 +0100108 ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-1726-"
Carel Combrink4035ecc2025-05-14 08:39:55 +0200109 "4e75-a04f-1ed9a6a89c4c\"},\"16\":{\"lst\":[\"uid\",2,\"fa1af5ec-fdc2-4355-"
110 "844a-9f0dbfd00e50\",\"1beece83-34f4-4fa3-b757-1ad1ac157fe3\"]}}}}"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200111 );
112
113 const std::string result(apache::thrift::ThriftJSONString(*n));
114
115 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
116 "Expected:\n" << expected_result << "\nGotten:\n" << result);
117}
118
cyy316723a2019-01-05 16:35:14 +0800119static std::shared_ptr<HolyMoley> hm;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200120
121void testCaseSetup_3() {
122 testCaseSetup_2();
123
124 hm.reset(new HolyMoley);
125
126 hm->big.push_back(*ooe);
127 hm->big.push_back(n->my_ooe);
128 hm->big[0].a_bite = 0x22;
129 hm->big[1].a_bite = 0x33;
David Reissdb0ea152008-02-18 01:49:37 +0000130
131 std::vector<std::string> stage1;
132 stage1.push_back("and a one");
133 stage1.push_back("and a two");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200134 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000135 stage1.clear();
136 stage1.push_back("then a one, two");
137 stage1.push_back("three!");
138 stage1.push_back("FOUR!!");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200139 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000140 stage1.clear();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200141 hm->contain.insert(stage1);
David Reissdb0ea152008-02-18 01:49:37 +0000142
143 std::vector<Bonk> stage2;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200144 hm->bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100145 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000146 stage2.back().type = 1;
147 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100148 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000149 stage2.back().type = 2;
150 stage2.back().message = "What?";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200151 hm->bonks["something"] = stage2;
David Reissdb0ea152008-02-18 01:49:37 +0000152 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000154 stage2.back().type = 3;
155 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100156 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000157 stage2.back().type = 4;
158 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100159 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +0000160 stage2.back().type = 5;
161 stage2.back().message = "nevermore";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200162 hm->bonks["poe"] = stage2;
163}
David Reissdb0ea152008-02-18 01:49:37 +0000164
Claudius Heine5ef662b2015-06-24 10:03:50 +0200165BOOST_AUTO_TEST_CASE(test_json_proto_3) {
166 testCaseSetup_3();
167
168 const std::string expected_result(
169 "{\"1\":{\"lst\":[\"rec\",2,{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":"
170 "34},\"4\":{\"i16\":27000},\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000"
Wang Yaofu0433d172016-02-15 10:43:09 +0800171 "},\"7\":{\"dbl\":3.1415926535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200172 "\"},\"9\":{\"str\":\"\xd7\\n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":"
173 "\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
CJCombrink1d886ca2024-03-23 21:32:28 +0100174 ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000"
Carel Combrink4035ecc2025-05-14 08:39:55 +0200175 "-0000-000000000000\"},\"16\":{\"lst\":[\"uid\",0]}},{\"1\":{\"tf\":1},\"2\":{\"tf\":0},"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200176 "\"3\":{\"i8\":51},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},"
Wang Yaofu0433d172016-02-15 10:43:09 +0800177 "\"7\":{\"dbl\":1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\""
Claudius Heine5ef662b2015-06-24 10:03:50 +0200178 ")\"},\"9\":{\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{"
179 "\"str\":\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16"
CJCombrink1d886ca2024-03-23 21:32:28 +0100180 "\",3,1,2,3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-"
Carel Combrink4035ecc2025-05-14 08:39:55 +0200181 "1726-4e75-a04f-1ed9a6a89c4c\"},\"16\":{\"lst\":[\"uid\",2,\"fa1af5ec-fdc2-4355-"
182 "844a-9f0dbfd00e50\",\"1beece83-34f4-4fa3-b757-1ad1ac157fe3\"]}}]},\"2\":{\"set\":[\"lst\",3"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200183 ",[\"str\",0],[\"str\",2,\"and a one\",\"and a two\"],[\"str\",3,\"then a one"
184 ", two\",\"three!\",\"FOUR!!\"]]},\"3\":{\"map\":[\"str\",\"lst\",3,{\"nothin"
185 "g\":[\"rec\",0],\"poe\":[\"rec\",3,{\"1\":{\"i32\":3},\"2\":{\"str\":\"quoth"
186 "\"}},{\"1\":{\"i32\":4},\"2\":{\"str\":\"the raven\"}},{\"1\":{\"i32\":5},\""
187 "2\":{\"str\":\"nevermore\"}}],\"something\":[\"rec\",2,{\"1\":{\"i32\":1},\""
188 "2\":{\"str\":\"Wait.\"}},{\"1\":{\"i32\":2},\"2\":{\"str\":\"What?\"}}]}]}}"
189 );
190
191 const std::string result(apache::thrift::ThriftJSONString(*hm));
192
193 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
194 "Expected:\n" << expected_result << "\nGotten:\n" << result);
195}
196
197BOOST_AUTO_TEST_CASE(test_json_proto_4) {
198 testCaseSetup_1();
David Reissdb0ea152008-02-18 01:49:37 +0000199
cyy316723a2019-01-05 16:35:14 +0800200 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
201 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
David Reissdb0ea152008-02-18 01:49:37 +0000202
Claudius Heine5ef662b2015-06-24 10:03:50 +0200203 ooe->write(proto.get());
David Reissdb0ea152008-02-18 01:49:37 +0000204 OneOfEach ooe2;
205 ooe2.read(proto.get());
206
CJCombrink1d886ca2024-03-23 21:32:28 +0100207 BOOST_TEST_INFO("written: " << *ooe);
208 BOOST_TEST_INFO("read : " << ooe2);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200209 BOOST_CHECK(*ooe == ooe2);
210}
David Reissdb0ea152008-02-18 01:49:37 +0000211
Claudius Heine5ef662b2015-06-24 10:03:50 +0200212BOOST_AUTO_TEST_CASE(test_json_proto_5) {
213 testCaseSetup_3();
David Reissdb0ea152008-02-18 01:49:37 +0000214
cyy316723a2019-01-05 16:35:14 +0800215 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
216 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Claudius Heine5ef662b2015-06-24 10:03:50 +0200217
218 hm->write(proto.get());
David Reissdb0ea152008-02-18 01:49:37 +0000219 HolyMoley hm2;
220 hm2.read(proto.get());
221
CJCombrink1d886ca2024-03-23 21:32:28 +0100222 BOOST_TEST_INFO("written: " << *hm);
223 BOOST_TEST_INFO("read : " << hm2);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200224 BOOST_CHECK(*hm == hm2);
David Reissdb0ea152008-02-18 01:49:37 +0000225
Christian Lavoiecbf87cb2010-11-28 14:34:26 +0000226 hm2.big[0].a_bite = 0x00;
David Reiss4a8f46c2008-03-07 20:12:07 +0000227
Claudius Heine5ef662b2015-06-24 10:03:50 +0200228 BOOST_CHECK(*hm != hm2);
229}
David Reiss4a8f46c2008-03-07 20:12:07 +0000230
Claudius Heine5ef662b2015-06-24 10:03:50 +0200231BOOST_AUTO_TEST_CASE(test_json_proto_6) {
David Reissdb0ea152008-02-18 01:49:37 +0000232 Doubles dub;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100233 dub.nan = HUGE_VAL / HUGE_VAL;
David Reissdb0ea152008-02-18 01:49:37 +0000234 dub.inf = HUGE_VAL;
235 dub.neginf = -HUGE_VAL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100236 dub.repeating = 10.0 / 3.0;
David Reissdb0ea152008-02-18 01:49:37 +0000237 dub.big = 1E+305;
Roger Meier22888ce2014-02-09 11:31:02 +0100238 dub.tiny = 1E-305;
David Reissdb0ea152008-02-18 01:49:37 +0000239 dub.zero = 0.0;
240 dub.negzero = -0.0;
David Reissdb0ea152008-02-18 01:49:37 +0000241
Claudius Heine5ef662b2015-06-24 10:03:50 +0200242 const std::string expected_result(
243 "{\"1\":{\"dbl\":\"NaN\"},\"2\":{\"dbl\":\"Infinity\"},\"3\":{\"dbl\":\"-Infi"
Wang Yaofu0433d172016-02-15 10:43:09 +0800244 "nity\"},\"4\":{\"dbl\":3.3333333333333335},\"5\":{\"dbl\":9.9999999999999994e+"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200245 "304},\"6\":{\"dbl\":1e-305},\"7\":{\"dbl\":0},\"8\":{\"dbl\":-0}}"
246 );
247
zeshuai00786396c62020-03-31 14:38:20 +0800248 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
CJCombrink1d886ca2024-03-23 21:32:28 +0100249 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
zeshuai00786396c62020-03-31 14:38:20 +0800250 dub.write(proto.get());
251 Doubles dub_1;
252 dub_1.read(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200253
zeshuai00786396c62020-03-31 14:38:20 +0800254 const std::string result(apache::thrift::ThriftJSONString(dub));
255 const std::string result_1(apache::thrift::ThriftJSONString(dub_1));
CJCombrink1d886ca2024-03-23 21:32:28 +0100256
Claudius Heine5ef662b2015-06-24 10:03:50 +0200257 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
258 "Expected:\n" << expected_result << "\nGotten:\n" << result);
zeshuai00786396c62020-03-31 14:38:20 +0800259 BOOST_CHECK_MESSAGE(!expected_result.compare(result_1),
260 "Expected:\n" << expected_result << "\nGotten:\n" << result_1);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200261}
262
263BOOST_AUTO_TEST_CASE(test_json_proto_7) {
cyy316723a2019-01-05 16:35:14 +0800264 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
265 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
David Reiss4a8f46c2008-03-07 20:12:07 +0000266
267 Base64 base;
268 base.a = 123;
269 base.b1 = "1";
270 base.b2 = "12";
271 base.b3 = "123";
272 base.b4 = "1234";
273 base.b5 = "12345";
274 base.b6 = "123456";
275
276 base.write(proto.get());
277 Base64 base2;
278 base2.read(proto.get());
279
Claudius Heine5ef662b2015-06-24 10:03:50 +0200280 BOOST_CHECK(base == base2);
David Reissdb0ea152008-02-18 01:49:37 +0000281}
Claudius Heine8f11f522015-07-01 10:35:38 +0200282
283BOOST_AUTO_TEST_CASE(test_json_proto_8) {
284 const char* json_string =
285 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
286 "\"5\":{\"i32\":16.77216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800287 "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
Claudius Heine8f11f522015-07-01 10:35:38 +0200288 "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
289 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
290 "\",3,1,2,3]}}";
291
James E. King, III7edc8fa2017-01-20 10:11:41 -0500292 const std::size_t bufSiz = strlen(json_string) * sizeof(char);
cyy316723a2019-01-05 16:35:14 +0800293 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
James E. King, III7edc8fa2017-01-20 10:11:41 -0500294 (uint8_t*)(json_string), static_cast<uint32_t>(bufSiz)));
cyy316723a2019-01-05 16:35:14 +0800295 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Claudius Heine8f11f522015-07-01 10:35:38 +0200296
297 OneOfEach ooe2;
298
299 BOOST_CHECK_THROW(ooe2.read(proto.get()),
300 apache::thrift::protocol::TProtocolException);
301}
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200302
303static std::string toHexSequence(const std::string& str) {
304 std::stringstream ss;
305 ss << std::hex << std::setfill('0');
306 for (std::size_t i = 0; i < str.size(); i++) {
307 ss << "\\x" << int(uint8_t(str[i]));
308 }
309 return ss.str();
310}
311
312BOOST_AUTO_TEST_CASE(test_json_unicode_escaped) {
313 const char json_string[] =
314 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
315 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800316 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\u0e01 \\ud835\\udd3e\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200317 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
318 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
319 "\",3,1,2,3]}}";
320 const char* expected_zomg_unicode = "\xe0\xb8\x81 \xf0\x9d\x94\xbe";
321
cyy316723a2019-01-05 16:35:14 +0800322 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200323 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800324 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200325
326 OneOfEach ooe2;
327 ooe2.read(proto.get());
328 BOOST_CHECK_MESSAGE(!ooe2.zomg_unicode.compare(expected_zomg_unicode),
329 "Expected:\n" << toHexSequence(expected_zomg_unicode) << "\nGotten:\n"
330 << toHexSequence(ooe2.zomg_unicode));
331
332}
333
334BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_low_surrogate) {
335 const char json_string[] =
336 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
337 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800338 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\ud835\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200339 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
340 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
341 "\",3,1,2,3]}}";
342
cyy316723a2019-01-05 16:35:14 +0800343 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200344 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800345 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200346
347 OneOfEach ooe2;
348 BOOST_CHECK_THROW(ooe2.read(proto.get()),
349 apache::thrift::protocol::TProtocolException);
350}
351
352BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_hi_surrogate) {
353 const char json_string[] =
354 "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
355 "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
Wang Yaofu0433d172016-02-15 10:43:09 +0800356 "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\udd3e\"},"
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200357 "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
358 ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
359 "\",3,1,2,3]}}";
360
cyy316723a2019-01-05 16:35:14 +0800361 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200362 (uint8_t*)(json_string), sizeof(json_string)));
cyy316723a2019-01-05 16:35:14 +0800363 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
Konrad Grochowskia84e1392015-10-16 11:22:10 +0200364
365 OneOfEach ooe2;
366 BOOST_CHECK_THROW(ooe2.read(proto.get()),
367 apache::thrift::protocol::TProtocolException);
368}
Hasnain Lakhanib0252a92025-05-18 02:39:47 +0200369
370BOOST_AUTO_TEST_CASE(test_json_invalid_byte_range) {
371 // Test case for byte values outside valid range
372 const char json_string[] = "\"3\":{\"i8\":849}";
373
374 std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
375 (uint8_t*)(json_string), sizeof(json_string)));
376 std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
377
378 OneOfEach ooe2;
379 BOOST_CHECK_THROW(ooe2.read(proto.get()),
380 apache::thrift::protocol::TProtocolException);
381}