From 47fb22194caf9e8dfeade5e8a63c10bf4f223fac Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 25 Feb 2009 22:32:55 -0800 Subject: Another test, another bugfix. --- pbstream.c | 2 +- tests.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pbstream.c b/pbstream.c index 82fc4d2..8452c03 100644 --- a/pbstream.c +++ b/pbstream.c @@ -184,7 +184,7 @@ static pbstream_status_t get_MESSAGE(struct pbstream_parse_state *s, char *buf, /* We're entering a sub-message. */ uint32_t tmp; char *b = buf; - CHECK(get_v_uint32_t(&buf, &tmp)); + CHECK(get_v_uint32_t(&b, &tmp)); s->offset += (b-buf); /* advance past length varint. */ wvtov_MESSAGE(tmp, &d->v.delimited, s->offset); /* Unlike STRING and BYTES, we *don't* advance past delimited here. */ diff --git a/tests.c b/tests.c index ea87d79..eaf703f 100644 --- a/tests.c +++ b/tests.c @@ -44,6 +44,8 @@ void test_get_v_uint64_t() void test_simple_proto() { + /* These are the examples from + * http://code.google.com/apis/protocolbuffers/docs/encoding.html */ struct pbstream_fieldset *fieldset1 = malloc(sizeof(*fieldset1) + 2*sizeof(struct pbstream_field)); fieldset1->num_fields = 2; @@ -75,7 +77,39 @@ void test_simple_proto() assert(val.v.delimited.len == 7); assert(s.offset == 9); pbstream_free_parser(&s); + + struct pbstream_fieldset *fieldset2 = malloc(sizeof(*fieldset1) + + 3*sizeof(struct pbstream_field)); + fieldset2->num_fields = 3; + fieldset2->fields[2].field_number = 3; + fieldset2->fields[2].type = PBSTREAM_TYPE_MESSAGE; + fieldset2->fields[2].fieldset = fieldset1; + char message3[] = {0x1a, 0x03, 0x08, 0x96, 0x01}; + pbstream_init_parser(&s, fieldset2); + assert(pbstream_parse_field(&s, message3, &fieldnum, &val, &wv) == + PBSTREAM_STATUS_OK); + assert(val.field == &fieldset2->fields[2]); + assert(val.v.delimited.offset == 2); + assert(val.v.delimited.len == 3); + assert(s.offset == 2); + assert(s.top-1 == s.base); + assert(s.top->fieldset == fieldset1); + assert(s.top->end_offset == 5); + + assert(pbstream_parse_field(&s, message3+s.offset, &fieldnum, &val, &wv) == + PBSTREAM_STATUS_OK); + assert(val.field == &fieldset1->fields[0]); + assert(val.v.int32 == 150); + assert(s.offset == 5); + + assert(pbstream_parse_field(&s, NULL /* shouldn't be read */, + &fieldnum, &val, &wv) == + PBSTREAM_STATUS_SUBMESSAGE_END); + assert(s.top == s.base); + pbstream_free_parser(&s); + free(fieldset1); + free(fieldset2); } int main() -- cgit v1.2.3