blob: d827d3c64135818178b8444121006fc7f017216f [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 <iostream>
38#include <cmath>
David Reissce161a92007-09-11 22:09:42 +000039#include <string>
David Reiss4e7530d2007-09-04 21:49:53 +000040#include "gen-cpp/DebugProtoTest_types.h"
David Reissce161a92007-09-11 22:09:42 +000041#include "gen-cpp/OptionalRequiredTest_types.h"
Roger Meier49ff8b12012-04-13 09:12:31 +000042#include <thrift/protocol/TDenseProtocol.h>
43#include <thrift/transport/TBufferTransports.h>
David Reiss4e7530d2007-09-04 21:49:53 +000044
David Reisse67c0e62007-09-07 01:34:12 +000045// Can't use memcmp here. GCC is too smart.
46bool my_memeq(const char* str1, const char* str2, int len) {
47 for (int i = 0; i < len; i++) {
48 if (str1[i] != str2[i]) {
49 return false;
50 }
51 }
52 return true;
53}
54
David Reiss4e7530d2007-09-04 21:49:53 +000055int main() {
David Reissce161a92007-09-11 22:09:42 +000056 using std::string;
David Reiss4e7530d2007-09-04 21:49:53 +000057 using std::cout;
58 using std::endl;
59 using boost::shared_ptr;
David Reiss2a4bfd62008-04-07 23:45:00 +000060 using namespace thrift::test::debug;
T Jake Lucianib5e62212009-01-31 22:36:20 +000061 using namespace apache::thrift::transport;
62 using namespace apache::thrift::protocol;
David Reiss4e7530d2007-09-04 21:49:53 +000063
David Reiss4e7530d2007-09-04 21:49:53 +000064 OneOfEach ooe;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 ooe.im_true = true;
66 ooe.im_false = false;
67 ooe.a_bite = 0xd6;
David Reiss4e7530d2007-09-04 21:49:53 +000068 ooe.integer16 = 27000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069 ooe.integer32 = 1 << 24;
David Reiss4e7530d2007-09-04 21:49:53 +000070 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
71 ooe.double_precision = M_PI;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072 ooe.some_characters = "Debug THIS!";
73 ooe.zomg_unicode = "\xd7\n\a\t";
David Reiss4e7530d2007-09-04 21:49:53 +000074
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075 // cout << apache::thrift::ThriftDebugString(ooe) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +000076
77 Nesting n;
78 n.my_ooe = ooe;
79 n.my_ooe.integer16 = 16;
80 n.my_ooe.integer32 = 32;
81 n.my_ooe.integer64 = 64;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082 n.my_ooe.double_precision = (std::sqrt(5) + 1) / 2;
83 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
David Reiss4e7530d2007-09-04 21:49:53 +000084 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
85 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
86 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 n.my_bonk.type = 31337;
David Reiss4e7530d2007-09-04 21:49:53 +000088 n.my_bonk.message = "I am a bonk... xor!";
89
Konrad Grochowski16a23a62014-11-13 15:33:38 +010090 // cout << apache::thrift::ThriftDebugString(n) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +000091
92 HolyMoley hm;
93
94 hm.big.push_back(ooe);
95 hm.big.push_back(n.my_ooe);
96 hm.big[0].a_bite = 0x22;
97 hm.big[1].a_bite = 0x33;
98
99 std::vector<std::string> stage1;
100 stage1.push_back("and a one");
101 stage1.push_back("and a two");
102 hm.contain.insert(stage1);
103 stage1.clear();
104 stage1.push_back("then a one, two");
105 stage1.push_back("three!");
106 stage1.push_back("FOUR!!");
107 hm.contain.insert(stage1);
108 stage1.clear();
109 hm.contain.insert(stage1);
110
111 std::vector<Bonk> stage2;
112 hm.bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000114 stage2.back().type = 1;
115 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000117 stage2.back().type = 2;
118 stage2.back().message = "What?";
119 hm.bonks["something"] = stage2;
120 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000122 stage2.back().type = 3;
123 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100124 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000125 stage2.back().type = 4;
126 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127 stage2.resize(stage2.size() + 1);
David Reiss4e7530d2007-09-04 21:49:53 +0000128 stage2.back().type = 5;
129 stage2.back().message = "nevermore";
130 hm.bonks["poe"] = stage2;
131
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100132 // cout << apache::thrift::ThriftDebugString(hm) << endl << endl;
David Reiss4e7530d2007-09-04 21:49:53 +0000133
134 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
135 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
136 proto->setTypeSpec(HolyMoley::local_reflection);
137
138 hm.write(proto.get());
139 HolyMoley hm2;
140 hm2.read(proto.get());
141
142 assert(hm == hm2);
143
David Reisse67c0e62007-09-07 01:34:12 +0000144 // Let's test out the variable-length ints, shall we?
David Reissce161a92007-09-11 22:09:42 +0000145 uint64_t vlq;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100146#define checkout(i, c) \
147 { \
148 buffer->resetBuffer(); \
149 proto->vlqWrite(i); \
150 proto->getTransport()->flush(); \
151 assert(my_memeq(buffer->getBufferAsString().data(), c, sizeof(c) - 1)); \
152 proto->vlqRead(vlq); \
153 assert(vlq == i); \
David Reisse67c0e62007-09-07 01:34:12 +0000154 }
155
156 checkout(0x00000000, "\x00");
157 checkout(0x00000040, "\x40");
158 checkout(0x0000007F, "\x7F");
159 checkout(0x00000080, "\x81\x00");
160 checkout(0x00002000, "\xC0\x00");
161 checkout(0x00003FFF, "\xFF\x7F");
162 checkout(0x00004000, "\x81\x80\x00");
163 checkout(0x00100000, "\xC0\x80\x00");
164 checkout(0x001FFFFF, "\xFF\xFF\x7F");
165 checkout(0x00200000, "\x81\x80\x80\x00");
166 checkout(0x08000000, "\xC0\x80\x80\x00");
167 checkout(0x0FFFFFFF, "\xFF\xFF\xFF\x7F");
168 checkout(0x10000000, "\x81\x80\x80\x80\x00");
169 checkout(0x20000000, "\x82\x80\x80\x80\x00");
170 checkout(0x1FFFFFFF, "\x81\xFF\xFF\xFF\x7F");
171 checkout(0xFFFFFFFF, "\x8F\xFF\xFF\xFF\x7F");
172
173 checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
174 checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
175 checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
176 checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
177 checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
178 checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
179 checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
180 checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
181 checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
182 checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
183
David Reissce161a92007-09-11 22:09:42 +0000184 // Test out the slow path with a TBufferedTransport.
185 shared_ptr<TBufferedTransport> buff_trans(new TBufferedTransport(buffer, 3));
186 proto.reset(new TDenseProtocol(buff_trans));
187 checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
188 checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
189 checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
190 checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
191 checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
192 checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
193 checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
194 checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
195 checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
196 checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
197
198 // Test optional stuff.
199 proto.reset(new TDenseProtocol(buffer));
200 proto->setTypeSpec(ManyOpt::local_reflection);
201 ManyOpt mo1, mo2, mo3, mo4, mo5, mo6;
202 mo1.opt1 = 923759347;
203 mo1.opt2 = 392749274;
204 mo1.opt3 = 395739402;
205 mo1.def4 = 294730928;
206 mo1.opt5 = 394309218;
207 mo1.opt6 = 832194723;
208 mo1.__isset.opt1 = true;
209 mo1.__isset.opt2 = true;
210 mo1.__isset.opt3 = true;
211 mo1.__isset.def4 = true;
212 mo1.__isset.opt5 = true;
213 mo1.__isset.opt6 = true;
214
215 mo1.write(proto.get());
216 mo2.read(proto.get());
217
218 assert(mo2.__isset.opt1 == true);
219 assert(mo2.__isset.opt2 == true);
220 assert(mo2.__isset.opt3 == true);
221 assert(mo2.__isset.def4 == true);
222 assert(mo2.__isset.opt5 == true);
223 assert(mo2.__isset.opt6 == true);
224
225 assert(mo1 == mo2);
226
227 mo1.__isset.opt1 = false;
228 mo1.__isset.opt3 = false;
229 mo1.__isset.opt5 = false;
230
231 mo1.write(proto.get());
232 mo3.read(proto.get());
233
234 assert(mo3.__isset.opt1 == false);
235 assert(mo3.__isset.opt2 == true);
236 assert(mo3.__isset.opt3 == false);
237 assert(mo3.__isset.def4 == true);
238 assert(mo3.__isset.opt5 == false);
239 assert(mo3.__isset.opt6 == true);
240
241 assert(mo1 == mo3);
242
243 mo1.__isset.opt1 = true;
244 mo1.__isset.opt3 = true;
245 mo1.__isset.opt5 = true;
246 mo1.__isset.opt2 = false;
247 mo1.__isset.opt6 = false;
248
249 mo1.write(proto.get());
250 mo4.read(proto.get());
251
252 assert(mo4.__isset.opt1 == true);
253 assert(mo4.__isset.opt2 == false);
254 assert(mo4.__isset.opt3 == true);
255 assert(mo4.__isset.def4 == true);
256 assert(mo4.__isset.opt5 == true);
257 assert(mo4.__isset.opt6 == false);
258
259 assert(mo1 == mo4);
260
261 mo1.__isset.opt1 = false;
262 mo1.__isset.opt5 = false;
263
264 mo1.write(proto.get());
265 mo5.read(proto.get());
266
267 assert(mo5.__isset.opt1 == false);
268 assert(mo5.__isset.opt2 == false);
269 assert(mo5.__isset.opt3 == true);
270 assert(mo5.__isset.def4 == true);
271 assert(mo5.__isset.opt5 == false);
272 assert(mo5.__isset.opt6 == false);
273
274 assert(mo1 == mo5);
275
276 mo1.__isset.opt3 = false;
277
278 mo1.write(proto.get());
279 mo6.read(proto.get());
280
281 assert(mo6.__isset.opt1 == false);
282 assert(mo6.__isset.opt2 == false);
283 assert(mo6.__isset.opt3 == false);
284 assert(mo6.__isset.def4 == true);
285 assert(mo6.__isset.opt5 == false);
286 assert(mo6.__isset.opt6 == false);
287
288 assert(mo1 == mo6);
289
David Reissce161a92007-09-11 22:09:42 +0000290 // Test fingerprint checking stuff.
291
292 {
293 // Default and required have the same fingerprint.
294 Tricky1 t1;
295 Tricky3 t3;
296 assert(string(Tricky1::ascii_fingerprint) == Tricky3::ascii_fingerprint);
297 proto->setTypeSpec(Tricky1::local_reflection);
298 t1.im_default = 227;
299 t1.write(proto.get());
300 proto->setTypeSpec(Tricky3::local_reflection);
301 t3.read(proto.get());
302 assert(t3.im_required == 227);
303 }
304
305 {
306 // Optional changes things.
307 Tricky1 t1;
308 Tricky2 t2;
309 assert(string(Tricky1::ascii_fingerprint) != Tricky2::ascii_fingerprint);
310 proto->setTypeSpec(Tricky1::local_reflection);
311 t1.im_default = 227;
312 t1.write(proto.get());
313 try {
314 proto->setTypeSpec(Tricky2::local_reflection);
315 t2.read(proto.get());
316 assert(false);
317 } catch (TProtocolException& ex) {
318 buffer->resetBuffer();
319 }
320 }
321
322 {
323 // Holy cow. We can use the Tricky1 typespec with the Tricky2 structure.
324 Tricky1 t1;
325 Tricky2 t2;
326 proto->setTypeSpec(Tricky1::local_reflection);
327 t1.im_default = 227;
328 t1.write(proto.get());
329 t2.read(proto.get());
330 assert(t2.__isset.im_optional == true);
331 assert(t2.im_optional == 227);
332 }
333
334 {
335 // And totally off the wall.
336 Tricky1 t1;
337 OneOfEach ooe2;
338 assert(string(Tricky1::ascii_fingerprint) != OneOfEach::ascii_fingerprint);
339 proto->setTypeSpec(Tricky1::local_reflection);
340 t1.im_default = 227;
341 t1.write(proto.get());
342 try {
343 proto->setTypeSpec(OneOfEach::local_reflection);
344 ooe2.read(proto.get());
345 assert(false);
346 } catch (TProtocolException& ex) {
347 buffer->resetBuffer();
348 }
349 }
350
351 // Okay, this is really off the wall.
352 // Just don't crash.
353 cout << "Starting fuzz test. This takes a while. (20 dots.)" << endl;
354 std::srand(12345);
355 for (int i = 0; i < 2000; i++) {
356 if (i % 100 == 0) {
357 cout << ".";
358 cout.flush();
359 }
360 buffer->resetBuffer();
361 // Make sure the fingerprint prefix is right.
362 buffer->write(Nesting::binary_fingerprint, 4);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100363 for (int j = 0; j < 1024 * 1024; j++) {
David Reissce161a92007-09-11 22:09:42 +0000364 uint8_t r = std::rand();
365 buffer->write(&r, 1);
366 }
367 Nesting n;
368 proto->setTypeSpec(OneOfEach::local_reflection);
369 try {
370 n.read(proto.get());
371 } catch (TProtocolException& ex) {
372 } catch (TTransportException& ex) {
373 }
374 }
375 cout << endl;
David Reisse67c0e62007-09-07 01:34:12 +0000376
David Reiss4e7530d2007-09-04 21:49:53 +0000377 return 0;
378}