mirror of
https://github.com/ApfelTeeSaft/Yaroze.NET.git
synced 2026-08-26 19:43:27 +00:00
235 lines
5.8 KiB
C#
235 lines
5.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using Xunit;
|
|
using Yaroze.Core.CDROM;
|
|
|
|
namespace Yaroze.Tests.CDROM;
|
|
|
|
public class DiscImageTests
|
|
{
|
|
[Fact]
|
|
public void DiscImage_LoadsRawBinFile()
|
|
{
|
|
// Arrange
|
|
var binPath = CreateTempBinFile(2352 * 100); // 100 sectors
|
|
|
|
try
|
|
{
|
|
// Act
|
|
using var disc = DiscImage.Load(binPath);
|
|
|
|
// Assert
|
|
Assert.Single(disc.Tracks);
|
|
Assert.Equal(1, disc.Tracks[0].Number);
|
|
Assert.Equal(TrackMode.Mode2_2352, disc.Tracks[0].Mode);
|
|
Assert.Equal(100, disc.GetSectorCount());
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(binPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_LoadsIsoFile()
|
|
{
|
|
// Arrange
|
|
var isoPath = CreateTempIsoFile(2048 * 50); // 50 sectors of MODE1/2048
|
|
|
|
try
|
|
{
|
|
// Act
|
|
using var disc = DiscImage.Load(isoPath);
|
|
|
|
// Assert
|
|
Assert.Single(disc.Tracks);
|
|
Assert.Equal(TrackMode.Mode2_2352, disc.Tracks[0].Mode); // Assumes raw format
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(isoPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_LoadsFromCueSheet()
|
|
{
|
|
// Arrange
|
|
var binPath = CreateTempBinFile(2352 * 100);
|
|
var binName = Path.GetFileName(binPath);
|
|
var cueDir = Path.GetDirectoryName(binPath)!;
|
|
var cuePath = Path.Combine(cueDir, Path.GetFileNameWithoutExtension(binPath) + ".cue");
|
|
|
|
var cueContent = $@"
|
|
FILE ""{binName}"" BINARY
|
|
TRACK 01 MODE2/2352
|
|
INDEX 01 00:00:00
|
|
";
|
|
File.WriteAllText(cuePath, cueContent);
|
|
|
|
try
|
|
{
|
|
// Act
|
|
using var disc = DiscImage.Load(cuePath);
|
|
|
|
// Assert
|
|
Assert.Single(disc.Tracks);
|
|
Assert.Equal(1, disc.Tracks[0].Number);
|
|
Assert.Equal(TrackMode.Mode2_2352, disc.Tracks[0].Mode);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(binPath);
|
|
File.Delete(cuePath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_AutoDetectsCueFile()
|
|
{
|
|
// Arrange
|
|
var binPath = CreateTempBinFile(2352 * 100);
|
|
var binName = Path.GetFileName(binPath);
|
|
var cueDir = Path.GetDirectoryName(binPath)!;
|
|
var cuePath = Path.Combine(cueDir, Path.GetFileNameWithoutExtension(binPath) + ".cue");
|
|
|
|
var cueContent = $@"
|
|
FILE ""{binName}"" BINARY
|
|
TRACK 01 MODE2/2352
|
|
INDEX 01 00:00:00
|
|
";
|
|
File.WriteAllText(cuePath, cueContent);
|
|
|
|
try
|
|
{
|
|
// Act - Load BIN file, should auto-detect CUE
|
|
using var disc = DiscImage.Load(binPath);
|
|
|
|
// Assert - Should have loaded from CUE sheet
|
|
Assert.Single(disc.Tracks);
|
|
Assert.Equal(TrackMode.Mode2_2352, disc.Tracks[0].Mode);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(binPath);
|
|
File.Delete(cuePath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_ReadsSector()
|
|
{
|
|
// Arrange
|
|
var data = new byte[2352 * 10];
|
|
for (int i = 0; i < data.Length; i++)
|
|
{
|
|
data[i] = (byte)(i % 256);
|
|
}
|
|
|
|
var binPath = CreateTempBinFile(data);
|
|
|
|
try
|
|
{
|
|
using var disc = DiscImage.Load(binPath);
|
|
|
|
// Act
|
|
var buffer = new byte[2352];
|
|
int bytesRead = disc.ReadSector(5, buffer);
|
|
|
|
// Assert
|
|
Assert.Equal(2352, bytesRead);
|
|
for (int i = 0; i < 2352; i++)
|
|
{
|
|
Assert.Equal((byte)((5 * 2352 + i) % 256), buffer[i]);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(binPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_ReadsUserData()
|
|
{
|
|
// Arrange
|
|
var sectorData = new byte[2352];
|
|
// Fill with header pattern (24 bytes) + user data
|
|
for (int i = 0; i < 24; i++)
|
|
sectorData[i] = 0xFF; // Header
|
|
|
|
for (int i = 24; i < 24 + 2048; i++)
|
|
sectorData[i] = (byte)((i - 24) % 256); // User data
|
|
|
|
var binPath = CreateTempBinFile(sectorData);
|
|
|
|
try
|
|
{
|
|
using var disc = DiscImage.Load(binPath);
|
|
|
|
// Act
|
|
var buffer = new byte[2048];
|
|
int bytesRead = disc.ReadSectorUserData(0, buffer);
|
|
|
|
// Assert
|
|
Assert.Equal(2048, bytesRead);
|
|
for (int i = 0; i < 2048; i++)
|
|
{
|
|
Assert.Equal((byte)(i % 256), buffer[i]);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(binPath);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_ThrowsOnNonExistentFile()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<FileNotFoundException>(() => DiscImage.Load("nonexistent.bin"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscImage_ThrowsOnUnsupportedFormat()
|
|
{
|
|
// Arrange
|
|
var txtPath = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.txt");
|
|
File.WriteAllText(txtPath, "test");
|
|
|
|
try
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<NotSupportedException>(() => DiscImage.Load(txtPath));
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(txtPath);
|
|
}
|
|
}
|
|
|
|
private string CreateTempBinFile(int size)
|
|
{
|
|
var data = new byte[size];
|
|
new Random(42).NextBytes(data);
|
|
return CreateTempBinFile(data);
|
|
}
|
|
|
|
private string CreateTempBinFile(byte[] data)
|
|
{
|
|
var tempPath = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.bin");
|
|
File.WriteAllBytes(tempPath, data);
|
|
return tempPath;
|
|
}
|
|
|
|
private string CreateTempIsoFile(int size)
|
|
{
|
|
var tempPath = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.iso");
|
|
var data = new byte[size];
|
|
new Random(42).NextBytes(data);
|
|
File.WriteAllBytes(tempPath, data);
|
|
return tempPath;
|
|
}
|
|
}
|