text/plain
•
4.64 KB
•
171 lines
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "storage.h"
#include "test_harness.h"
#include "test_helpers.h"
#define CONFIG_BACKUP_PATH ".tests/config_backup/config.toml.orig"
static bool test_config_with_fixture(const char* fixture_rel,
bool expect_loaded,
config_t* out_cfg) {
bool ok = true;
bool had_original = false;
if (!test_prepare_storage()) {
return false;
}
if (!test_preserve_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
&had_original)) {
return false;
}
if (!test_copy_fixture_to_storage(fixture_rel, CONFIG_FILE)) {
test_fail_impl(__FILE__, __LINE__, "Failed to copy fixture %s",
fixture_rel);
ok = false;
goto cleanup;
}
if (config_load(out_cfg) != expect_loaded) {
test_fail_impl(__FILE__, __LINE__,
"config_load() mismatch for fixture %s", fixture_rel);
ok = false;
}
cleanup:
if (!test_restore_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
had_original)) {
test_fail_impl(__FILE__, __LINE__, "Failed to restore %s", CONFIG_FILE);
ok = false;
}
return ok;
}
static bool test_config_parses_wifi_entries(void) {
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
ASSERT_TRUE(test_config_with_fixture("config/basic.toml", true, &cfg));
ASSERT_EQ_INT(2, cfg.wifi_count);
ASSERT_EQ_STR("HomeNet", cfg.wifi[0].ssid);
ASSERT_EQ_STR("secret1", cfg.wifi[0].psk);
ASSERT_EQ_STR("OfficeWiFi", cfg.wifi[1].ssid);
ASSERT_EQ_STR("secret2", cfg.wifi[1].psk);
return true;
}
static bool test_config_invalid_returns_false(void) {
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
ASSERT_TRUE(test_config_with_fixture("config/invalid.toml", false, &cfg));
ASSERT_EQ_INT(0, cfg.wifi_count);
return true;
}
static bool test_config_caps_at_max_wifi(void) {
bool ok = true;
bool had_original = false;
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
if (!test_prepare_storage()) {
return false;
}
if (!test_preserve_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
&had_original)) {
return false;
}
if (!test_storage_mkdir_p("Onigiri")) {
test_fail_impl(__FILE__, __LINE__, "Failed to create Onigiri dir");
ok = false;
goto cleanup;
}
storage_file_t f = storage_open(CONFIG_FILE, "w");
if (!f) {
test_fail_impl(__FILE__, __LINE__, "Failed to open %s", CONFIG_FILE);
ok = false;
goto cleanup;
}
for (int i = 0; i < CONFIG_MAX_WIFI + 3; i++) {
char line[256];
int n = snprintf(line, sizeof(line),
"[[wifi]]\nssid = \"S%d\"\npsk = \"P%d\"\n\n", i,
i);
if (n < 0 || n >= (int)sizeof(line)) {
test_fail_impl(__FILE__, __LINE__, "snprintf failed");
ok = false;
break;
}
if (storage_write(f, line, (size_t)n) != (size_t)n) {
test_fail_impl(__FILE__, __LINE__, "Failed writing generated config");
ok = false;
break;
}
}
storage_close(f);
if (ok && !config_load(&cfg)) {
test_fail_impl(__FILE__, __LINE__, "Expected config_load success");
ok = false;
}
if (ok && cfg.wifi_count != CONFIG_MAX_WIFI) {
test_fail_impl(__FILE__, __LINE__, "Expected wifi_count=%d got %d",
CONFIG_MAX_WIFI, cfg.wifi_count);
ok = false;
}
cleanup:
if (!test_restore_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
had_original)) {
test_fail_impl(__FILE__, __LINE__, "Failed to restore %s", CONFIG_FILE);
ok = false;
}
return ok;
}
static bool test_config_missing_file_returns_false(void) {
bool ok = true;
bool had_original = false;
config_t cfg;
memset(&cfg, 0, sizeof(cfg));
if (!test_prepare_storage()) {
return false;
}
if (!test_preserve_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
&had_original)) {
return false;
}
if (!storage_remove(CONFIG_FILE)) {
test_fail_impl(__FILE__, __LINE__, "Failed removing %s", CONFIG_FILE);
ok = false;
goto cleanup;
}
if (config_load(&cfg)) {
test_fail_impl(__FILE__, __LINE__, "Expected config_load false");
ok = false;
}
cleanup:
if (!test_restore_storage_file(CONFIG_FILE, CONFIG_BACKUP_PATH,
had_original)) {
test_fail_impl(__FILE__, __LINE__, "Failed to restore %s", CONFIG_FILE);
ok = false;
}
return ok;
}
void register_config_tests(void) {
test_register("config_parses_wifi_entries", test_config_parses_wifi_entries);
test_register("config_invalid_returns_false", test_config_invalid_returns_false);
test_register("config_caps_at_max_wifi", test_config_caps_at_max_wifi);
test_register("config_missing_file_returns_false",
test_config_missing_file_returns_false);
}