Thrift: Clean up and test TDenseProtocol
Summary:
- TDenseProtocol now includes a part of the struct fingerprint in
the serialized message, to protect from unserialzing trash.
- A lot of cleanups and commenting for TDenseProtocol.
- A lot of test cases for same.
Reviewed By: mcslee
Test Plan: test/DenseProtoTest.cpp
Revert Plan: ok
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665257 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/DenseProtoTest.cpp b/test/DenseProtoTest.cpp
index 0953c19..f50faca 100644
--- a/test/DenseProtoTest.cpp
+++ b/test/DenseProtoTest.cpp
@@ -1,8 +1,10 @@
/*
../compiler/cpp/thrift -cpp -dense DebugProtoTest.thrift
+../compiler/cpp/thrift -cpp -dense OptionalRequiredTest.thrift
g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
- DenseProtoTest.cpp gen-cpp/DebugProtoTest_types.cpp \
- ../lib/cpp/.libs/libthrift.a -o DenseProtoTest
+ gen-cpp/OptionalRequiredTest_types.cpp \
+ gen-cpp/DebugProtoTest_types.cpp \
+ DenseProtoTest.cpp ../lib/cpp/.libs/libthrift.a -o DenseProtoTest
./DenseProtoTest
*/
@@ -11,10 +13,13 @@
#define inline
#undef NDEBUG
+#include <cstdlib>
#include <cassert>
#include <iostream>
#include <cmath>
+#include <string>
#include "gen-cpp/DebugProtoTest_types.h"
+#include "gen-cpp/OptionalRequiredTest_types.h"
#include <protocol/TDenseProtocol.h>
#include <transport/TTransportUtils.h>
@@ -31,6 +36,7 @@
int main() {
+ using std::string;
using std::cout;
using std::endl;
using boost::shared_ptr;
@@ -123,13 +129,14 @@
// Let's test out the variable-length ints, shall we?
- uint64_t vli;
+ uint64_t vlq;
#define checkout(i, c) { \
buffer->resetBuffer(); \
- proto->vliWrite(i); \
+ proto->vlqWrite(i); \
+ proto->getTransport()->flush(); \
assert(my_memeq(buffer->getBufferAsString().data(), c, sizeof(c)-1)); \
- proto->vliRead(vli); \
- assert(vli == i); \
+ proto->vlqRead(vlq); \
+ assert(vlq == i); \
}
checkout(0x00000000, "\x00");
@@ -160,6 +167,199 @@
checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+ // Test out the slow path with a TBufferedTransport.
+ shared_ptr<TBufferedTransport> buff_trans(new TBufferedTransport(buffer, 3));
+ proto.reset(new TDenseProtocol(buff_trans));
+ checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
+ checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
+ checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
+ checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
+ checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
+ checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
+ checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
+ checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+ checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+ checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
+
+ // Test optional stuff.
+ proto.reset(new TDenseProtocol(buffer));
+ proto->setTypeSpec(ManyOpt::local_reflection);
+ ManyOpt mo1, mo2, mo3, mo4, mo5, mo6;
+ mo1.opt1 = 923759347;
+ mo1.opt2 = 392749274;
+ mo1.opt3 = 395739402;
+ mo1.def4 = 294730928;
+ mo1.opt5 = 394309218;
+ mo1.opt6 = 832194723;
+ mo1.__isset.opt1 = true;
+ mo1.__isset.opt2 = true;
+ mo1.__isset.opt3 = true;
+ mo1.__isset.def4 = true;
+ mo1.__isset.opt5 = true;
+ mo1.__isset.opt6 = true;
+
+ mo1.write(proto.get());
+ mo2.read(proto.get());
+
+ assert(mo2.__isset.opt1 == true);
+ assert(mo2.__isset.opt2 == true);
+ assert(mo2.__isset.opt3 == true);
+ assert(mo2.__isset.def4 == true);
+ assert(mo2.__isset.opt5 == true);
+ assert(mo2.__isset.opt6 == true);
+
+ assert(mo1 == mo2);
+
+ mo1.__isset.opt1 = false;
+ mo1.__isset.opt3 = false;
+ mo1.__isset.opt5 = false;
+
+ mo1.write(proto.get());
+ mo3.read(proto.get());
+
+ assert(mo3.__isset.opt1 == false);
+ assert(mo3.__isset.opt2 == true);
+ assert(mo3.__isset.opt3 == false);
+ assert(mo3.__isset.def4 == true);
+ assert(mo3.__isset.opt5 == false);
+ assert(mo3.__isset.opt6 == true);
+
+ assert(mo1 == mo3);
+
+ mo1.__isset.opt1 = true;
+ mo1.__isset.opt3 = true;
+ mo1.__isset.opt5 = true;
+ mo1.__isset.opt2 = false;
+ mo1.__isset.opt6 = false;
+
+ mo1.write(proto.get());
+ mo4.read(proto.get());
+
+ assert(mo4.__isset.opt1 == true);
+ assert(mo4.__isset.opt2 == false);
+ assert(mo4.__isset.opt3 == true);
+ assert(mo4.__isset.def4 == true);
+ assert(mo4.__isset.opt5 == true);
+ assert(mo4.__isset.opt6 == false);
+
+ assert(mo1 == mo4);
+
+ mo1.__isset.opt1 = false;
+ mo1.__isset.opt5 = false;
+
+ mo1.write(proto.get());
+ mo5.read(proto.get());
+
+ assert(mo5.__isset.opt1 == false);
+ assert(mo5.__isset.opt2 == false);
+ assert(mo5.__isset.opt3 == true);
+ assert(mo5.__isset.def4 == true);
+ assert(mo5.__isset.opt5 == false);
+ assert(mo5.__isset.opt6 == false);
+
+ assert(mo1 == mo5);
+
+ mo1.__isset.opt3 = false;
+
+ mo1.write(proto.get());
+ mo6.read(proto.get());
+
+ assert(mo6.__isset.opt1 == false);
+ assert(mo6.__isset.opt2 == false);
+ assert(mo6.__isset.opt3 == false);
+ assert(mo6.__isset.def4 == true);
+ assert(mo6.__isset.opt5 == false);
+ assert(mo6.__isset.opt6 == false);
+
+ assert(mo1 == mo6);
+
+
+ // Test fingerprint checking stuff.
+
+ {
+ // Default and required have the same fingerprint.
+ Tricky1 t1;
+ Tricky3 t3;
+ assert(string(Tricky1::ascii_fingerprint) == Tricky3::ascii_fingerprint);
+ proto->setTypeSpec(Tricky1::local_reflection);
+ t1.im_default = 227;
+ t1.write(proto.get());
+ proto->setTypeSpec(Tricky3::local_reflection);
+ t3.read(proto.get());
+ assert(t3.im_required == 227);
+ }
+
+ {
+ // Optional changes things.
+ Tricky1 t1;
+ Tricky2 t2;
+ assert(string(Tricky1::ascii_fingerprint) != Tricky2::ascii_fingerprint);
+ proto->setTypeSpec(Tricky1::local_reflection);
+ t1.im_default = 227;
+ t1.write(proto.get());
+ try {
+ proto->setTypeSpec(Tricky2::local_reflection);
+ t2.read(proto.get());
+ assert(false);
+ } catch (TProtocolException& ex) {
+ buffer->resetBuffer();
+ }
+ }
+
+ {
+ // Holy cow. We can use the Tricky1 typespec with the Tricky2 structure.
+ Tricky1 t1;
+ Tricky2 t2;
+ proto->setTypeSpec(Tricky1::local_reflection);
+ t1.im_default = 227;
+ t1.write(proto.get());
+ t2.read(proto.get());
+ assert(t2.__isset.im_optional == true);
+ assert(t2.im_optional == 227);
+ }
+
+ {
+ // And totally off the wall.
+ Tricky1 t1;
+ OneOfEach ooe2;
+ assert(string(Tricky1::ascii_fingerprint) != OneOfEach::ascii_fingerprint);
+ proto->setTypeSpec(Tricky1::local_reflection);
+ t1.im_default = 227;
+ t1.write(proto.get());
+ try {
+ proto->setTypeSpec(OneOfEach::local_reflection);
+ ooe2.read(proto.get());
+ assert(false);
+ } catch (TProtocolException& ex) {
+ buffer->resetBuffer();
+ }
+ }
+
+ // Okay, this is really off the wall.
+ // Just don't crash.
+ cout << "Starting fuzz test. This takes a while. (20 dots.)" << endl;
+ std::srand(12345);
+ for (int i = 0; i < 2000; i++) {
+ if (i % 100 == 0) {
+ cout << ".";
+ cout.flush();
+ }
+ buffer->resetBuffer();
+ // Make sure the fingerprint prefix is right.
+ buffer->write(Nesting::binary_fingerprint, 4);
+ for (int j = 0; j < 1024*1024; j++) {
+ uint8_t r = std::rand();
+ buffer->write(&r, 1);
+ }
+ Nesting n;
+ proto->setTypeSpec(OneOfEach::local_reflection);
+ try {
+ n.read(proto.get());
+ } catch (TProtocolException& ex) {
+ } catch (TTransportException& ex) {
+ }
+ }
+ cout << endl;
return 0;
}
diff --git a/test/OptionalRequiredTest.thrift b/test/OptionalRequiredTest.thrift
index b2652dd..431a0b0 100644
--- a/test/OptionalRequiredTest.thrift
+++ b/test/OptionalRequiredTest.thrift
@@ -40,3 +40,12 @@
5: required Simple req_simp;
6: optional Simple opt_simp;
}
+
+struct ManyOpt {
+ 1: optional i32 opt1;
+ 2: optional i32 opt2;
+ 3: optional i32 opt3;
+ 4: i32 def4;
+ 5: optional i32 opt5;
+ 6: optional i32 opt6;
+}