blob: 0beaa380b2bcbae1c6ba28b79f6f7c5ad903c8e2 [file] [log] [blame]
David Reiss4e7530d2007-09-04 21:49:53 +00001/*
David Reissea2cba82009-03-30 21:35:00 +00002 * 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
20/*
David Reissb139f642009-02-17 20:28:46 +000021../compiler/cpp/thrift --gen cpp:dense DebugProtoTest.thrift
22../compiler/cpp/thrift --gen cpp:dense OptionalRequiredTest.thrift
David Reiss4e7530d2007-09-04 21:49:53 +000023g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
David Reissce161a92007-09-11 22:09:42 +000024 gen-cpp/OptionalRequiredTest_types.cpp \
25 gen-cpp/DebugProtoTest_types.cpp \
26 DenseProtoTest.cpp ../lib/cpp/.libs/libthrift.a -o DenseProtoTest
David Reiss4e7530d2007-09-04 21:49:53 +000027./DenseProtoTest
28*/
29
David Reisse67c0e62007-09-07 01:34:12 +000030// I do this to reach into the guts of TDenseProtocol. Sorry.
31#define private public
32#define inline
33
34#undef NDEBUG
David Reissce161a92007-09-11 22:09:42 +000035#include <cstdlib>
David Reisse67c0e62007-09-07 01:34:12 +000036#include <cassert>
David Reiss4e7530d2007-09-04 21:49:53 +000037#include <cmath>
David Reissce161a92007-09-11 22:09:42 +000038#include <string>
David Reiss4e7530d2007-09-04 21:49:53 +000039#include "gen-cpp/DebugProtoTest_types.h"
David Reissce161a92007-09-11 22:09:42 +000040#include "gen-cpp/OptionalRequiredTest_types.h"
Roger Meier49ff8b12012-04-13 09:12:31 +000041#include <thrift/protocol/TDenseProtocol.h>
42#include <thrift/transport/TBufferTransports.h>
David Reiss4e7530d2007-09-04 21:49:53 +000043
Claudius Heine5ef662b2015-06-24 10:03:50 +020044#define BOOST_TEST_MODULE DenseProtoTest
45#include <boost/test/unit_test.hpp>
46
47using std::string;
48using boost::shared_ptr;
49using namespace thrift::test;
50using namespace thrift::test::debug;
51using namespace apache::thrift::transport;
52using namespace apache::thrift::protocol;
53
David Reisse67c0e62007-09-07 01:34:12 +000054// Can't use memcmp here. GCC is too smart.
55bool my_memeq(const char* str1, const char* str2, int len) {
56 for (int i = 0; i < len; i++) {
57 if (str1[i] != str2[i]) {
58 return false;
59 }
60 }
61 return true;
62}
63
Claudius Heine5ef662b2015-06-24 10:03:50 +020064BOOST_AUTO_TEST_CASE(test_dense_proto_1) {
David Reiss4e7530d2007-09-04 21:49:53 +000065 OneOfEach ooe;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066 ooe.im_true = true;
67 ooe.im_false = false;
68 ooe.a_bite = 0xd6;
David Reiss4e7530d2007-09-04 21:49:53 +000069 ooe.integer16 = 27000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070 ooe.integer32 = 1 << 24;
David Reiss4e7530d2007-09-04 21:49:53 +000071 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
72 ooe.double_precision = M_PI;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 ooe.some_characters = "Debug THIS!";
74 ooe.zomg_unicode = "\xd7\n\a\t";
David Reiss4e7530d2007-09-04 21:49:53 +000075
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076 // cout << apache::thrift::ThriftDebugString(ooe) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +000077
78 Nesting n;
79 n.my_ooe = ooe;
80 n.my_ooe.integer16 = 16;
81 n.my_ooe.integer32 = 32;
82 n.my_ooe.integer64 = 64;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 n.my_ooe.double_precision = (std::sqrt(5) + 1) / 2;
84 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
David Reiss4e7530d2007-09-04 21:49:53 +000085 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
86 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
87 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010088 n.my_bonk.type = 31337;
David Reiss4e7530d2007-09-04 21:49:53 +000089 n.my_bonk.message = "I am a bonk... xor!";
90
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091 // cout << apache::thrift::ThriftDebugString(n) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +000092
93 HolyMoley hm;
94
95 hm.big.push_back(ooe);
96 hm.big.push_back(n.my_ooe);
97 hm.big[0].a_bite = 0x22;
98 hm.big[1].a_bite = 0x33;
99
100 std::vector<std::string> stage1;
101 stage1.push_back("and a one");
102 stage1.push_back("and a two");
103 hm.contain.insert(stage1);
104 stage1.clear();
105 stage1.push_back("then a one, two");
106 stage1.push_back("three!");
107 stage1.push_back("FOUR!!");
108 hm.contain.insert(stage1);
109 stage1.clear();
110 hm.contain.insert(stage1);
111
112 std::vector<Bonk> stage2;
113 hm.bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000115 stage2.back().type = 1;
116 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100117 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000118 stage2.back().type = 2;
119 stage2.back().message = "What?";
120 hm.bonks["something"] = stage2;
121 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100122 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000123 stage2.back().type = 3;
124 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000126 stage2.back().type = 4;
127 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100128 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000129 stage2.back().type = 5;
130 stage2.back().message = "nevermore";
131 hm.bonks["poe"] = stage2;
132
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100133 // cout << apache::thrift::ThriftDebugString(hm) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +0000134
135 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
136 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
137 proto->setTypeSpec(HolyMoley::local_reflection);
138
139 hm.write(proto.get());
140 HolyMoley hm2;
141 hm2.read(proto.get());
142
Claudius Heine5ef662b2015-06-24 10:03:50 +0200143 BOOST_CHECK(hm == hm2);
144}
David Reiss4e7530d2007-09-04 21:49:53 +0000145
Claudius Heine5ef662b2015-06-24 10:03:50 +0200146/*
147 * Following Testcases are currently disabled, because vlqWrite and vlqRead are
148 * private members.
149 */
150#if 0
151#define checkout(i, c) \
152 { \
153 uint64_t vlq; \
154 buffer->resetBuffer(); \
155 proto->vlqWrite(i); \
156 proto->getTransport()->flush(); \
157 BOOST_CHECK(my_memeq(buffer->getBufferAsString().data(), c, sizeof(c) - 1));\
158 proto->vlqRead(vlq); \
159 assert(vlq == i); \
David Reisse67c0e62007-09-07 01:34:12 +0000160 }
161
Claudius Heine5ef662b2015-06-24 10:03:50 +0200162BOOST_AUTO_TEST_CASE(test_dense_proto_2) {
163 // Let's test out the variable-length ints, shall we?
164 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
165 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
166
167 proto->setTypeSpec(HolyMoley::local_reflection);
168
David Reisse67c0e62007-09-07 01:34:12 +0000169 checkout(0x00000000, "\x00");
170 checkout(0x00000040, "\x40");
171 checkout(0x0000007F, "\x7F");
172 checkout(0x00000080, "\x81\x00");
173 checkout(0x00002000, "\xC0\x00");
174 checkout(0x00003FFF, "\xFF\x7F");
175 checkout(0x00004000, "\x81\x80\x00");
176 checkout(0x00100000, "\xC0\x80\x00");
177 checkout(0x001FFFFF, "\xFF\xFF\x7F");
178 checkout(0x00200000, "\x81\x80\x80\x00");
179 checkout(0x08000000, "\xC0\x80\x80\x00");
180 checkout(0x0FFFFFFF, "\xFF\xFF\xFF\x7F");
181 checkout(0x10000000, "\x81\x80\x80\x80\x00");
182 checkout(0x20000000, "\x82\x80\x80\x80\x00");
183 checkout(0x1FFFFFFF, "\x81\xFF\xFF\xFF\x7F");
184 checkout(0xFFFFFFFF, "\x8F\xFF\xFF\xFF\x7F");
185
186 checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
187 checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
188 checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
189 checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
190 checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
191 checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
192 checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
193 checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
194 checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
195 checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200196}
David Reisse67c0e62007-09-07 01:34:12 +0000197
Claudius Heine5ef662b2015-06-24 10:03:50 +0200198BOOST_AUTO_TEST_CASE(test_dense_proto_3) {
David Reissce161a92007-09-11 22:09:42 +0000199 // Test out the slow path with a TBufferedTransport.
Claudius Heine5ef662b2015-06-24 10:03:50 +0200200 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
201 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
David Reissce161a92007-09-11 22:09:42 +0000202 shared_ptr<TBufferedTransport> buff_trans(new TBufferedTransport(buffer, 3));
Claudius Heine5ef662b2015-06-24 10:03:50 +0200203
204 proto->setTypeSpec(HolyMoley::local_reflection);
David Reissce161a92007-09-11 22:09:42 +0000205 proto.reset(new TDenseProtocol(buff_trans));
Claudius Heine5ef662b2015-06-24 10:03:50 +0200206
David Reissce161a92007-09-11 22:09:42 +0000207 checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
208 checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
209 checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
210 checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
211 checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
212 checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
213 checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
214 checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
215 checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
216 checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200217}
218#endif
David Reissce161a92007-09-11 22:09:42 +0000219
Claudius Heine5ef662b2015-06-24 10:03:50 +0200220// Test optional stuff.
221BOOST_AUTO_TEST_CASE(test_dense_proto_4_1) {
222 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
223 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
David Reissce161a92007-09-11 22:09:42 +0000224 proto.reset(new TDenseProtocol(buffer));
225 proto->setTypeSpec(ManyOpt::local_reflection);
Claudius Heine5ef662b2015-06-24 10:03:50 +0200226
227 ManyOpt mo1, mo2;
David Reissce161a92007-09-11 22:09:42 +0000228 mo1.opt1 = 923759347;
229 mo1.opt2 = 392749274;
230 mo1.opt3 = 395739402;
231 mo1.def4 = 294730928;
232 mo1.opt5 = 394309218;
233 mo1.opt6 = 832194723;
234 mo1.__isset.opt1 = true;
235 mo1.__isset.opt2 = true;
236 mo1.__isset.opt3 = true;
237 mo1.__isset.def4 = true;
238 mo1.__isset.opt5 = true;
239 mo1.__isset.opt6 = true;
240
241 mo1.write(proto.get());
242 mo2.read(proto.get());
243
Claudius Heine5ef662b2015-06-24 10:03:50 +0200244 BOOST_CHECK(mo2.__isset.opt1 == true);
245 BOOST_CHECK(mo2.__isset.opt2 == true);
246 BOOST_CHECK(mo2.__isset.opt3 == true);
247 BOOST_CHECK(mo2.__isset.def4 == true);
248 BOOST_CHECK(mo2.__isset.opt5 == true);
249 BOOST_CHECK(mo2.__isset.opt6 == true);
David Reissce161a92007-09-11 22:09:42 +0000250
Claudius Heine5ef662b2015-06-24 10:03:50 +0200251 BOOST_CHECK(mo1 == mo2);
252}
David Reissce161a92007-09-11 22:09:42 +0000253
Claudius Heine5ef662b2015-06-24 10:03:50 +0200254BOOST_AUTO_TEST_CASE(test_dense_proto_4_2) {
255 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
256 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
257 proto.reset(new TDenseProtocol(buffer));
258 proto->setTypeSpec(ManyOpt::local_reflection);
259
260 ManyOpt mo1, mo2;
261 mo1.opt1 = 923759347;
262 mo1.opt2 = 392749274;
263 mo1.opt3 = 395739402;
264 mo1.def4 = 294730928;
265 mo1.opt5 = 394309218;
266 mo1.opt6 = 832194723;
David Reissce161a92007-09-11 22:09:42 +0000267 mo1.__isset.opt1 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200268 mo1.__isset.opt2 = true;
David Reissce161a92007-09-11 22:09:42 +0000269 mo1.__isset.opt3 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200270 mo1.__isset.def4 = true;
David Reissce161a92007-09-11 22:09:42 +0000271 mo1.__isset.opt5 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200272 mo1.__isset.opt6 = true;
David Reissce161a92007-09-11 22:09:42 +0000273
274 mo1.write(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200275 mo2.read(proto.get());
David Reissce161a92007-09-11 22:09:42 +0000276
Claudius Heine5ef662b2015-06-24 10:03:50 +0200277 BOOST_CHECK(mo2.__isset.opt1 == false);
278 BOOST_CHECK(mo2.__isset.opt2 == true);
279 BOOST_CHECK(mo2.__isset.opt3 == false);
280 BOOST_CHECK(mo2.__isset.def4 == true);
281 BOOST_CHECK(mo2.__isset.opt5 == false);
282 BOOST_CHECK(mo2.__isset.opt6 == true);
David Reissce161a92007-09-11 22:09:42 +0000283
Claudius Heine5ef662b2015-06-24 10:03:50 +0200284 BOOST_CHECK(mo1 == mo2);
285}
David Reissce161a92007-09-11 22:09:42 +0000286
Claudius Heine5ef662b2015-06-24 10:03:50 +0200287BOOST_AUTO_TEST_CASE(test_dense_proto_4_3) {
288 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
289 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
290 proto.reset(new TDenseProtocol(buffer));
291 proto->setTypeSpec(ManyOpt::local_reflection);
292
293 ManyOpt mo1, mo2;
294 mo1.opt1 = 923759347;
295 mo1.opt2 = 392749274;
296 mo1.opt3 = 395739402;
297 mo1.def4 = 294730928;
298 mo1.opt5 = 394309218;
299 mo1.opt6 = 832194723;
David Reissce161a92007-09-11 22:09:42 +0000300 mo1.__isset.opt1 = true;
David Reissce161a92007-09-11 22:09:42 +0000301 mo1.__isset.opt2 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200302 mo1.__isset.opt3 = true;
303 mo1.__isset.def4 = true;
304 mo1.__isset.opt5 = true;
David Reissce161a92007-09-11 22:09:42 +0000305 mo1.__isset.opt6 = false;
306
307 mo1.write(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200308 mo2.read(proto.get());
David Reissce161a92007-09-11 22:09:42 +0000309
Claudius Heine5ef662b2015-06-24 10:03:50 +0200310 BOOST_CHECK(mo2.__isset.opt1 == true);
311 BOOST_CHECK(mo2.__isset.opt2 == false);
312 BOOST_CHECK(mo2.__isset.opt3 == true);
313 BOOST_CHECK(mo2.__isset.def4 == true);
314 BOOST_CHECK(mo2.__isset.opt5 == true);
315 BOOST_CHECK(mo2.__isset.opt6 == false);
David Reissce161a92007-09-11 22:09:42 +0000316
Claudius Heine5ef662b2015-06-24 10:03:50 +0200317 BOOST_CHECK(mo1 == mo2);
318}
David Reissce161a92007-09-11 22:09:42 +0000319
Claudius Heine5ef662b2015-06-24 10:03:50 +0200320BOOST_AUTO_TEST_CASE(test_dense_proto_4_4) {
321 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
322 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
323 proto.reset(new TDenseProtocol(buffer));
324 proto->setTypeSpec(ManyOpt::local_reflection);
325
326 ManyOpt mo1, mo2;
327 mo1.opt1 = 923759347;
328 mo1.opt2 = 392749274;
329 mo1.opt3 = 395739402;
330 mo1.def4 = 294730928;
331 mo1.opt5 = 394309218;
332 mo1.opt6 = 832194723;
David Reissce161a92007-09-11 22:09:42 +0000333 mo1.__isset.opt1 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200334 mo1.__isset.opt2 = false;
335 mo1.__isset.opt3 = true;
336 mo1.__isset.def4 = true;
David Reissce161a92007-09-11 22:09:42 +0000337 mo1.__isset.opt5 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200338 mo1.__isset.opt6 = false;
David Reissce161a92007-09-11 22:09:42 +0000339
340 mo1.write(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200341 mo2.read(proto.get());
David Reissce161a92007-09-11 22:09:42 +0000342
Claudius Heine5ef662b2015-06-24 10:03:50 +0200343 BOOST_CHECK(mo2.__isset.opt1 == false);
344 BOOST_CHECK(mo2.__isset.opt2 == false);
345 BOOST_CHECK(mo2.__isset.opt3 == true);
346 BOOST_CHECK(mo2.__isset.def4 == true);
347 BOOST_CHECK(mo2.__isset.opt5 == false);
348 BOOST_CHECK(mo2.__isset.opt6 == false);
David Reissce161a92007-09-11 22:09:42 +0000349
Claudius Heine5ef662b2015-06-24 10:03:50 +0200350 BOOST_CHECK(mo1 == mo2);
351}
David Reissce161a92007-09-11 22:09:42 +0000352
Claudius Heine5ef662b2015-06-24 10:03:50 +0200353BOOST_AUTO_TEST_CASE(test_dense_proto_4_5) {
354 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
355 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
356 proto.reset(new TDenseProtocol(buffer));
357 proto->setTypeSpec(ManyOpt::local_reflection);
358
359 ManyOpt mo1, mo2;
360 mo1.opt1 = 923759347;
361 mo1.opt2 = 392749274;
362 mo1.opt3 = 395739402;
363 mo1.def4 = 294730928;
364 mo1.opt5 = 394309218;
365 mo1.opt6 = 832194723;
366 mo1.__isset.opt1 = false;
367 mo1.__isset.opt2 = false;
David Reissce161a92007-09-11 22:09:42 +0000368 mo1.__isset.opt3 = false;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200369 mo1.__isset.def4 = true;
370 mo1.__isset.opt5 = false;
371 mo1.__isset.opt6 = false;
David Reissce161a92007-09-11 22:09:42 +0000372
373 mo1.write(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200374 mo2.read(proto.get());
David Reissce161a92007-09-11 22:09:42 +0000375
Claudius Heine5ef662b2015-06-24 10:03:50 +0200376 BOOST_CHECK(mo2.__isset.opt1 == false);
377 BOOST_CHECK(mo2.__isset.opt2 == false);
378 BOOST_CHECK(mo2.__isset.opt3 == false);
379 BOOST_CHECK(mo2.__isset.def4 == true);
380 BOOST_CHECK(mo2.__isset.opt5 == false);
381 BOOST_CHECK(mo2.__isset.opt6 == false);
David Reissce161a92007-09-11 22:09:42 +0000382
Claudius Heine5ef662b2015-06-24 10:03:50 +0200383 BOOST_CHECK(mo1 == mo2);
384}
David Reissce161a92007-09-11 22:09:42 +0000385
Claudius Heine5ef662b2015-06-24 10:03:50 +0200386// Test fingerprint checking stuff.
387BOOST_AUTO_TEST_CASE(test_dense_proto_5_1) {
388 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
389 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
David Reissce161a92007-09-11 22:09:42 +0000390
Claudius Heine5ef662b2015-06-24 10:03:50 +0200391 // Default and required have the same fingerprint.
392 Tricky1 t1;
393 Tricky3 t3;
394 BOOST_CHECK(string(Tricky1::ascii_fingerprint) == Tricky3::ascii_fingerprint);
395 proto->setTypeSpec(Tricky1::local_reflection);
396 t1.im_default = 227;
397 t1.write(proto.get());
398 proto->setTypeSpec(Tricky3::local_reflection);
399 t3.read(proto.get());
400 BOOST_CHECK(t3.im_required == 227);
401}
David Reissce161a92007-09-11 22:09:42 +0000402
Claudius Heine5ef662b2015-06-24 10:03:50 +0200403BOOST_AUTO_TEST_CASE(test_dense_proto_5_2) {
404 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
405 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
David Reissce161a92007-09-11 22:09:42 +0000406
Claudius Heine5ef662b2015-06-24 10:03:50 +0200407 // Optional changes things.
408 Tricky1 t1;
409 Tricky2 t2;
410 BOOST_CHECK(string(Tricky1::ascii_fingerprint) != Tricky2::ascii_fingerprint);
411 proto->setTypeSpec(Tricky1::local_reflection);
412 t1.im_default = 227;
413 t1.write(proto.get());
414 try {
415 proto->setTypeSpec(Tricky2::local_reflection);
David Reissce161a92007-09-11 22:09:42 +0000416 t2.read(proto.get());
Claudius Heine5ef662b2015-06-24 10:03:50 +0200417 BOOST_CHECK(false);
418 } catch (TProtocolException& ex) {
419 buffer->resetBuffer();
David Reissce161a92007-09-11 22:09:42 +0000420 }
Claudius Heine5ef662b2015-06-24 10:03:50 +0200421}
David Reissce161a92007-09-11 22:09:42 +0000422
Claudius Heine5ef662b2015-06-24 10:03:50 +0200423BOOST_AUTO_TEST_CASE(test_dense_proto_5_3) {
424 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
425 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
426
427 // Holy cow. We can use the Tricky1 typespec with the Tricky2 structure.
428 Tricky1 t1;
429 Tricky2 t2;
430 proto->setTypeSpec(Tricky1::local_reflection);
431 t1.im_default = 227;
432 t1.write(proto.get());
433 t2.read(proto.get());
434 BOOST_CHECK(t2.__isset.im_optional == true);
435 BOOST_CHECK(t2.im_optional == 227);
436}
437
438BOOST_AUTO_TEST_CASE(test_dense_proto_5_4) {
439 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
440 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
441
442 // And totally off the wall.
443 Tricky1 t1;
444 OneOfEach ooe2;
445 BOOST_CHECK(string(Tricky1::ascii_fingerprint) != OneOfEach::ascii_fingerprint);
446 proto->setTypeSpec(Tricky1::local_reflection);
447 t1.im_default = 227;
448 t1.write(proto.get());
449 try {
450 proto->setTypeSpec(OneOfEach::local_reflection);
451 ooe2.read(proto.get());
452 BOOST_CHECK(false);
453 } catch (TProtocolException& ex) {
454 buffer->resetBuffer();
David Reissce161a92007-09-11 22:09:42 +0000455 }
Claudius Heine5ef662b2015-06-24 10:03:50 +0200456}
David Reissce161a92007-09-11 22:09:42 +0000457
Claudius Heine5ef662b2015-06-24 10:03:50 +0200458BOOST_AUTO_TEST_CASE(test_dense_proto_6) {
David Reissce161a92007-09-11 22:09:42 +0000459 // Okay, this is really off the wall.
460 // Just don't crash.
Claudius Heine5ef662b2015-06-24 10:03:50 +0200461 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
462 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
463
464 BOOST_TEST_MESSAGE("Starting fuzz test. This takes a while.");
David Reissce161a92007-09-11 22:09:42 +0000465 std::srand(12345);
466 for (int i = 0; i < 2000; i++) {
467 if (i % 100 == 0) {
Claudius Heine5ef662b2015-06-24 10:03:50 +0200468 BOOST_TEST_MESSAGE("Do " << i / 100 << "/" << 2000 / 100);
David Reissce161a92007-09-11 22:09:42 +0000469 }
470 buffer->resetBuffer();
471 // Make sure the fingerprint prefix is right.
472 buffer->write(Nesting::binary_fingerprint, 4);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100473 for (int j = 0; j < 1024 * 1024; j++) {
David Reissce161a92007-09-11 22:09:42 +0000474 uint8_t r = std::rand();
475 buffer->write(&r, 1);
476 }
477 Nesting n;
478 proto->setTypeSpec(OneOfEach::local_reflection);
479 try {
480 n.read(proto.get());
481 } catch (TProtocolException& ex) {
482 } catch (TTransportException& ex) {
483 }
484 }
David Reiss4e7530d2007-09-04 21:49:53 +0000485}