Add unit test for resuming images.

This commit is contained in:
2025-10-21 16:42:40 +01:00
parent 4557822a66
commit b600857b2e

View File

@@ -412,6 +412,119 @@ TEST_F(CreateImageFixture, create_image_compresed_deduplicated)
EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
}
TEST_F(CreateImageFixture, create_image_resume)
{
char path[PATH_MAX];
char filename[PATH_MAX];
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/random", path);
// Open random file
FILE *f = fopen(filename, "rb");
ASSERT_NE(f, nullptr) << "Failed to open random data file";
uint8_t *buffer = static_cast<uint8_t *>(malloc(1048576));
fread(buffer, 1, 1048576, f);
fclose(f);
constexpr size_t total_sectors = 128 * 1024 * 1024 / 512; // 128 MiB / 512 bytes
// Create image
void *context = aaruf_create("test.aif", 1, 512, total_sectors, 0, 0, "deduplicate=true;compress=true",
reinterpret_cast<const uint8_t *>("gtest"), 10, 0, 0, false);
// Verify that the file was successfully opened
ASSERT_NE(context, nullptr) << "Failed to create test.aif";
crc64_ctx *ctx = aaruf_crc64_init();
uint64_t generated_crc = 0;
// Write data in sectors of 512 bytes
for(size_t sector = 0; sector < total_sectors / 2; ++sector)
{
const size_t buffer_offset = sector * 512 % 1048576; // Roll back to start when reaching end
const int32_t write_result =
aaruf_write_sector(context, sector, false, buffer + buffer_offset, SectorStatusDumped, 512);
ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write sector " << sector;
aaruf_crc64_update(ctx, buffer + buffer_offset, 512);
}
// Close the image
int32_t close_result = aaruf_close(context);
ASSERT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
context = aaruf_open("test.aif", true, "deduplicate=true;compress=true");
// Verify that the file was successfully opened
ASSERT_NE(context, nullptr) << "Failed to re-open test.aif for resume";
// Write data in sectors of 512 bytes
for(size_t sector = total_sectors / 2; sector < total_sectors; ++sector)
{
const size_t buffer_offset = sector * 512 % 1048576; // Roll back to start when reaching end
const int32_t write_result =
aaruf_write_sector(context, sector, false, buffer + buffer_offset, SectorStatusDumped, 512);
ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write sector " << sector;
aaruf_crc64_update(ctx, buffer + buffer_offset, 512);
}
aaruf_crc64_final(ctx, &generated_crc);
aaruf_crc64_free(ctx);
// Close the image
close_result = aaruf_close(context);
ASSERT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
free(buffer);
// Reopen the image
context = aaruf_open("test.aif", false, NULL);
ASSERT_NE(context, nullptr) << "Failed to open test.aif";
// Get image info to verify it's a valid image
ImageInfo image_info;
const int32_t result = aaruf_get_image_info(context, &image_info);
ASSERT_EQ(result, AARUF_STATUS_OK) << "Failed to get image info";
// Basic sanity checks on the image info
ASSERT_EQ(image_info.HasPartitions, false) << "Image should not have partitions";
ASSERT_EQ(image_info.HasSessions, false) << "Image should not have sessions";
ASSERT_EQ(image_info.Sectors, total_sectors) << "Unexpected number of sectors";
ASSERT_EQ(image_info.SectorSize, 512) << "Unexpected sector size";
ASSERT_STREQ(image_info.Version, "2.0") << "Unexpected image version";
ASSERT_STREQ(image_info.Application, "gtest") << "Unexpected application name";
ASSERT_STREQ(image_info.ApplicationVersion, "0.0") << "Unexpected application version";
ASSERT_EQ(image_info.MediaType, 1) << "Unexpected media type";
ASSERT_EQ(image_info.MetadataMediaType, 1) << "Unexpected metadata media type";
ctx = aaruf_crc64_init();
uint64_t crc = 0;
for(int i = 0; i < total_sectors; i++)
{
uint8_t sector_buffer[512];
uint32_t length = sizeof(sector_buffer);
uint8_t sector_status = 0;
const int32_t read_result = aaruf_read_sector(context, i, false, sector_buffer, &length, &sector_status);
EXPECT_EQ(read_result, AARUF_STATUS_OK) << "Failed to read sector " << i;
EXPECT_EQ(length, 512U) << "Unexpected length for sector " << i;
aaruf_crc64_update(ctx, sector_buffer, 512);
}
aaruf_crc64_final(ctx, &crc);
aaruf_crc64_free(ctx);
ASSERT_EQ(crc, generated_crc) << "Unexpected CRC64 for image data";
// Close the image
close_result = aaruf_close(context);
EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image";
}
TEST_F(CreateImageFixture, create_image_table_shift_9)
{
char path[PATH_MAX];