Something went wrong. Try again.
wirecall is a low-latency RPC library for C. fast calls over the wire!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118#include "wirecall/protocol.h"
#include <assert.h>#include <math.h>#include <stdlib.h>#include <string.h>
typedef struct alloc_stats { size_t allocs; size_t reallocs; size_t frees;} alloc_stats;
static void *test_alloc(void *ctx, size_t size) { alloc_stats *stats = ctx; stats->allocs++; return malloc(size);}
static void *test_realloc(void *ctx, void *ptr, size_t size) { alloc_stats *stats = ctx; stats->reallocs++; return realloc(ptr, size);}
static void test_free(void *ctx, void *ptr) { alloc_stats *stats = ctx; stats->frees++; free(ptr);}
int main(void) { uint8_t buf[WIRECALL_HEADER_SIZE]; wirecall_header h = { .op = WIRECALL_OP_RPC, .flags = WIRECALL_FLAG_MORE, .proc_id = 0x1122334455667788ull, .size = 9, .call_id = 0x0102030405060708ull, }; uint8_t packet_payload[9] = {WIRECALL_TYPE_I64, 0, 0, 0, 0, 0, 0, 0, 42}; uint8_t mac_key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; assert(wirecall_packet_sign(&h, packet_payload, sizeof(packet_payload)) == 0); assert(h.checksum != 0); assert(h.mac == 0); assert(wirecall_header_encode(&h, buf) == 0); wirecall_header out; assert(wirecall_header_decode(buf, &out) == 0); assert(out.op == h.op); assert(out.flags == h.flags); assert(out.proc_id == h.proc_id); assert(out.size == h.size); assert(out.call_id == h.call_id); assert(out.checksum == h.checksum); assert(out.mac == h.mac); assert(wirecall_packet_verify(&out, packet_payload, sizeof(packet_payload)) == 0); packet_payload[8] = 43; assert(wirecall_packet_verify(&out, packet_payload, sizeof(packet_payload)) != 0); packet_payload[8] = 42; assert(wirecall_packet_sign_ex(&h, packet_payload, sizeof(packet_payload), WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) == 0); assert(h.checksum != 0); assert(h.mac != 0); assert(wirecall_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) == 0); h.mac ^= 1u; assert(wirecall_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) != 0); h.mac ^= 1u;
buf[0] = 99; assert(wirecall_header_decode(buf, &out) != 0);
alloc_stats stats = {0}; wirecall_allocator allocator = { .ctx = &stats, .alloc = test_alloc, .realloc = test_realloc, .free = test_free, }; assert(wirecall_set_allocator(&allocator) == 0);
wirecall_writer w; wirecall_writer_init(&w); assert(wirecall_writer_null(&w) == 0); assert(wirecall_writer_bool(&w, true) == 0); assert(wirecall_writer_i64(&w, -42) == 0); assert(wirecall_writer_u64(&w, 42) == 0); assert(wirecall_writer_f64(&w, 3.5) == 0); assert(wirecall_writer_bytes(&w, "abc", 3) == 0); assert(wirecall_writer_string(&w, "hello", 5) == 0);
wirecall_value *values = NULL; size_t count = 0; assert(wirecall_payload_decode(w.data, w.len, &values, &count) == 0); assert(count == 7); assert(values[0].type == WIRECALL_TYPE_NULL); assert(values[1].as.boolean); assert(values[2].as.i64 == -42); assert(values[3].as.u64 == 42); assert(fabs(values[4].as.f64 - 3.5) < 0.001); assert(values[5].as.bytes.len == 3); assert(memcmp(values[5].as.bytes.data, "abc", 3) == 0); assert(values[6].as.string.len == 5); assert(memcmp(values[6].as.string.data, "hello", 5) == 0); wirecall_values_free(values);
uint8_t malformed[] = {WIRECALL_TYPE_STRING, 0, 0, 0, 10, 'x'}; assert(wirecall_payload_decode(malformed, sizeof(malformed), &values, &count) != 0); wirecall_writer_free(&w); assert(stats.reallocs > 0); assert(stats.frees > 0); assert(wirecall_set_allocator(NULL) == 0); return 0;}