aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-07-30 09:59:08 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-07-30 09:59:08 +0000
commitf8364310d3ca85d2cf59019bb4bc5e9ff4c52dc3 (patch)
treee5ddbe4c28353630fb4881717058ed5f9cd83db7 /tests
parentd7863b7424b1be5da33e9d632365f3e2c0538461 (diff)
unittests (some)
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@949 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile12
-rw-r--r--tests/decode_unittests.c111
-rw-r--r--tests/test_decode1.c30
3 files changed, 134 insertions, 19 deletions
diff --git a/tests/Makefile b/tests/Makefile
index d357c674..ddf91ebb 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,13 +1,17 @@
-CFLAGS=-ansi -pedantic -Wall -I .. -g -O0
+CFLAGS=-ansi -Wall -Werror -I .. -g -O0
DEPS=../pb_decode.c ../pb_decode.h ../pb.h
+TESTS=test_decode1 decode_unittests
-all: test_decode1
+all: $(TESTS)
clean:
rm -f test_decode1
-test_decode1: test_decode1.c $(DEPS)
+%: %.c $(DEPS)
$(CC) $(CFLAGS) -o $@ $< ../pb_decode.c
-fuzztest: test_decode1
+run_unittests: decode_unittests
+ ./decode_unittests
+
+run_fuzztest: test_decode1
bash -c 'I=1; while cat /dev/urandom | ./test_decode1 > /dev/null; do I=$$(($$I+1)); echo -en "\r$$I"; done' \ No newline at end of file
diff --git a/tests/decode_unittests.c b/tests/decode_unittests.c
new file mode 100644
index 00000000..746b7e96
--- /dev/null
+++ b/tests/decode_unittests.c
@@ -0,0 +1,111 @@
+#include <stdio.h>
+#include <string.h>
+#include "pb_decode.h"
+
+#define COMMENT(x) printf("\n----" x "----\n");
+#define STR(x) #x
+#define STR2(x) STR(x)
+#define TEST(x) \
+ if (!(x)) { \
+ fprintf(stderr, __FILE__ ":" STR2(__LINE__) " FAILED:" #x "\n"); \
+ status = 1; \
+ } else { \
+ printf("OK: " #x "\n"); \
+ }
+
+#define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x))
+
+bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ if (stream->state != NULL)
+ return false; /* Simulate error */
+
+ if (buf != NULL)
+ memset(buf, 'x', count);
+ return true;
+}
+
+int main()
+{
+ int status = 0;
+
+ {
+ uint8_t buffer1[] = "foobartest1234";
+ uint8_t buffer2[sizeof(buffer1)];
+ pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
+
+ COMMENT("Test pb_read and pb_istream_t");
+ TEST(pb_read(&stream, buffer2, 6))
+ TEST(memcmp(buffer2, "foobar", 6) == 0)
+ TEST(stream.bytes_left == sizeof(buffer1) - 6)
+ TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
+ TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
+ TEST(stream.bytes_left == 0)
+ TEST(!pb_read(&stream, buffer2, 1))
+ }
+
+ {
+ uint8_t buffer[20];
+ pb_istream_t stream = {&stream_callback, NULL, 20};
+
+ COMMENT("Test pb_read with custom callback");
+ TEST(pb_read(&stream, buffer, 5))
+ TEST(memcmp(buffer, "xxxxx", 5) == 0)
+ TEST(!pb_read(&stream, buffer, 50))
+ stream.state = (void*)1; /* Simulated error return from callback */
+ TEST(!pb_read(&stream, buffer, 5))
+ stream.state = NULL;
+ TEST(pb_read(&stream, buffer, 15))
+ }
+
+ {
+ pb_istream_t s;
+ uint32_t u;
+ int32_t i;
+
+ COMMENT("Test pb_decode_varint32");
+ TEST((s = S("\x00"), pb_decode_varint32(&s, &u) && u == 0));
+ TEST((s = S("\x01"), pb_decode_varint32(&s, &u) && u == 1));
+ TEST((s = S("\xAC\x02"), pb_decode_varint32(&s, &u) && u == 300));
+ TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, &u) && u == UINT32_MAX));
+ TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, (uint32_t*)&i) && i == -1));
+ }
+
+ {
+ pb_istream_t s;
+ uint64_t u;
+ int64_t i;
+
+ COMMENT("Test pb_decode_varint64");
+ TEST((s = S("\x00"), pb_decode_varint64(&s, &u) && u == 0));
+ TEST((s = S("\x01"), pb_decode_varint64(&s, &u) && u == 1));
+ TEST((s = S("\xAC\x02"), pb_decode_varint64(&s, &u) && u == 300));
+ TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, &u) && u == UINT32_MAX));
+ TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, (uint64_t*)&i) && i == UINT32_MAX));
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
+ pb_decode_varint64(&s, (uint64_t*)&i) && i == -1));
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
+ pb_decode_varint64(&s, &u) && u == UINT64_MAX));
+ }
+
+ {
+ pb_istream_t s;
+ COMMENT("Test pb_skip_varint");
+ TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
+ TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
+ pb_skip_varint(&s) && s.bytes_left == 7))
+ }
+
+ {
+ pb_istream_t s;
+ COMMENT("Test pb_skip_string")
+ TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 7))
+ TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 7))
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
diff --git a/tests/test_decode1.c b/tests/test_decode1.c
index a507b0dc..4b34022f 100644
--- a/tests/test_decode1.c
+++ b/tests/test_decode1.c
@@ -27,20 +27,20 @@ typedef struct {
} Person;
/* Field descriptions */
-#define membersize(st, m) (sizeof ((st*)0)->m)
-const Person_PhoneType Person_PhoneType_type_default = Person_PhoneType_HOME;
+
+const Person_PhoneType Person_PhoneNumber_type_default = Person_PhoneType_HOME;
const pb_field_t Person_PhoneNumber_fields[] = {
{1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
offsetof(Person_PhoneNumber, number), 0,
- membersize(Person_PhoneNumber, number), 0, 0},
+ pb_membersize(Person_PhoneNumber, number), 0, 0},
{2, PB_HTYPE_OPTIONAL | PB_LTYPE_VARINT,
- offsetof(Person_PhoneNumber, type) - offsetof(Person_PhoneNumber, number),
- (int)offsetof(Person_PhoneNumber, has_type) - (int)offsetof(Person_PhoneNumber, type),
- membersize(Person_PhoneNumber, type), 0,
- &Person_PhoneType_type_default},
+ pb_delta(Person_PhoneNumber, type, number),
+ pb_delta(Person_PhoneNumber, has_type, type),
+ pb_membersize(Person_PhoneNumber, type), 0,
+ &Person_PhoneNumber_type_default},
PB_LAST_FIELD
};
@@ -48,22 +48,22 @@ const pb_field_t Person_PhoneNumber_fields[] = {
const pb_field_t Person_fields[] = {
{1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
offsetof(Person, name), 0,
- membersize(Person, name), 0, 0},
+ pb_membersize(Person, name), 0, 0},
{2, PB_HTYPE_REQUIRED | PB_LTYPE_VARINT,
- offsetof(Person, id) - offsetof(Person, name), 0,
- membersize(Person, id), 0, 0},
+ pb_delta(Person, id, name), 0,
+ pb_membersize(Person, id), 0, 0},
{3, PB_HTYPE_OPTIONAL | PB_LTYPE_STRING,
offsetof(Person, email) - offsetof(Person, id),
- (int)offsetof(Person, has_email) - (int)offsetof(Person, email),
- membersize(Person, email), 0, 0},
+ pb_delta(Person, has_email, email),
+ pb_membersize(Person, email), 0, 0},
{4, PB_HTYPE_ARRAY | PB_LTYPE_SUBMESSAGE,
offsetof(Person, phone) - offsetof(Person, email),
- (int)offsetof(Person, phone_size) - (int)offsetof(Person, phone),
- membersize(Person, phone[0]),
- membersize(Person, phone) / membersize(Person, phone[0]),
+ pb_delta(Person, phone_size, phone),
+ pb_membersize(Person, phone[0]),
+ pb_arraysize(Person, phone),
Person_PhoneNumber_fields},
PB_LAST_FIELD