From f677c46b97c81b6ffadf970db317b9e9110a657b Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 02:28:42 +0100 Subject: [PATCH] Target local attachment transactions and observe recovery timers in tests --- tests/Makefile.am | 2 +- tests/test_chat_attachment.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index 9f10d59f7..f9a1a9254 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -489,7 +489,7 @@ test_chat_attachment_SOURCES = test_chat_attachment.c test_chat_attachment_LDADD = $(LIBUTUN) $(CRYPTO_LIBS) $(COMMON_LIBS) test_chat_attachment_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_step -Wl,--wrap=sqlite3_finalize \ - -Wl,--wrap=media_async_submit -Wl,--wrap=sqlite3_changes -Wl,--wrap=uasync_set_timeout + -Wl,--wrap=media_async_submit -Wl,--wrap=sqlite3_changes -Wl,--wrap=uasync_set_timeout -Wl,--wrap=sqlite3_bind_int endif test_attachment_send_SOURCES = test_attachment_send.c test_attachment_send_LDADD = $(LIBUTUN) $(CRYPTO_LIBS) $(COMMON_LIBS) diff --git a/tests/test_chat_attachment.c b/tests/test_chat_attachment.c index 4036c36ba..4dd72b0d8 100644 --- a/tests/test_chat_attachment.c +++ b/tests/test_chat_attachment.c @@ -23,6 +23,9 @@ static const char* insert_prefix = "INSERT INTO attachment_keys(channel,crypto_i static const char* fault_prefix; static int target_written; static timeout_callback_t attachment_tick; +static timeout_callback_t channel_backfill_tick, full_backfill_tick; +static const char* fault_transaction; +static int fault_transaction_open, block_index; static unsigned completion_calls; static int block_presence; static unsigned blocked_completions; @@ -36,8 +39,8 @@ struct fixture { sqlite3* reader; char directory[256], source[512], channel[32]; uint8_t id[16], key[32]; - unsigned rows, events, submissions, decrypt_submissions, key_events, deleted_events; - int success, inspect_submit, decrypt_mode, allow_worker, result_error, delete_mode; + unsigned rows, events, submissions, decrypt_submissions, key_events, deleted_events, received_events, attachment_events; + int success, inspect_submit, decrypt_mode, allow_worker, result_error, delete_mode, local_mode; }; static struct fixture* active; @@ -59,6 +62,8 @@ static int is_target(sqlite3_stmt* st) { 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; + if (name && !strcmp(name, "chat_md_chbl")) channel_backfill_tick = callback; + if (name && !strcmp(name, "chat_media_bl")) full_backfill_tick = callback; return __real_uasync_set_timeout(ua, delay, arg, callback, name); } @@ -70,15 +75,17 @@ int __wrap_sqlite3_exec(sqlite3* db, const char* sql, int (*cb)(void*, int, char } int binding_scope = fault_prefix && !strcmp(fault_prefix, "UPDATE attachment_keys SET message_id="); int completion_scope = fault_prefix && !binding_scope && strcmp(fault_prefix, "UPDATE attachment_keys SET decrypting=1 WHERE"); - int targeted = binding_scope ? strstr(sql, "/* attachment binding */") != NULL : + int targeted = fault_transaction ? strstr(sql, fault_transaction) != NULL : binding_scope ? strstr(sql, "/* attachment binding */") != NULL : !completion_scope || strstr(sql, "/* attachment completion */"); if (targeted && !strncmp(sql, "BEGIN IMMEDIATE", strlen("BEGIN IMMEDIATE")) && take(BEGIN)) return SQLITE_BUSY; if (targeted && !strncmp(sql, "COMMIT", strlen("COMMIT")) && take(COMMIT)) return SQLITE_BUSY; - if (!strcmp(sql, "ROLLBACK") && fail_rollback && (!completion_scope || completion_transaction)) { + if (!strcmp(sql, "ROLLBACK") && fail_rollback && (!completion_scope || completion_transaction || fault_transaction_open)) { fail_rollback = 0; rollback_fault++; DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "attachment test: injected rollback failure"); return SQLITE_IOERR; } int rc = __real_sqlite3_exec(db, sql, cb, arg, error); + if (fault_transaction && strstr(sql, fault_transaction) && !strncmp(sql, "BEGIN", 5) && rc == SQLITE_OK) fault_transaction_open = 1; + if (!strncmp(sql, "COMMIT", strlen("COMMIT")) || !strcmp(sql, "ROLLBACK")) fault_transaction_open = 0; if (strstr(sql, "RELEASE delete_topo_channel")) channel_transaction = 0; if (!strncmp(sql, "COMMIT", strlen("COMMIT")) || !strcmp(sql, "ROLLBACK")) completion_transaction = 0; return rc; @@ -86,6 +93,9 @@ int __wrap_sqlite3_exec(sqlite3* db, const char* sql, int (*cb)(void*, int, char extern int __real_sqlite3_prepare_v2(sqlite3*, const char*, int, sqlite3_stmt**, const char**); int __wrap_sqlite3_prepare_v2(sqlite3* db, const char* sql, int length, sqlite3_stmt** st, const char** tail) { + if (block_index && !strncmp(sql, "INSERT INTO attachment_keys(channel,crypto_id,message_id,author", 61)) { + DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "index test: blocked observation prepare"); *st = NULL; return SQLITE_BUSY; + } if (!strncmp(sql, "SELECT 1 FROM attachment_keys WHERE", strlen("SELECT 1 FROM attachment_keys WHERE"))) { completion_calls++; completion_transaction = 1; if (block_presence) { blocked_completions++; *st = NULL; return SQLITE_BUSY; } @@ -117,6 +127,12 @@ int __wrap_sqlite3_bind_int64(sqlite3_stmt* st, int index, sqlite3_int64 value) return __real_sqlite3_bind_int64(st, index, value); } +extern int __real_sqlite3_bind_int(sqlite3_stmt*, int, int); +int __wrap_sqlite3_bind_int(sqlite3_stmt* st, int index, int value) { + if (is_target(st) && take((enum fault)(BIND_1 + index - 1))) return SQLITE_NOMEM; + return __real_sqlite3_bind_int(st, index, value); +} + extern int __real_sqlite3_step(sqlite3_stmt*); int __wrap_sqlite3_step(sqlite3_stmt* st) { target_written = is_target(st); @@ -194,8 +210,14 @@ void __wrap_media_async_submit(struct media_async* ma, struct UASYNC* ua, ma_wor } static void channel_state(struct fixture* f, int deleted); +static void local_event(struct fixture* f); static void event(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int length) { + if (type == CHAT_EVT_MSG_RECEIVED && active) { active->received_events++; return; } + if (type == CHAT_EVT_ATTACHMENT_KEYS_CHANGED && active) { + active->attachment_events++; + if (active->local_mode) { assert(inst == active->inst && sqlite3_get_autocommit(inst->topo_sqlite_db)); local_event(active); return; } + } if (type == CHAT_EVT_CHANNEL_DELETED && active && active->delete_mode) { assert(sqlite3_get_autocommit(inst->topo_sqlite_db)); channel_state(active, 1); active->deleted_events++; return; }