2009-03-30 21:35:00 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
|
|
* distributed with this work for additional information
|
|
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
|
|
* under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2014-01-07 21:42:01 -05:00
|
|
|
|
#define _USE_MATH_DEFINES
|
2008-02-18 01:49:37 +00:00
|
|
|
|
#include <cmath>
|
2015-10-16 11:22:10 +02:00
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
#include <sstream>
|
2012-04-13 09:12:31 +00:00
|
|
|
|
#include <thrift/protocol/TJSONProtocol.h>
|
2019-01-05 16:35:14 +08:00
|
|
|
|
#include <memory>
|
2017-08-05 12:23:54 -04:00
|
|
|
|
#include <thrift/transport/TBufferTransports.h>
|
2008-02-18 01:49:37 +00:00
|
|
|
|
#include "gen-cpp/DebugProtoTest_types.h"
|
|
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
#define BOOST_TEST_MODULE JSONProtoTest
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace thrift::test::debug;
|
2017-08-05 12:23:54 -04:00
|
|
|
|
using namespace apache::thrift;
|
2015-06-24 10:03:50 +02:00
|
|
|
|
using apache::thrift::transport::TMemoryBuffer;
|
|
|
|
|
|
using apache::thrift::protocol::TJSONProtocol;
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
static std::shared_ptr<OneOfEach> ooe;
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
|
|
|
|
|
void testCaseSetup_1() {
|
|
|
|
|
|
ooe.reset(new OneOfEach);
|
|
|
|
|
|
ooe->im_true = true;
|
|
|
|
|
|
ooe->im_false = false;
|
|
|
|
|
|
ooe->a_bite = 0x7f;
|
|
|
|
|
|
ooe->integer16 = 27000;
|
|
|
|
|
|
ooe->integer32 = 1 << 24;
|
|
|
|
|
|
ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
|
|
|
|
|
|
ooe->double_precision = M_PI;
|
|
|
|
|
|
ooe->some_characters = "JSON THIS! \"\1";
|
|
|
|
|
|
ooe->zomg_unicode = "\xd7\n\a\t";
|
|
|
|
|
|
ooe->base64 = "\1\2\3\255";
|
2024-04-27 19:51:39 +02:00
|
|
|
|
ooe->rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
|
2015-06-24 10:03:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_1) {
|
|
|
|
|
|
testCaseSetup_1();
|
|
|
|
|
|
|
|
|
|
|
|
const std::string expected_result(
|
|
|
|
|
|
"{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
|
|
|
|
|
|
"\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
"n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
|
|
|
|
|
|
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
|
2024-03-23 21:32:28 +01:00
|
|
|
|
"\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000-0000-000000000000\"}}");
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
|
|
|
|
|
const std::string result(apache::thrift::ThriftJSONString(*ooe));
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(!expected_result.compare(result),
|
|
|
|
|
|
"Expected:\n" << expected_result << "\nGotten:\n" << result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
static std::shared_ptr<Nesting> n;
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
|
|
|
|
|
void testCaseSetup_2() {
|
|
|
|
|
|
testCaseSetup_1();
|
|
|
|
|
|
|
|
|
|
|
|
n.reset(new Nesting);
|
|
|
|
|
|
n->my_ooe = *ooe;
|
|
|
|
|
|
n->my_ooe.integer16 = 16;
|
|
|
|
|
|
n->my_ooe.integer32 = 32;
|
|
|
|
|
|
n->my_ooe.integer64 = 64;
|
|
|
|
|
|
n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
|
|
|
|
|
|
n->my_ooe.some_characters = ":R (me going \"rrrr\")";
|
|
|
|
|
|
n->my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
|
|
|
|
|
|
"\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
|
|
|
|
|
|
"\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
|
|
|
|
|
|
"\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
|
|
|
|
|
|
"\xbc";
|
2024-04-27 19:51:39 +02:00
|
|
|
|
n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
|
2015-06-24 10:03:50 +02:00
|
|
|
|
n->my_bonk.type = 31337;
|
|
|
|
|
|
n->my_bonk.message = "I am a bonk... xor!";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_2) {
|
|
|
|
|
|
testCaseSetup_2();
|
|
|
|
|
|
|
|
|
|
|
|
const std::string expected_result(
|
|
|
|
|
|
"{\"1\":{\"rec\":{\"1\":{\"i32\":31337},\"2\":{\"str\":\"I am a bonk... xor"
|
|
|
|
|
|
"!\"}}},\"2\":{\"rec\":{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127"
|
|
|
|
|
|
"},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},\"7\":{\"dbl\":"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\")\"},\"9\":{"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
"\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{\"str\":\""
|
|
|
|
|
|
"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
|
2024-03-23 21:32:28 +01:00
|
|
|
|
",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-1726-"
|
|
|
|
|
|
"4e75-a04f-1ed9a6a89c4c\"}}}}"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const std::string result(apache::thrift::ThriftJSONString(*n));
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(!expected_result.compare(result),
|
|
|
|
|
|
"Expected:\n" << expected_result << "\nGotten:\n" << result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
static std::shared_ptr<HolyMoley> hm;
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
|
|
|
|
|
void testCaseSetup_3() {
|
|
|
|
|
|
testCaseSetup_2();
|
|
|
|
|
|
|
|
|
|
|
|
hm.reset(new HolyMoley);
|
|
|
|
|
|
|
|
|
|
|
|
hm->big.push_back(*ooe);
|
|
|
|
|
|
hm->big.push_back(n->my_ooe);
|
|
|
|
|
|
hm->big[0].a_bite = 0x22;
|
|
|
|
|
|
hm->big[1].a_bite = 0x33;
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> stage1;
|
|
|
|
|
|
stage1.push_back("and a one");
|
|
|
|
|
|
stage1.push_back("and a two");
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->contain.insert(stage1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage1.clear();
|
|
|
|
|
|
stage1.push_back("then a one, two");
|
|
|
|
|
|
stage1.push_back("three!");
|
|
|
|
|
|
stage1.push_back("FOUR!!");
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->contain.insert(stage1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage1.clear();
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->contain.insert(stage1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
|
|
|
|
|
std::vector<Bonk> stage2;
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->bonks["nothing"] = stage2;
|
2014-11-13 15:33:38 +01:00
|
|
|
|
stage2.resize(stage2.size() + 1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.back().type = 1;
|
|
|
|
|
|
stage2.back().message = "Wait.";
|
2014-11-13 15:33:38 +01:00
|
|
|
|
stage2.resize(stage2.size() + 1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.back().type = 2;
|
|
|
|
|
|
stage2.back().message = "What?";
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->bonks["something"] = stage2;
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.clear();
|
2014-11-13 15:33:38 +01:00
|
|
|
|
stage2.resize(stage2.size() + 1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.back().type = 3;
|
|
|
|
|
|
stage2.back().message = "quoth";
|
2014-11-13 15:33:38 +01:00
|
|
|
|
stage2.resize(stage2.size() + 1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.back().type = 4;
|
|
|
|
|
|
stage2.back().message = "the raven";
|
2014-11-13 15:33:38 +01:00
|
|
|
|
stage2.resize(stage2.size() + 1);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
stage2.back().type = 5;
|
|
|
|
|
|
stage2.back().message = "nevermore";
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->bonks["poe"] = stage2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_3) {
|
|
|
|
|
|
testCaseSetup_3();
|
|
|
|
|
|
|
|
|
|
|
|
const std::string expected_result(
|
|
|
|
|
|
"{\"1\":{\"lst\":[\"rec\",2,{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":"
|
|
|
|
|
|
"34},\"4\":{\"i16\":27000},\"5\":{\"i32\":16777216},\"6\":{\"i64\":6000000000"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"},\"7\":{\"dbl\":3.1415926535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
"\"},\"9\":{\"str\":\"\xd7\\n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":"
|
|
|
|
|
|
"\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2"
|
2024-03-23 21:32:28 +01:00
|
|
|
|
",3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"00000000-0000-0000"
|
|
|
|
|
|
"-0000-000000000000\"}},{\"1\":{\"tf\":1},\"2\":{\"tf\":0},"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
"\"3\":{\"i8\":51},\"4\":{\"i16\":16},\"5\":{\"i32\":32},\"6\":{\"i64\":64},"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"\"7\":{\"dbl\":1.6180339887498949},\"8\":{\"str\":\":R (me going \\\"rrrr\\\""
|
2015-06-24 10:03:50 +02:00
|
|
|
|
")\"},\"9\":{\"str\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκǃ‼\"},\"10\":{\"tf\":0},\"11\":{"
|
|
|
|
|
|
"\"str\":\"AQIDrQ\"},\"12\":{\"lst\":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16"
|
2024-03-23 21:32:28 +01:00
|
|
|
|
"\",3,1,2,3]},\"14\":{\"lst\":[\"i64\",3,1,2,3]},\"15\":{\"uid\":\"5e2ab188-"
|
|
|
|
|
|
"1726-4e75-a04f-1ed9a6a89c4c\"}}]},\"2\":{\"set\":[\"lst\",3"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
",[\"str\",0],[\"str\",2,\"and a one\",\"and a two\"],[\"str\",3,\"then a one"
|
|
|
|
|
|
", two\",\"three!\",\"FOUR!!\"]]},\"3\":{\"map\":[\"str\",\"lst\",3,{\"nothin"
|
|
|
|
|
|
"g\":[\"rec\",0],\"poe\":[\"rec\",3,{\"1\":{\"i32\":3},\"2\":{\"str\":\"quoth"
|
|
|
|
|
|
"\"}},{\"1\":{\"i32\":4},\"2\":{\"str\":\"the raven\"}},{\"1\":{\"i32\":5},\""
|
|
|
|
|
|
"2\":{\"str\":\"nevermore\"}}],\"something\":[\"rec\",2,{\"1\":{\"i32\":1},\""
|
|
|
|
|
|
"2\":{\"str\":\"Wait.\"}},{\"1\":{\"i32\":2},\"2\":{\"str\":\"What?\"}}]}]}}"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const std::string result(apache::thrift::ThriftJSONString(*hm));
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(!expected_result.compare(result),
|
|
|
|
|
|
"Expected:\n" << expected_result << "\nGotten:\n" << result);
|
|
|
|
|
|
}
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_4) {
|
|
|
|
|
|
testCaseSetup_1();
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
|
|
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
ooe->write(proto.get());
|
2008-02-18 01:49:37 +00:00
|
|
|
|
OneOfEach ooe2;
|
|
|
|
|
|
ooe2.read(proto.get());
|
|
|
|
|
|
|
2024-03-23 21:32:28 +01:00
|
|
|
|
BOOST_TEST_INFO("written: " << *ooe);
|
|
|
|
|
|
BOOST_TEST_INFO("read : " << ooe2);
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_CHECK(*ooe == ooe2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_5) {
|
|
|
|
|
|
testCaseSetup_3();
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
|
|
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
hm->write(proto.get());
|
2008-02-18 01:49:37 +00:00
|
|
|
|
HolyMoley hm2;
|
|
|
|
|
|
hm2.read(proto.get());
|
|
|
|
|
|
|
2024-03-23 21:32:28 +01:00
|
|
|
|
BOOST_TEST_INFO("written: " << *hm);
|
|
|
|
|
|
BOOST_TEST_INFO("read : " << hm2);
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_CHECK(*hm == hm2);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
|
2010-11-28 14:34:26 +00:00
|
|
|
|
hm2.big[0].a_bite = 0x00;
|
2008-03-07 20:12:07 +00:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_CHECK(*hm != hm2);
|
|
|
|
|
|
}
|
2008-03-07 20:12:07 +00:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_6) {
|
2008-02-18 01:49:37 +00:00
|
|
|
|
Doubles dub;
|
2014-11-13 15:33:38 +01:00
|
|
|
|
dub.nan = HUGE_VAL / HUGE_VAL;
|
2008-02-18 01:49:37 +00:00
|
|
|
|
dub.inf = HUGE_VAL;
|
|
|
|
|
|
dub.neginf = -HUGE_VAL;
|
2014-11-13 15:33:38 +01:00
|
|
|
|
dub.repeating = 10.0 / 3.0;
|
2008-02-18 01:49:37 +00:00
|
|
|
|
dub.big = 1E+305;
|
2014-02-09 11:31:02 +01:00
|
|
|
|
dub.tiny = 1E-305;
|
2008-02-18 01:49:37 +00:00
|
|
|
|
dub.zero = 0.0;
|
|
|
|
|
|
dub.negzero = -0.0;
|
|
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
const std::string expected_result(
|
|
|
|
|
|
"{\"1\":{\"dbl\":\"NaN\"},\"2\":{\"dbl\":\"Infinity\"},\"3\":{\"dbl\":\"-Infi"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"nity\"},\"4\":{\"dbl\":3.3333333333333335},\"5\":{\"dbl\":9.9999999999999994e+"
|
2015-06-24 10:03:50 +02:00
|
|
|
|
"304},\"6\":{\"dbl\":1e-305},\"7\":{\"dbl\":0},\"8\":{\"dbl\":-0}}"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2020-03-31 14:38:20 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
|
2024-03-23 21:32:28 +01:00
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2020-03-31 14:38:20 +08:00
|
|
|
|
dub.write(proto.get());
|
|
|
|
|
|
Doubles dub_1;
|
|
|
|
|
|
dub_1.read(proto.get());
|
2015-06-24 10:03:50 +02:00
|
|
|
|
|
2020-03-31 14:38:20 +08:00
|
|
|
|
const std::string result(apache::thrift::ThriftJSONString(dub));
|
|
|
|
|
|
const std::string result_1(apache::thrift::ThriftJSONString(dub_1));
|
2024-03-23 21:32:28 +01:00
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_CHECK_MESSAGE(!expected_result.compare(result),
|
|
|
|
|
|
"Expected:\n" << expected_result << "\nGotten:\n" << result);
|
2020-03-31 14:38:20 +08:00
|
|
|
|
BOOST_CHECK_MESSAGE(!expected_result.compare(result_1),
|
|
|
|
|
|
"Expected:\n" << expected_result << "\nGotten:\n" << result_1);
|
2015-06-24 10:03:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_7) {
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
|
|
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2008-03-07 20:12:07 +00:00
|
|
|
|
|
|
|
|
|
|
Base64 base;
|
|
|
|
|
|
base.a = 123;
|
|
|
|
|
|
base.b1 = "1";
|
|
|
|
|
|
base.b2 = "12";
|
|
|
|
|
|
base.b3 = "123";
|
|
|
|
|
|
base.b4 = "1234";
|
|
|
|
|
|
base.b5 = "12345";
|
|
|
|
|
|
base.b6 = "123456";
|
|
|
|
|
|
|
|
|
|
|
|
base.write(proto.get());
|
|
|
|
|
|
Base64 base2;
|
|
|
|
|
|
base2.read(proto.get());
|
|
|
|
|
|
|
2015-06-24 10:03:50 +02:00
|
|
|
|
BOOST_CHECK(base == base2);
|
2008-02-18 01:49:37 +00:00
|
|
|
|
}
|
2015-07-01 10:35:38 +02:00
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_proto_8) {
|
|
|
|
|
|
const char* json_string =
|
|
|
|
|
|
"{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
|
|
|
|
|
|
"\"5\":{\"i32\":16.77216},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"535897931},\"8\":{\"str\":\"JSON THIS! \\\"\\u0001\"},\"9\":{\"str\":\"\xd7\\"
|
2015-07-01 10:35:38 +02:00
|
|
|
|
"n\\u0007\\t\"},\"10\":{\"tf\":0},\"11\":{\"str\":\"AQIDrQ\"},\"12\":{\"lst\""
|
|
|
|
|
|
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
|
|
|
|
|
|
"\",3,1,2,3]}}";
|
|
|
|
|
|
|
2017-01-20 10:11:41 -05:00
|
|
|
|
const std::size_t bufSiz = strlen(json_string) * sizeof(char);
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
|
2017-01-20 10:11:41 -05:00
|
|
|
|
(uint8_t*)(json_string), static_cast<uint32_t>(bufSiz)));
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2015-07-01 10:35:38 +02:00
|
|
|
|
|
|
|
|
|
|
OneOfEach ooe2;
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(ooe2.read(proto.get()),
|
|
|
|
|
|
apache::thrift::protocol::TProtocolException);
|
|
|
|
|
|
}
|
2015-10-16 11:22:10 +02:00
|
|
|
|
|
|
|
|
|
|
static std::string toHexSequence(const std::string& str) {
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
ss << std::hex << std::setfill('0');
|
|
|
|
|
|
for (std::size_t i = 0; i < str.size(); i++) {
|
|
|
|
|
|
ss << "\\x" << int(uint8_t(str[i]));
|
|
|
|
|
|
}
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_unicode_escaped) {
|
|
|
|
|
|
const char json_string[] =
|
|
|
|
|
|
"{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
|
|
|
|
|
|
"\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\u0e01 \\ud835\\udd3e\"},"
|
2015-10-16 11:22:10 +02:00
|
|
|
|
"\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
|
|
|
|
|
|
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
|
|
|
|
|
|
"\",3,1,2,3]}}";
|
|
|
|
|
|
const char* expected_zomg_unicode = "\xe0\xb8\x81 \xf0\x9d\x94\xbe";
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
|
2015-10-16 11:22:10 +02:00
|
|
|
|
(uint8_t*)(json_string), sizeof(json_string)));
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2015-10-16 11:22:10 +02:00
|
|
|
|
|
|
|
|
|
|
OneOfEach ooe2;
|
|
|
|
|
|
ooe2.read(proto.get());
|
|
|
|
|
|
BOOST_CHECK_MESSAGE(!ooe2.zomg_unicode.compare(expected_zomg_unicode),
|
|
|
|
|
|
"Expected:\n" << toHexSequence(expected_zomg_unicode) << "\nGotten:\n"
|
|
|
|
|
|
<< toHexSequence(ooe2.zomg_unicode));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_low_surrogate) {
|
|
|
|
|
|
const char json_string[] =
|
|
|
|
|
|
"{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
|
|
|
|
|
|
"\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\ud835\"},"
|
2015-10-16 11:22:10 +02:00
|
|
|
|
"\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
|
|
|
|
|
|
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
|
|
|
|
|
|
"\",3,1,2,3]}}";
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
|
2015-10-16 11:22:10 +02:00
|
|
|
|
(uint8_t*)(json_string), sizeof(json_string)));
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2015-10-16 11:22:10 +02:00
|
|
|
|
|
|
|
|
|
|
OneOfEach ooe2;
|
|
|
|
|
|
BOOST_CHECK_THROW(ooe2.read(proto.get()),
|
|
|
|
|
|
apache::thrift::protocol::TProtocolException);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_json_unicode_escaped_missing_hi_surrogate) {
|
|
|
|
|
|
const char json_string[] =
|
|
|
|
|
|
"{\"1\":{\"tf\":1},\"2\":{\"tf\":0},\"3\":{\"i8\":127},\"4\":{\"i16\":27000},"
|
|
|
|
|
|
"\"5\":{\"i32\":16},\"6\":{\"i64\":6000000000},\"7\":{\"dbl\":3.1415926"
|
2016-02-15 10:43:09 +08:00
|
|
|
|
"535897931},\"8\":{\"str\":\"JSON THIS!\"},\"9\":{\"str\":\"\\udd3e\"},"
|
2015-10-16 11:22:10 +02:00
|
|
|
|
"\"10\":{\"tf\":0},\"11\":{\"str\":\"000000\"},\"12\":{\"lst\""
|
|
|
|
|
|
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
|
|
|
|
|
|
"\",3,1,2,3]}}";
|
|
|
|
|
|
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
|
2015-10-16 11:22:10 +02:00
|
|
|
|
(uint8_t*)(json_string), sizeof(json_string)));
|
2019-01-05 16:35:14 +08:00
|
|
|
|
std::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
2015-10-16 11:22:10 +02:00
|
|
|
|
|
|
|
|
|
|
OneOfEach ooe2;
|
|
|
|
|
|
BOOST_CHECK_THROW(ooe2.read(proto.get()),
|
|
|
|
|
|
apache::thrift::protocol::TProtocolException);
|
|
|
|
|
|
}
|