523 lines
19 KiB
C
523 lines
19 KiB
C
#include "sync.h"
|
|
#include "sync_nodes.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
typedef struct TestCtx {
|
|
char token[64];
|
|
char expires[32];
|
|
char device_private_key[129];
|
|
char device_public_key[65];
|
|
char device_key_id[65];
|
|
char registered_device_key_id[65];
|
|
char last_method[16];
|
|
char last_url[256];
|
|
char last_body[10000];
|
|
char last_auth[128];
|
|
char last_user[128];
|
|
char last_signature[256];
|
|
char last_transaction[10000];
|
|
int apply_called;
|
|
int purge_called;
|
|
int http_calls;
|
|
char last_storage_key[192];
|
|
} TestCtx;
|
|
|
|
static int failures = 0;
|
|
|
|
static void
|
|
check(int condition, const char *message)
|
|
{
|
|
if(!condition) {
|
|
fprintf(stderr, "FAIL: %s\n", message);
|
|
failures++;
|
|
}
|
|
}
|
|
|
|
static const char *
|
|
test_get_text(const char *key, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
|
|
snprintf(ctx->last_storage_key, sizeof(ctx->last_storage_key), "%s", key);
|
|
if(strcmp(key, "sync_device_private_key") == 0)
|
|
return ctx->device_private_key;
|
|
if(strcmp(key, "sync_device_public_key") == 0)
|
|
return ctx->device_public_key;
|
|
if(strcmp(key, "sync_device_key_id") == 0)
|
|
return ctx->device_key_id;
|
|
if(strstr(key, "sync_auth_token_expires_at") != NULL) {
|
|
return ctx->expires;
|
|
}
|
|
if(strstr(key, "sync_auth_token") != NULL) {
|
|
return ctx->token;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
static void
|
|
test_set_text(const char *key, const char *value, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
|
|
snprintf(ctx->last_storage_key, sizeof(ctx->last_storage_key), "%s", key);
|
|
if(strcmp(key, "sync_device_private_key") == 0) {
|
|
snprintf(ctx->device_private_key, sizeof(ctx->device_private_key), "%s",
|
|
value != NULL ? value : "");
|
|
} else if(strcmp(key, "sync_device_public_key") == 0) {
|
|
snprintf(ctx->device_public_key, sizeof(ctx->device_public_key), "%s",
|
|
value != NULL ? value : "");
|
|
} else if(strcmp(key, "sync_device_key_id") == 0) {
|
|
snprintf(ctx->device_key_id, sizeof(ctx->device_key_id), "%s",
|
|
value != NULL ? value : "");
|
|
} else if(strstr(key, "sync_auth_token_expires_at") != NULL) {
|
|
snprintf(ctx->expires, sizeof(ctx->expires), "%s", value != NULL ? value : "");
|
|
} else if(strstr(key, "sync_auth_token") != NULL) {
|
|
snprintf(ctx->token, sizeof(ctx->token), "%s", value != NULL ? value : "");
|
|
}
|
|
}
|
|
|
|
static int
|
|
test_http(const char *method, const char *url, const char *body,
|
|
const char *const *headers, int header_count,
|
|
SyncBuffer *response, long *status, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
ctx->http_calls++;
|
|
snprintf(ctx->last_method, sizeof(ctx->last_method), "%s", method != NULL ? method : "");
|
|
snprintf(ctx->last_url, sizeof(ctx->last_url), "%s", url != NULL ? url : "");
|
|
snprintf(ctx->last_body, sizeof(ctx->last_body), "%s", body != NULL ? body : "");
|
|
ctx->last_auth[0] = '\0';
|
|
ctx->last_user[0] = '\0';
|
|
ctx->last_signature[0] = '\0';
|
|
ctx->last_transaction[0] = '\0';
|
|
for(int i = 0; i < header_count; i++) {
|
|
if(headers[i] != NULL && strncmp(headers[i], "Authorization:", 14) == 0)
|
|
snprintf(ctx->last_auth, sizeof(ctx->last_auth), "%s", headers[i]);
|
|
if(headers[i] != NULL &&
|
|
(strncmp(headers[i], "X-Daochi-User:", strlen("X-Daochi-User:")) == 0 ||
|
|
strncmp(headers[i], "X-Custom-User:", strlen("X-Custom-User:")) == 0))
|
|
snprintf(ctx->last_user, sizeof(ctx->last_user), "%s", headers[i]);
|
|
if(headers[i] != NULL &&
|
|
(strncmp(headers[i], "X-Daochi-Signature:", strlen("X-Daochi-Signature:")) == 0 ||
|
|
strncmp(headers[i], "X-Custom-Signature:", strlen("X-Custom-Signature:")) == 0))
|
|
snprintf(ctx->last_signature, sizeof(ctx->last_signature), "%s", headers[i]);
|
|
if(headers[i] != NULL &&
|
|
strncmp(headers[i], "X-Daochi-Tx:", strlen("X-Daochi-Tx:")) == 0)
|
|
snprintf(ctx->last_transaction, sizeof(ctx->last_transaction), "%s",
|
|
headers[i]);
|
|
}
|
|
if(strstr(url, "/api/v1/account/devices") != NULL)
|
|
FindSyncJSONString(body, "device_key_id", ctx->registered_device_key_id,
|
|
sizeof(ctx->registered_device_key_id));
|
|
if(status != NULL)
|
|
*status = 200;
|
|
return AppendSyncBuffer(response, "{\"status\":\"ok\",\"changes\":{}}",
|
|
strlen("{\"status\":\"ok\",\"changes\":{}}"));
|
|
}
|
|
|
|
static int
|
|
test_failover_http(const char *method, const char *url, const char *body,
|
|
const char *const *headers, int header_count,
|
|
SyncBuffer *response, long *status, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
|
|
if(strstr(url, "192.168.1.10") != NULL) {
|
|
ctx->http_calls++;
|
|
if(status != NULL) {
|
|
*status = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
return test_http(method, url, body, headers, header_count,
|
|
response, status, user);
|
|
}
|
|
|
|
static char *
|
|
test_build_payload(const char *user_id_hash, const char *public_key_hex, void *user)
|
|
{
|
|
(void)public_key_hex;
|
|
(void)user;
|
|
char *payload = malloc(96);
|
|
if(payload != NULL)
|
|
snprintf(payload, 96, "{\"user_id_hash\":\"%s\",\"client_id\":\"client-123\"}",
|
|
user_id_hash);
|
|
return payload;
|
|
}
|
|
|
|
static void
|
|
test_free_payload(char *payload, void *user)
|
|
{
|
|
(void)user;
|
|
free(payload);
|
|
}
|
|
|
|
static int
|
|
test_apply_response(const char *response_json, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
ctx->apply_called++;
|
|
return response_json != NULL && strstr(response_json, "\"status\":\"ok\"") != NULL;
|
|
}
|
|
|
|
static void
|
|
test_purge(void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
ctx->purge_called++;
|
|
}
|
|
|
|
static void
|
|
fill_account(SyncAccount *account)
|
|
{
|
|
memset(account, 0, sizeof(*account));
|
|
memset(account->public_id, 'a', 64);
|
|
account->public_id[64] = '\0';
|
|
snprintf(account->public_key_hex, sizeof(account->public_key_hex), "pk");
|
|
snprintf(account->private_key_hex, sizeof(account->private_key_hex), "sk");
|
|
}
|
|
|
|
static void
|
|
test_url_helpers(void)
|
|
{
|
|
char out[128];
|
|
|
|
check(IsSyncURLValid("https://api.example.test"), "https url valid");
|
|
check(IsSyncURLValid("localhost:8080"), "localhost shorthand valid");
|
|
check(IsSyncURLValid("192.168.100.97:18080"), "private lan shorthand valid");
|
|
check(IsSyncURLValid("http://10.1.2.3:18080"), "private 10/8 http valid");
|
|
check(IsSyncURLValid("http://172.16.0.1:18080"), "private 172.16/12 lower valid");
|
|
check(IsSyncURLValid("http://172.31.255.254:18080"), "private 172.16/12 upper valid");
|
|
check(!IsSyncURLValid("http://example.test"), "plain remote http rejected");
|
|
check(!IsSyncURLValid("http://192.168.100.97.evil.test"),
|
|
"private-looking http hostname rejected");
|
|
check(!IsSyncURLValid("http://172.32.0.1:18080"),
|
|
"public 172 range http rejected");
|
|
check(NormalizeSyncURL("localhost:8080", out, sizeof(out)) &&
|
|
strcmp(out, "http://localhost:8080") == 0,
|
|
"normalize localhost");
|
|
check(NormalizeSyncURL("192.168.100.97:18080", out, sizeof(out)) &&
|
|
strcmp(out, "http://192.168.100.97:18080") == 0,
|
|
"normalize private lan");
|
|
check(JoinSyncURL(out, sizeof(out), "https://api.example.test/",
|
|
"/api/v1/sync") &&
|
|
strcmp(out, "https://api.example.test/api/v1/sync") == 0,
|
|
"join trims trailing slash");
|
|
check(JoinSyncWebSocketURL(out, sizeof(out), "https://api.example.test",
|
|
"/api/v1/sync/ws") &&
|
|
strcmp(out, "wss://api.example.test/api/v1/sync/ws") == 0,
|
|
"join websocket https");
|
|
}
|
|
|
|
static void
|
|
test_json_helpers(void)
|
|
{
|
|
SyncBuffer buffer = {0};
|
|
char value[32];
|
|
|
|
check(AppendSyncBufferJSONString(&buffer, "a\"b\n") &&
|
|
strcmp(buffer.data, "\"a\\\"b\\n\"") == 0,
|
|
"json string escaping");
|
|
FreeSyncBuffer(&buffer);
|
|
check(FindSyncJSONString("{\"auth_token\":\"tok\"}", "auth_token",
|
|
value, sizeof(value)) &&
|
|
strcmp(value, "tok") == 0,
|
|
"json string find");
|
|
check(FindSyncJSONInt64("{\"expires_in_seconds\":42}",
|
|
"expires_in_seconds", 0) == 42,
|
|
"json int find");
|
|
}
|
|
|
|
static void
|
|
test_sync_run_with_valid_token(void)
|
|
{
|
|
TestCtx ctx = {0};
|
|
SyncAccount account;
|
|
SyncConfig cfg;
|
|
SyncResult result;
|
|
|
|
fill_account(&account);
|
|
snprintf(ctx.token, sizeof(ctx.token), "saved-token");
|
|
snprintf(ctx.expires, sizeof(ctx.expires), "%lld", (long long)time(NULL) + 3600);
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.base_url = "https://api.example.test";
|
|
cfg.account = &account;
|
|
cfg.client_id = "client-123";
|
|
cfg.http_request = test_http;
|
|
cfg.get_text = test_get_text;
|
|
cfg.set_text = test_set_text;
|
|
cfg.build_payload = test_build_payload;
|
|
cfg.free_payload = test_free_payload;
|
|
cfg.apply_response = test_apply_response;
|
|
cfg.purge_synced_deleted = test_purge;
|
|
cfg.user = &ctx;
|
|
|
|
result = RunSync(&cfg);
|
|
check(result == SYNC_OK, "sync run returns ok");
|
|
check(strcmp(ctx.last_method, "POST") == 0, "sync uses post");
|
|
check(strcmp(ctx.last_url, "https://api.example.test/api/v1/sync") == 0,
|
|
"sync posts to sync path");
|
|
check(strcmp(ctx.last_auth, "Authorization: Bearer saved-token") == 0,
|
|
"sync sends bearer token");
|
|
check(strncmp(ctx.last_user, "X-Daochi-User:",
|
|
strlen("X-Daochi-User:")) == 0,
|
|
"sync uses Daochi user header by default");
|
|
check(ctx.apply_called == 1, "sync applies response");
|
|
check(ctx.purge_called == 1, "sync purges after success");
|
|
}
|
|
|
|
static void
|
|
test_sync_node_failover(void)
|
|
{
|
|
TestCtx ctx = {0};
|
|
SyncAccount account;
|
|
SyncConfig cfg;
|
|
SyncNodePool pool;
|
|
const SyncNode *active;
|
|
SyncResult result;
|
|
|
|
fill_account(&account);
|
|
snprintf(ctx.token, sizeof(ctx.token), "saved-token");
|
|
snprintf(ctx.expires, sizeof(ctx.expires), "%lld", (long long)time(NULL) + 3600);
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.account = &account;
|
|
cfg.client_id = "client-123";
|
|
cfg.http_request = test_failover_http;
|
|
cfg.get_text = test_get_text;
|
|
cfg.set_text = test_set_text;
|
|
cfg.build_payload = test_build_payload;
|
|
cfg.free_payload = test_free_payload;
|
|
cfg.apply_response = test_apply_response;
|
|
cfg.user = &ctx;
|
|
|
|
InitSyncNodePool(&pool);
|
|
check(AddSyncNode(&pool, "local-node", "Home", "192.168.1.10:8080",
|
|
SYNC_NODE_PAIRED, 1),
|
|
"add paired local node");
|
|
check(AddSyncNode(&pool, "public-node", "Public", "https://api.example.test",
|
|
SYNC_NODE_PUBLIC, 0),
|
|
"add public fallback node");
|
|
check(AddSyncNode(&pool, "local-node", "Home paired", "http://192.168.1.10:8080/",
|
|
SYNC_NODE_PAIRED, 1),
|
|
"update a normalized duplicate node");
|
|
check(pool.count == 2 && strcmp(pool.nodes[0].name, "Home paired") == 0,
|
|
"duplicate node updates without creating another entry");
|
|
result = RunSyncWithNodes(&pool, &cfg);
|
|
check(result == SYNC_OK, "node sync falls back after local failure");
|
|
active = GetActiveSyncNode(&pool);
|
|
check(active != NULL && strcmp(active->name, "Public") == 0,
|
|
"public fallback becomes active");
|
|
check(ctx.http_calls == 2, "local and public nodes attempted once");
|
|
check(strstr(ctx.last_storage_key, "sync_node_public-node_") == ctx.last_storage_key,
|
|
"credentials scoped to active node");
|
|
check(strncmp(ctx.last_user, "X-Daochi-User:", strlen("X-Daochi-User:")) == 0,
|
|
"clean node sync uses Daochi wire header");
|
|
}
|
|
|
|
static int
|
|
test_login_http(const char *method, const char *url, const char *body,
|
|
const char *const *headers, int header_count,
|
|
SyncBuffer *response, long *status, void *user)
|
|
{
|
|
TestCtx *ctx = user;
|
|
|
|
test_http(method, url, body, headers, header_count, response, status, user);
|
|
if(strstr(url, "/challenge") != NULL) {
|
|
FreeSyncBuffer(response);
|
|
return AppendSyncBuffer(response,
|
|
"{\"nonce\":\"0000000000000000000000000000000000000000000000000000000000000000\"}",
|
|
strlen("{\"nonce\":\"0000000000000000000000000000000000000000000000000000000000000000\"}"));
|
|
}
|
|
if(strstr(url, "/login") != NULL) {
|
|
(void)ctx;
|
|
FreeSyncBuffer(response);
|
|
return AppendSyncBuffer(response,
|
|
"{\"auth_token\":\"new-token\",\"expires_in_seconds\":3600}",
|
|
strlen("{\"auth_token\":\"new-token\",\"expires_in_seconds\":3600}"));
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
test_login_uses_configured_wire_names(void)
|
|
{
|
|
TestCtx ctx = {0};
|
|
SyncAccount account;
|
|
SyncConfig cfg;
|
|
SyncResult result;
|
|
|
|
if(!CreateSyncAccount(&account)) {
|
|
check(0, "create account for login override test");
|
|
return;
|
|
}
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.base_url = "https://api.example.test";
|
|
cfg.account = &account;
|
|
cfg.client_id = "client-123";
|
|
cfg.signature_context = "custom-sync-v1";
|
|
cfg.user_header_name = "X-Custom-User";
|
|
cfg.signature_header_name = "X-Custom-Signature";
|
|
cfg.http_request = test_login_http;
|
|
cfg.get_text = test_get_text;
|
|
cfg.set_text = test_set_text;
|
|
cfg.user = &ctx;
|
|
|
|
result = LoginSync(&cfg);
|
|
check(result == SYNC_OK, "login with configured wire names returns ok");
|
|
check(strncmp(ctx.last_user, "X-Custom-User:", strlen("X-Custom-User:")) == 0,
|
|
"login uses configured user header");
|
|
check(strncmp(ctx.last_signature, "X-Custom-Signature:",
|
|
strlen("X-Custom-Signature:")) == 0,
|
|
"login uses configured signature header");
|
|
check(strcmp(ctx.token, "new-token") == 0, "login saves auth token");
|
|
}
|
|
|
|
static void
|
|
test_json_hardening(void)
|
|
{
|
|
SyncBuffer buffer = {0};
|
|
char value[64];
|
|
|
|
/* control characters must be escaped, never emitted raw */
|
|
check(AppendSyncBufferJSONString(&buffer, "a\x01\x08" "b") &&
|
|
strcmp(buffer.data, "\"a\\u0001\\bb\"") == 0,
|
|
"json escapes control chars");
|
|
FreeSyncBuffer(&buffer);
|
|
/* escapes in values must survive the round trip */
|
|
check(FindSyncJSONString("{\"nonce\":\"a\\\\b\\n\u00e9\"}", "nonce",
|
|
value, sizeof(value)) &&
|
|
strcmp(value, "a\\b\n\xc3\xa9") == 0,
|
|
"json find decodes escapes");
|
|
check(FindSyncJSONString("{\"nonce\":\"\\uD83D\\uDE00\"}", "nonce",
|
|
value, sizeof(value)) &&
|
|
strcmp(value, "\xf0\x9f\x98\x80") == 0,
|
|
"json find decodes surrogate pairs");
|
|
/* a substring key must not satisfy a lookup for the short key */
|
|
check(!FindSyncJSONString("{\"sync_nonce\":\"x\"}", "nonce",
|
|
value, sizeof(value)),
|
|
"json find rejects substring keys");
|
|
/* key embedded inside a string value must not match */
|
|
check(!FindSyncJSONString("{\"noise\":\"\\\"nonce\\\":1\",\"x\":\"y\"}",
|
|
"nonce", value, sizeof(value)),
|
|
"json find rejects keys inside strings");
|
|
}
|
|
|
|
static void
|
|
test_v6_device_key_is_stable(void)
|
|
{
|
|
TestCtx ctx = {0};
|
|
SyncAccount account;
|
|
SyncConfig cfg;
|
|
char first_key_id[65];
|
|
SyncResult result;
|
|
|
|
check(CreateSyncAccount(&account), "create v6 test account");
|
|
if(!HasSyncAccountValues(&account))
|
|
return;
|
|
snprintf(ctx.token, sizeof(ctx.token), "token-v6");
|
|
snprintf(ctx.expires, sizeof(ctx.expires), "%lld",
|
|
(long long)time(NULL) + 3600);
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.base_url = "https://node.example";
|
|
cfg.account = &account;
|
|
cfg.client_id = "client-v6-device";
|
|
cfg.app_id = "fixture-app";
|
|
cfg.protocol_version = 6;
|
|
cfg.http_request = test_http;
|
|
cfg.get_text = test_get_text;
|
|
cfg.set_text = test_set_text;
|
|
cfg.build_payload = test_build_payload;
|
|
cfg.free_payload = test_free_payload;
|
|
cfg.apply_response = test_apply_response;
|
|
cfg.user = &ctx;
|
|
|
|
result = RunSync(&cfg);
|
|
check(result == SYNC_OK, "v6 sync succeeds");
|
|
check(ctx.registered_device_key_id[0] != '\0', "v6 registers device key");
|
|
check(strstr(ctx.last_transaction, ctx.registered_device_key_id) != NULL,
|
|
"v6 transaction uses registered device key");
|
|
snprintf(first_key_id, sizeof(first_key_id), "%s",
|
|
ctx.registered_device_key_id);
|
|
|
|
result = RunSync(&cfg);
|
|
check(result == SYNC_OK, "second v6 sync succeeds");
|
|
check(strcmp(first_key_id, ctx.registered_device_key_id) == 0,
|
|
"v6 reuses installation device key");
|
|
check(strstr(ctx.last_transaction, first_key_id) != NULL,
|
|
"second v6 transaction uses stable device key");
|
|
}
|
|
|
|
static void
|
|
test_payload_encryption(void)
|
|
{
|
|
SyncAccount account;
|
|
char *wrapped = NULL;
|
|
char *unwrapped = NULL;
|
|
char large_payload[2048];
|
|
size_t off = 0;
|
|
|
|
/* payload encryption derives its key from the real private key, so a
|
|
* genuine keypair is required */
|
|
if(!CreateSyncAccount(&account)) {
|
|
check(0, "payload wrap ok (account creation failed)");
|
|
return;
|
|
}
|
|
check(WrapSyncPayload(&account, "{\"sessions\":[1,2,3]}", &wrapped),
|
|
"payload wrap ok");
|
|
check(wrapped != NULL && strncmp(wrapped, "{\"v\":1", 6) == 0,
|
|
"small payload keeps v1 envelope");
|
|
check(strstr(wrapped, "sessions") == NULL,
|
|
"payload wrap hides plaintext");
|
|
check(UnwrapSyncPayload(&account, wrapped, &unwrapped) &&
|
|
unwrapped != NULL &&
|
|
strcmp(unwrapped, "{\"sessions\":[1,2,3]}") == 0,
|
|
"payload unwrap roundtrip");
|
|
free(wrapped);
|
|
free(unwrapped);
|
|
check(!UnwrapSyncPayload(&account, "{\"v\":1,\"oops\":1}", &unwrapped),
|
|
"payload unwrap rejects malformed envelope");
|
|
|
|
wrapped = NULL;
|
|
unwrapped = NULL;
|
|
off += snprintf(large_payload + off, sizeof(large_payload) - off,
|
|
"{\"changes\":[");
|
|
for(int i = 0; i < 42 && off + 80 < sizeof(large_payload); i++) {
|
|
off += snprintf(large_payload + off, sizeof(large_payload) - off,
|
|
"%s{\"type\":\"session\",\"state\":\"synced\",\"title\":\"same-title\"}",
|
|
i == 0 ? "" : ",");
|
|
}
|
|
snprintf(large_payload + off, sizeof(large_payload) - off, "]}");
|
|
check(WrapSyncPayload(&account, large_payload, &wrapped),
|
|
"large payload wrap ok");
|
|
check(wrapped != NULL && strncmp(wrapped, "{\"v\":2", 6) == 0,
|
|
"large payload uses compressed v2 envelope");
|
|
check(strstr(wrapped, "\"compression\":\"lzss1\"") != NULL,
|
|
"compressed envelope marks codec");
|
|
check(strstr(wrapped, "same-title") == NULL,
|
|
"compressed envelope hides plaintext");
|
|
check(UnwrapSyncPayload(&account, wrapped, &unwrapped) &&
|
|
unwrapped != NULL &&
|
|
strcmp(unwrapped, large_payload) == 0,
|
|
"compressed payload unwrap roundtrip");
|
|
free(wrapped);
|
|
free(unwrapped);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_url_helpers();
|
|
test_json_helpers();
|
|
test_json_hardening();
|
|
test_payload_encryption();
|
|
test_v6_device_key_is_stable();
|
|
test_sync_run_with_valid_token();
|
|
test_sync_node_failover();
|
|
test_login_uses_configured_wire_names();
|
|
return failures == 0 ? 0 : 1;
|
|
}
|