protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit 14a6ef1da341da1b406b28b5849efcc67b11ed08
parent dc5449b4f116f2adacbcf10077681cfa2af4a094
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 22 Sep 2020 19:32:35 -0700

test: refactor, add fetch response packet tests

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mtest.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 50 insertions(+), 16 deletions(-)

diff --git a/test.c b/test.c @@ -4,6 +4,9 @@ #include <stdio.h> #include <string.h> +static unsigned char bufs[3][1024]; +static struct cursor cursors[3]; + static void print_mem(unsigned char *a, int len) { @@ -15,27 +18,16 @@ static void print_mem(unsigned char *a, int len) } -int main(int argc, char *argv[]) +static void test_packet_serialization(struct packet packet) { - static unsigned char bufs[3][1024]; - - struct cursor cursors[3]; - struct packet packet; struct packet packet_out; + int pushed[2], pulled; - int pushed[2], pulled, i; - - (void)argc; - (void)argv; - + int i; for (i = 0; i < 3; i++) { make_cursor(bufs[i], bufs[i] + sizeof(bufs[i]), &cursors[i]); } - packet.type = PKT_CHAT; - packet.data.chat.sender = 0xFFFF; - packet.data.chat.message = "hello there"; - pushed[0] = push_packet(bufs[0], sizeof(bufs[0]), &packet); assert(pushed[0]); @@ -45,17 +37,59 @@ int main(int argc, char *argv[]) pushed[1] = push_packet(bufs[2], sizeof(bufs[2]), &packet_out); assert(pushed[1]); - printf("chat packet\n"); print_mem(bufs[0], pushed[0]); /* printf("pushed %d,%d pulled %d\n", pushed[0], pushed[1], pulled); */ assert(pushed[0] == pulled && pushed[1] == pushed[0]); - assert(!memcmp(bufs[0], bufs[2], pulled)); assert(packet_eq(&packet, &packet_out)); print_packet(&packet); +} + +static void test_chat_packet_serialization(void) +{ + struct packet packet; + packet.type = PKT_CHAT; + packet.data.chat.sender = 0xFFFF; + packet.data.chat.message = "hello there"; + + printf("chat packet\n"); + test_packet_serialization(packet); +} + +static void test_fetch_packet_serialization(void) +{ + struct packet packet; + packet.type = PKT_FETCH_DATA; + packet.data.fetch.path = "derp"; + + printf("fetch packet\n"); + test_packet_serialization(packet); +} + + +static void test_fetch_response_packet_serialization(void) +{ + struct packet packet; + packet.type = PKT_FETCH_DATA_RESPONSE; + packet.data.fetch_response.path = "derp"; + packet.data.fetch_response.data = (unsigned char[]){0xDE,0xEA,0xDB,0xEE,0xFF}; + packet.data.fetch_response.data_len = 5; + + printf("fetch_response packet\n"); + test_packet_serialization(packet); +} + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + + test_chat_packet_serialization(); + test_fetch_packet_serialization(); + test_fetch_response_packet_serialization(); return 0; }