feat: add config_repl Phase 1 — HEAD-file storage, SHA-256, trim, stale check, sanitise_cn
- config_repl.h/.c: atomic versioned file save, pointer file, trim, stale check - test_config_repl.c: 17 Unity tests covering all Phase 1 storage functions - typemap.h: add repl/heartbeat types + union members for xce_request_t/xce_reply_t - clusterconfig.h: add config_generation field to cluster_config_t (capnp schema ref) - meson.build: add config_repl.c and test_config_repl.c to build targets - controller_test.c: register test_config_repl dispatch function
This commit is contained in:
parent
1c4a103a66
commit
491c2550c2
7 changed files with 926 additions and 7 deletions
|
|
@ -31,6 +31,7 @@ extern void test_p2_integration(void);
|
|||
extern void test_p3_hardening(void);
|
||||
extern void test_p3_manual_failover(void);
|
||||
extern void test_ctrl_metrics(void);
|
||||
extern void test_config_repl(void);
|
||||
|
||||
void setUp(void) { return; }
|
||||
|
||||
|
|
@ -131,6 +132,7 @@ int main(int argc, char* argv[])
|
|||
TEST(test_p3_hardening);
|
||||
TEST(test_p3_manual_failover);
|
||||
TEST(test_ctrl_metrics);
|
||||
TEST(test_config_repl);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
|
|
|||
391
src/controller/testsuite/test_config_repl.c
Normal file
391
src/controller/testsuite/test_config_repl.c
Normal file
|
|
@ -0,0 +1,391 @@
|
|||
/* test_config_repl.c — Unity tests for config replication Phase 1 */
|
||||
#define _GNU_SOURCE
|
||||
#include <unity/unity.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <utime.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "config_repl.h"
|
||||
#include "myconf.h"
|
||||
|
||||
/* Per-test directory helpers — not Unity setUp/tearDown (those are in
|
||||
* controller_test.c). Each test that needs a temp dir calls cr_setup()
|
||||
* at the top and cr_teardown() via the label before returning. */
|
||||
|
||||
static char g_tmpdir[256];
|
||||
static char g_conf_dir[300];
|
||||
|
||||
static void cr_setup(void)
|
||||
{
|
||||
snprintf(g_tmpdir, sizeof(g_tmpdir), "/tmp/test_cr_%d_%ld",
|
||||
(int)getpid(), (long)time(NULL));
|
||||
mkdir(g_tmpdir, 0700);
|
||||
snprintf(g_conf_dir, sizeof(g_conf_dir), "%s/conf", g_tmpdir);
|
||||
mkdir(g_conf_dir, 0700);
|
||||
}
|
||||
|
||||
static void cr_teardown(void)
|
||||
{
|
||||
char cmd[512];
|
||||
snprintf(cmd, sizeof(cmd), "rm -rf %s", g_tmpdir);
|
||||
system(cmd);
|
||||
g_tmpdir[0] = '\0';
|
||||
g_conf_dir[0] = '\0';
|
||||
}
|
||||
|
||||
/* ── conf_read_current_filename ─────────────────── */
|
||||
|
||||
static void test_conf_read_current_filename_missing(void)
|
||||
{
|
||||
cr_setup();
|
||||
char out[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, out, sizeof(out));
|
||||
TEST_ASSERT_EQUAL_INT(-ENOENT, rc);
|
||||
cr_teardown();
|
||||
printf("test_conf_read_current_filename_missing: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_read_current_filename_empty_file(void)
|
||||
{
|
||||
cr_setup();
|
||||
char ptr_path[400];
|
||||
snprintf(ptr_path, sizeof(ptr_path), "%s/current", g_conf_dir);
|
||||
int fd = open(ptr_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
TEST_ASSERT_NOT_EQUAL(-1, fd);
|
||||
close(fd);
|
||||
|
||||
char out[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, out, sizeof(out));
|
||||
TEST_ASSERT_EQUAL_INT(-ENODATA, rc);
|
||||
cr_teardown();
|
||||
printf("test_conf_read_current_filename_empty_file: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_read_current_filename_path_separator(void)
|
||||
{
|
||||
cr_setup();
|
||||
char ptr_path[400];
|
||||
snprintf(ptr_path, sizeof(ptr_path), "%s/current", g_conf_dir);
|
||||
FILE *f = fopen(ptr_path, "w");
|
||||
fprintf(f, "cluster.bin/../evil\n");
|
||||
fclose(f);
|
||||
|
||||
char out[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, out, sizeof(out));
|
||||
TEST_ASSERT_EQUAL_INT(-EINVAL, rc);
|
||||
cr_teardown();
|
||||
printf("test_conf_read_current_filename_path_separator: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_read_current_filename_bad_prefix(void)
|
||||
{
|
||||
cr_setup();
|
||||
char ptr_path[400];
|
||||
snprintf(ptr_path, sizeof(ptr_path), "%s/current", g_conf_dir);
|
||||
FILE *f = fopen(ptr_path, "w");
|
||||
fprintf(f, "badprefix.aabbccddeeff\n");
|
||||
fclose(f);
|
||||
|
||||
char out[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, out, sizeof(out));
|
||||
TEST_ASSERT_EQUAL_INT(-EINVAL, rc);
|
||||
cr_teardown();
|
||||
printf("test_conf_read_current_filename_bad_prefix: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_read_current_filename_wrong_hex_length(void)
|
||||
{
|
||||
cr_setup();
|
||||
char ptr_path[400];
|
||||
snprintf(ptr_path, sizeof(ptr_path), "%s/current", g_conf_dir);
|
||||
FILE *f = fopen(ptr_path, "w");
|
||||
fprintf(f, "cluster.bin.aabb\n"); /* only 4 hex chars, need 12 */
|
||||
fclose(f);
|
||||
|
||||
char out[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, out, sizeof(out));
|
||||
TEST_ASSERT_EQUAL_INT(-EINVAL, rc);
|
||||
cr_teardown();
|
||||
printf("test_conf_read_current_filename_wrong_hex_length: PASS\n");
|
||||
}
|
||||
|
||||
/* ── conf_save_config + conf_read_current_filename round-trip ─ */
|
||||
|
||||
static void test_conf_save_creates_pointer_file(void)
|
||||
{
|
||||
cr_setup();
|
||||
const uint8_t data[] = "fake capnproto config blob";
|
||||
size_t len = sizeof(data) - 1;
|
||||
uint8_t sha256[32];
|
||||
SHA256(data, len, sha256);
|
||||
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
|
||||
int rc = conf_save_config(&myconf, data, len, sha256);
|
||||
TEST_ASSERT_EQUAL_INT(0, rc);
|
||||
|
||||
/* conf/current must exist */
|
||||
char ptr_path[400];
|
||||
snprintf(ptr_path, sizeof(ptr_path), "%s/current", g_conf_dir);
|
||||
TEST_ASSERT_EQUAL_INT(0, access(ptr_path, F_OK));
|
||||
|
||||
free(myconf.conffile);
|
||||
cr_teardown();
|
||||
printf("test_conf_save_creates_pointer_file: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_save_filename_has_12_hex_suffix(void)
|
||||
{
|
||||
cr_setup();
|
||||
const uint8_t data[] = "another fake blob";
|
||||
size_t len = sizeof(data) - 1;
|
||||
uint8_t sha256[32];
|
||||
SHA256(data, len, sha256);
|
||||
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
|
||||
conf_save_config(&myconf, data, len, sha256);
|
||||
|
||||
char fname[256] = {0};
|
||||
int rc = conf_read_current_filename(g_tmpdir, fname, sizeof(fname));
|
||||
TEST_ASSERT_EQUAL_INT(0, rc);
|
||||
TEST_ASSERT_EQUAL_INT(0, strncmp(fname, "cluster.bin.", 12));
|
||||
const char *hex = fname + 12;
|
||||
TEST_ASSERT_EQUAL_INT(12, (int)strlen(hex));
|
||||
TEST_ASSERT_EQUAL_INT(12, (int)strspn(hex, "0123456789abcdef"));
|
||||
|
||||
free(myconf.conffile);
|
||||
cr_teardown();
|
||||
printf("test_conf_save_filename_has_12_hex_suffix: PASS\n");
|
||||
}
|
||||
|
||||
/* ── conf_trim_old_versions ─────────────────────── */
|
||||
|
||||
static void test_conf_trim_keeps_only_n_versions(void)
|
||||
{
|
||||
cr_setup();
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
|
||||
uint8_t sha256[32];
|
||||
for (int i = 0; i < 3; i++) {
|
||||
char data[32];
|
||||
snprintf(data, sizeof(data), "config version %d", i);
|
||||
SHA256((uint8_t *)data, strlen(data), sha256);
|
||||
usleep(15000); /* ensure distinct mtime ordering */
|
||||
conf_save_config(&myconf, (uint8_t *)data, strlen(data), sha256);
|
||||
free(myconf.conffile);
|
||||
myconf.conffile = NULL;
|
||||
}
|
||||
|
||||
/* Count cluster.bin.* files (exclude .tmp) */
|
||||
int count = 0;
|
||||
DIR *d = opendir(g_conf_dir);
|
||||
TEST_ASSERT_NOT_NULL(d);
|
||||
struct dirent *e;
|
||||
while ((e = readdir(d)) != NULL) {
|
||||
if (strncmp(e->d_name, "cluster.bin.", 12) == 0 &&
|
||||
strstr(e->d_name, ".tmp") == NULL)
|
||||
count++;
|
||||
}
|
||||
closedir(d);
|
||||
TEST_ASSERT_EQUAL_INT(2, count);
|
||||
|
||||
free(myconf.conffile);
|
||||
cr_teardown();
|
||||
printf("test_conf_trim_keeps_only_n_versions: PASS\n");
|
||||
}
|
||||
|
||||
/* ── conf_get_saved_tag ─────────────────────────── */
|
||||
|
||||
static void test_conf_get_saved_tag_no_config(void)
|
||||
{
|
||||
cr_setup();
|
||||
uint64_t tag = conf_get_saved_tag(g_tmpdir);
|
||||
TEST_ASSERT_EQUAL_UINT64(0, tag);
|
||||
cr_teardown();
|
||||
printf("test_conf_get_saved_tag_no_config: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_get_saved_tag_after_save(void)
|
||||
{
|
||||
cr_setup();
|
||||
const uint8_t data[] = "tag test blob";
|
||||
size_t len = sizeof(data) - 1;
|
||||
uint8_t sha256[32];
|
||||
SHA256(data, len, sha256);
|
||||
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
|
||||
conf_save_config(&myconf, data, len, sha256);
|
||||
free(myconf.conffile);
|
||||
|
||||
uint64_t tag = conf_get_saved_tag(g_tmpdir);
|
||||
TEST_ASSERT_NOT_EQUAL(0, tag);
|
||||
|
||||
/* Tag must equal first 8 bytes of sha256 in host order */
|
||||
uint64_t expected;
|
||||
memcpy(&expected, sha256, 8);
|
||||
/* be64toh conversion */
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
expected = __builtin_bswap64(expected);
|
||||
#endif
|
||||
TEST_ASSERT_EQUAL_UINT64(expected, tag);
|
||||
|
||||
cr_teardown();
|
||||
printf("test_conf_get_saved_tag_after_save: PASS\n");
|
||||
}
|
||||
|
||||
/* ── conf_check_stale ───────────────────────────── */
|
||||
|
||||
static void test_conf_check_stale_disabled(void)
|
||||
{
|
||||
cr_setup();
|
||||
/* timeout 0 = disabled, never stale */
|
||||
TEST_ASSERT_EQUAL_INT(0, conf_check_stale(g_tmpdir, 0));
|
||||
cr_teardown();
|
||||
printf("test_conf_check_stale_disabled: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_check_stale_fresh(void)
|
||||
{
|
||||
cr_setup();
|
||||
const uint8_t data[] = "stale test blob";
|
||||
uint8_t sha256[32];
|
||||
SHA256(data, sizeof(data) - 1, sha256);
|
||||
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
conf_save_config(&myconf, data, sizeof(data) - 1, sha256);
|
||||
free(myconf.conffile);
|
||||
|
||||
/* Just saved — not stale with 24h timeout */
|
||||
TEST_ASSERT_EQUAL_INT(0, conf_check_stale(g_tmpdir, 86400));
|
||||
cr_teardown();
|
||||
printf("test_conf_check_stale_fresh: PASS\n");
|
||||
}
|
||||
|
||||
static void test_conf_check_stale_aged(void)
|
||||
{
|
||||
cr_setup();
|
||||
const uint8_t data[] = "old config blob";
|
||||
uint8_t sha256[32];
|
||||
SHA256(data, sizeof(data) - 1, sha256);
|
||||
|
||||
myconf_t myconf;
|
||||
memset(&myconf, 0, sizeof(myconf));
|
||||
myconf.workdir = g_tmpdir;
|
||||
myconf.name = "ctrl0";
|
||||
myconf.conf_keep_n = 2;
|
||||
myconf.conffile = NULL;
|
||||
conf_save_config(&myconf, data, sizeof(data) - 1, sha256);
|
||||
free(myconf.conffile);
|
||||
|
||||
/* Age the versioned file by 20 seconds via utime */
|
||||
char fname[256] = {0};
|
||||
conf_read_current_filename(g_tmpdir, fname, sizeof(fname));
|
||||
char ver_path[512];
|
||||
snprintf(ver_path, sizeof(ver_path), "%s/%s", g_conf_dir, fname);
|
||||
|
||||
struct utimbuf old_time;
|
||||
old_time.actime = time(NULL) - 20;
|
||||
old_time.modtime = time(NULL) - 20;
|
||||
utime(ver_path, &old_time);
|
||||
|
||||
/* 5-second threshold → stale */
|
||||
TEST_ASSERT_EQUAL_INT(1, conf_check_stale(g_tmpdir, 5));
|
||||
/* 30-second threshold → not stale */
|
||||
TEST_ASSERT_EQUAL_INT(0, conf_check_stale(g_tmpdir, 30));
|
||||
|
||||
cr_teardown();
|
||||
printf("test_conf_check_stale_aged: PASS\n");
|
||||
}
|
||||
|
||||
/* ── config_contains_reserved_name ─────────────── */
|
||||
|
||||
static void test_config_contains_reserved_name_absent(void)
|
||||
{
|
||||
/* NULL config — no reserved name */
|
||||
TEST_ASSERT_EQUAL_INT(0, config_contains_reserved_name(NULL));
|
||||
printf("test_config_contains_reserved_name_absent: PASS\n");
|
||||
}
|
||||
|
||||
/* ── sanitise_cn ────────────────────────────────── */
|
||||
|
||||
static void test_sanitise_cn_printable(void)
|
||||
{
|
||||
char *out = sanitise_cn("ctrl01.example.com", 64);
|
||||
TEST_ASSERT_EQUAL_STRING("ctrl01.example.com", out);
|
||||
printf("test_sanitise_cn_printable: PASS\n");
|
||||
}
|
||||
|
||||
static void test_sanitise_cn_control_chars(void)
|
||||
{
|
||||
char *out = sanitise_cn("evil\ninjection", 64);
|
||||
/* newline replaced with '?' */
|
||||
TEST_ASSERT_NOT_NULL(strchr(out, '?'));
|
||||
TEST_ASSERT_NULL(strchr(out, '\n'));
|
||||
printf("test_sanitise_cn_control_chars: PASS\n");
|
||||
}
|
||||
|
||||
static void test_sanitise_cn_truncated(void)
|
||||
{
|
||||
char *out = sanitise_cn("abcdefghij", 5);
|
||||
TEST_ASSERT_EQUAL_INT(5, (int)strlen(out));
|
||||
printf("test_sanitise_cn_truncated: PASS\n");
|
||||
}
|
||||
|
||||
/* ── dispatch ───────────────────────────────────── */
|
||||
|
||||
void test_config_repl(void)
|
||||
{
|
||||
RUN_TEST(test_conf_read_current_filename_missing);
|
||||
RUN_TEST(test_conf_read_current_filename_empty_file);
|
||||
RUN_TEST(test_conf_read_current_filename_path_separator);
|
||||
RUN_TEST(test_conf_read_current_filename_bad_prefix);
|
||||
RUN_TEST(test_conf_read_current_filename_wrong_hex_length);
|
||||
RUN_TEST(test_conf_save_creates_pointer_file);
|
||||
RUN_TEST(test_conf_save_filename_has_12_hex_suffix);
|
||||
RUN_TEST(test_conf_trim_keeps_only_n_versions);
|
||||
RUN_TEST(test_conf_get_saved_tag_no_config);
|
||||
RUN_TEST(test_conf_get_saved_tag_after_save);
|
||||
RUN_TEST(test_conf_check_stale_disabled);
|
||||
RUN_TEST(test_conf_check_stale_fresh);
|
||||
RUN_TEST(test_conf_check_stale_aged);
|
||||
RUN_TEST(test_config_contains_reserved_name_absent);
|
||||
RUN_TEST(test_sanitise_cn_printable);
|
||||
RUN_TEST(test_sanitise_cn_control_chars);
|
||||
RUN_TEST(test_sanitise_cn_truncated);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue