diff --git a/test/test_rcon.c b/test/test_rcon.c index 1b985c1..4808302 100644 --- a/test/test_rcon.c +++ b/test/test_rcon.c @@ -16,84 +16,93 @@ static void assert_packet_equals(const rcon_packet_t *packet, (int32_t)(sizeof(int32_t) * 2 + strlen(expected_body) + 2)); } +typedef struct { + int client; + int server; +} socketpair_t; + +static socketpair_t create_socketpair() { + int sv[2]; + assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + socketpair_t sp = {.client = sv[0], .server = sv[1]}; + return sp; +} + +static void close_socketpair(socketpair_t *sockets) { + close(sockets->client); + close(sockets->server); +} + static void test_serialization_roundtrip_command_packet(void **state) { (void)state; - int sv[2]; - assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + socketpair_t sockets = create_socketpair(); const int32_t id = 123; const int32_t type = RCON_SERVERDATA_EXECCOMMAND; const char *body = "execute as @a run data get entity @s Pos"; - send_packet(sv[0], id, type, body); + send_packet(sockets.client, id, type, body); rcon_packet_t packet = {0}; - assert_int_equal(recv_packet(sv[1], &packet), 0); + assert_int_equal(recv_packet(sockets.server, &packet), 0); assert_packet_equals(&packet, id, type, body); - close(sv[0]); - close(sv[1]); + close_socketpair(&sockets); } static void test_serialization_roundtrip_auth_packet(void **state) { (void)state; - int sv[2]; - assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + socketpair_t sockets = create_socketpair(); const int32_t id = 1; const int32_t type = RCON_SERVERDATA_AUTH; const char *body = "correct horse battery staple"; - send_packet(sv[0], id, type, body); + send_packet(sockets.client, id, type, body); rcon_packet_t packet = {0}; - assert_int_equal(recv_packet(sv[1], &packet), 0); + assert_int_equal(recv_packet(sockets.server, &packet), 0); assert_packet_equals(&packet, id, type, body); - close(sv[0]); - close(sv[1]); + close_socketpair(&sockets); } static void test_serialization_roundtrip_empty_body_packet(void **state) { (void)state; - int sv[2]; - assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + socketpair_t sockets = create_socketpair(); const int32_t id = 101; const int32_t type = RCON_SERVERDATA_EXECCOMMAND; const char *body = ""; - send_packet(sv[0], id, type, body); + send_packet(sockets.client, id, type, body); rcon_packet_t packet = {0}; - assert_int_equal(recv_packet(sv[1], &packet), 0); + assert_int_equal(recv_packet(sockets.server, &packet), 0); assert_packet_equals(&packet, id, type, body); - close(sv[0]); - close(sv[1]); + close_socketpair(&sockets); } static void test_serialization_roundtrip_max_supported_body(void **state) { (void)state; - int sv[2]; - assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + socketpair_t sockets = create_socketpair(); char body[4095]; memset(body, 'A', sizeof(body) - 1); body[sizeof(body) - 1] = '\0'; - send_packet(sv[0], 777, RCON_SERVERDATA_RESPONSE_VALUE, body); + send_packet(sockets.client, 777, RCON_SERVERDATA_RESPONSE_VALUE, body); rcon_packet_t packet = {0}; - assert_int_equal(recv_packet(sv[1], &packet), 0); + assert_int_equal(recv_packet(sockets.server, &packet), 0); assert_packet_equals(&packet, 777, RCON_SERVERDATA_RESPONSE_VALUE, body); - close(sv[0]); - close(sv[1]); + close_socketpair(&sockets); } int main(void) {