1 #include <ccan/pushpull/pushpull.h>
2 /* Include the C files directly. */
3 #include <ccan/pushpull/push.c>
4 #include <ccan/pushpull/pull.c>
5 #include <ccan/tap/tap.h>
21 static void *fail_reallocfn(void *ptr, size_t size)
26 static bool push_foo(char **p, size_t *len, const struct foo *foo)
28 return push_u64(p, len, foo->vu64) &&
29 push_u32(p, len, foo->vu32) &&
30 push_u16(p, len, foo->vu16) &&
31 push_u8(p, len, foo->vu8) &&
32 push_uchar(p, len, foo->vuchar) &&
33 push_s64(p, len, foo->vs64) &&
34 push_s32(p, len, foo->vs32) &&
35 push_s16(p, len, foo->vs16) &&
36 push_s8(p, len, foo->vs8) &&
37 push_char(p, len, foo->vchar) &&
38 push_bytes(p, len, foo->bytes, sizeof(foo->bytes));
41 static bool pull_foo(const char **p, size_t *len, struct foo *foo)
45 ret = pull_u64(p, len, &foo->vu64) +
46 pull_u32(p, len, &foo->vu32) +
47 pull_u16(p, len, &foo->vu16) +
48 pull_u8(p, len, &foo->vu8) +
49 pull_uchar(p, len, &foo->vuchar) +
50 pull_s64(p, len, &foo->vs64) +
51 pull_s32(p, len, &foo->vs32) +
52 pull_s16(p, len, &foo->vs16) +
53 pull_s8(p, len, &foo->vs8) +
54 pull_char(p, len, &foo->vchar) +
55 pull_bytes(p, len, foo->bytes, sizeof(foo->bytes));
58 ok1(len == 0 && *p == NULL);
62 static bool foo_equal(const struct foo *f1, const struct foo *f2)
64 return f1->vu64 == f2->vu64 &&
65 f1->vu32 == f2->vu32 &&
66 f1->vu16 == f2->vu16 &&
68 f1->vuchar == f2->vuchar &&
69 f1->vs64 == f2->vs64 &&
70 f1->vs32 == f2->vs32 &&
71 f1->vs16 == f2->vs16 &&
73 f1->vchar == f2->vchar &&
74 memcmp(f1->bytes, f2->bytes, sizeof(f1->bytes)) == 0;
82 struct foo *foo, *foo2;
84 /* This is how many tests you plan to run */
87 /* Valgrind will make sure we don't read padding! */
88 foo = malloc(sizeof(*foo));
89 foo->vu64 = 0x01020304050607ULL;
90 foo->vu32 = 0x08090a0b;
94 foo->vs64 = -0x1011121314151617LL;
95 foo->vs32 = -0x18191a1b;
99 memset(foo->bytes, 0x20, sizeof(foo->bytes));
100 strcpy(foo->bytes, "This is a test");
104 ok1(push_foo(&buffer, &len, foo));
105 ok1(len <= sizeof(*foo));
107 /* Triggers valgrind's uninitialized value warning */
108 ok1(!memchr(buffer, 0x21, len));
112 foo2 = malloc(sizeof(*foo2));
113 ok1(pull_foo(&p, &left, foo2));
115 ok1(p == buffer + len);
116 ok1(foo_equal(foo, foo2));
118 /* Too-small for pull, it should fail and set ptr/len to 0 */
121 ok1(!pull_u64(&p, &left, &foo2->vu64));
122 ok1(p == NULL && left == 0);
123 /* Shouldn't change field! */
124 ok1(foo_equal(foo, foo2));
127 ok1(!pull_u64(&p, &left, &foo2->vu64));
128 ok1(p == NULL && left == 0);
129 /* Shouldn't change field! */
130 ok1(foo_equal(foo, foo2));
132 /* Discard should work. */
134 ok1(pull_bytes(&p, &left, NULL, sizeof(foo->bytes)));
135 ok1(left == len - sizeof(foo->bytes));
137 /* Push failures should be clean. */
138 push_set_realloc(fail_reallocfn);
141 ok1(!push_u64(&buffer, &left, foo->vu64));
142 ok1(p == buffer && left == len);
148 /* This exits depending on whether all tests passed */
149 return exit_status();