From 76a013cd04ddfa06679548c0a4fbc1025de2e9e2 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 10 Dec 2025 08:45:15 +0000 Subject: [PATCH] Add tests for subchannel data. --- tests/create_image.cpp | 327 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 326 insertions(+), 1 deletion(-) diff --git a/tests/create_image.cpp b/tests/create_image.cpp index eeec0ea..19321b9 100644 --- a/tests/create_image.cpp +++ b/tests/create_image.cpp @@ -739,7 +739,6 @@ TEST_F(CreateImageFixture, create_audio_image) EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image"; } - TEST_F(CreateImageFixture, create_image_negative_sectors) { char path[PATH_MAX]; @@ -855,3 +854,329 @@ TEST_F(CreateImageFixture, create_image_negative_sectors) close_result = aaruf_close(context); EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image"; } + +TEST_F(CreateImageFixture, create_subchannel_uncompressed_image) +{ + char path[PATH_MAX]; + char filename[PATH_MAX]; + + getcwd(path, PATH_MAX); + snprintf(filename, PATH_MAX, "%s/data/audio.bin", path); + + // Open audio file + FILE *f = fopen(filename, "rb"); + ASSERT_NE(f, nullptr) << "Failed to open audio.bin data file"; + + // Get file size + fseek(f, 0, SEEK_END); + const long audio_size = ftell(f); + fseek(f, 0, SEEK_SET); + + // Read entire audio file into buffer + uint8_t *buffer = static_cast(malloc(audio_size)); + ASSERT_NE(buffer, nullptr) << "Failed to allocate memory for audio data"; + + size_t bytes_read = fread(buffer, 1, audio_size, f); + fclose(f); + ASSERT_EQ(bytes_read, static_cast(audio_size)) << "Failed to read complete audio data"; + + // Calculate total size: three times the audio.bin size + const size_t total_size = audio_size * 3; + + // Create image with default options (NULL for options means defaults) + void *context = aaruf_create("test_audio.aif", 11, 2352, total_size / 2352, 0, 0, "compress=false", + reinterpret_cast("gtest"), 10, 0, 0, false); + + // Verify that the file was successfully opened + ASSERT_NE(context, nullptr) << "Failed to create test_audio.aif"; + + // Set up a single audio track spanning the entire media + TrackEntry track; + memset(&track, 0, sizeof(TrackEntry)); + track.sequence = 1; // Track 1 + track.type = Audio; // Audio track type (0) + track.start = 0; // Start at sector 0 + track.end = (audio_size * 3 / 2352) - 1; // End at last sector (inclusive) + track.pregap = 0; // No pregap + track.session = 1; // Session 1 + memset(track.isrc, 0, 13); // No ISRC + track.flags = 0; // No special flags + + int32_t track_result = aaruf_set_tracks(context, &track, 1); + ASSERT_EQ(track_result, AARUF_STATUS_OK) << "Failed to set tracks"; + + crc64_ctx *ctx = aaruf_crc64_init(); + uint64_t generated_crc = 0; + + // Write audio data three times using write_sector_long + size_t total_sectors = total_size / 2352; + + for(size_t sector = 0; sector < total_sectors; ++sector) + { + // Calculate offset in the original audio buffer (wrap around after each iteration) + const size_t buffer_offset = (sector * 2352) % audio_size; + const int32_t write_result = + aaruf_write_sector_long(context, sector, false, buffer + buffer_offset, SectorStatusDumped, 2352); + ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write sector " << sector; + + aaruf_crc64_update(ctx, buffer + buffer_offset, 2352); + } + + aaruf_crc64_final(ctx, &generated_crc); + aaruf_crc64_free(ctx); + + // Write subchannel data for each sector and calculate CRC64 + crc64_ctx *subchannel_ctx = aaruf_crc64_init(); + uint64_t generated_subchannel_crc = 0; + uint8_t subchannel_data[96]; + + for(size_t sector = 0; sector < total_sectors; ++sector) + { + // Generate subchannel pattern based on sector number (96 bytes per sector) + for(size_t i = 0; i < 96; ++i) { subchannel_data[i] = static_cast((sector * 96 + i) & 0xFF); } + + const int32_t subchannel_result = + aaruf_write_sector_tag(context, sector, false, subchannel_data, 96, CdSectorSubchannelAaru); + ASSERT_EQ(subchannel_result, AARUF_STATUS_OK) << "Failed to write subchannel for sector " << sector; + + // Update CRC64 with the subchannel data we just wrote + aaruf_crc64_update(subchannel_ctx, subchannel_data, 96); + } + + aaruf_crc64_final(subchannel_ctx, &generated_subchannel_crc); + aaruf_crc64_free(subchannel_ctx); + + // Close the image + int32_t 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_audio.aif", false, NULL); + ASSERT_NE(context, nullptr) << "Failed to open test_audio.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, true) << "Image should not have partitions"; + ASSERT_EQ(image_info.HasSessions, true) << "Image should not have sessions"; + ASSERT_EQ(image_info.Sectors, total_sectors) << "Unexpected number of sectors"; + ASSERT_EQ(image_info.SectorSize, 2352) << "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, 11) << "Unexpected media type"; + ASSERT_EQ(image_info.MetadataMediaType, 0) << "Unexpected metadata media type"; + + ctx = aaruf_crc64_init(); + uint64_t crc = 0; + + for(size_t i = 0; i < total_sectors; i++) + { + uint8_t sector_buffer[2352]; + uint32_t length = sizeof(sector_buffer); + uint8_t sector_status = 0; + + const int32_t read_result = aaruf_read_sector_long(context, i, false, sector_buffer, &length, §or_status); + EXPECT_EQ(read_result, AARUF_STATUS_OK) << "Failed to read sector " << i; + EXPECT_EQ(length, 2352U) << "Unexpected length for sector " << i; + aaruf_crc64_update(ctx, sector_buffer, 2352); + } + + aaruf_crc64_final(ctx, &crc); + aaruf_crc64_free(ctx); + + ASSERT_EQ(crc, generated_crc) << "Unexpected CRC64 for image data"; + + // Read back and verify subchannel data + crc64_ctx *subchannel_read_ctx = aaruf_crc64_init(); + uint64_t read_subchannel_crc = 0; + + for(size_t i = 0; i < total_sectors; i++) + { + uint8_t subchannel_buffer[96]; + uint32_t subchannel_length = sizeof(subchannel_buffer); + + const int32_t subchannel_read_result = + aaruf_read_sector_tag(context, i, false, subchannel_buffer, &subchannel_length, CdSectorSubchannelAaru); + EXPECT_EQ(subchannel_read_result, AARUF_STATUS_OK) << "Failed to read subchannel for sector " << i; + EXPECT_EQ(subchannel_length, 96U) << "Unexpected subchannel length for sector " << i; + aaruf_crc64_update(subchannel_read_ctx, subchannel_buffer, 96); + } + + aaruf_crc64_final(subchannel_read_ctx, &read_subchannel_crc); + aaruf_crc64_free(subchannel_read_ctx); + + ASSERT_EQ(read_subchannel_crc, generated_subchannel_crc) << "Unexpected CRC64 for subchannel data"; + + // Close the image + close_result = aaruf_close(context); + EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image"; +} + +TEST_F(CreateImageFixture, create_subchannel_compressed_image) +{ + char path[PATH_MAX]; + char filename[PATH_MAX]; + + getcwd(path, PATH_MAX); + snprintf(filename, PATH_MAX, "%s/data/audio.bin", path); + + // Open audio file + FILE *f = fopen(filename, "rb"); + ASSERT_NE(f, nullptr) << "Failed to open audio.bin data file"; + + // Get file size + fseek(f, 0, SEEK_END); + const long audio_size = ftell(f); + fseek(f, 0, SEEK_SET); + + // Read entire audio file into buffer + uint8_t *buffer = static_cast(malloc(audio_size)); + ASSERT_NE(buffer, nullptr) << "Failed to allocate memory for audio data"; + + size_t bytes_read = fread(buffer, 1, audio_size, f); + fclose(f); + ASSERT_EQ(bytes_read, static_cast(audio_size)) << "Failed to read complete audio data"; + + // Calculate total size: three times the audio.bin size + const size_t total_size = audio_size * 3; + + // Create image with default options (NULL for options means defaults) + void *context = aaruf_create("test_audio.aif", 11, 2352, total_size / 2352, 0, 0, "compress=true", + reinterpret_cast("gtest"), 10, 0, 0, false); + + // Verify that the file was successfully opened + ASSERT_NE(context, nullptr) << "Failed to create test_audio.aif"; + + // Set up a single audio track spanning the entire media + TrackEntry track; + memset(&track, 0, sizeof(TrackEntry)); + track.sequence = 1; // Track 1 + track.type = Audio; // Audio track type (0) + track.start = 0; // Start at sector 0 + track.end = (audio_size * 3 / 2352) - 1; // End at last sector (inclusive) + track.pregap = 0; // No pregap + track.session = 1; // Session 1 + memset(track.isrc, 0, 13); // No ISRC + track.flags = 0; // No special flags + + int32_t track_result = aaruf_set_tracks(context, &track, 1); + ASSERT_EQ(track_result, AARUF_STATUS_OK) << "Failed to set tracks"; + + crc64_ctx *ctx = aaruf_crc64_init(); + uint64_t generated_crc = 0; + + // Write audio data three times using write_sector_long + size_t total_sectors = total_size / 2352; + + for(size_t sector = 0; sector < total_sectors; ++sector) + { + // Calculate offset in the original audio buffer (wrap around after each iteration) + const size_t buffer_offset = (sector * 2352) % audio_size; + const int32_t write_result = + aaruf_write_sector_long(context, sector, false, buffer + buffer_offset, SectorStatusDumped, 2352); + ASSERT_EQ(write_result, AARUF_STATUS_OK) << "Failed to write sector " << sector; + + aaruf_crc64_update(ctx, buffer + buffer_offset, 2352); + } + + aaruf_crc64_final(ctx, &generated_crc); + aaruf_crc64_free(ctx); + + // Write subchannel data for each sector and calculate CRC64 + crc64_ctx *subchannel_ctx = aaruf_crc64_init(); + uint64_t generated_subchannel_crc = 0; + uint8_t subchannel_data[96]; + + for(size_t sector = 0; sector < total_sectors; ++sector) + { + // Generate subchannel pattern based on sector number (96 bytes per sector) + for(size_t i = 0; i < 96; ++i) { subchannel_data[i] = static_cast((sector * 96 + i) & 0xFF); } + + const int32_t subchannel_result = + aaruf_write_sector_tag(context, sector, false, subchannel_data, 96, CdSectorSubchannelAaru); + ASSERT_EQ(subchannel_result, AARUF_STATUS_OK) << "Failed to write subchannel for sector " << sector; + + // Update CRC64 with the subchannel data we just wrote + aaruf_crc64_update(subchannel_ctx, subchannel_data, 96); + } + + aaruf_crc64_final(subchannel_ctx, &generated_subchannel_crc); + aaruf_crc64_free(subchannel_ctx); + + // Close the image + int32_t 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_audio.aif", false, NULL); + ASSERT_NE(context, nullptr) << "Failed to open test_audio.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, true) << "Image should not have partitions"; + ASSERT_EQ(image_info.HasSessions, true) << "Image should not have sessions"; + ASSERT_EQ(image_info.Sectors, total_sectors) << "Unexpected number of sectors"; + ASSERT_EQ(image_info.SectorSize, 2352) << "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, 11) << "Unexpected media type"; + ASSERT_EQ(image_info.MetadataMediaType, 0) << "Unexpected metadata media type"; + + ctx = aaruf_crc64_init(); + uint64_t crc = 0; + + for(size_t i = 0; i < total_sectors; i++) + { + uint8_t sector_buffer[2352]; + uint32_t length = sizeof(sector_buffer); + uint8_t sector_status = 0; + + const int32_t read_result = aaruf_read_sector_long(context, i, false, sector_buffer, &length, §or_status); + EXPECT_EQ(read_result, AARUF_STATUS_OK) << "Failed to read sector " << i; + EXPECT_EQ(length, 2352U) << "Unexpected length for sector " << i; + aaruf_crc64_update(ctx, sector_buffer, 2352); + } + + aaruf_crc64_final(ctx, &crc); + aaruf_crc64_free(ctx); + + ASSERT_EQ(crc, generated_crc) << "Unexpected CRC64 for image data"; + + // Read back and verify subchannel data + crc64_ctx *subchannel_read_ctx = aaruf_crc64_init(); + uint64_t read_subchannel_crc = 0; + + for(size_t i = 0; i < total_sectors; i++) + { + uint8_t subchannel_buffer[96]; + uint32_t subchannel_length = sizeof(subchannel_buffer); + + const int32_t subchannel_read_result = + aaruf_read_sector_tag(context, i, false, subchannel_buffer, &subchannel_length, CdSectorSubchannelAaru); + EXPECT_EQ(subchannel_read_result, AARUF_STATUS_OK) << "Failed to read subchannel for sector " << i; + EXPECT_EQ(subchannel_length, 96U) << "Unexpected subchannel length for sector " << i; + aaruf_crc64_update(subchannel_read_ctx, subchannel_buffer, 96); + } + + aaruf_crc64_final(subchannel_read_ctx, &read_subchannel_crc); + aaruf_crc64_free(subchannel_read_ctx); + + ASSERT_EQ(read_subchannel_crc, generated_subchannel_crc) << "Unexpected CRC64 for subchannel data"; + + // Close the image + close_result = aaruf_close(context); + EXPECT_EQ(close_result, AARUF_STATUS_OK) << "Failed to close image"; +} \ No newline at end of file