summaryrefslogtreecommitdiffstats
path: root/tests/test_decode_callbacks.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-13 16:14:08 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-13 16:14:08 +0000
commitfcfc99f766f278564c062922a2857c21ac9af041 (patch)
treebad83fb95b48917d36ced09debc9e6e565ff7649 /tests/test_decode_callbacks.c
parentd4abb63c052dc9d4c23ce72e498b4c7483f3b7cb (diff)
Fixed a bunch of bugs related to callback fields.
Most importantly, callback fields in submessages were being overwritten with garbage, causing segfaults. Additionally, converted PB_LTYPE_FIXED to PB_LTYPE_FIXED32 and PB_LTYPE_FIXED64. This makes the interface a bit easier to use, and in addition runs faster. git-svn-id: https://svn.kapsi.fi/jpa/nanopb@975 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests/test_decode_callbacks.c')
-rw-r--r--tests/test_decode_callbacks.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/tests/test_decode_callbacks.c b/tests/test_decode_callbacks.c
index 1c8d43a5..6c0072de 100644
--- a/tests/test_decode_callbacks.c
+++ b/tests/test_decode_callbacks.c
@@ -8,17 +8,49 @@
bool print_string(pb_istream_t *stream, const pb_field_t *field, void *arg)
{
- uint8_t buffer[1024];
+ uint8_t buffer[1024] = {0};
/* We could read block-by-block to avoid the large buffer... */
- if (stream->bytes_left > sizeof(buffer))
+ if (stream->bytes_left > sizeof(buffer) - 1)
return false;
if (!pb_read(stream, buffer, stream->bytes_left))
return false;
- /* Print the string, in format comparable with protoc --decode. */
- printf("%s: \"%s\"\n", (char*)arg, buffer);
+ /* Print the string, in format comparable with protoc --decode.
+ * Format comes from the arg defined in main().
+ */
+ printf((char*)arg, buffer);
+ return true;
+}
+
+bool print_int32(pb_istream_t *stream, const pb_field_t *field, void *arg)
+{
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ printf((char*)arg, (int32_t)value);
+ return true;
+}
+
+bool print_fixed32(pb_istream_t *stream, const pb_field_t *field, void *arg)
+{
+ uint32_t value;
+ if (!pb_dec_fixed32(stream, NULL, &value))
+ return false;
+
+ printf((char*)arg, value);
+ return true;
+}
+
+bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void *arg)
+{
+ uint64_t value;
+ if (!pb_dec_fixed64(stream, NULL, &value))
+ return false;
+
+ printf((char*)arg, value);
return true;
}
@@ -34,8 +66,23 @@ int main()
*/
TestMessage testmessage = {};
+ testmessage.submsg.stringvalue.funcs.decode = &print_string;
+ testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
+ testmessage.submsg.int32value.funcs.decode = &print_int32;
+ testmessage.submsg.int32value.arg = " int32value: %d\n";
+ testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
+ testmessage.submsg.fixed32value.arg = " fixed32value: %d\n";
+ testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
+ testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n";
+
testmessage.stringvalue.funcs.decode = &print_string;
- testmessage.stringvalue.arg = "stringvalue";
+ testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
+ testmessage.int32value.funcs.decode = &print_int32;
+ testmessage.int32value.arg = "int32value: %d\n";
+ testmessage.fixed32value.funcs.decode = &print_fixed32;
+ testmessage.fixed32value.arg = "fixed32value: %d\n";
+ testmessage.fixed64value.funcs.decode = &print_fixed64;
+ testmessage.fixed64value.arg = "fixed64value: %lld\n";
if (!pb_decode(&stream, TestMessage_fields, &testmessage))
return 1;