asfur/test.h
Lorenzo Torres 238eff5bb3 Add unit test suite and complete protocol documentation
- Add test framework (test.h) with assertion macros and test runner
- Add comprehensive unit tests (test_network.c) covering:
  - Packet parsing and error handling
  - User registration and authentication
  - Room operations (create, join, leave, delete, list)
  - Direct messaging functionality
  - Message broadcasting
  - Password hashing
- Update Makefile with 'make test' target
- Rewrite PROTOCOL file with complete specification:
  - All 14 packet types with data layouts
  - All error codes with descriptions
  - Typical usage flows
2026-01-08 15:51:01 +01:00

53 lines
1.1 KiB
C

/* Simple unit test framework for asfur */
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int tests_run = 0;
static int tests_passed = 0;
static int tests_failed = 0;
#define TEST_ASSERT(cond, msg) do { \
if (!(cond)) { \
printf(" FAIL: %s\n", msg); \
return 1; \
} \
} while(0)
#define TEST_ASSERT_EQ(a, b, msg) do { \
if ((a) != (b)) { \
printf(" FAIL: %s (expected %d, got %d)\n", msg, (int)(b), (int)(a)); \
return 1; \
} \
} while(0)
#define TEST_ASSERT_STR_EQ(a, b, msg) do { \
if (strcmp((a), (b)) != 0) { \
printf(" FAIL: %s (expected '%s', got '%s')\n", msg, (b), (a)); \
return 1; \
} \
} while(0)
#define RUN_TEST(test_fn) do { \
tests_run++; \
printf("Running %s...\n", #test_fn); \
if (test_fn() == 0) { \
printf(" PASS\n"); \
tests_passed++; \
} else { \
tests_failed++; \
} \
} while(0)
#define TEST_SUMMARY() do { \
printf("\n=== Test Summary ===\n"); \
printf("Total: %d\n", tests_run); \
printf("Passed: %d\n", tests_passed); \
printf("Failed: %d\n", tests_failed); \
return tests_failed > 0 ? 1 : 0; \
} while(0)
#endif