void RequiredFieldsTest(const From& from) { typename Writer::Buffer buffer(1024); Writer writer(buffer); bond::Serialize(from, writer); { To to; Reader reader(buffer.GetBuffer()); bond::bonded<From, Reader&> bonded(reader); bonded.Deserialize(to); UT_AssertIsTrue(Equal(from, to)); } { To to; Reader reader(buffer.GetBuffer()); bond::bonded<void, Reader&> bonded(reader, bond::GetRuntimeSchema<From>()); bonded.Deserialize(to); UT_AssertIsTrue(Equal(from, to)); } }
bond::blob Marshal(const T& obj) { typename Writer::Buffer buffer; Writer writer(buffer); bond::Marshal(obj, writer); return buffer.GetBuffer(); }
bond::blob Serialize(const T& obj, Param param) { typename Writer::Buffer buffer; Writer writer(buffer, param); bond::Serialize(obj, writer); return buffer.GetBuffer(); }
static bond::blob PartialField() { // Payload indicates a field of type T and id 2 and then ends. // Attempt to de-serialize or skip the field leads to Eof exception. typename Writer::Buffer buffer; Writer writer(buffer); writer.WriteStructBegin(bond::Metadata(), false); writer.WriteFieldBegin(bond::get_type_id<T>::value, 2); return buffer.GetBuffer(); }
static bond::blob OomField() { // Payload indicates a container field with id 2 and _UI32_MAX elements and then ends. // Attempt to de-serialize leads to OOM exception. typename Writer::Buffer buffer; Writer writer(buffer); writer.WriteStructBegin(bond::Metadata(), false); writer.WriteFieldBegin(bond::get_type_id<T>::value, 2); writer.WriteContainerBegin(0xffffffff, bond::get_type_id<typename bond::element_type<T>::type>::value); return buffer.GetBuffer(); }
void MarshalingTest(uint16_t version = bond::v1) { T from = InitRandom<T>(); typename Writer::Buffer output_buffer; Factory<Writer>::Call(output_buffer, version, boost::bind( bond::Marshal<bond::BuiltInProtocols, T, Writer>, from, _1)); T to; bond::InputBuffer input(output_buffer.GetBuffer()); bond::Unmarshal(input, to); UT_Compare(from, to); auto to2 = bond::Unmarshal<T>(input); UT_Compare(from, to2); }
void TestEncoding(const std::vector<T>& data) { typename Writer::Buffer output_buffer; Writer output(output_buffer); for(size_t i = 0; i < data.size(); ++i) { output.Write(data[i]); } typename Reader::Buffer input_buffer(output_buffer.GetBuffer()); Reader input(input_buffer); for(size_t i = 0; i < data.size(); ++i) { T value; input.Read(value); UT_AssertIsTrue(data[i] == value); } }
void TestEncodingInteger() { typename Writer::Buffer output_buffer; Writer output(output_buffer); for(uint64_t i = 0; i <= typename boost::make_unsigned<T>::type(-1); ++i) { output.Write(T(i)); } typename Reader::Buffer input_buffer(output_buffer.GetBuffer()); Reader input(input_buffer); for(uint64_t i = 0; i <= typename boost::make_unsigned<T>::type(-1); ++i) { T value; input.Read(value); UT_AssertAreEqual(T(i), value); } }
void MissingRequiredFieldsTest(const From& from) { typename Writer::Buffer buffer(1024); Writer writer(buffer); bond::Serialize(from, writer); { To to; Reader reader(buffer.GetBuffer()); bond::bonded<From, Reader&> bonded(reader); UT_AssertThrows((bonded.Deserialize(to)), bond::CoreException); } { To to; Reader reader(buffer.GetBuffer()); bond::bonded<void, Reader&> bonded(reader, bond::GetRuntimeSchema<From>()); UT_AssertThrows((bonded.Deserialize(to)), bond::CoreException); } }
void operator()(const T&) { typename Writer::Buffer output_buffer; Writer output(output_buffer); for(uint16_t i = 0; i < USHRT_MAX/2; ++i) { output.WriteFieldBegin(bond::get_type_id<T>::value, i); } for(uint16_t i = USHRT_MAX; i >= USHRT_MAX/2; i -= 3) { output.WriteFieldBegin(bond::get_type_id<T>::value, i); } typename Reader::Buffer input_buffer(output_buffer.GetBuffer()); Reader input(input_buffer); uint16_t id; bond::BondDataType type; for(uint16_t i = 0; i < USHRT_MAX/2; ++i) { input.ReadFieldBegin(type, id); UT_AssertAreEqual(bond::get_type_id<T>::value, type); UT_AssertAreEqual(i, id); } for(uint16_t i = USHRT_MAX; i >= USHRT_MAX/2; i -= 3) { input.ReadFieldBegin(type, id); UT_AssertAreEqual(bond::get_type_id<T>::value, type); UT_AssertAreEqual(i, id); } }
void TranscodingTest(uint16_t version = bond::v1) { T from = InitRandom<T>(); typename Writer::Buffer output_buffer; Factory<Writer>::Call(output_buffer, version, boost::bind( bond::Marshal<bond::BuiltInProtocols, T, Writer>, from, _1)); // Trans-marshal to Simple Protocol using runtime schema bond::OutputBuffer simple_buffer; bond::SimpleBinaryWriter<bond::OutputBuffer> simple_writer(simple_buffer, Writer::Reader::version); { bond::InputBuffer input(output_buffer.GetBuffer()); bond::ProtocolType proto = bond::SelectProtocolAndApply(bond::GetRuntimeSchema<T>(), input, bond::MarshalTo(simple_writer)).first; UT_AssertAreEqual(proto, Writer::Reader::magic); } { // Trans-marshal back to Writer using compile-time schema typename Writer::Buffer writer_buffer; { bond::InputBuffer input(simple_buffer.GetBuffer()); Factory<Writer>::Call(writer_buffer, version, boost::bind( bond::SelectProtocolAndApply<T, bond::BuiltInProtocols, bond::InputBuffer, bond::Marshaler<Writer> >, input, boost::bind(bond::MarshalTo<bond::BuiltInProtocols, Writer>, _1))); } T to; bond::InputBuffer input(writer_buffer.GetBuffer()); Unmarshal(input, to); UT_Compare(from, to); } { // Trans-marshal back to Writer using runtime schema typename Writer::Buffer writer_buffer; { bond::InputBuffer input(simple_buffer.GetBuffer()); Factory<Writer>::Call(writer_buffer, version, boost::bind( bond::SelectProtocolAndApply<bond::BuiltInProtocols, bond::InputBuffer, bond::Marshaler<Writer> >, bond::GetRuntimeSchema<T>(), input, boost::bind(bond::MarshalTo<bond::BuiltInProtocols, Writer>, _1))); } T to; bond::InputBuffer input(writer_buffer.GetBuffer()); Unmarshal(input, to); UT_Compare(from, to); } }