/* * 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. */ #ifndef _THRIFT_TEST_GENERICPROTOCOLTEST_TCC_ #define _THRIFT_TEST_GENERICPROTOCOLTEST_TCC_ 1 #include #include #include #include #include "GenericHelpers.h" using boost::shared_ptr; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; #define ERR_LEN 512 extern char errorMessage[ERR_LEN]; template void testNaked(Val val) { shared_ptr transport(new TMemoryBuffer()); shared_ptr protocol(new TProto(transport)); GenericIO::write(protocol, val); Val out; GenericIO::read(protocol, out); if (out != val) { snprintf(errorMessage, ERR_LEN, "Invalid naked test (type: %s)", ClassNames::getName()); throw TException(errorMessage); } } template void testField(const Val val) { shared_ptr transport(new TMemoryBuffer()); shared_ptr protocol(new TProto(transport)); protocol->writeStructBegin("test_struct"); protocol->writeFieldBegin("test_field", type, (int16_t)15); GenericIO::write(protocol, val); protocol->writeFieldEnd(); protocol->writeStructEnd(); std::string name; TType fieldType; int16_t fieldId; protocol->readStructBegin(name); protocol->readFieldBegin(name, fieldType, fieldId); if (fieldId != 15) { snprintf(errorMessage, ERR_LEN, "Invalid ID (type: %s)", typeid(val).name()); throw TException(errorMessage); } if (fieldType != type) { snprintf(errorMessage, ERR_LEN, "Invalid Field Type (type: %s)", typeid(val).name()); throw TException(errorMessage); } Val out; GenericIO::read(protocol, out); if (out != val) { snprintf(errorMessage, ERR_LEN, "Invalid value read (type: %s)", typeid(val).name()); throw TException(errorMessage); } protocol->readFieldEnd(); protocol->readStructEnd(); } template void testMessage() { struct TMessage { const char* name; TMessageType type; int32_t seqid; } messages[4] = { {"short message name", T_CALL, 0}, {"1", T_REPLY, 12345}, {"loooooooooooooooooooooooooooooooooong", T_EXCEPTION, 1 << 16}, {"Janky", T_CALL, 0} }; for (int i = 0; i < 4; i++) { shared_ptr transport(new TMemoryBuffer()); shared_ptr protocol(new TProto(transport)); protocol->writeMessageBegin(messages[i].name, messages[i].type, messages[i].seqid); protocol->writeMessageEnd(); std::string name; TMessageType type; int32_t seqid; protocol->readMessageBegin(name, type, seqid); if (name != messages[i].name || type != messages[i].type || seqid != messages[i].seqid) { throw TException("readMessageBegin failed."); } } } template void testProtocol(const char* protoname) { try { testNaked((int8_t)123); for (int32_t i = 0; i < 128; i++) { testField((int8_t)i); testField((int8_t)-i); } testNaked((int16_t)0); testNaked((int16_t)1); testNaked((int16_t)15000); testNaked((int16_t)0x7fff); testNaked((int16_t)-1); testNaked((int16_t)-15000); testNaked((int16_t)-0x7fff); testNaked(std::numeric_limits::min()); testNaked(std::numeric_limits::max()); testField((int16_t)0); testField((int16_t)1); testField((int16_t)7); testField((int16_t)150); testField((int16_t)15000); testField((int16_t)0x7fff); testField((int16_t)-1); testField((int16_t)-7); testField((int16_t)-150); testField((int16_t)-15000); testField((int16_t)-0x7fff); testNaked(0); testNaked(1); testNaked(15000); testNaked(0xffff); testNaked(-1); testNaked(-15000); testNaked(-0xffff); testNaked(std::numeric_limits::min()); testNaked(std::numeric_limits::max()); testField(0); testField(1); testField(7); testField(150); testField(15000); testField(31337); testField(0xffff); testField(0xffffff); testField(-1); testField(-7); testField(-150); testField(-15000); testField(-0xffff); testField(-0xffffff); testNaked(std::numeric_limits::min()); testNaked(std::numeric_limits::max()); testNaked(std::numeric_limits::min() + 10); testNaked(std::numeric_limits::max() - 16); testNaked(std::numeric_limits::min()); testNaked(std::numeric_limits::max()); testNaked(0); for (int64_t i = 0; i < 62; i++) { testNaked(1L << i); testNaked(-(1L << i)); } testField(0); for (int i = 0; i < 62; i++) { testField(1L << i); testField(-(1L << i)); } testNaked(123.456); testNaked(""); testNaked("short"); testNaked("borderlinetiny"); testNaked("a bit longer than the smallest possible"); testNaked("\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA"); //kinda binary test testField(""); testField("short"); testField("borderlinetiny"); testField("a bit longer than the smallest possible"); testMessage(); printf("%s => OK\n", protoname); } catch (TException e) { snprintf(errorMessage, ERR_LEN, "%s => Test FAILED: %s", protoname, e.what()); throw TException(errorMessage); } } #endif