From 33a68acb14759cb6fcf796b41ad001c93de4b8e4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 26 Sep 2009 11:46:38 -0700 Subject: Use a status object for errors so a message can be returned. Also delay deletion of subfields until the entire message is deleted. --- tests/test_vs_proto2.cc | 11 +++-- tests/tests.c | 118 ++++++++++++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 58 deletions(-) (limited to 'tests') diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index f1966ef..71dc1da 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -168,7 +168,9 @@ void parse_and_compare(MESSAGE_CIDENT *proto2_msg, struct upb_msg *upb_msg, { // Parse to both proto2 and upb. ASSERT(proto2_msg->ParseFromArray(str->ptr, str->byte_len)); - ASSERT(upb_msg_parsestr(upb_msg, str->ptr, str->byte_len) == UPB_STATUS_OK); + struct upb_status status = UPB_STATUS_INIT; + upb_msg_parsestr(upb_msg, str->ptr, str->byte_len, &status); + ASSERT(upb_ok(&status)); compare(*proto2_msg, upb_msg); } @@ -189,14 +191,17 @@ int main(int argc, char *argv[]) } // Initialize upb state, parse descriptor. + struct upb_status status = UPB_STATUS_INIT; struct upb_context *c = upb_context_new(); struct upb_string *fds = upb_strreadfile(MESSAGE_DESCRIPTOR_FILE); if(!fds) { fprintf(stderr, "Couldn't read " MESSAGE_DESCRIPTOR_FILE ".\n"); return 1; } - if(!upb_context_parsefds(c, fds)) { - fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ".\n"); + upb_context_parsefds(c, fds, &status); + if(!upb_ok(&status)) { + fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ": %s.\n", + status.msg); return 1; } upb_string_unref(fds); diff --git a/tests/tests.c b/tests/tests.c index 7f6f6a4..419383e 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -15,18 +15,18 @@ int num_assertions = 0; static void test_get_v_uint64_t() { #define TEST(name, bytes, val) {\ - upb_status_t status; \ + struct upb_status status = UPB_STATUS_INIT; \ uint8_t name[] = bytes; \ uint8_t *name ## _buf = name; \ uint64_t name ## _val = 0; \ - status = upb_get_v_uint64_t(name, name + sizeof(name) - 1, &name ## _val, &name ## _buf); \ - ASSERT(status == UPB_STATUS_OK); \ + name ## _buf = upb_get_v_uint64_t(name, name + sizeof(name) - 1, &name ## _val, &status); \ + ASSERT(upb_ok(&status)); \ ASSERT(name ## _val == val); \ ASSERT(name ## _buf == name + sizeof(name) - 1); /* - 1 for NULL */ \ /* Test NEED_MORE_DATA. */ \ if(sizeof(name) > 2) { \ - status = upb_get_v_uint64_t(name, name + sizeof(name) - 2, &name ## _val, &name ## _buf); \ - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); \ + name ## _buf = upb_get_v_uint64_t(name, name + sizeof(name) - 2, &name ## _val, &status); \ + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); \ } \ } @@ -44,18 +44,18 @@ static void test_get_v_uint64_t() #undef TEST uint8_t twelvebyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01, 0x01}; - uint8_t *twelvebyte_buf = twelvebyte; uint64_t twelvebyte_val = 0; - upb_status_t status; + struct upb_status status = UPB_STATUS_INIT; /* A varint that terminates before hitting the end of the provided buffer, * but in too many bytes (11 instead of 10). */ - status = upb_get_v_uint64_t(twelvebyte_buf, twelvebyte + 12, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_get_v_uint64_t(twelvebyte, twelvebyte + 12, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint that terminates simultaneously with the end of the provided * buffer, but in too many bytes (11 instead of 10). */ - status = upb_get_v_uint64_t(twelvebyte_buf, twelvebyte + 11, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_reset(&status); + upb_get_v_uint64_t(twelvebyte, twelvebyte + 11, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint whose buffer ends on exactly the byte where the varint must * terminate, but the final byte does not terminate. The absolutely most @@ -66,29 +66,31 @@ static void test_get_v_uint64_t() * then receive a UPB_ERROR_UNTERMINATED_VARINT error; clients who have no * more data to supply will (rightly) conclude that their protobuf is corrupt. */ - status = upb_get_v_uint64_t(twelvebyte_buf, twelvebyte + 10, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT || - status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_get_v_uint64_t(twelvebyte, twelvebyte + 10, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT || + status.code == UPB_STATUS_NEED_MORE_DATA); - status = upb_get_v_uint64_t(twelvebyte_buf, twelvebyte + 9, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_get_v_uint64_t(twelvebyte, twelvebyte + 9, &twelvebyte_val, &status); + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); } static void test_get_v_uint32_t() { #define TEST(name, bytes, val) {\ - upb_status_t status; \ + struct upb_status status = UPB_STATUS_INIT; \ uint8_t name[] = bytes; \ uint8_t *name ## _buf = name; \ uint32_t name ## _val = 0; \ - status = upb_get_v_uint32_t(name, name + sizeof(name), &name ## _val, &name ## _buf); \ - ASSERT(status == UPB_STATUS_OK); \ + name ## _buf = upb_get_v_uint32_t(name, name + sizeof(name), &name ## _val, &status); \ + ASSERT(upb_ok(&status)); \ ASSERT(name ## _val == val); \ ASSERT(name ## _buf == name + sizeof(name) - 1); /* - 1 for NULL */ \ /* Test NEED_MORE_DATA. */ \ if(sizeof(name) > 2) { \ - status = upb_get_v_uint32_t(name, name + sizeof(name) - 2, &name ## _val, &name ## _buf); \ - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); \ + name ## _buf = upb_get_v_uint32_t(name, name + sizeof(name) - 2, &name ## _val, &status); \ + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); \ } \ } @@ -107,18 +109,18 @@ static void test_get_v_uint32_t() #undef TEST uint8_t twelvebyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01, 0x01}; - uint8_t *twelvebyte_buf = twelvebyte; uint32_t twelvebyte_val = 0; - upb_status_t status; + struct upb_status status = UPB_STATUS_INIT; /* A varint that terminates before hitting the end of the provided buffer, * but in too many bytes (11 instead of 10). */ - status = upb_get_v_uint32_t(twelvebyte_buf, twelvebyte + 12, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_get_v_uint32_t(twelvebyte, twelvebyte + 12, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint that terminates simultaneously with the end of the provided * buffer, but in too many bytes (11 instead of 10). */ - status = upb_get_v_uint32_t(twelvebyte_buf, twelvebyte + 11, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_reset(&status); + upb_get_v_uint32_t(twelvebyte, twelvebyte + 11, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint whose buffer ends on exactly the byte where the varint must * terminate, but the final byte does not terminate. The absolutely most @@ -129,27 +131,29 @@ static void test_get_v_uint32_t() * then receive a UPB_ERROR_UNTERMINATED_VARINT error; clients who have no * more data to supply will (rightly) conclude that their protobuf is corrupt. */ - status = upb_get_v_uint32_t(twelvebyte_buf, twelvebyte + 10, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT || - status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_get_v_uint32_t(twelvebyte, twelvebyte + 10, &twelvebyte_val, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT || + status.code == UPB_STATUS_NEED_MORE_DATA); - status = upb_get_v_uint32_t(twelvebyte_buf, twelvebyte + 9, &twelvebyte_val, &twelvebyte_buf); - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_get_v_uint32_t(twelvebyte, twelvebyte + 9, &twelvebyte_val, &status); + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); } static void test_skip_v_uint64_t() { #define TEST(name, bytes) {\ - upb_status_t status; \ + struct upb_status status = UPB_STATUS_INIT; \ uint8_t name[] = bytes; \ uint8_t *name ## _buf = name; \ - status = upb_skip_v_uint64_t(name ## _buf, name + sizeof(name), &name ## _buf); \ - ASSERT(status == UPB_STATUS_OK); \ + name ## _buf = upb_skip_v_uint64_t(name ## _buf, name + sizeof(name), &status); \ + ASSERT(upb_ok(&status)); \ ASSERT(name ## _buf == name + sizeof(name) - 1); /* - 1 for NULL */ \ /* Test NEED_MORE_DATA. */ \ if(sizeof(name) > 2) { \ - status = upb_skip_v_uint64_t(name, name + sizeof(name) - 2, &name ## _buf); \ - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); \ + name ## _buf = upb_skip_v_uint64_t(name, name + sizeof(name) - 2, &status); \ + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); \ } \ } @@ -167,17 +171,17 @@ static void test_skip_v_uint64_t() #undef TEST uint8_t twelvebyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01, 0x01}; - uint8_t *twelvebyte_buf = twelvebyte; - upb_status_t status; + struct upb_status status = UPB_STATUS_INIT; /* A varint that terminates before hitting the end of the provided buffer, * but in too many bytes (11 instead of 10). */ - status = upb_skip_v_uint64_t(twelvebyte_buf, twelvebyte + 12, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_skip_v_uint64_t(twelvebyte, twelvebyte + 12, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint that terminates simultaneously with the end of the provided * buffer, but in too many bytes (11 instead of 10). */ - status = upb_skip_v_uint64_t(twelvebyte_buf, twelvebyte + 11, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT); + upb_reset(&status); + upb_skip_v_uint64_t(twelvebyte, twelvebyte + 11, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT); /* A varint whose buffer ends on exactly the byte where the varint must * terminate, but the final byte does not terminate. The absolutely most @@ -188,23 +192,25 @@ static void test_skip_v_uint64_t() * then receive a UPB_ERROR_UNTERMINATED_VARINT error; clients who have no * more data to supply will (rightly) conclude that their protobuf is corrupt. */ - status = upb_skip_v_uint64_t(twelvebyte_buf, twelvebyte + 10, &twelvebyte_buf); - ASSERT(status == UPB_ERROR_UNTERMINATED_VARINT || - status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_skip_v_uint64_t(twelvebyte, twelvebyte + 10, &status); + ASSERT(status.code == UPB_ERROR_UNTERMINATED_VARINT || + status.code == UPB_STATUS_NEED_MORE_DATA); - status = upb_skip_v_uint64_t(twelvebyte_buf, twelvebyte + 9, &twelvebyte_buf); - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); + upb_reset(&status); + upb_skip_v_uint64_t(twelvebyte, twelvebyte + 9, &status); + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); } static void test_get_f_uint32_t() { #define TEST(name, bytes, val) {\ - upb_status_t status; \ + struct upb_status status = UPB_STATUS_INIT; \ uint8_t name[] = bytes; \ uint8_t *name ## _buf = name; \ uint32_t name ## _val = 0; \ - status = upb_get_f_uint32_t(name ## _buf, name + sizeof(name), &name ## _val, &name ## _buf); \ - ASSERT(status == UPB_STATUS_OK); \ + name ## _buf = upb_get_f_uint32_t(name ## _buf, name + sizeof(name), &name ## _val, &status); \ + ASSERT(upb_ok(&status)); \ ASSERT(name ## _val == val); \ ASSERT(name ## _buf == name + sizeof(name) - 1); /* - 1 for NULL */ \ } @@ -213,10 +219,10 @@ static void test_get_f_uint32_t() TEST(one, "\x01\x00\x00\x00", 0x1UL); uint8_t threeb[] = {0x00, 0x00, 0x00}; - uint8_t *threeb_buf = threeb; uint32_t threeb_val; - upb_status_t status = upb_get_f_uint32_t(threeb, threeb + sizeof(threeb), &threeb_val, &threeb_buf); - ASSERT(status == UPB_STATUS_NEED_MORE_DATA); + struct upb_status status = UPB_STATUS_INIT; + upb_get_f_uint32_t(threeb, threeb + sizeof(threeb), &threeb_val, &status); + ASSERT(status.code == UPB_STATUS_NEED_MORE_DATA); #undef TEST } @@ -243,7 +249,9 @@ static void test_cbparser() ASSERT(p); upb_cbparser_reset(p, NULL, tag_cb, NULL, NULL, NULL, NULL); size_t read; - ASSERT(upb_cbparser_parse(p, NULL, 0, &read) == UPB_STATUS_OK); + struct upb_status status = UPB_STATUS_INIT; + read = upb_cbparser_parse(p, NULL, 0, &status); + ASSERT(upb_ok(&status)); ASSERT(read == 0); } -- cgit v1.2.3