Cover atomic outgoing attachment request creation and timer guard
This commit is contained in:
1 parent
72671f8307
commit
10c766ed0b
2 files changed
+120
-11
No files matched your search
+1
-1
@@ -473,7 +473,7 @@ test_attachment_requests_LDADD = $(LIBUTUN) $(CRYPTO_LIBS) $(COMMON_LIBS)
|
||||
test_attachment_requests_LDFLAGS = -Wl,--wrap=sqlite3_exec -Wl,--wrap=sqlite3_prepare_v2 -Wl,--wrap=sqlite3_bind_blob \
|
||||
-Wl,--wrap=sqlite3_bind_text -Wl,--wrap=sqlite3_bind_int64 -Wl,--wrap=sqlite3_bind_int -Wl,--wrap=sqlite3_step \
|
||||
-Wl,--wrap=sqlite3_finalize -Wl,--wrap=sqlite3_changes -Wl,--wrap=etcp_router_has_route -Wl,--wrap=etcp_router_conn_get \
|
||||
-Wl,--wrap=etcp_router_send_q_has_room -Wl,--wrap=etcp_router_conn_send
|
||||
-Wl,--wrap=etcp_router_send_q_has_room -Wl,--wrap=etcp_router_conn_send -Wl,--wrap=uasync_set_timeout
|
||||
test_attachment_decisions_SOURCES = test_attachment_decisions.c
|
||||
test_attachment_decisions_LDADD = $(LIBUTUN) $(CRYPTO_LIBS) $(COMMON_LIBS)
|
||||
test_attachment_decisions_LDFLAGS = -Wl,--wrap=sqlite3_exec -Wl,--wrap=sqlite3_prepare_v2 -Wl,--wrap=sqlite3_bind_blob \
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "../src/utun_instance.h"
|
||||
#include "../src/chat/chat_core.h"
|
||||
#include "../src/chat/chat_attachment.h"
|
||||
#include "../src/chat/chat_core_priv.h"
|
||||
#include "../src/chat/chat_event.h"
|
||||
#include "../src/chat/member_sync.h"
|
||||
#include "../src/routing_layer/topo_node_sqlite.h"
|
||||
@@ -18,7 +19,7 @@
|
||||
#include "../lib/debug_config.h"
|
||||
#include "../lib/mem.h"
|
||||
|
||||
enum operation { NO_OPERATION, IDENTITY, MEMBER, AUTHOR, READ, INSERT, OUTBOX };
|
||||
enum operation { NO_OPERATION, IDENTITY, MEMBER, AUTHOR, READ, INSERT, OUTBOX, PENDING, ELIGIBLE };
|
||||
enum failure { NO_FAILURE, BEGIN, PREPARE, BIND, STEP, FINALIZE, CHANGES, COMMIT };
|
||||
static enum failure fault;
|
||||
static enum operation fault_operation, last_operation;
|
||||
@@ -27,7 +28,7 @@ static unsigned fault_hits, cases;
|
||||
|
||||
struct snapshot {
|
||||
uint8_t key[32];
|
||||
int exists, incoming, state, command, error;
|
||||
int exists, incoming, state, command, error, key_bytes;
|
||||
int64_t created, retry;
|
||||
};
|
||||
struct fixture {
|
||||
@@ -40,18 +41,27 @@ struct fixture {
|
||||
uint64_t peer;
|
||||
struct snapshot expected;
|
||||
int64_t started;
|
||||
int allow_result, route_up, room, send_rc, corrupt, flags;
|
||||
int allow_result, route_up, room, send_rc, corrupt, flags, outgoing;
|
||||
unsigned acks, events;
|
||||
};
|
||||
static struct fixture* active;
|
||||
static timeout_callback_t attachment_tick;
|
||||
|
||||
/* Вызываем зарегистрированный production callback без ожидания секунды; старый таймер отменяем. */
|
||||
extern void* __real_uasync_set_timeout(struct UASYNC*, int, void*, timeout_callback_t, const char*);
|
||||
void* __wrap_uasync_set_timeout(struct UASYNC* ua, int delay, void* arg, timeout_callback_t callback, const char* name) {
|
||||
if (name && !strcmp(name, "attachment keys")) attachment_tick = callback;
|
||||
return __real_uasync_set_timeout(ua, delay, arg, callback, name);
|
||||
}
|
||||
|
||||
static enum operation operation(const char* query) {
|
||||
if (!query) return NO_OPERATION;
|
||||
static const char* prefixes[] = {NULL, "SELECT crypto_id,cipher_hash,key FROM attachment_keys WHERE",
|
||||
"SELECT x25519_pubkey,ed25519_pubkey,signed_by,signature,join_sig,join_ts", "SELECT 1 FROM msg_",
|
||||
"SELECT incoming,state FROM attachment_key_requests WHERE", "INSERT INTO attachment_key_requests(",
|
||||
"INSERT INTO attachment_key_outbox("};
|
||||
for (enum operation op = IDENTITY; op <= OUTBOX; op++)
|
||||
"INSERT INTO attachment_key_outbox(", "SELECT channel,message_id,crypto_id,author,key,cipher_hash,cipher_path,plain_path",
|
||||
"SELECT 1 FROM attachment_keys a WHERE"};
|
||||
for (enum operation op = IDENTITY; op <= ELIGIBLE; op++)
|
||||
if (!strncmp(query, prefixes[op], strlen(prefixes[op]))) return op;
|
||||
return NO_OPERATION;
|
||||
}
|
||||
@@ -141,7 +151,8 @@ static struct snapshot snapshot(struct fixture* f) {
|
||||
assert(sqlite3_bind_text(st, 1, f->channel, -1, SQLITE_STATIC) == SQLITE_OK);
|
||||
assert(sqlite3_bind_blob(st, 2, f->message, 32, SQLITE_STATIC) == SQLITE_OK);
|
||||
assert(sqlite3_bind_int64(st, 3, (sqlite3_int64)f->peer) == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW);
|
||||
assert(sqlite3_column_bytes(st, 0) == 32); memcpy(result.key, sqlite3_column_blob(st, 0), 32);
|
||||
result.key_bytes = sqlite3_column_bytes(st, 0); assert(result.key_bytes == 0 || result.key_bytes == 32);
|
||||
if (result.key_bytes) memcpy(result.key, sqlite3_column_blob(st, 0), 32);
|
||||
result.error = sqlite3_column_int(st, 1); result.exists = sqlite3_column_int(st, 2);
|
||||
result.incoming = sqlite3_column_int(st, 3); result.state = sqlite3_column_int(st, 4);
|
||||
result.created = sqlite3_column_int64(st, 5); result.command = sqlite3_column_int(st, 6); result.retry = sqlite3_column_int64(st, 7);
|
||||
@@ -154,7 +165,8 @@ static void equal(struct fixture* f) {
|
||||
if (f->expected.created < 0) {
|
||||
assert(current.created >= f->started && current.created <= (int64_t)time(NULL)); f->expected.created = current.created;
|
||||
}
|
||||
assert(!memcmp(current.key, f->expected.key, 32) && current.error == f->expected.error && current.exists == f->expected.exists);
|
||||
assert(current.key_bytes == f->expected.key_bytes && !memcmp(current.key, f->expected.key, 32));
|
||||
assert(current.error == f->expected.error && current.exists == f->expected.exists);
|
||||
assert(current.incoming == f->expected.incoming && current.state == f->expected.state && current.created == f->expected.created);
|
||||
assert(current.command == f->expected.command && current.retry == f->expected.retry);
|
||||
sqlite3_stmt* st = NULL;
|
||||
@@ -187,15 +199,24 @@ int __wrap_etcp_router_send_q_has_room(struct UTUN_INSTANCE* inst, uint64_t grou
|
||||
extern int __real_etcp_router_conn_send(struct ETCP_ROUTER_CONN*, const uint8_t*, size_t, int);
|
||||
int __wrap_etcp_router_conn_send(struct ETCP_ROUTER_CONN* conn, const uint8_t* data, size_t length, int flags) {
|
||||
if (!active || conn != &active->route) return __real_etcp_router_conn_send(conn, data, length, flags);
|
||||
assert(active->allow_result && length == 85 && !memcmp(data, "AK01", 4) && data[4] == 2);
|
||||
assert(active->allow_result && length == 85 && !memcmp(data, "AK01", 4) && data[4] == (active->outgoing ? 1 : 2));
|
||||
assert(flags == (ROUTER_FLAG_SIGNED | ROUTER_FLAG_ENCRYPTED) && sqlite3_get_autocommit(active->inst->topo_sqlite_db));
|
||||
assert(!memcmp(data + 5, active->message, 32) && !memcmp(data + 37, active->id, 16) && !memcmp(data + 53, active->hash, 32));
|
||||
if (active->outgoing) {
|
||||
active->expected.retry = snapshot(active).retry;
|
||||
assert(active->expected.retry >= active->started + 5 && active->expected.retry <= (int64_t)time(NULL) + 5);
|
||||
}
|
||||
equal(active); active->acks++; return active->send_rc;
|
||||
}
|
||||
|
||||
static void event(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int bytes) {
|
||||
if (type != CHAT_EVT_ATTACHMENT_KEYS_CHANGED) return;
|
||||
struct fixture* f = active;
|
||||
if (f && !f->allow_result) {
|
||||
struct snapshot seen = snapshot(f);
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "request test: unexpected event outgoing=%d committed request=%d state=%d command=%d autocommit=%d",
|
||||
f->outgoing, seen.exists, seen.state, seen.command, sqlite3_get_autocommit(inst->topo_sqlite_db));
|
||||
}
|
||||
assert(f && f->inst == inst && f->allow_result && sqlite3_get_autocommit(inst->topo_sqlite_db));
|
||||
assert(bytes == (int)strlen(f->channel) + 1 && data[0] == strlen(f->channel) && !memcmp(data + 1, f->channel, strlen(f->channel)));
|
||||
equal(f); f->events++;
|
||||
@@ -374,10 +395,98 @@ static void test_rollback_failure(void) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS attachment requests: failed rollback sends no ACK/event, quiesces/stops core, owner close rolls back request");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* Получатель уже имеет проверенный ciphertext; именно эти локальные строки потребляет таймер. */
|
||||
static void outgoing_reset(struct fixture* f, int queued) {
|
||||
reset(f, -1, 0); f->outgoing = 1;
|
||||
char query[768];
|
||||
snprintf(query, sizeof(query), "UPDATE attachment_keys SET author=%lld,key=NULL,downloaded=1,wanted=1,ready=0,decrypting=0,error=0"
|
||||
" WHERE channel='%s'", (long long)(sqlite3_int64)f->peer, f->channel); sql(f, query);
|
||||
if (queued) {
|
||||
snprintf(query, sizeof(query), "INSERT INTO attachment_key_outbox(channel,message_id,peer,command,retry_at)"
|
||||
" SELECT channel,message_id,author,4,9223372036854775807 FROM attachment_keys WHERE channel='%s'", f->channel); sql(f, query);
|
||||
}
|
||||
}
|
||||
|
||||
static void outgoing_tick(struct fixture* f, int create, int sends) {
|
||||
f->expected = snapshot(f); f->allow_result = create || sends; f->acks = f->events = 0; f->started = (int64_t)time(NULL);
|
||||
if (create) {
|
||||
f->expected.exists = 1; f->expected.incoming = 0; f->expected.state = 0;
|
||||
f->expected.created = -1; f->expected.command = 1; f->expected.retry = 0;
|
||||
}
|
||||
assert(attachment_tick && CC(f->inst)->attachment_timer);
|
||||
uasync_cancel_timeout(f->ua, CC(f->inst)->attachment_timer); attachment_tick(f->inst);
|
||||
if (sends || (create && (!f->route_up || !f->room))) {
|
||||
f->expected.retry = snapshot(f).retry;
|
||||
assert(f->expected.retry >= f->started + 5 && f->expected.retry <= (int64_t)time(NULL) + 5);
|
||||
}
|
||||
equal(f); assert(f->events == (unsigned)create && f->acks == (unsigned)sends);
|
||||
}
|
||||
|
||||
static void test_outgoing_controls(void) {
|
||||
struct fixture f; setup(&f); outgoing_reset(&f, 0);
|
||||
const char* triggers[] = {
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE INSERT ON attachment_key_requests BEGIN SELECT RAISE(IGNORE); END",
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE INSERT ON attachment_key_requests BEGIN SELECT RAISE(ABORT,'request'); END",
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE INSERT ON attachment_key_outbox BEGIN SELECT RAISE(IGNORE); END",
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE INSERT ON attachment_key_outbox BEGIN SELECT RAISE(ABORT,'outbox'); END",
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE UPDATE ON attachment_key_outbox BEGIN SELECT RAISE(IGNORE); END",
|
||||
"CREATE TEMP TRIGGER reject_request BEFORE UPDATE ON attachment_key_outbox BEGIN SELECT RAISE(ABORT,'upsert'); END"
|
||||
};
|
||||
for (unsigned i = 0; i < sizeof(triggers) / sizeof(triggers[0]); i++) {
|
||||
outgoing_reset(&f, i >= 4); sql(&f, triggers[i]); outgoing_tick(&f, 0, 0);
|
||||
sql(&f, "DROP TRIGGER reject_request"); outgoing_tick(&f, 1, 1); outgoing_tick(&f, 0, 0);
|
||||
}
|
||||
outgoing_reset(&f, 0); sql(&f, "BEGIN;UPDATE attachment_keys SET error=8 WHERE channel='900'");
|
||||
outgoing_tick(&f, 0, 0); assert(!sqlite3_get_autocommit(f.inst->topo_sqlite_db));
|
||||
sql(&f, "ROLLBACK"); outgoing_tick(&f, 1, 1);
|
||||
/* Уже сохранённая очередь во внешней транзакции также не уходит в сеть. */
|
||||
sql(&f, "UPDATE attachment_key_outbox SET retry_at=0;BEGIN;UPDATE attachment_keys SET error=8 WHERE channel='900'");
|
||||
outgoing_tick(&f, 0, 0); assert(!sqlite3_get_autocommit(f.inst->topo_sqlite_db));
|
||||
sql(&f, "ROLLBACK"); outgoing_tick(&f, 0, 1);
|
||||
outgoing_reset(&f, 0); unsigned commits = 0; sqlite3_commit_hook(f.inst->topo_sqlite_db, veto, &commits);
|
||||
outgoing_tick(&f, 0, 0); assert(commits == 1); sqlite3_commit_hook(f.inst->topo_sqlite_db, NULL, NULL);
|
||||
outgoing_tick(&f, 1, 1); cleanup(&f);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS outgoing requests: ABORT/IGNORE, commit veto, foreign transaction postpones creation and send");
|
||||
}
|
||||
|
||||
static void test_outgoing_faults(void) {
|
||||
struct fixture f; setup(&f); unsigned count = 0;
|
||||
for (int queued = 0; queued < 2; queued++) {
|
||||
const enum operation operations[] = {NO_OPERATION, PENDING, ELIGIBLE, INSERT, OUTBOX, NO_OPERATION};
|
||||
for (unsigned i = 0; i < sizeof(operations) / sizeof(operations[0]); i++) {
|
||||
enum operation op = operations[i];
|
||||
int binds = op == ELIGIBLE || op == INSERT ? 3 : op == OUTBOX ? 4 : 0;
|
||||
int writes = op == INSERT || op == OUTBOX;
|
||||
int points = op ? 3 + binds + writes : 1;
|
||||
for (int point = 0; point < points; point++) {
|
||||
outgoing_reset(&f, queued);
|
||||
fault = !op ? (i ? COMMIT : BEGIN) : point == 0 ? PREPARE : point <= binds ? BIND :
|
||||
point == binds + 1 ? STEP : point == binds + 2 ? FINALIZE : CHANGES;
|
||||
fault_operation = op; fault_index = point; unsigned previous = fault_hits;
|
||||
outgoing_tick(&f, 0, 0); assert(!fault && fault_hits == previous + 1 && sqlite3_get_autocommit(f.inst->topo_sqlite_db));
|
||||
outgoing_tick(&f, 1, 1); outgoing_tick(&f, 0, 0); count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
cleanup(&f);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS outgoing requests: %u SQL faults/retries, both outbox paths, exact committed snapshots", count);
|
||||
}
|
||||
|
||||
static void test_outgoing_rollback_failure(void) {
|
||||
struct fixture f; setup(&f); outgoing_reset(&f, 0);
|
||||
fault = COMMIT; fault_operation = NO_OPERATION; fail_rollback = 1; outgoing_tick(&f, 0, 0);
|
||||
assert(!fault && !fail_rollback && !f.inst->running && !sqlite3_get_autocommit(f.inst->topo_sqlite_db));
|
||||
assert(!CC(f.inst)->attachment_timer && !f.inst->router_bindings.callbacks[ETCP_RT_ID_ATTACHMENT_KEYS]);
|
||||
chat_event_set_handler(f.inst, NULL); active = NULL; utun_instance_destroy(f.inst);
|
||||
equal(&f); assert(sqlite3_close(f.reader) == SQLITE_OK); uasync_destroy(f.ua, 0);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS outgoing requests: rollback failure stops core without event/send or rearming timer");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR); debug_set_category_level(DEBUG_CATEGORY_MEDIA, DEBUG_LEVEL_INFO);
|
||||
utun_instance_set_tun_init_enabled(0); size_t baseline = u_get_allocated_count();
|
||||
test_faults(); test_controls(); test_ack_retry(); test_rollback_failure();
|
||||
if (argc != 2 || strcmp(argv[1], "--outgoing")) { test_faults(); test_controls(); test_ack_retry(); test_rollback_failure(); }
|
||||
test_outgoing_controls(); test_outgoing_faults(); test_outgoing_rollback_failure();
|
||||
assert(!fault && !fail_rollback && u_get_allocated_count() == baseline);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS attachment requests: reader before ACK/events, stable duplicate date, neighboring key and allocations preserved");
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user