From c3e9a57a6fff08640f3ad05e8df971d5ddb37d51 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 18 Jun 2015 12:20:33 -0700 Subject: Fixed some bad bugs in upb_env. Also added a unit test for upb_encoder that demonstrates the bugs and the fix. --- tests/pb/test_encoder.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/pb/test_encoder.cc (limited to 'tests/pb/test_encoder.cc') diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc new file mode 100644 index 0000000..8a222f4 --- /dev/null +++ b/tests/pb/test_encoder.cc @@ -0,0 +1,52 @@ + +#include "tests/upb_test.h" +#include "upb/bindings/stdc++/string.h" +#include "upb/descriptor/descriptor.upb.h" +#include "upb/pb/decoder.h" +#include "upb/pb/encoder.h" +#include "upb/pb/glue.h" + +std::string read_string(const char *filename) { + size_t len; + char *str = upb_readfile(filename, &len); + ASSERT(str); + if (!str) { return std::string(); } + std::string ret = std::string(str, len); + free(str); + return ret; +} + +void test_pb_roundtrip() { + upb::reffed_ptr md( + upbdefs::google::protobuf::FileDescriptorSet::MessageDef()); + upb::reffed_ptr encoder_handlers( + upb::pb::Encoder::NewHandlers(md.get())); + upb::reffed_ptr method( + upb::pb::DecoderMethod::New( + upb::pb::DecoderMethodOptions(encoder_handlers.get()))); + + char buf[512]; + upb::SeededAllocator alloc(buf, sizeof(buf)); + upb::Environment env; + env.SetAllocator(&alloc); + std::string input = read_string("upb/descriptor/descriptor.pb"); + std::string output; + upb::StringSink string_sink(&output); + upb::pb::Encoder* encoder = + upb::pb::Encoder::Create(&env, encoder_handlers.get(), + string_sink.input()); + upb::pb::Decoder* decoder = + upb::pb::Decoder::Create(&env, method.get(), encoder->input()); + bool ok = upb::BufferSource::PutBuffer(input, decoder->input()); + ASSERT(ok); + ASSERT(input == output); +} + +extern "C" { +int run_tests(int argc, char *argv[]) { + UPB_UNUSED(argc); + UPB_UNUSED(argv); + test_pb_roundtrip(); + return 0; +} +} -- cgit v1.2.3