David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 20 | #define _USE_MATH_DEFINES |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 21 | #include <cmath> |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 22 | #include <iomanip> |
| 23 | #include <sstream> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 24 | #include <thrift/protocol/TJSONProtocol.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 25 | #include <memory> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 26 | #include <thrift/transport/TBufferTransports.h> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 27 | #include "gen-cpp/DebugProtoTest_types.h" |
| 28 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 29 | #define BOOST_TEST_MODULE JSONProtoTest |
| 30 | #include <boost/test/unit_test.hpp> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 31 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 32 | using namespace thrift::test::debug; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 33 | using namespace apache::thrift; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 34 | using apache::thrift::transport::TMemoryBuffer; |
| 35 | using apache::thrift::protocol::TJSONProtocol; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 36 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 37 | static std::shared_ptr<OneOfEach> ooe; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 38 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 39 | void 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"; |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 51 | ooe->rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"}; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 52 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 53 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(test_json_proto_1) { |
| 55 | testCaseSetup_1(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 56 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 57 | 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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 60 | "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 61 | "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 Combrink | 4035ecc | 2025-05-14 08:39:55 +0200 | [diff] [blame] | 63 | "\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000-0000-000000000000\"},\"16\"" |
| 64 | ":{\"lst\":[\"uid\",0]}}"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 65 | |
| 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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 72 | static std::shared_ptr<Nesting> n; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 73 | |
| 74 | void 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"; |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 89 | n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"}; |
Carel Combrink | 4035ecc | 2025-05-14 08:39:55 +0200 | [diff] [blame] | 90 | 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 94 | n->my_bonk.type = 31337; |
| 95 | n->my_bonk.message = "I am a bonk... xor!"; |
| 96 | } |
| 97 | |
| 98 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 105 | "1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\")\"},\"9\":{" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 106 | "\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"" |
| 107 | "AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2" |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 108 | ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-1726-" |
Carel Combrink | 4035ecc | 2025-05-14 08:39:55 +0200 | [diff] [blame] | 109 | "4e75-a04f-1ed9a6a89c4c\"},\"16\":{\"lst\":[\"uid\",2,\"fa1af5ec-fdc2-4355-" |
| 110 | "844a-9f0dbfd00e50\",\"1beece83-34f4-4fa3-b757-1ad1ac157fe3\"]}}}}" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 111 | ); |
| 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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 119 | static std::shared_ptr<HolyMoley> hm; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 120 | |
| 121 | void 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 Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 130 | |
| 131 | std::vector<std::string> stage1; |
| 132 | stage1.push_back("and a one"); |
| 133 | stage1.push_back("and a two"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 134 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 135 | stage1.clear(); |
| 136 | stage1.push_back("then a one, two"); |
| 137 | stage1.push_back("three!"); |
| 138 | stage1.push_back("FOUR!!"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 139 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 140 | stage1.clear(); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 141 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 142 | |
| 143 | std::vector<Bonk> stage2; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 144 | hm->bonks["nothing"] = stage2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 145 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 146 | stage2.back().type = 1; |
| 147 | stage2.back().message = "Wait."; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 149 | stage2.back().type = 2; |
| 150 | stage2.back().message = "What?"; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 151 | hm->bonks["something"] = stage2; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 152 | stage2.clear(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 153 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 154 | stage2.back().type = 3; |
| 155 | stage2.back().message = "quoth"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 156 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 157 | stage2.back().type = 4; |
| 158 | stage2.back().message = "the raven"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 159 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 160 | stage2.back().type = 5; |
| 161 | stage2.back().message = "nevermore"; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 162 | hm->bonks["poe"] = stage2; |
| 163 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 164 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 165 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 171 | "},\"7\":{\"dbl\":3.1415926535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 172 | "\"},\"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" |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 174 | ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000" |
Carel Combrink | 4035ecc | 2025-05-14 08:39:55 +0200 | [diff] [blame] | 175 | "-0000-000000000000\"},\"16\":{\"lst\":[\"uid\",0]}},{\"1\":{\"tf\":1},\"2\":{\"tf\":0}," |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 176 | "\"3\":{\"i8\":51},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64}," |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 177 | "\"7\":{\"dbl\":1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\"" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 178 | ")\"},\"9\":{\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{" |
| 179 | "\"str\":\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16" |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 180 | "\",3,1,2,3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-" |
Carel Combrink | 4035ecc | 2025-05-14 08:39:55 +0200 | [diff] [blame] | 181 | "1726-4e75-a04f-1ed9a6a89c4c\"},\"16\":{\"lst\":[\"uid\",2,\"fa1af5ec-fdc2-4355-" |
| 182 | "844a-9f0dbfd00e50\",\"1beece83-34f4-4fa3-b757-1ad1ac157fe3\"]}}]},\"2\":{\"set\":[\"lst\",3" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 183 | ",[\"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 | |
| 197 | BOOST_AUTO_TEST_CASE(test_json_proto_4) { |
| 198 | testCaseSetup_1(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 199 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 200 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 201 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 202 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 203 | ooe->write(proto.get()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 204 | OneOfEach ooe2; |
| 205 | ooe2.read(proto.get()); |
| 206 | |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 207 | BOOST_TEST_INFO("written: " << *ooe); |
| 208 | BOOST_TEST_INFO("read : " << ooe2); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 209 | BOOST_CHECK(*ooe == ooe2); |
| 210 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 211 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 212 | BOOST_AUTO_TEST_CASE(test_json_proto_5) { |
| 213 | testCaseSetup_3(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 214 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 215 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 216 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 217 | |
| 218 | hm->write(proto.get()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 219 | HolyMoley hm2; |
| 220 | hm2.read(proto.get()); |
| 221 | |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 222 | BOOST_TEST_INFO("written: " << *hm); |
| 223 | BOOST_TEST_INFO("read : " << hm2); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 224 | BOOST_CHECK(*hm == hm2); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 225 | |
Christian Lavoie | cbf87cb | 2010-11-28 14:34:26 +0000 | [diff] [blame] | 226 | hm2.big[0].a_bite = 0x00; |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 227 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 228 | BOOST_CHECK(*hm != hm2); |
| 229 | } |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 230 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(test_json_proto_6) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 232 | Doubles dub; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 233 | dub.nan = HUGE_VAL / HUGE_VAL; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 234 | dub.inf = HUGE_VAL; |
| 235 | dub.neginf = -HUGE_VAL; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 236 | dub.repeating = 10.0 / 3.0; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 237 | dub.big = 1E+305; |
Roger Meier | 22888ce | 2014-02-09 11:31:02 +0100 | [diff] [blame] | 238 | dub.tiny = 1E-305; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 239 | dub.zero = 0.0; |
| 240 | dub.negzero = -0.0; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 241 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 242 | const std::string expected_result( |
| 243 | "{\"1\":{\"dbl\":\"NaN\"},\"2\":{\"dbl\":\"Infinity\"},\"3\":{\"dbl\":\"-Infi" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 244 | "nity\"},\"4\":{\"dbl\":3.3333333333333335},\"5\":{\"dbl\":9.9999999999999994e+" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 245 | "304},\"6\":{\"dbl\":1e-305},\"7\":{\"dbl\":0},\"8\":{\"dbl\":-0}}" |
| 246 | ); |
| 247 | |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 248 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 249 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 250 | dub.write(proto.get()); |
| 251 | Doubles dub_1; |
| 252 | dub_1.read(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 253 | |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 254 | const std::string result(apache::thrift::ThriftJSONString(dub)); |
| 255 | const std::string result_1(apache::thrift::ThriftJSONString(dub_1)); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 256 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 257 | BOOST_CHECK_MESSAGE(!expected_result.compare(result), |
| 258 | "Expected:\n" << expected_result << "\nGotten:\n" << result); |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 259 | BOOST_CHECK_MESSAGE(!expected_result.compare(result_1), |
| 260 | "Expected:\n" << expected_result << "\nGotten:\n" << result_1); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_CASE(test_json_proto_7) { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 264 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 265 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 266 | |
| 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 280 | BOOST_CHECK(base == base2); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 281 | } |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 282 | |
| 283 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 287 | "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\" |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 288 | "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, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 292 | const std::size_t bufSiz = strlen(json_string) * sizeof(char); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 293 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
James E. King, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 294 | (uint8_t*)(json_string), static_cast<uint32_t>(bufSiz))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 295 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 296 | |
| 297 | OneOfEach ooe2; |
| 298 | |
| 299 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 300 | apache::thrift::protocol::TProtocolException); |
| 301 | } |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 302 | |
| 303 | static 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 | |
| 312 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 316 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\u0e01 \\ud835\\udd3e\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 317 | "\"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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 322 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 323 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 324 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 325 | |
| 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 | |
| 334 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 338 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\ud835\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 339 | "\"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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 343 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 344 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 345 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 346 | |
| 347 | OneOfEach ooe2; |
| 348 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 349 | apache::thrift::protocol::TProtocolException); |
| 350 | } |
| 351 | |
| 352 | BOOST_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 Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 356 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\udd3e\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 357 | "\"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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 361 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 362 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 363 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 364 | |
| 365 | OneOfEach ooe2; |
| 366 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 367 | apache::thrift::protocol::TProtocolException); |
| 368 | } |
Hasnain Lakhani | b0252a9 | 2025-05-18 02:39:47 +0200 | [diff] [blame] | 369 | |
| 370 | BOOST_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 | } |
Hasnain Lakhani | ce6139b | 2025-05-25 22:24:06 -0700 | [diff] [blame] | 382 | |
| 383 | BOOST_AUTO_TEST_CASE(test_json_base64_padding_validation) { |
| 384 | auto test_base64_padding = [](const std::string& base64_value) { |
| 385 | std::string json_string = "{\"11\":{\"str\":\"" + base64_value + "\"}}"; |
| 386 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
| 387 | (uint8_t*)(json_string.c_str()), static_cast<uint32_t>(json_string.size()))); |
| 388 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
| 389 | |
| 390 | OneOfEach ooe; |
| 391 | BOOST_CHECK_NO_THROW(ooe.read(proto.get())); |
| 392 | }; |
| 393 | |
| 394 | // Valid base64 with 1 padding character |
| 395 | test_base64_padding("QWE="); |
| 396 | // Valid base64 with 2 padding characters |
| 397 | test_base64_padding("QQ=="); |
| 398 | // Just padding |
| 399 | test_base64_padding("="); |
| 400 | test_base64_padding("=="); |
| 401 | // Malformed base64 with excessive padding (processed conservatively) |
| 402 | test_base64_padding("==="); |
| 403 | test_base64_padding("===="); |
| 404 | } |