summaryrefslogtreecommitdiffstats
path: root/tests/test_decode_callbacks.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-12 18:53:33 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-09-12 18:53:33 +0000
commitd4abb63c052dc9d4c23ce72e498b4c7483f3b7cb (patch)
tree340d5abed388ef4abda42f6e4208948abfb9efb9 /tests/test_decode_callbacks.c
parent35814517ad9da0edad3905269ea41facfb120f3c (diff)
Tests for callback fields
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@974 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests/test_decode_callbacks.c')
-rw-r--r--tests/test_decode_callbacks.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/test_decode_callbacks.c b/tests/test_decode_callbacks.c
new file mode 100644
index 00000000..1c8d43a5
--- /dev/null
+++ b/tests/test_decode_callbacks.c
@@ -0,0 +1,44 @@
+/* Decoding testcase for callback fields.
+ * Run e.g. ./test_encode_callbacks | ./test_decode_callbacks
+ */
+
+#include <stdio.h>
+#include <pb_decode.h>
+#include "callbacks.pb.h"
+
+bool print_string(pb_istream_t *stream, const pb_field_t *field, void *arg)
+{
+ uint8_t buffer[1024];
+
+ /* We could read block-by-block to avoid the large buffer... */
+ if (stream->bytes_left > sizeof(buffer))
+ 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);
+ return true;
+}
+
+int main()
+{
+ uint8_t buffer[1024];
+ size_t length = fread(buffer, 1, 1024, stdin);
+ pb_istream_t stream = pb_istream_from_buffer(buffer, length);
+
+ /* Note: empty initializer list initializes the struct with all-0.
+ * This is recommended so that unused callbacks are set to NULL instead
+ * of crashing at runtime.
+ */
+ TestMessage testmessage = {};
+
+ testmessage.stringvalue.funcs.decode = &print_string;
+ testmessage.stringvalue.arg = "stringvalue";
+
+ if (!pb_decode(&stream, TestMessage_fields, &testmessage))
+ return 1;
+
+ return 0;
+} \ No newline at end of file