diff --git a/tests/Makefile.am b/tests/Makefile.am index 791c41735..7ffab362d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -465,7 +465,7 @@ test_encrypted_attachment_LDADD = $(LIBUTUN) $(CRYPTO_LIBS) $(COMMON_LIBS) test_encrypted_attachment_LDFLAGS = $(test_clock_ldflags) if OS_LINUX test_encrypted_attachment_LDFLAGS += -Wl,--wrap=uasync_set_timeout -Wl,--wrap=media_download_handle_chunk \ - -Wl,--wrap=sqlite3_bind_blob -Wl,--wrap=etcp_router_conn_send + -Wl,--wrap=sqlite3_bind_blob -Wl,--wrap=etcp_router_conn_send -Wl,--wrap=etcp_router_bind endif if OS_LINUX test_attachment_requests_SOURCES = test_attachment_requests.c diff --git a/tests/test_encrypted_attachment.c b/tests/test_encrypted_attachment.c index abdb46419..04857257e 100644 --- a/tests/test_encrypted_attachment.c +++ b/tests/test_encrypted_attachment.c @@ -42,6 +42,8 @@ struct media_test { unsigned phase, ticks, heartbeats, hold, forced_cleanup; unsigned grant_bind_faults, grant_rollbacks, result_acks; /* Linux: отказ записи GRANT и настоящий повтор до расшифровки. */ unsigned decision_failures, decision_events; /* Linux: два отказа автора, restart и одно событие после commit. */ + unsigned request_rejections, request_restarts, request_sends, request_acks, request_events; + int request_external; /* Linux: транзакция фикстуры вокруг первого настоящего REQUEST от B. */ int grant_state, grant_outbox; /* Снимок получателя до единственного отказа bind. */ int failed; void* timer; @@ -63,6 +65,53 @@ static void failure(struct media_test* t, const char* reason) { #ifdef __linux__ static struct media_test* active_test; static timeout_callback_t backfill_cb; +static etcp_recv_fn attachment_receive; +static void request_restart(void* arg); + +static int author_request_count(struct media_test* t) { + char query[256]; + snprintf(query, sizeof(query), "SELECT count(*) FROM attachment_key_requests WHERE channel='" CHANNEL + "' AND peer=%lld AND incoming=1", (long long)(sqlite3_int64)t->ids[B]); + return value(t->nodes[A], query); +} + +static void rejected_request_event(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int bytes) { + (void)inst; (void)data; (void)bytes; + if (type == CHAT_EVT_ATTACHMENT_KEYS_CHANGED) { + active_test->request_events++; failure(active_test, "REQUEST event inside external transaction"); + } +} + +/* Router уже проверил реальный E2E пакет. Внешняя транзакция открывается до входа в обработчик сервиса. */ +static void receive_with_transaction(struct ETCP_CONN* conn, struct ll_entry* entry) { + struct media_test* t = active_test; uint64_t source = 0; + if (entry->len >= ROUTER_SVC_PAYLOAD_OFF + 85) memcpy(&source, entry->dgram + ROUTER_SVC_SRC_OFF, 8); + if (!t || conn->instance != t->nodes[A] || t->request_rejections || source != t->ids[B] || + entry->len != ROUTER_SVC_PAYLOAD_OFF + 85 || entry->dgram[ROUTER_SVC_PAYLOAD_OFF + 4] != 1) { + attachment_receive(conn, entry); return; + } + struct UTUN_INSTANCE* a = t->nodes[A]; sqlite3* db = a->topo_sqlite_db; + int rc = sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL); + if (rc != SQLITE_OK) { failure(t, "cannot open REQUEST outer transaction"); attachment_receive(conn, entry); return; } + rc = sqlite3_exec(db, "UPDATE attachment_keys SET error=7 WHERE channel='" CHANNEL "'", NULL, NULL, NULL); + chat_event_handler_fn previous = a->chat_event_handler; + chat_event_set_handler(a, rejected_request_event); t->request_external = 1; t->request_rejections++; + attachment_receive(conn, entry); /* Освобождает entry; фикстура больше не обращается к нему. */ + int preserved = rc == SQLITE_OK && !t->failed && !sqlite3_get_autocommit(db) && !author_request_count(t) && + value(a, "SELECT error FROM attachment_keys") == 7 && !t->request_acks && !t->request_events; + rc = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL); + t->request_external = 0; chat_event_set_handler(a, previous); + if (!preserved || rc != SQLITE_OK || !sqlite3_get_autocommit(db)) { failure(t, "REQUEST changed outer transaction or sent ACK"); return; } + DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS REQUEST rejection: external marker preserved, no author row/ACK/event, owner rollback completed"); + if (!uasync_set_timeout(t->ua, 100, t, request_restart, "restart after rejected REQUEST")) + failure(t, "cannot schedule REQUEST author restart"); +} + +extern int __real_etcp_router_bind(struct UTUN_INSTANCE*, uint8_t, etcp_recv_fn); +int __wrap_etcp_router_bind(struct UTUN_INSTANCE* inst, uint8_t service, etcp_recv_fn callback) { + if (service == ETCP_RT_ID_ATTACHMENT_KEYS) { attachment_receive = callback; callback = receive_with_transaction; } + return __real_etcp_router_bind(inst, service, callback); +} /* Авторское событие должно видеть уже сохранённые решение и GRANT через независимый reader. */ static void decision_event(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int bytes) { @@ -149,6 +198,25 @@ int __wrap_sqlite3_bind_blob(sqlite3_stmt* st, int index, const void* data, int 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 mode) { struct media_test* t = active_test; + if (t && conn->svc_id == ETCP_RT_ID_ATTACHMENT_KEYS && length == 85 && !memcmp(data, "AK01", 4)) { + if (conn->inst == t->nodes[B] && conn->remote_node_id == t->ids[A] && data[4] == 1) t->request_sends++; + if (conn->inst == t->nodes[A] && conn->remote_node_id == t->ids[B] && data[4] == 2) { + sqlite3* reader = NULL; sqlite3_stmt* st = NULL; + int rc = sqlite3_open_v2(sqlite3_db_filename(conn->inst->topo_sqlite_db, "main"), &reader, SQLITE_OPEN_READONLY, NULL); + if (rc == SQLITE_OK) rc = sqlite3_prepare_v2(reader, "SELECT state FROM attachment_key_requests" + " WHERE channel=? AND message_id=? AND peer=? AND incoming=1", -1, &st, NULL); + if (rc == SQLITE_OK) rc = sqlite3_bind_text(st, 1, CHANNEL, -1, SQLITE_STATIC); + if (rc == SQLITE_OK) rc = sqlite3_bind_blob(st, 2, data + 5, 32, SQLITE_STATIC); + if (rc == SQLITE_OK) rc = sqlite3_bind_int64(st, 3, (sqlite3_int64)t->ids[B]); + int saved = rc == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW && sqlite3_column_int(st, 0) == 0; + if (sqlite3_finalize(st) != SQLITE_OK) saved = 0; + if (sqlite3_close(reader) != SQLITE_OK) saved = 0; + if (t->request_external || !saved || !sqlite3_get_autocommit(conn->inst->topo_sqlite_db)) + failure(t, "REQUEST_ACK before committed request"); + t->request_acks++; + DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS REQUEST_ACK: readonly pending request visible before real ACK, sends=%u", t->request_sends); + } + } if (t && t->nodes[B] && conn->inst == t->nodes[B] && conn->svc_id == ETCP_RT_ID_ATTACHMENT_KEYS && length == 85 && !memcmp(data, "AK01", 4) && data[4] == 5) { sqlite3* reader = NULL; sqlite3_stmt* st = NULL; @@ -266,6 +334,21 @@ static int start_node(struct media_test* t, int index) { return t->nodes[index] ? start_with_recovery(t->nodes[index], db) : -1; } +#ifdef __linux__ +/* Отказ не удалил исходящую очередь B; автор перезапускается вне своего callback. */ +static void request_restart(void* arg) { + struct media_test* t = arg; + if (author_request_count(t) || value(t->nodes[B], "SELECT state FROM attachment_key_requests WHERE incoming=0") != 0 || + value(t->nodes[B], "SELECT count(*) FROM attachment_key_outbox WHERE command=1") != 1 || t->request_acks || t->request_events) { + failure(t, "rejected REQUEST lost recipient retry"); return; + } + utun_instance_destroy(t->nodes[A]); t->nodes[A] = NULL; + if (start_node(t, A)) { failure(t, "author restart after rejected REQUEST"); return; } + t->request_restarts++; + DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS REQUEST retry retained: B state=0 REQUEST=1, author restarted; waiting for real resend"); +} +#endif + /* Фикстура создаёт канал напрямую, поэтому сама снимает gate после реального Merkle round. */ static void members_ready(uint64_t peer, const char* channel, int result, void* arg) { struct UTUN_INSTANCE* inst = arg; @@ -538,6 +621,8 @@ int main(void) { failure(&t, "GRANT SQL fault, rollback and real ACK after retry were not exercised"); if (!t.failed && (t.decision_failures != 2 || t.decision_events != 1)) failure(&t, "author decision failures, restart and committed event were not exercised"); + if (!t.failed && (t.request_rejections != 1 || t.request_restarts != 1 || t.request_sends < 2 || !t.request_acks || + t.request_events || t.request_external)) failure(&t, "REQUEST rejection, restart, real retry and committed ACK were not exercised"); #endif for (int i = 0; i < NODES; i++) if (t.nodes[i]) utun_instance_destroy(t.nodes[i]); uasync_destroy(t.ua, 0);