Exercise real GRANT retry and verify committed key before network ACK
This commit is contained in:
1 parent
7f9ba549df
commit
df39f58f84
2 files changed
+59
-1
No files matched your search
+2
-1
@@ -462,7 +462,8 @@ test_encrypted_attachment_CFLAGS = $(test_dm_e2e_CFLAGS)
|
||||
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
|
||||
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
|
||||
endif
|
||||
if OS_LINUX
|
||||
test_attachment_results_SOURCES = test_attachment_results.c
|
||||
|
||||
@@ -39,6 +39,8 @@ struct media_test {
|
||||
uint8_t ed[NODES][32], ch_x[32], ch_ed[32], ch_priv[32], ch_sig[64], message[32];
|
||||
char dir[512], file[768];
|
||||
unsigned phase, ticks, heartbeats, hold, forced_cleanup;
|
||||
unsigned grant_bind_faults, grant_rollbacks, result_acks; /* Linux: отказ записи GRANT и настоящий повтор до расшифровки. */
|
||||
int grant_state, grant_outbox; /* Снимок получателя до единственного отказа bind. */
|
||||
int failed;
|
||||
void* timer;
|
||||
};
|
||||
@@ -59,6 +61,59 @@ static void failure(struct media_test* t, const char* reason) {
|
||||
#ifdef __linux__
|
||||
static struct media_test* active_test;
|
||||
static timeout_callback_t backfill_cb;
|
||||
|
||||
/* Проверить полный откат до очередного повтора автора; ACK получателя ещё не отправлялся. */
|
||||
static void verify_grant_rollback(void* arg) {
|
||||
struct media_test* t = arg;
|
||||
struct UTUN_INSTANCE* b = t->nodes[B];
|
||||
char sql[256];
|
||||
snprintf(sql, sizeof(sql), "SELECT count(*) FROM attachment_key_outbox WHERE command=3 AND peer=%lld",
|
||||
(long long)(sqlite3_int64)t->ids[B]);
|
||||
if (!b || !sqlite3_get_autocommit(b->topo_sqlite_db) || value(b, "SELECT coalesce(length(key),0) FROM attachment_keys") != 0 ||
|
||||
value(b, "SELECT state FROM attachment_key_requests WHERE incoming=0") != t->grant_state ||
|
||||
value(b, "SELECT count(*) FROM attachment_key_outbox") != t->grant_outbox || t->result_acks || value(t->nodes[A], sql) != 1) {
|
||||
failure(t, "GRANT SQL fault changed committed result or acknowledged before retry"); return;
|
||||
}
|
||||
t->grant_rollbacks++;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS grant rollback: key absent state=%d request_outbox=%d ACK=0 author retains GRANT for retry",
|
||||
t->grant_state, t->grant_outbox);
|
||||
}
|
||||
|
||||
extern int __real_sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int, void (*)(void*));
|
||||
int __wrap_sqlite3_bind_blob(sqlite3_stmt* st, int index, const void* data, int bytes, void (*destroy)(void*)) {
|
||||
struct media_test* t = active_test;
|
||||
if (t && !t->grant_bind_faults && t->nodes[B] && index == 1 && sqlite3_db_handle(st) == t->nodes[B]->topo_sqlite_db &&
|
||||
strstr(sqlite3_sql(st), "UPDATE attachment_keys SET key=?,error=0 WHERE channel=? AND message_id=? AND author=?")) {
|
||||
t->grant_bind_faults++;
|
||||
t->grant_state = value(t->nodes[B], "SELECT state FROM attachment_key_requests WHERE incoming=0");
|
||||
t->grant_outbox = value(t->nodes[B], "SELECT count(*) FROM attachment_key_outbox");
|
||||
if (!uasync_set_timeout(t->ua, 100, t, verify_grant_rollback, "verify grant rollback"))
|
||||
failure(t, "cannot schedule GRANT rollback check");
|
||||
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "encrypted attachment fixture: one GRANT key bind failure bytes=%d rc=SQLITE_NOMEM", bytes);
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
return __real_sqlite3_bind_blob(st, index, data, bytes, destroy);
|
||||
}
|
||||
|
||||
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 && 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;
|
||||
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 length(key) FROM attachment_keys WHERE message_id=?", -1, &st, NULL);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_blob(st, 1, data + 5, 32, SQLITE_STATIC);
|
||||
int ready = rc == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW && sqlite3_column_int(st, 0) == 32;
|
||||
if (sqlite3_finalize(st) != SQLITE_OK) ready = 0;
|
||||
if (sqlite3_close(reader) != SQLITE_OK) ready = 0;
|
||||
if (!ready || !sqlite3_get_autocommit(conn->inst->topo_sqlite_db)) failure(t, "RESULT_ACK before independent reader sees key");
|
||||
t->result_acks++;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "encrypted attachment fixture: real RESULT_ACK after committed key readable=%d", ready);
|
||||
}
|
||||
return __real_etcp_router_conn_send(conn, data, length, mode);
|
||||
}
|
||||
|
||||
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 cb, const char* name) {
|
||||
if (name && !strcmp(name, "chat_media_bl")) backfill_cb = cb;
|
||||
@@ -416,6 +471,8 @@ int main(void) {
|
||||
test_clock_mainloop(t.ua);
|
||||
#ifdef __linux__
|
||||
if (!t.failed && t.forced_cleanup != 1) failure(&t, "cleanup during active block was not exercised");
|
||||
if (!t.failed && (t.grant_bind_faults != 1 || t.grant_rollbacks != 1 || !t.result_acks))
|
||||
failure(&t, "GRANT SQL fault, rollback and real ACK after retry 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);
|
||||
|
||||
Reference in new issue
Block a user