Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 67 additions & 45 deletions tests/unit/test_storage_archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,63 @@ static void test_unsafe_bucket_configuration_rejected(void) {
TEST_ASSERT_EQUAL_INT(DB_STORAGE_TARGET_INVALID, db_storage_target_validate(&target, error, sizeof(error)));
}

/* Run the resume half in a new image so no inherited mutex or library state
* can make a forked child look like a successful process restart. */
static int multipart_resume_helper(char **args) {
const char *catalog = args[2], *fixture_root = args[3];
const char *recording_path = args[4], *job_uuid = args[5];
const char *prior_upload_id = args[6];
uint64_t expected_size = strtoull(args[7], NULL, 10);
uint64_t expected_hash = strtoull(args[8], NULL, 10);
safe_strcpy(g_config.storage_path, fixture_root, sizeof(g_config.storage_path), 0);
if (init_database_ex(catalog, DB_INIT_NO_BACKUP | DB_INIT_NO_CHECK) != 0) return 1;

char query[512], upload_id_after[256] = {0};
snprintf(query, sizeof(query),
"SELECT upload_id FROM storage_migration_jobs WHERE uuid='%s';", job_uuid);
if (!database_text(query, upload_id_after, sizeof(upload_id_after)) ||
strcmp(prior_upload_id, upload_id_after) != 0) return 3;
if (sql_noassert("UPDATE storage_migration_jobs SET next_attempt_at=0;") != SQLITE_OK)
return 4;
if (storage_migration_process_one() != 1) return 5;
storage_migration_job_t resumed_job;
if (db_storage_migration_get(job_uuid, &resumed_job) != DB_STORAGE_MIGRATION_OK ||
strcmp(resumed_job.state, "completed") != 0)
return 6;
snprintf(query, sizeof(query),
"SELECT upload_id FROM storage_migration_jobs WHERE uuid='%s';", job_uuid);
char completed_upload_id[256] = {0};
if (!database_text(query, completed_upload_id, sizeof(completed_upload_id)) ||
completed_upload_id[0] != '\0') return 6;
if (access(recording_path, F_OK) == 0) return 7;

storage_target_t target;
if (db_storage_target_get(resumed_job.destination_target_uuid, &target) != DB_STORAGE_TARGET_OK)
return 8;
char verified_path[512], error[STORAGE_TARGET_ERROR_MAX] = {0};
snprintf(verified_path, sizeof(verified_path), "%s/multipart-verified.bin", fixture_root);
if (storage_s3_download(&target, resumed_job.destination_object_key, verified_path,
expected_size, resumed_job.checksum, NULL, error) != STORAGE_S3_OK)
return 9;
FILE *archived = fopen(verified_path, "rb");
if (!archived) return 10;
uint64_t archived_hash = 14695981039346656037ULL, archived_size = 0;
unsigned char block[65536];
size_t bytes_read;
while ((bytes_read = fread(block, 1, sizeof(block), archived)) > 0) {
archived_size += bytes_read;
for (size_t i = 0; i < bytes_read; i++) {
archived_hash ^= block[i];
archived_hash *= 1099511628211ULL;
}
}
int read_error = ferror(archived);
fclose(archived);
unlink(verified_path);
shutdown_database();
return read_error || archived_size != expected_size || archived_hash != expected_hash ? 11 : 0;
}

static void test_multipart_resume_after_restart(void) {
uint64_t id = recording(false);
FILE *file = fopen(source_path, "wb");
Expand Down Expand Up @@ -939,53 +996,16 @@ static void test_multipart_resume_after_restart(void) {
TEST_ASSERT_TRUE(database_text(query, upload_id_before, sizeof(upload_id_before)));
TEST_ASSERT_TRUE(upload_id_before[0] != '\0');
shutdown_database();
char expected_size_text[32], expected_hash_text[32];
snprintf(expected_size_text, sizeof(expected_size_text), "%llu", (unsigned long long)expected_size);
snprintf(expected_hash_text, sizeof(expected_hash_text), "%llu", (unsigned long long)expected_hash);
pid_t pid = fork();
TEST_ASSERT_TRUE(pid >= 0);
if (pid == 0) {
if (init_database_ex(db_path, DB_INIT_NO_BACKUP | DB_INIT_NO_CHECK) != 0) _exit(1);
char upload_id_after[256] = {0};
if (!database_text(query, upload_id_after, sizeof(upload_id_after))) _exit(1);
if (strcmp(upload_id_before, upload_id_after) != 0) _exit(1);
if (sql_noassert("UPDATE storage_migration_jobs SET next_attempt_at=0;") != SQLITE_OK) _exit(1);
if (storage_migration_process_one() != 1) _exit(1);
storage_migration_job_t resumed_job;
if (db_storage_migration_get(job.uuid, &resumed_job) != DB_STORAGE_MIGRATION_OK) _exit(1);
if (strcmp(resumed_job.state, "completed") != 0) _exit(1);
snprintf(query, sizeof(query),
"SELECT count(*) FROM storage_migration_jobs WHERE uuid='%s' AND upload_id='';",
job.uuid);
char completed_count[16] = {0};
if (!database_text(query, completed_count, sizeof(completed_count)) ||
strcmp(completed_count, "1") != 0) _exit(1);
if (access(source_path, F_OK) == 0) _exit(1);

/* Verify archived object content integrity after multipart resume. */
char verified_path[512], error[STORAGE_TARGET_ERROR_MAX] = {0};
snprintf(verified_path, sizeof(verified_path), "%s/multipart-verified.bin", root);
if (storage_s3_download(&target, resumed_job.destination_object_key, verified_path,
expected_size, resumed_job.checksum, NULL, error) != STORAGE_S3_OK) _exit(1);
FILE *archived = fopen(verified_path, "rb");
if (!archived) _exit(1);
uint64_t archived_hash = 14695981039346656037ULL;
uint64_t archived_size = 0;
size_t bytes_read;
while ((bytes_read = fread(block, 1, sizeof(block), archived)) > 0) {
archived_size += bytes_read;
for (size_t i = 0; i < bytes_read; i++) {
archived_hash ^= block[i];
archived_hash *= 1099511628211ULL;
}
}
if (ferror(archived) != 0) {
fclose(archived);
_exit(1);
}
fclose(archived);
unlink(verified_path);
if (archived_size != expected_size) _exit(1);
if (archived_hash != expected_hash) _exit(1);
shutdown_database();
_exit(0);
char *const argv_exec[] = { "/proc/self/exe", "--resume-helper", db_path, root,
source_path, job.uuid, upload_id_before, expected_size_text, expected_hash_text, NULL };
execv("/proc/self/exe", argv_exec);
_exit(127);
}
int status = 0;
pid_t waited = waitpid(pid, &status, 0);
Expand Down Expand Up @@ -2706,7 +2726,9 @@ static void test_stalled_archive_writes_renew_lease_until_completion(void) {
TEST_ASSERT_EQUAL_INT(0, scalar("SELECT count(*) FROM recordings;"));
}

int main(void) {
int main(int argc, char **argv) {
if (argc == 9 && strcmp(argv[1], "--resume-helper") == 0)
return multipart_resume_helper(argv);
if (!getenv("LIGHTNVR_TEST_S3_ENDPOINT") || !mkdtemp(root)) return 2;
snprintf(db_path, sizeof(db_path), "%s/catalog.sqlite", root);
snprintf(source_path, sizeof(source_path), "%s/clip.mp4", root);
Expand Down
Loading