From b8f0671fd01f8025bd1907e879a2e8f0cda28f68 Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 02:46:59 +0100 Subject: [PATCH] Check complete signed manifests beyond the old manual download limit --- tests/Makefile.am | 3 +- tests/test_chat_attachment.c | 67 +++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index f9a1a9254..ded624e1d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -489,7 +489,8 @@ 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=sqlite3_bind_int + -Wl,--wrap=media_async_submit -Wl,--wrap=sqlite3_changes -Wl,--wrap=uasync_set_timeout -Wl,--wrap=sqlite3_bind_int \ + -Wl,--wrap=media_download_start 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 7b79453cb..4d4d70c1d 100644 --- a/tests/test_chat_attachment.c +++ b/tests/test_chat_attachment.c @@ -10,6 +10,7 @@ #include "../src/chat/chat_core_priv.h" #include "../src/chat/chat_event.h" #include "../src/media_async/media_async.h" +#include "../src/media_delivery/media_download.h" #include "../lib/platform_compat.h" #include "../lib/debug_config.h" #include "../lib/mem.h" @@ -26,6 +27,8 @@ 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 const struct media_index_result* expected_download; +static unsigned download_starts; static unsigned completion_calls; static int block_presence; static unsigned blocked_completions; @@ -937,8 +940,8 @@ static void test_backfill_capacity(void) { snprintf(f.channel, sizeof(f.channel), "%s", sqlite3_column_text(st, 0)); assert(sqlite3_finalize(st) == SQLITE_OK); } source(&f); publish(&f); erase_index(&f); f.rows--; - chat_media_autodownload_backfill_channel(f.inst, f.channel); } + chat_media_autodownload_backfill(f.inst); assert(CC(f.inst)->media_channel_backfill_pending == CHAT_MEDIA_BACKFILL_CHANNELS); assert(CC(f.inst)->media_channel_backfill_timer && CC(f.inst)->media_backfill_timer); unsigned received = f.received_events, changed = f.attachment_events; @@ -949,6 +952,67 @@ static void test_backfill_capacity(void) { DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS backfill capacity:17 channels, bounded16 queue, full-scan fallback restores all without duplicate history/events"); } +/* Только порт запуска загрузки; проверяем полный разбор подписанного manifest без сетевых/дисковых гигабайтов. */ +extern int __real_media_download_start(struct UTUN_INSTANCE*, uint64_t, const struct media_index_result*, const char*, const char*, + uint64_t, void (*)(void*, int), void*, void (*)(void*, int, int), void*); +int __wrap_media_download_start(struct UTUN_INSTANCE* inst, uint64_t group, const struct media_index_result* result, + const char* path, const char* base, uint64_t author, void (*done)(void*, int), void* arg, + void (*progress)(void*, int, int), void* progress_arg) { + if (!expected_download) return __real_media_download_start(inst, group, result, path, base, author, done, arg, progress, progress_arg); + assert(inst == active->inst && group == strtoull(active->channel, NULL, 10) && author == inst->node_id); + assert(done && arg && progress && progress_arg && !strncmp(path, active->directory, strlen(active->directory)) && base); + assert(result->file_size == expected_download->file_size && result->block_size == expected_download->block_size && + result->num_blocks == expected_download->num_blocks && !memcmp(result->media_id, expected_download->media_id, 16) && + !memcmp(result->content_hash, expected_download->content_hash, 32)); + assert(!memcmp(result->block_ids, expected_download->block_ids, (size_t)result->num_blocks * 16)); + assert(!memcmp(result->block_sigs, expected_download->block_sigs, (size_t)result->num_blocks * 64)); + download_starts++; return -1; // Refusal без done; вызывающий обязан освободить собственный контекст. +} + +static void test_long_manual_manifest(void) { + struct fixture f; setup(&f); enum { BLOCKS = 30 }; + uint8_t content[BLOCKS], ids[BLOCKS * 16], signatures[BLOCKS * 64]; + struct media_index_result result = {.file_size = BLOCKS, .block_size = 1, .num_blocks = BLOCKS, .block_ids = ids, .block_sigs = signatures}; + result.media_id[0] = 99; memset(ids, 0, sizeof(ids)); + for (unsigned i = 0; i < BLOCKS; i++) { + content[i] = (uint8_t)('a' + i); ids[i * 16] = (uint8_t)(i + 1); + assert(!ma_sign_block(f.inst->my_ed25519_privkey, content + i, 1, signatures + i * 64)); + } + assert(SHA256(content, sizeof(content), result.content_hash)); + char body[6000] = "*"; const char* filename = "long.bin"; + size_t offset = 1 + b64_encode((const uint8_t*)filename, strlen(filename), body + 1, sizeof(body) - 1); + offset += (size_t)snprintf(body + offset, sizeof(body) - offset, "|%d|1|%d|", BLOCKS, BLOCKS); + for (unsigned i = 0; i < 16; i++) offset += (size_t)snprintf(body + offset, sizeof(body) - offset, "%02x", result.media_id[i]); + body[offset++] = '|'; + for (unsigned i = 0; i < 32; i++) offset += (size_t)snprintf(body + offset, sizeof(body) - offset, "%02x", result.content_hash[i]); + body[offset++] = '|'; + for (unsigned block = 0; block < BLOCKS; block++) { + if (block) body[offset++] = ','; + for (unsigned i = 0; i < 16; i++) offset += (size_t)snprintf(body + offset, sizeof(body) - offset, "%02x", ids[block * 16 + i]); + body[offset++] = ','; + for (unsigned i = 0; i < 64; i++) offset += (size_t)snprintf(body + offset, sizeof(body) - offset, "%02x", signatures[block * 64 + i]); + assert(offset < sizeof(body)); + } + body[offset] = 0; assert(offset > 4096); + char json[6500]; int length = snprintf(json, sizeof(json), "{\"n\":%llu,\"ch\":\"%s\",\"ct\":\"application/octet-stream\",\"d\":\"%s\"}", + (unsigned long long)f.inst->node_id, f.channel, body); + assert(length > 0 && (size_t)length < sizeof(json)); + struct DB_SYNC_INSTANCE* si = si_find(f.inst, f.channel); assert(si); + uint64_t timestamp = db_sync_next_timestamp(si); struct db_message_revision revision = {.ver = 1}; revision.nonce[0] = 33; + uint8_t signature[64], message[32]; + assert(!db_sync_sign_message(strtoull(f.channel, NULL, 10), timestamp, f.inst->node_id, &revision, json, (size_t)length, + f.inst->my_ed25519_privkey, signature)); + assert(!chat_message_id(strtoull(f.channel, NULL, 10), timestamp, f.inst->node_id, revision.nonce, message)); + assert(!db_sync_insert_signed(si, json, (size_t)length, signature, 64, timestamp, NULL, &revision)); + char query[160]; snprintf(query, sizeof(query), "SELECT id FROM msg_%s WHERE message_id=?", f.channel); sqlite3_stmt* st = NULL; + assert(sqlite3_prepare_v2(f.reader, query, -1, &st, NULL) == SQLITE_OK); + assert(sqlite3_bind_blob(st, 1, message, 32, SQLITE_STATIC) == SQLITE_OK && sqlite3_step(st) == SQLITE_ROW); + int64_t id = sqlite3_column_int64(st, 0); assert(sqlite3_finalize(st) == SQLITE_OK); + expected_download = &result; download_starts = 0; chat_core_attachment_download(f.inst, f.channel, id); + assert(download_starts == 1); expected_download = NULL; cleanup(&f); + DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS manual manifest:%zu bytes, all30 signed blocks reach download port; rejected start frees context", offset); +} + int main(void) { 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(); @@ -961,6 +1025,7 @@ int main(void) { test_attachment_binding(); test_attachment_identity(); test_observation(); test_local_flags(); test_index_recovery(); test_backfill_capacity(); + test_long_manual_manifest(); assert(u_get_allocated_count() == baseline); DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "PASS attachment: independent committed key/metadata before worker, allocations restored"); return 0;