protoverse

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

test.c (2039B)


      1 
      2 #include "net.h"
      3 #include <assert.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 
      7 static unsigned char bufs[3][1024];
      8 static struct cursor cursors[3];
      9 
     10 
     11 static void print_mem(unsigned char *a, int len)
     12 {
     13 	int i;
     14 	for (i = 0; i < len; i++) {
     15 		printf("%02x ", a[i]);
     16 	}
     17 	printf("\n");
     18 
     19 }
     20 
     21 static void test_packet_serialization(struct packet packet)
     22 {
     23 	struct packet packet_out;
     24 	struct env env;
     25 	int pushed[2], pulled;
     26 
     27 	int i;
     28 	for (i = 0; i < 3; i++) {
     29 		make_cursor(bufs[i], bufs[i] + sizeof(bufs[i]), &cursors[i]);
     30 	}
     31 
     32 	pushed[0] = push_packet(bufs[0], sizeof(bufs[0]), &packet);
     33 	assert(pushed[0]);
     34 
     35 	pulled = pull_packet(&cursors[0], &cursors[1], &packet_out, pushed[0]);
     36 	assert(pulled);
     37 
     38 	pushed[1] = push_packet(bufs[2], sizeof(bufs[2]), &packet_out);
     39 	assert(pushed[1]);
     40 
     41 	print_mem(bufs[0], pushed[0]);
     42 
     43 	/* printf("pushed %d,%d pulled %d\n", pushed[0], pushed[1], pulled); */
     44 	assert(pushed[0] == pulled && pushed[1] == pushed[0]);
     45 
     46 	assert(!memcmp(bufs[0], bufs[2], pulled));
     47 	assert(packet_eq(&packet, &packet_out));
     48 
     49 	print_packet(&env, &packet);
     50 }
     51 
     52 static void test_chat_packet_serialization(void)
     53 {
     54 	struct packet packet;
     55 	packet.type = PKT_MESSAGE;
     56 	packet.data.message.message = "hello there";
     57 
     58 	printf("chat packet\n");
     59 	test_packet_serialization(packet);
     60 }
     61 
     62 static void test_fetch_packet_serialization(void)
     63 {
     64 	struct packet packet;
     65 	packet.type = PKT_FETCH_DATA;
     66 	packet.data.fetch.path = "derp";
     67 
     68 	printf("fetch packet\n");
     69 	test_packet_serialization(packet);
     70 }
     71 
     72 
     73 static void test_fetch_response_packet_serialization(void)
     74 {
     75 	struct packet packet;
     76 	packet.type = PKT_FETCH_DATA_RESPONSE;
     77 	packet.data.fetch_response.path = "derp";
     78 	packet.data.fetch_response.data = (unsigned char[]){0xDE,0xEA,0xDB,0xEE,0xFF};
     79 	packet.data.fetch_response.data_len = 5;
     80 
     81 	printf("fetch_response packet\n");
     82 	test_packet_serialization(packet);
     83 }
     84 
     85 int main(int argc, char *argv[])
     86 {
     87 	(void)argc;
     88 	(void)argv;
     89 
     90 	test_chat_packet_serialization();
     91 	test_fetch_packet_serialization();
     92 	test_fetch_response_packet_serialization();
     93 
     94 	return 0;
     95 }