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
This commit is contained in:
Lorenzo Torres 2026-01-08 15:38:28 +01:00
parent 670e38a105
commit 238eff5bb3
9 changed files with 2156 additions and 27 deletions

23
main.c
View file

@ -5,7 +5,9 @@
#include <uv.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "config.h"
#include "client.h"
#include "network.h"
uv_loop_t *loop;
SSL_CTX *ctx;
@ -13,6 +15,7 @@ SSL_CTX *ctx;
void cleanup_client(uv_handle_t *handle)
{
struct client *client = (struct client*) handle;
network_client_remove(client);
if (client->ssl) {
SSL_free(client->ssl);
}
@ -65,11 +68,11 @@ void on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
char plain_buf[4096];
int p;
while ((p = SSL_read(client->ssl, plain_buf, sizeof(plain_buf))) > 0) {
ssl_write_msg(client, plain_buf, p);
network_handle_data(client, plain_buf, p);
}
flush_ssl_to_socket(client);
}
else if (nread < 0) {
@ -102,7 +105,13 @@ void on_new_connection(uv_stream_t *server, int status)
client->wbio = BIO_new(BIO_s_mem());
SSL_set_bio(client->ssl, client->rbio, client->wbio);
SSL_set_accept_state(client->ssl);
client->user_id = 0;
client->current_room_id = 0;
client->is_authenticated = false;
client->username[0] = '\0';
network_client_add(client);
uv_read_start((uv_stream_t*) &client->handle, alloc_buffer, on_read);
} else {
uv_close((uv_handle_t*) &client->handle, cleanup_client);
@ -132,6 +141,12 @@ void init_openssl()
int main()
{
init_openssl();
if (network_init() != 0) {
fprintf(stderr, "Failed to initialize network/database\n");
return 1;
}
loop = uv_default_loop();
uv_tcp_t server;