112 lines
3.1 KiB
C
112 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "client_cmd.h"
|
|
#include "plugins.h"
|
|
|
|
static int tests_passed = 0;
|
|
static int tests_failed = 0;
|
|
|
|
#define ASSERT_EQ(expected, actual, msg) do { \
|
|
if ((expected) != (actual)) { \
|
|
fprintf(stderr, "FAIL: %s (expected %d, got %d)\n", msg, (int)(expected), (int)(actual)); \
|
|
tests_failed++; \
|
|
} else { \
|
|
printf("PASS: %s\n", msg); \
|
|
tests_passed++; \
|
|
} \
|
|
} while(0)
|
|
|
|
#define ASSERT_STR_EQ(expected, actual, msg) do { \
|
|
if (strcmp((expected), (actual)) != 0) { \
|
|
fprintf(stderr, "FAIL: %s (expected \"%s\", got \"%s\")\n", msg, (expected), (actual)); \
|
|
tests_failed++; \
|
|
} else { \
|
|
printf("PASS: %s\n", msg); \
|
|
tests_passed++; \
|
|
} \
|
|
} while(0)
|
|
|
|
/* Test invalid commands */
|
|
static void test_unknown_command(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("unknown_command", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('unknown_command', ...) returns -1");
|
|
}
|
|
|
|
static void test_empty_input(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('', ...) returns -1");
|
|
}
|
|
|
|
static void test_null_input(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command(NULL, &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command(NULL, ...) returns -1");
|
|
}
|
|
|
|
/* Test addplugin command */
|
|
static void test_addplugin_no_path(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin', ...) returns -1 (no path)");
|
|
}
|
|
|
|
static void test_addplugin_empty_path(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin ", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin ', ...) returns -1 (empty path)");
|
|
}
|
|
|
|
static void test_addplugin_valid(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin /does/not/exist.so", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin /does/not/exist.so', ...) returns -1 (no such file)");
|
|
}
|
|
|
|
/* Test connect command */
|
|
static void test_connect_no_args(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect', ...) returns -1 (no args)");
|
|
}
|
|
|
|
static void test_connect_missing_to(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect plugin:out_1", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect plugin:out_1', ...) returns -1 (missing 'to')");
|
|
}
|
|
|
|
static void test_connect_invalid_id(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect plugin:out looper:in", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect plugin:out looper:in', ...) returns -1 (stub)");
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
printf("=== Client command parser unit tests ===\n");
|
|
|
|
test_unknown_command();
|
|
test_empty_input();
|
|
test_null_input();
|
|
test_addplugin_no_path();
|
|
test_addplugin_empty_path();
|
|
test_addplugin_valid();
|
|
test_connect_no_args();
|
|
test_connect_missing_to();
|
|
test_connect_invalid_id();
|
|
|
|
printf("\nResults: %d passed, %d failed\n", tests_passed, tests_failed);
|
|
return tests_failed > 0 ? 1 : 0;
|
|
}
|