David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 1 | /* |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
| 20 | /* |
David Reiss | b139f64 | 2009-02-17 20:28:46 +0000 | [diff] [blame] | 21 | ../compiler/cpp/thrift --gen cpp:dense DebugProtoTest.thrift |
| 22 | ../compiler/cpp/thrift --gen cpp:dense OptionalRequiredTest.thrift |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 23 | g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \ |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 24 | gen-cpp/OptionalRequiredTest_types.cpp \ |
| 25 | gen-cpp/DebugProtoTest_types.cpp \ |
| 26 | DenseProtoTest.cpp ../lib/cpp/.libs/libthrift.a -o DenseProtoTest |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 27 | ./DenseProtoTest |
| 28 | */ |
| 29 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 30 | // I do this to reach into the guts of TDenseProtocol. Sorry. |
| 31 | #define private public |
| 32 | #define inline |
| 33 | |
| 34 | #undef NDEBUG |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 35 | #include <cstdlib> |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 36 | #include <cassert> |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 37 | #include <cmath> |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 38 | #include <string> |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 39 | #include "gen-cpp/DebugProtoTest_types.h" |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 40 | #include "gen-cpp/OptionalRequiredTest_types.h" |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 41 | #include <thrift/protocol/TDenseProtocol.h> |
| 42 | #include <thrift/transport/TBufferTransports.h> |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 43 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 44 | #define BOOST_TEST_MODULE DenseProtoTest |
| 45 | #include <boost/test/unit_test.hpp> |
| 46 | |
| 47 | using std::string; |
| 48 | using boost::shared_ptr; |
| 49 | using namespace thrift::test; |
| 50 | using namespace thrift::test::debug; |
| 51 | using namespace apache::thrift::transport; |
| 52 | using namespace apache::thrift::protocol; |
| 53 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 54 | // Can't use memcmp here. GCC is too smart. |
| 55 | bool 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(test_dense_proto_1) { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 65 | OneOfEach ooe; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 66 | ooe.im_true = true; |
| 67 | ooe.im_false = false; |
| 68 | ooe.a_bite = 0xd6; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 69 | ooe.integer16 = 27000; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | ooe.integer32 = 1 << 24; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 71 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 72 | ooe.double_precision = M_PI; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 73 | ooe.some_characters = "Debug THIS!"; |
| 74 | ooe.zomg_unicode = "\xd7\n\a\t"; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 75 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | // cout << apache::thrift::ThriftDebugString(ooe) << endl << endl; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 77 | |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | n.my_ooe.double_precision = (std::sqrt(5) + 1) / 2; |
| 84 | n.my_ooe.some_characters = ":R (me going \"rrrr\")"; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 85 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 88 | n.my_bonk.type = 31337; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 89 | n.my_bonk.message = "I am a bonk... xor!"; |
| 90 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 91 | // cout << apache::thrift::ThriftDebugString(n) << endl << endl; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 92 | |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | stage2.resize(stage2.size() + 1); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 115 | stage2.back().type = 1; |
| 116 | stage2.back().message = "Wait."; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 117 | stage2.resize(stage2.size() + 1); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 118 | stage2.back().type = 2; |
| 119 | stage2.back().message = "What?"; |
| 120 | hm.bonks["something"] = stage2; |
| 121 | stage2.clear(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 122 | stage2.resize(stage2.size() + 1); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 123 | stage2.back().type = 3; |
| 124 | stage2.back().message = "quoth"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | stage2.resize(stage2.size() + 1); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 126 | stage2.back().type = 4; |
| 127 | stage2.back().message = "the raven"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | stage2.resize(stage2.size() + 1); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 129 | stage2.back().type = 5; |
| 130 | stage2.back().message = "nevermore"; |
| 131 | hm.bonks["poe"] = stage2; |
| 132 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 133 | // cout << apache::thrift::ThriftDebugString(hm) << endl << endl; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 134 | |
| 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 143 | BOOST_CHECK(hm == hm2); |
| 144 | } |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 145 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 146 | /* |
| 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 Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 162 | BOOST_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 Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 169 | 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 196 | } |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 197 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(test_dense_proto_3) { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 199 | // Test out the slow path with a TBufferedTransport. |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 200 | shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 201 | shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer)); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 202 | shared_ptr<TBufferedTransport> buff_trans(new TBufferedTransport(buffer, 3)); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 203 | |
| 204 | proto->setTypeSpec(HolyMoley::local_reflection); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 205 | proto.reset(new TDenseProtocol(buff_trans)); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 206 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 207 | 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 217 | } |
| 218 | #endif |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 219 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 220 | // Test optional stuff. |
| 221 | BOOST_AUTO_TEST_CASE(test_dense_proto_4_1) { |
| 222 | shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 223 | shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer)); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 224 | proto.reset(new TDenseProtocol(buffer)); |
| 225 | proto->setTypeSpec(ManyOpt::local_reflection); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 226 | |
| 227 | ManyOpt mo1, mo2; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 228 | 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 Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 244 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 250 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 251 | BOOST_CHECK(mo1 == mo2); |
| 252 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 253 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 254 | BOOST_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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 267 | mo1.__isset.opt1 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 268 | mo1.__isset.opt2 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 269 | mo1.__isset.opt3 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 270 | mo1.__isset.def4 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 271 | mo1.__isset.opt5 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 272 | mo1.__isset.opt6 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 273 | |
| 274 | mo1.write(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 275 | mo2.read(proto.get()); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 276 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 277 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 283 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 284 | BOOST_CHECK(mo1 == mo2); |
| 285 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 286 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 287 | BOOST_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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 300 | mo1.__isset.opt1 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 301 | mo1.__isset.opt2 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 302 | mo1.__isset.opt3 = true; |
| 303 | mo1.__isset.def4 = true; |
| 304 | mo1.__isset.opt5 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 305 | mo1.__isset.opt6 = false; |
| 306 | |
| 307 | mo1.write(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 308 | mo2.read(proto.get()); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 309 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 310 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 316 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 317 | BOOST_CHECK(mo1 == mo2); |
| 318 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 319 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 320 | BOOST_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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 333 | mo1.__isset.opt1 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 334 | mo1.__isset.opt2 = false; |
| 335 | mo1.__isset.opt3 = true; |
| 336 | mo1.__isset.def4 = true; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 337 | mo1.__isset.opt5 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 338 | mo1.__isset.opt6 = false; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 339 | |
| 340 | mo1.write(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 341 | mo2.read(proto.get()); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 342 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 343 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 349 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 350 | BOOST_CHECK(mo1 == mo2); |
| 351 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 352 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 353 | BOOST_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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 368 | mo1.__isset.opt3 = false; |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 369 | mo1.__isset.def4 = true; |
| 370 | mo1.__isset.opt5 = false; |
| 371 | mo1.__isset.opt6 = false; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 372 | |
| 373 | mo1.write(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 374 | mo2.read(proto.get()); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 375 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 376 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 382 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 383 | BOOST_CHECK(mo1 == mo2); |
| 384 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 385 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 386 | // Test fingerprint checking stuff. |
| 387 | BOOST_AUTO_TEST_CASE(test_dense_proto_5_1) { |
| 388 | shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 389 | shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer)); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 390 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 391 | // 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 402 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 403 | BOOST_AUTO_TEST_CASE(test_dense_proto_5_2) { |
| 404 | shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 405 | shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer)); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 406 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 407 | // 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 416 | t2.read(proto.get()); |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 417 | BOOST_CHECK(false); |
| 418 | } catch (TProtocolException& ex) { |
| 419 | buffer->resetBuffer(); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 420 | } |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 421 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 422 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 423 | BOOST_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 | |
| 438 | BOOST_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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 455 | } |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 456 | } |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 457 | |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 458 | BOOST_AUTO_TEST_CASE(test_dense_proto_6) { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 459 | // Okay, this is really off the wall. |
| 460 | // Just don't crash. |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 461 | 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 Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 465 | std::srand(12345); |
| 466 | for (int i = 0; i < 2000; i++) { |
| 467 | if (i % 100 == 0) { |
Claudius Heine | 5ef662b | 2015-06-24 10:03:50 +0200 | [diff] [blame] | 468 | BOOST_TEST_MESSAGE("Do " << i / 100 << "/" << 2000 / 100); |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 469 | } |
| 470 | buffer->resetBuffer(); |
| 471 | // Make sure the fingerprint prefix is right. |
| 472 | buffer->write(Nesting::binary_fingerprint, 4); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 473 | for (int j = 0; j < 1024 * 1024; j++) { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 474 | 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 Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 485 | } |