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" |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 63 | "\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000-0000-000000000000\"}}"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 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 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 71 | static std::shared_ptr<Nesting> n; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 72 | |
| 73 | void 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"; |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 88 | n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"}; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 89 | n->my_bonk.type = 31337; |
| 90 | n->my_bonk.message = "I am a bonk... xor!"; |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(test_json_proto_2) { |
| 94 | testCaseSetup_2(); |
| 95 | |
| 96 | const std::string expected_result( |
| 97 | "{\"1\":{\"rec\":{\"1\":{\"i32\":31337},\"2\":{\"str\":\"I am a bonk... xor" |
| 98 | "!\"}}},\"2\":{\"rec\":{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127" |
| 99 | "},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},\"7\":{\"dbl\":" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 100 | "1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\")\"},\"9\":{" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 101 | "\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"" |
| 102 | "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] | 103 | ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-1726-" |
| 104 | "4e75-a04f-1ed9a6a89c4c\"}}}}" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 105 | ); |
| 106 | |
| 107 | const std::string result(apache::thrift::ThriftJSONString(*n)); |
| 108 | |
| 109 | BOOST_CHECK_MESSAGE(!expected_result.compare(result), |
| 110 | "Expected:\n" << expected_result << "\nGotten:\n" << result); |
| 111 | } |
| 112 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 113 | static std::shared_ptr<HolyMoley> hm; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 114 | |
| 115 | void testCaseSetup_3() { |
| 116 | testCaseSetup_2(); |
| 117 | |
| 118 | hm.reset(new HolyMoley); |
| 119 | |
| 120 | hm->big.push_back(*ooe); |
| 121 | hm->big.push_back(n->my_ooe); |
| 122 | hm->big[0].a_bite = 0x22; |
| 123 | hm->big[1].a_bite = 0x33; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 124 | |
| 125 | std::vector<std::string> stage1; |
| 126 | stage1.push_back("and a one"); |
| 127 | stage1.push_back("and a two"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 128 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 129 | stage1.clear(); |
| 130 | stage1.push_back("then a one, two"); |
| 131 | stage1.push_back("three!"); |
| 132 | stage1.push_back("FOUR!!"); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 133 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 134 | stage1.clear(); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 135 | hm->contain.insert(stage1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 136 | |
| 137 | std::vector<Bonk> stage2; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 138 | hm->bonks["nothing"] = stage2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 139 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 140 | stage2.back().type = 1; |
| 141 | stage2.back().message = "Wait."; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 142 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 143 | stage2.back().type = 2; |
| 144 | stage2.back().message = "What?"; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 145 | hm->bonks["something"] = stage2; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 146 | stage2.clear(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 147 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 148 | stage2.back().type = 3; |
| 149 | stage2.back().message = "quoth"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 150 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 151 | stage2.back().type = 4; |
| 152 | stage2.back().message = "the raven"; |
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 = 5; |
| 155 | stage2.back().message = "nevermore"; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 156 | hm->bonks["poe"] = stage2; |
| 157 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 158 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 159 | BOOST_AUTO_TEST_CASE(test_json_proto_3) { |
| 160 | testCaseSetup_3(); |
| 161 | |
| 162 | const std::string expected_result( |
| 163 | "{\"1\":{\"lst\":[\"rec\",2,{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":" |
| 164 | "34},\"4\":{\"i16\":27000},\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 165 | "},\"7\":{\"dbl\":3.1415926535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 166 | "\"},\"9\":{\"str\":\"\xd7\\n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":" |
| 167 | "\"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] | 168 | ",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000" |
| 169 | "-0000-000000000000\"}},{\"1\":{\"tf\":1},\"2\":{\"tf\":0}," |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 170 | "\"3\":{\"i8\":51},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64}," |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 171 | "\"7\":{\"dbl\":1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\"" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 172 | ")\"},\"9\":{\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{" |
| 173 | "\"str\":\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16" |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 174 | "\",3,1,2,3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-" |
| 175 | "1726-4e75-a04f-1ed9a6a89c4c\"}}]},\"2\":{\"set\":[\"lst\",3" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 176 | ",[\"str\",0],[\"str\",2,\"and a one\",\"and a two\"],[\"str\",3,\"then a one" |
| 177 | ", two\",\"three!\",\"FOUR!!\"]]},\"3\":{\"map\":[\"str\",\"lst\",3,{\"nothin" |
| 178 | "g\":[\"rec\",0],\"poe\":[\"rec\",3,{\"1\":{\"i32\":3},\"2\":{\"str\":\"quoth" |
| 179 | "\"}},{\"1\":{\"i32\":4},\"2\":{\"str\":\"the raven\"}},{\"1\":{\"i32\":5},\"" |
| 180 | "2\":{\"str\":\"nevermore\"}}],\"something\":[\"rec\",2,{\"1\":{\"i32\":1},\"" |
| 181 | "2\":{\"str\":\"Wait.\"}},{\"1\":{\"i32\":2},\"2\":{\"str\":\"What?\"}}]}]}}" |
| 182 | ); |
| 183 | |
| 184 | const std::string result(apache::thrift::ThriftJSONString(*hm)); |
| 185 | |
| 186 | BOOST_CHECK_MESSAGE(!expected_result.compare(result), |
| 187 | "Expected:\n" << expected_result << "\nGotten:\n" << result); |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(test_json_proto_4) { |
| 191 | testCaseSetup_1(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 192 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 193 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 194 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 195 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 196 | ooe->write(proto.get()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 197 | OneOfEach ooe2; |
| 198 | ooe2.read(proto.get()); |
| 199 | |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 200 | BOOST_TEST_INFO("written: " << *ooe); |
| 201 | BOOST_TEST_INFO("read : " << ooe2); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 202 | BOOST_CHECK(*ooe == ooe2); |
| 203 | } |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 204 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 205 | BOOST_AUTO_TEST_CASE(test_json_proto_5) { |
| 206 | testCaseSetup_3(); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 207 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 208 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 209 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 210 | |
| 211 | hm->write(proto.get()); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 212 | HolyMoley hm2; |
| 213 | hm2.read(proto.get()); |
| 214 | |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 215 | BOOST_TEST_INFO("written: " << *hm); |
| 216 | BOOST_TEST_INFO("read : " << hm2); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 217 | BOOST_CHECK(*hm == hm2); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 218 | |
Christian Lavoie | cbf87cb | 2010-11-28 14:34:26 +0000 | [diff] [blame] | 219 | hm2.big[0].a_bite = 0x00; |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 220 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 221 | BOOST_CHECK(*hm != hm2); |
| 222 | } |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 223 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 224 | BOOST_AUTO_TEST_CASE(test_json_proto_6) { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 225 | Doubles dub; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 226 | dub.nan = HUGE_VAL / HUGE_VAL; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 227 | dub.inf = HUGE_VAL; |
| 228 | dub.neginf = -HUGE_VAL; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 229 | dub.repeating = 10.0 / 3.0; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 230 | dub.big = 1E+305; |
Roger Meier | 22888ce | 2014-02-09 11:31:02 +0100 | [diff] [blame] | 231 | dub.tiny = 1E-305; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 232 | dub.zero = 0.0; |
| 233 | dub.negzero = -0.0; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 234 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 235 | const std::string expected_result( |
| 236 | "{\"1\":{\"dbl\":\"NaN\"},\"2\":{\"dbl\":\"Infinity\"},\"3\":{\"dbl\":\"-Infi" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 237 | "nity\"},\"4\":{\"dbl\":3.3333333333333335},\"5\":{\"dbl\":9.9999999999999994e+" |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 238 | "304},\"6\":{\"dbl\":1e-305},\"7\":{\"dbl\":0},\"8\":{\"dbl\":-0}}" |
| 239 | ); |
| 240 | |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 241 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 242 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 243 | dub.write(proto.get()); |
| 244 | Doubles dub_1; |
| 245 | dub_1.read(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 246 | |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 247 | const std::string result(apache::thrift::ThriftJSONString(dub)); |
| 248 | const std::string result_1(apache::thrift::ThriftJSONString(dub_1)); |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 249 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 250 | BOOST_CHECK_MESSAGE(!expected_result.compare(result), |
| 251 | "Expected:\n" << expected_result << "\nGotten:\n" << result); |
zeshuai007 | 86396c6 | 2020-03-31 14:38:20 +0800 | [diff] [blame] | 252 | BOOST_CHECK_MESSAGE(!expected_result.compare(result_1), |
| 253 | "Expected:\n" << expected_result << "\nGotten:\n" << result_1); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(test_json_proto_7) { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 257 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 258 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 259 | |
| 260 | Base64 base; |
| 261 | base.a = 123; |
| 262 | base.b1 = "1"; |
| 263 | base.b2 = "12"; |
| 264 | base.b3 = "123"; |
| 265 | base.b4 = "1234"; |
| 266 | base.b5 = "12345"; |
| 267 | base.b6 = "123456"; |
| 268 | |
| 269 | base.write(proto.get()); |
| 270 | Base64 base2; |
| 271 | base2.read(proto.get()); |
| 272 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 273 | BOOST_CHECK(base == base2); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 274 | } |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 275 | |
| 276 | BOOST_AUTO_TEST_CASE(test_json_proto_8) { |
| 277 | const char* json_string = |
| 278 | "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000}," |
| 279 | "\"5\":{\"i32\":16.77216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 280 | "535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\" |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 281 | "n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\"" |
| 282 | ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64" |
| 283 | "\",3,1,2,3]}}"; |
| 284 | |
James E. King, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 285 | const std::size_t bufSiz = strlen(json_string) * sizeof(char); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 286 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
James E. King, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 287 | (uint8_t*)(json_string), static_cast<uint32_t>(bufSiz))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 288 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Claudius Heine | 8f11f52 | 2015-07-01 10:35:38 +0200 | [diff] [blame] | 289 | |
| 290 | OneOfEach ooe2; |
| 291 | |
| 292 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 293 | apache::thrift::protocol::TProtocolException); |
| 294 | } |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 295 | |
| 296 | static std::string toHexSequence(const std::string& str) { |
| 297 | std::stringstream ss; |
| 298 | ss << std::hex << std::setfill('0'); |
| 299 | for (std::size_t i = 0; i < str.size(); i++) { |
| 300 | ss << "\\x" << int(uint8_t(str[i])); |
| 301 | } |
| 302 | return ss.str(); |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE(test_json_unicode_escaped) { |
| 306 | const char json_string[] = |
| 307 | "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000}," |
| 308 | "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 309 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\u0e01 \\ud835\\udd3e\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 310 | "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\"" |
| 311 | ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64" |
| 312 | "\",3,1,2,3]}}"; |
| 313 | const char* expected_zomg_unicode = "\xe0\xb8\x81 \xf0\x9d\x94\xbe"; |
| 314 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 315 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 316 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 317 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 318 | |
| 319 | OneOfEach ooe2; |
| 320 | ooe2.read(proto.get()); |
| 321 | BOOST_CHECK_MESSAGE(!ooe2.zomg_unicode.compare(expected_zomg_unicode), |
| 322 | "Expected:\n" << toHexSequence(expected_zomg_unicode) << "\nGotten:\n" |
| 323 | << toHexSequence(ooe2.zomg_unicode)); |
| 324 | |
| 325 | } |
| 326 | |
| 327 | BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_low_surrogate) { |
| 328 | const char json_string[] = |
| 329 | "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000}," |
| 330 | "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 331 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\ud835\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 332 | "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\"" |
| 333 | ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64" |
| 334 | "\",3,1,2,3]}}"; |
| 335 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 336 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 337 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 338 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 339 | |
| 340 | OneOfEach ooe2; |
| 341 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 342 | apache::thrift::protocol::TProtocolException); |
| 343 | } |
| 344 | |
| 345 | BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_hi_surrogate) { |
| 346 | const char json_string[] = |
| 347 | "{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000}," |
| 348 | "\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926" |
Wang Yaofu | 0433d17 | 2016-02-15 10:43:09 +0800 | [diff] [blame] | 349 | "535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\udd3e\"}," |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 350 | "\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\"" |
| 351 | ":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64" |
| 352 | "\",3,1,2,3]}}"; |
| 353 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 354 | std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer( |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 355 | (uint8_t*)(json_string), sizeof(json_string))); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 356 | std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
Konrad Grochowski | a84e139 | 2015-10-16 11:22:10 +0200 | [diff] [blame] | 357 | |
| 358 | OneOfEach ooe2; |
| 359 | BOOST_CHECK_THROW(ooe2.read(proto.get()), |
| 360 | apache::thrift::protocol::TProtocolException); |
| 361 | } |