119 lines
3.1 KiB
C
119 lines
3.1 KiB
C
#define _GNU_SOURCE
|
|
|
|
#include <setjmp.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cmocka.h>
|
|
|
|
#define main wamf_main
|
|
#include "../src/main.c"
|
|
#undef main
|
|
|
|
static void assert_packet_equals(const rcon_packet_t *packet,
|
|
int32_t expected_id, int32_t expected_type,
|
|
const char *expected_body) {
|
|
assert_int_equal(packet->id, expected_id);
|
|
assert_int_equal(packet->type, expected_type);
|
|
assert_string_equal(packet->body, expected_body);
|
|
assert_int_equal(packet->_padding, 0);
|
|
assert_int_equal(packet->size,
|
|
(int32_t)(sizeof(int32_t) * 2 + strlen(expected_body) + 2));
|
|
}
|
|
|
|
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);
|
|
|
|
const int32_t id = 123;
|
|
const int32_t type = SERVERDATA_EXECCOMMAND;
|
|
const char *body = "execute as @a run data get entity @s Pos";
|
|
|
|
send_packet(sv[0], id, type, body);
|
|
|
|
rcon_packet_t packet = {0};
|
|
assert_int_equal(recv_packet(sv[1], &packet), 0);
|
|
assert_packet_equals(&packet, id, type, body);
|
|
|
|
close(sv[0]);
|
|
close(sv[1]);
|
|
}
|
|
|
|
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);
|
|
|
|
const int32_t id = 1;
|
|
const int32_t type = SERVERDATA_AUTH;
|
|
const char *body = "correct horse battery staple";
|
|
|
|
send_packet(sv[0], id, type, body);
|
|
|
|
rcon_packet_t packet = {0};
|
|
assert_int_equal(recv_packet(sv[1], &packet), 0);
|
|
assert_packet_equals(&packet, id, type, body);
|
|
|
|
close(sv[0]);
|
|
close(sv[1]);
|
|
}
|
|
|
|
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);
|
|
|
|
const int32_t id = 101;
|
|
const int32_t type = SERVERDATA_EXECCOMMAND;
|
|
const char *body = "";
|
|
|
|
send_packet(sv[0], id, type, body);
|
|
|
|
rcon_packet_t packet = {0};
|
|
assert_int_equal(recv_packet(sv[1], &packet), 0);
|
|
assert_packet_equals(&packet, id, type, body);
|
|
|
|
close(sv[0]);
|
|
close(sv[1]);
|
|
}
|
|
|
|
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);
|
|
|
|
char body[4095];
|
|
memset(body, 'A', sizeof(body) - 1);
|
|
body[sizeof(body) - 1] = '\0';
|
|
|
|
send_packet(sv[0], 777, SERVERDATA_RESPONSE_VALUE, body);
|
|
|
|
rcon_packet_t packet = {0};
|
|
assert_int_equal(recv_packet(sv[1], &packet), 0);
|
|
assert_packet_equals(&packet, 777, SERVERDATA_RESPONSE_VALUE, body);
|
|
|
|
close(sv[0]);
|
|
close(sv[1]);
|
|
}
|
|
|
|
int main(void) {
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test(test_serialization_roundtrip_command_packet),
|
|
cmocka_unit_test(test_serialization_roundtrip_auth_packet),
|
|
cmocka_unit_test(test_serialization_roundtrip_empty_body_packet),
|
|
cmocka_unit_test(test_serialization_roundtrip_max_supported_body),
|
|
};
|
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
}
|