diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bindings/googlepb/test_vs_proto2.cc | 9 | ||||
-rw-r--r-- | tests/json/test_json.cc | 12 | ||||
-rw-r--r-- | tests/pb/test_decoder.cc | 51 | ||||
-rw-r--r-- | tests/test_cpp.cc | 1 |
4 files changed, 45 insertions, 28 deletions
diff --git a/tests/bindings/googlepb/test_vs_proto2.cc b/tests/bindings/googlepb/test_vs_proto2.cc index 49dfe49..8e68791 100644 --- a/tests/bindings/googlepb/test_vs_proto2.cc +++ b/tests/bindings/googlepb/test_vs_proto2.cc @@ -77,13 +77,14 @@ void parse_and_compare(google::protobuf::Message *msg1, cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(protomsg_handlers))); upb::Status status; - upb::pb::Decoder decoder(decoder_method.get(), &status); + upb::Environment env; + env.ReportErrorsTo(&status); upb::Sink protomsg_sink(protomsg_handlers, msg2); - - decoder.ResetOutput(&protomsg_sink); + upb::pb::Decoder* decoder = + upb::pb::Decoder::Create(&env, decoder_method.get(), &protomsg_sink); msg2->Clear(); - bool ok = upb::BufferSource::PutBuffer(str, len, decoder.input()); + bool ok = upb::BufferSource::PutBuffer(str, len, decoder->input()); if (!ok) { fprintf(stderr, "error parsing: %s\n", status.error_message()); print_diff(*msg1, *msg2); diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 828e603..cb60bad 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -292,14 +292,14 @@ void test_json_roundtrip_message(const char* json_src, const upb::Handlers* serialize_handlers, int seam) { upb::Status st; - upb::json::Parser parser(&st); - upb::json::Printer printer(serialize_handlers); + upb::Environment env; + env.ReportErrorsTo(&st); StringSink data_sink; + upb::json::Printer* printer = + upb::json::Printer::Create(&env, serialize_handlers, data_sink.Sink()); + upb::json::Parser* parser = upb::json::Parser::Create(&env, printer->input()); - parser.ResetOutput(printer.input()); - printer.ResetOutput(data_sink.Sink()); - - upb::BytesSink* input = parser.input(); + upb::BytesSink* input = parser->input(); void *sub; size_t len = strlen(json_src); size_t ofs = 0; diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 3aea777..98926a6 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -64,6 +64,8 @@ (float)completed * 100 / total); \ } +#define MAX_NESTING 64 + uint32_t filter_hash = 0; double completed; double total; @@ -210,7 +212,7 @@ string submsg(uint32_t fn, const string& buf) { // using the closure depth to test that the stack of closures is properly // handled. -int closures[UPB_DECODER_MAX_NESTING]; +int closures[MAX_NESTING]; string output; void indentbuf(string *buf, int depth) { @@ -508,6 +510,15 @@ upb::reffed_ptr<const upb::Handlers> NewHandlers(TestMode mode) { const upb::Handlers *global_handlers; const upb::pb::DecoderMethod *global_method; +upb::pb::Decoder* CreateDecoder(upb::Environment* env, + const upb::pb::DecoderMethod* method, + upb::Sink* sink) { + upb::pb::Decoder *ret = upb::pb::Decoder::Create(env, method, sink); + ASSERT(ret != NULL); + ret->set_max_nesting(MAX_NESTING); + return ret; +} + uint32_t Hash(const string& proto, const string* expected_output, size_t seam1, size_t seam2) { uint32_t hash = MurmurHash2(proto.c_str(), proto.size(), 0); @@ -545,19 +556,21 @@ static bool parse(upb::pb::Decoder* decoder, void* subc, const char* buf, #define LINE(x) x "\n" void run_decoder(const string& proto, const string* expected_output) { upb::Status status; - upb::pb::Decoder decoder(global_method, &status); upb::Sink sink(global_handlers, &closures[0]); - decoder.ResetOutput(&sink); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { + // TODO(haberman): hoist this again once the environment supports reset. + upb::Environment env; + env.ReportErrorsTo(&status); + upb::pb::Decoder *decoder = CreateDecoder(&env, global_method, &sink); + testhash = Hash(proto, expected_output, i, j); if (filter_hash && testhash != filter_hash) continue; if (test_mode != COUNT_ONLY) { - decoder.Reset(); output.clear(); status.Clear(); size_t ofs = 0; - upb::BytesSink* input = decoder.input(); + upb::BytesSink* input = decoder->input(); void *sub; if (filter_hash) { @@ -576,9 +589,9 @@ void run_decoder(const string& proto, const string* expected_output) { } bool ok = input->Start(proto.size(), &sub) && - parse(&decoder, sub, proto.c_str(), 0, i, &ofs, &status) && - parse(&decoder, sub, proto.c_str(), i, j, &ofs, &status) && - parse(&decoder, sub, proto.c_str(), j, proto.size(), &ofs, + parse(decoder, sub, proto.c_str(), 0, i, &ofs, &status) && + parse(decoder, sub, proto.c_str(), i, j, &ofs, &status) && + parse(decoder, sub, proto.c_str(), j, proto.size(), &ofs, &status) && ofs == proto.size(); @@ -852,7 +865,7 @@ void test_invalid() { // Test exceeding the resource limit of stack depth. string buf; - for (int i = 0; i <= UPB_DECODER_MAX_NESTING; i++) { + for (int i = 0; i <= MAX_NESTING; i++) { buf.assign(submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, buf)); } assert_does_not_parse(buf); @@ -871,11 +884,12 @@ void test_valid() { if (!filter_hash || filter_hash == testhash) { testhash = emptyhash; upb::Status status; - upb::pb::Decoder decoder(global_method, &status); + upb::Environment env; + env.ReportErrorsTo(&status); upb::Sink sink(global_handlers, &closures[0]); - decoder.ResetOutput(&sink); + upb::pb::Decoder* decoder = CreateDecoder(&env, global_method, &sink); output.clear(); - bool ok = upb::BufferSource::PutBuffer("", 0, decoder.input()); + bool ok = upb::BufferSource::PutBuffer("", 0, decoder->input()); ASSERT(ok); ASSERT(status.ok()); if (test_mode == ALL_HANDLERS) { @@ -1076,7 +1090,7 @@ void test_valid() { // Staying within the stack limit should work properly. string buf; string textbuf; - int total = UPB_DECODER_MAX_NESTING - 1; + int total = MAX_NESTING - 1; for (int i = 0; i < total; i++) { buf.assign(submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, buf)); indentbuf(&textbuf, i); @@ -1135,11 +1149,12 @@ upb::reffed_ptr<const upb::pb::DecoderMethod> method = { NULL, 0 }, }; for (int i = 0; testdata[i].data; i++) { + upb::Environment env; upb::Status status; - upb::pb::Decoder decoder(method.get(), &status); - upb::Sink sink(global_handlers, &closures[0]); - decoder.ResetOutput(&sink); - upb::BytesSink* input = decoder.input(); + env.ReportErrorsTo(&status); + upb::Sink sink(method->dest_handlers(), &closures[0]); + upb::pb::Decoder* decoder = CreateDecoder(&env, method.get(), &sink); + upb::BytesSink* input = decoder->input(); void* subc; ASSERT(input->Start(0, &subc)); size_t ofs = 0; @@ -1182,7 +1197,7 @@ extern "C" { int run_tests(int argc, char *argv[]) { if (argc > 1) filter_hash = strtol(argv[1], NULL, 16); - for (int i = 0; i < UPB_DECODER_MAX_NESTING; i++) { + for (int i = 0; i < MAX_NESTING; i++) { closures[i] = i; } diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 71f354c..c2de6c3 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -15,6 +15,7 @@ #include <sstream> #include "upb/def.h" +#include "upb/env.h" #include "upb/descriptor/reader.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" |