diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pb/test_encoder.cc | 5 | ||||
-rw-r--r-- | tests/test_cpp.cc | 15 | ||||
-rw-r--r-- | tests/test_def.c | 20 | ||||
-rw-r--r-- | tests/test_handlers.c | 9 | ||||
-rw-r--r-- | tests/test_util.h | 99 |
5 files changed, 99 insertions, 49 deletions
diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 8a222f4..6219e08 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -1,7 +1,8 @@ +#include "tests/test_util.h" #include "tests/upb_test.h" #include "upb/bindings/stdc++/string.h" -#include "upb/descriptor/descriptor.upb.h" +#include "upb/descriptor/descriptor.upbdefs.h" #include "upb/pb/decoder.h" #include "upb/pb/encoder.h" #include "upb/pb/glue.h" @@ -18,7 +19,7 @@ std::string read_string(const char *filename) { void test_pb_roundtrip() { upb::reffed_ptr<const upb::MessageDef> md( - upbdefs::google::protobuf::FileDescriptorSet::MessageDef()); + upbdefs::google::protobuf::FileDescriptorSet::get()); upb::reffed_ptr<const upb::Handlers> encoder_handlers( upb::pb::Encoder::NewHandlers(md.get())); upb::reffed_ptr<const upb::pb::DecoderMethod> method( diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 48f0a3f..c27526b 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -6,6 +6,7 @@ #include <stdio.h> #include <string.h> +#include <fstream> #include <iostream> #include <set> #include <sstream> @@ -144,12 +145,22 @@ static void TestCastsConst10() { } static void TestSymbolTable(const char *descriptor_file) { - upb::reffed_ptr<upb::SymbolTable> s(upb::SymbolTable::New()); upb::Status status; - if (!upb::LoadDescriptorFileIntoSymtab(s.get(), descriptor_file, &status)) { + std::ifstream file_in(descriptor_file, std::ios::binary); + std::string descriptor((std::istreambuf_iterator<char>(file_in)), + (std::istreambuf_iterator<char>())); + std::vector<upb::reffed_ptr<upb::FileDef> > files; + if (!upb::LoadDescriptor(descriptor, &status, &files)) { std::cerr << "Couldn't load descriptor: " << status.error_message(); exit(1); } + + upb::reffed_ptr<upb::SymbolTable> s(upb::SymbolTable::New()); + + for (size_t i = 0; i < files.size(); i++) { + ASSERT(s->AddFile(files[i].get(), &status)); + } + ASSERT(!s->IsFrozen()); s->Freeze(); ASSERT(s->IsFrozen()); diff --git a/tests/test_def.c b/tests/test_def.c index 52d780b..de3bcb2 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -3,6 +3,7 @@ ** (like attempts to link defs that don't have required properties set). */ +#include "tests/test_util.h" #include "upb/def.h" #include "upb/pb/glue.h" #include "upb_test.h" @@ -41,12 +42,22 @@ static void test_noreftracking() { static upb_symtab *load_test_proto(void *owner) { upb_symtab *s = upb_symtab_new(owner); upb_status status = UPB_STATUS_INIT; + size_t len; + char *data = upb_readfile(descriptor_file, &len); + upb_filedef **files; ASSERT(s); - if (!upb_load_descriptor_file_into_symtab(s, descriptor_file, &status)) { - fprintf(stderr, "Error loading descriptor file: %s\n", - upb_status_errmsg(&status)); - ASSERT(false); + ASSERT(data); + files = upb_loaddescriptor(data, len, &files, &status); + ASSERT(files); + free(data); + + while (*files) { + bool ok = upb_symtab_addfile(s, *files, &status); + ASSERT(ok); + upb_filedef_unref(*files, &files); + files++; } + ASSERT(!upb_symtab_isfrozen(s)); upb_symtab_freeze(s); ASSERT(upb_symtab_isfrozen(s)); @@ -391,6 +402,7 @@ static void test_mapentry_check() { /* Should not have succeeded: non-repeated field pointing to a MapEntry. */ ASSERT(!upb_ok(&s)); + upb_status_clear(&s); upb_fielddef_setlabel(f, UPB_LABEL_REPEATED); upb_symtab_add(symtab, defs, 2, NULL, &s); ASSERT(upb_ok(&s)); diff --git a/tests/test_handlers.c b/tests/test_handlers.c index b591ad2..fe6fb82 100644 --- a/tests/test_handlers.c +++ b/tests/test_handlers.c @@ -1,6 +1,6 @@ #include "upb/handlers.h" -#include "upb/descriptor/descriptor.upb.h" +#include "upb/descriptor/descriptor.upbdefs.h" #include "upb_test.h" #include <stdlib.h> #include <string.h> @@ -13,10 +13,9 @@ static bool startmsg(void *c, const void *hd) { static void test_error() { /* Test creating handlers of a static msgdef. */ - const upb_symtab *s = upbdefs_google_protobuf_descriptor(&s); - upb_handlers *h = - upb_handlers_new(upbdefs_google_protobuf_DescriptorProto(s), &h); - upb_symtab_unref(s, &s); + const upb_msgdef *m = upbdefs_google_protobuf_DescriptorProto_get(&m); + upb_handlers *h = upb_handlers_new(m, &h); + upb_msgdef_unref(m, &m); /* Attempt to set the same handler twice causes error. */ ASSERT(upb_ok(upb_handlers_status(h))); diff --git a/tests/test_util.h b/tests/test_util.h index caf9ee8..c6438fc 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -11,23 +11,26 @@ #include "upb/env.h" #include "upb/sink.h" +#ifdef __cplusplus + upb::BufferHandle global_handle; -// A convenience class for parser tests. Provides some useful features: -// -// - can support multiple calls to parse, to test the parser's handling -// of buffer seams. -// -// - can output verbose output about each parse call when requested, for -// ease of debugging. -// -// - can pass NULL for skipped regions of the input if requested. -// -// - allocates and passes a separate buffer for each parsed region, to -// ensure that the parser is not erroneously overreading its buffer. +/* A convenience class for parser tests. Provides some useful features: + * + * - can support multiple calls to parse, to test the parser's handling + * of buffer seams. + * + * - can output verbose output about each parse call when requested, for + * ease of debugging. + * + * - can pass NULL for skipped regions of the input if requested. + * + * - allocates and passes a separate buffer for each parsed region, to + * ensure that the parser is not erroneously overreading its buffer. + */ class VerboseParserEnvironment { public: - // Pass verbose=true to print detailed diagnostics to stderr. + /* Pass verbose=true to print detailed diagnostics to stderr. */ VerboseParserEnvironment(bool verbose) : verbose_(verbose) { env_.SetErrorFunction(&VerboseParserEnvironment::OnError, this); } @@ -60,15 +63,16 @@ class VerboseParserEnvironment { skipped_with_null_ = false; } - // The user should call a series of: - // - // Reset(buf, len, may_skip); - // Start() - // ParseBuffer(X); - // ParseBuffer(Y); - // // Repeat ParseBuffer as desired, but last call should pass -1. - // ParseBuffer(-1); - // End(); + /* The user should call a series of: + * + * Reset(buf, len, may_skip); + * Start() + * ParseBuffer(X); + * ParseBuffer(Y); + * // Repeat ParseBuffer as desired, but last call should pass -1. + * ParseBuffer(-1); + * End(); + */ bool Start() { @@ -111,9 +115,9 @@ class VerboseParserEnvironment { ASSERT((size_t)bytes <= (len_ - ofs_)); - // Copy buffer into a separate, temporary buffer. - // This is necessary to verify that the parser is not erroneously - // reading outside the specified bounds. + /* Copy buffer into a separate, temporary buffer. + * This is necessary to verify that the parser is not erroneously + * reading outside the specified bounds. */ char *buf2 = NULL; if ((int)(ofs_ + bytes) <= skip_until_) { @@ -125,7 +129,7 @@ class VerboseParserEnvironment { } if (buf2 == NULL && bytes == 0) { - // Decoders dont' support buf=NULL, bytes=0. + /* Decoders dont' support buf=NULL, bytes=0. */ return true; } @@ -189,18 +193,41 @@ class VerboseParserEnvironment { bool end_ok_; bool end_ok_set_; - // When our parse call returns a value greater than the number of bytes - // we passed in, the decoder is indicating to us that the next N bytes - // in the stream are not needed and can be skipped. The user is allowed - // to pass a NULL buffer for those N bytes. - // - // skip_until_ is initially set to 0 if we should do this NULL-buffer - // skipping or -1 if we should not. If we are open to doing NULL-buffer - // skipping and we get an opportunity to do it, we set skip_until to the - // stream offset where we can skip until. The user can then test whether - // this happened by testing SkippedWithNull(). + /* When our parse call returns a value greater than the number of bytes + * we passed in, the decoder is indicating to us that the next N bytes + * in the stream are not needed and can be skipped. The user is allowed + * to pass a NULL buffer for those N bytes. + * + * skip_until_ is initially set to 0 if we should do this NULL-buffer + * skipping or -1 if we should not. If we are open to doing NULL-buffer + * skipping and we get an opportunity to do it, we set skip_until to the + * stream offset where we can skip until. The user can then test whether + * this happened by testing SkippedWithNull(). */ int skip_until_; bool skipped_with_null_; }; +#endif /* __cplusplus */ + +UPB_INLINE char *upb_readfile(const char *filename, size_t *len) { + long size; + char *buf; + FILE *f = fopen(filename, "rb"); + if(!f) return NULL; + if(fseek(f, 0, SEEK_END) != 0) goto error; + size = ftell(f); + if(size < 0) goto error; + if(fseek(f, 0, SEEK_SET) != 0) goto error; + buf = (char*)malloc(size + 1); + if(size && fread(buf, size, 1, f) != 1) goto error; + fclose(f); + if (len) *len = size; + buf[size] = '\0'; + return buf; + +error: + fclose(f); + return NULL; +} + #endif |