store.c keeps accounts.json, secrets.json and identity.json in a 0700 directory with 0600 files, writes through a temporary file with fsync/rename/dir-fsync, repairs a wrong file mode on load, moves invalid JSON to NAME.bad and rejects path names. test_store covers accounts/secrets/identity, overwrite, corrupt/missing files, stale tmp, read-only directory, default HOME path and permissions. make mcde-host exit=0; test_json, test_frame, test_loop and test_store all ok; ASan/UBSan clean.
318 lines
7.6 KiB
C
318 lines
7.6 KiB
C
/*
|
|
* test_store.c -- unit tests for dtchat's atomic store
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
static int fails;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
|
#cond); \
|
|
fails++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
path_join(char *out, size_t cap, const char *dir, const char *name)
|
|
{
|
|
if (snprintf(out, cap, "%s/%s", dir, name) >= (int)cap) {
|
|
fprintf(stderr, "path too long: %s/%s\n", dir, name);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
static void
|
|
check_mode(const char *path, mode_t want)
|
|
{
|
|
struct stat st;
|
|
|
|
CHECK(stat(path, &st) == 0);
|
|
if (stat(path, &st) == 0)
|
|
CHECK((st.st_mode & 0777) == want);
|
|
}
|
|
|
|
static void
|
|
write_file(const char *path, const char *text)
|
|
{
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
size_t len = strlen(text);
|
|
|
|
CHECK(fd >= 0);
|
|
if (fd < 0)
|
|
return;
|
|
CHECK(write(fd, text, len) == (ssize_t)len);
|
|
close(fd);
|
|
}
|
|
|
|
static int
|
|
count_tmp(const char *dir)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *e;
|
|
int n = 0;
|
|
|
|
if (d == NULL)
|
|
return -1;
|
|
while ((e = readdir(d)) != NULL) {
|
|
if (strstr(e->d_name, ".tmp") != NULL)
|
|
n++;
|
|
}
|
|
closedir(d);
|
|
return n;
|
|
}
|
|
|
|
static DcJson *
|
|
accounts_json(const char *id)
|
|
{
|
|
DcJson *root = dc_json_new_object();
|
|
DcJson *array = dc_json_new_array();
|
|
DcJson *account = dc_json_new_object();
|
|
|
|
CHECK(root != NULL && array != NULL && account != NULL);
|
|
if (root == NULL || array == NULL || account == NULL) {
|
|
dc_json_free(root);
|
|
dc_json_free(array);
|
|
dc_json_free(account);
|
|
return NULL;
|
|
}
|
|
CHECK(dc_json_object_set(account, "id", dc_json_new_string(id)) == 0);
|
|
CHECK(dc_json_array_append(array, account) == 0);
|
|
CHECK(dc_json_object_set(root, "accounts", array) == 0);
|
|
return root;
|
|
}
|
|
|
|
static const char *
|
|
accounts_id(const DcJson *json)
|
|
{
|
|
const DcJson *accounts = dc_json_get(json, "accounts");
|
|
const DcJson *account = dc_json_array_get(accounts, 0);
|
|
|
|
return dc_json_string(dc_json_get(account, "id"));
|
|
}
|
|
|
|
static void
|
|
test_store(DcStore *store, const char *dir)
|
|
{
|
|
char path[512];
|
|
char err[256];
|
|
DcJson *json;
|
|
int missing = -1;
|
|
|
|
CHECK(dc_store_dir(store) != NULL);
|
|
check_mode(dir, 0700);
|
|
|
|
json = accounts_json("libera");
|
|
CHECK(json != NULL);
|
|
CHECK(dc_store_save_accounts(store, json) == 0);
|
|
dc_json_free(json);
|
|
|
|
path_join(path, sizeof(path), dir, "accounts.json");
|
|
check_mode(path, 0600);
|
|
CHECK(count_tmp(dir) == 0);
|
|
|
|
json = dc_store_load(store, "accounts.json", &missing, err, sizeof(err));
|
|
CHECK(json != NULL);
|
|
CHECK(missing == 0);
|
|
if (json != NULL) {
|
|
CHECK(strcmp(accounts_id(json), "libera") == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
/* overwrite */
|
|
json = accounts_json("oftc");
|
|
CHECK(json != NULL);
|
|
CHECK(dc_store_save_accounts(store, json) == 0);
|
|
dc_json_free(json);
|
|
json = dc_store_load_accounts(store);
|
|
CHECK(json != NULL);
|
|
if (json != NULL) {
|
|
CHECK(strcmp(accounts_id(json), "oftc") == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
/* secrets */
|
|
json = dc_json_new_object();
|
|
{
|
|
DcJson *secret = dc_json_new_object();
|
|
|
|
CHECK(dc_json_object_set(secret, "password",
|
|
dc_json_new_string("s3cret")) == 0);
|
|
CHECK(dc_json_object_set(json, "libera", secret) == 0);
|
|
}
|
|
CHECK(dc_store_save_secrets(store, json) == 0);
|
|
dc_json_free(json);
|
|
path_join(path, sizeof(path), dir, "secrets.json");
|
|
check_mode(path, 0600);
|
|
json = dc_store_load_secrets(store);
|
|
CHECK(json != NULL);
|
|
if (json != NULL) {
|
|
CHECK(strcmp(dc_json_string(dc_json_get(
|
|
dc_json_get(json, "libera"), "password")), "s3cret") == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
/* identity, and no default before it exists */
|
|
CHECK(dc_store_load_identity(store) == NULL);
|
|
json = dc_json_new_object();
|
|
CHECK(dc_json_object_set(json, "id",
|
|
dc_json_new_string("0123456789abcdef")) == 0);
|
|
CHECK(dc_json_object_set(json, "name",
|
|
dc_json_new_string("Test")) == 0);
|
|
CHECK(dc_store_save_identity(store, json) == 0);
|
|
dc_json_free(json);
|
|
path_join(path, sizeof(path), dir, "identity.json");
|
|
check_mode(path, 0600);
|
|
json = dc_store_load_identity(store);
|
|
CHECK(json != NULL);
|
|
if (json != NULL) {
|
|
CHECK(strcmp(dc_json_string(dc_json_get(json, "name")),
|
|
"Test") == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
/* a wrong mode is repaired on load */
|
|
path_join(path, sizeof(path), dir, "accounts.json");
|
|
CHECK(chmod(path, 0644) == 0);
|
|
json = dc_store_load_accounts(store);
|
|
CHECK(json != NULL);
|
|
dc_json_free(json);
|
|
check_mode(path, 0600);
|
|
|
|
/* a corrupt file is moved to .bad, not parsed */
|
|
write_file(path, "{not json");
|
|
json = dc_store_load(store, "accounts.json", &missing, err, sizeof(err));
|
|
CHECK(json == NULL);
|
|
CHECK(missing == 0);
|
|
CHECK(err[0] != '\0');
|
|
{
|
|
char bad[512];
|
|
|
|
path_join(bad, sizeof(bad), dir, "accounts.json.bad");
|
|
CHECK(access(bad, F_OK) == 0);
|
|
}
|
|
json = dc_store_load_accounts(store);
|
|
CHECK(json != NULL);
|
|
if (json != NULL) {
|
|
CHECK(dc_json_array_size(dc_json_get(json, "accounts")) == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
/* missing file */
|
|
json = dc_store_load(store, "nope.json", &missing, err, sizeof(err));
|
|
CHECK(json == NULL);
|
|
CHECK(missing == 1);
|
|
|
|
/* invalid names */
|
|
CHECK(dc_store_load(store, "../x", NULL, NULL, 0) == NULL);
|
|
CHECK(dc_store_load(store, "a/b", NULL, NULL, 0) == NULL);
|
|
CHECK(dc_store_load(store, "", NULL, NULL, 0) == NULL);
|
|
json = accounts_json("x");
|
|
CHECK(dc_store_save(store, "../x", json) == -1);
|
|
CHECK(dc_store_save(store, "a/b", json) == -1);
|
|
dc_json_free(json);
|
|
|
|
/* a stale temporary file is ignored */
|
|
path_join(path, sizeof(path), dir, "accounts.json.tmp");
|
|
write_file(path, "stale");
|
|
json = dc_store_load_accounts(store);
|
|
CHECK(json != NULL);
|
|
dc_json_free(json);
|
|
CHECK(count_tmp(dir) >= 1);
|
|
unlink(path);
|
|
CHECK(count_tmp(dir) == 0);
|
|
|
|
/* a read-only directory refuses the write and leaves nothing */
|
|
CHECK(chmod(dir, 0500) == 0);
|
|
json = accounts_json("y");
|
|
CHECK(dc_store_save_accounts(store, json) == -1);
|
|
dc_json_free(json);
|
|
CHECK(chmod(dir, 0700) == 0);
|
|
CHECK(count_tmp(dir) == 0);
|
|
|
|
/* the save path is writable again */
|
|
json = accounts_json("z");
|
|
CHECK(dc_store_save_accounts(store, json) == 0);
|
|
dc_json_free(json);
|
|
json = dc_store_load_accounts(store);
|
|
CHECK(json != NULL);
|
|
if (json != NULL) {
|
|
CHECK(strcmp(accounts_id(json), "z") == 0);
|
|
dc_json_free(json);
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
|
test_default_dir(const char *tmpdir)
|
|
{
|
|
char want[512];
|
|
DcStore *store;
|
|
|
|
CHECK(setenv("HOME", tmpdir, 1) == 0);
|
|
store = dc_store_open(NULL);
|
|
CHECK(store != NULL);
|
|
if (store != NULL) {
|
|
snprintf(want, sizeof(want), "%s/.dt/dtchat", tmpdir);
|
|
CHECK(strcmp(dc_store_dir(store), want) == 0);
|
|
dc_store_close(store);
|
|
}
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char tmpdir[] = "/tmp/opencode/dtchat-store-XXXXXX";
|
|
char dir[512];
|
|
char path[512];
|
|
DcStore *store;
|
|
|
|
if (mkdtemp(tmpdir) == NULL) {
|
|
perror("mkdtemp");
|
|
return 1;
|
|
}
|
|
snprintf(dir, sizeof(dir), "%s/state", tmpdir);
|
|
store = dc_store_open(dir);
|
|
if (store == NULL) {
|
|
perror("dc_store_open");
|
|
return 1;
|
|
}
|
|
test_store(store, dir);
|
|
dc_store_close(store);
|
|
test_default_dir(tmpdir);
|
|
|
|
/* clean up */
|
|
path_join(path, sizeof(path), dir, "accounts.json");
|
|
unlink(path);
|
|
path_join(path, sizeof(path), dir, "accounts.json.bad");
|
|
unlink(path);
|
|
path_join(path, sizeof(path), dir, "secrets.json");
|
|
unlink(path);
|
|
path_join(path, sizeof(path), dir, "identity.json");
|
|
unlink(path);
|
|
rmdir(dir);
|
|
snprintf(path, sizeof(path), "%s/.dt/dtchat", tmpdir);
|
|
rmdir(path);
|
|
snprintf(path, sizeof(path), "%s/.dt", tmpdir);
|
|
rmdir(path);
|
|
rmdir(tmpdir);
|
|
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_store: all ok\n");
|
|
return 0;
|
|
}
|