2023-03-19 09:49:03 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace CodexDistTests.TestCore
|
|
|
|
|
{
|
|
|
|
|
public interface IFileManager
|
|
|
|
|
{
|
|
|
|
|
TestFile CreateEmptyTestFile();
|
2023-03-21 08:20:09 +00:00
|
|
|
|
TestFile GenerateTestFile(ByteSize size);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
void DeleteAllTestFiles();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FileManager : IFileManager
|
|
|
|
|
{
|
|
|
|
|
public const int ChunkSize = 1024;
|
|
|
|
|
private const string Folder = "TestDataFiles";
|
|
|
|
|
private readonly Random random = new Random();
|
|
|
|
|
private readonly List<TestFile> activeFiles = new List<TestFile>();
|
|
|
|
|
|
2023-03-19 10:18:56 +00:00
|
|
|
|
public FileManager()
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(Folder)) Directory.CreateDirectory(Folder);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
public TestFile CreateEmptyTestFile()
|
|
|
|
|
{
|
|
|
|
|
var result = new TestFile(Path.Combine(Folder, Guid.NewGuid().ToString() + "_test.bin"));
|
2023-03-19 10:18:56 +00:00
|
|
|
|
File.Create(result.Filename).Close();
|
2023-03-19 09:49:03 +00:00
|
|
|
|
activeFiles.Add(result);
|
2023-03-20 10:37:02 +00:00
|
|
|
|
TestLog.Log($"Created test file '{result.Filename}'.");
|
2023-03-19 09:49:03 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 08:20:09 +00:00
|
|
|
|
public TestFile GenerateTestFile(ByteSize size)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
|
|
|
|
var result = CreateEmptyTestFile();
|
|
|
|
|
GenerateFileBytes(result, size);
|
2023-03-20 10:37:02 +00:00
|
|
|
|
TestLog.Log($"Generated {size} bytes of content for file '{result.Filename}'.");
|
2023-03-19 09:49:03 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteAllTestFiles()
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in activeFiles) File.Delete(file.Filename);
|
|
|
|
|
activeFiles.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 08:20:09 +00:00
|
|
|
|
private void GenerateFileBytes(TestFile result, ByteSize size)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-21 08:20:09 +00:00
|
|
|
|
long bytesLeft = size.SizeInBytes;
|
|
|
|
|
while (bytesLeft > 0)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-21 08:20:09 +00:00
|
|
|
|
var length = Math.Min(bytesLeft, ChunkSize);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
AppendRandomBytesToFile(result, length);
|
2023-03-21 08:20:09 +00:00
|
|
|
|
bytesLeft -= length;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 08:20:09 +00:00
|
|
|
|
private void AppendRandomBytesToFile(TestFile result, long length)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[length];
|
|
|
|
|
random.NextBytes(bytes);
|
|
|
|
|
using var stream = new FileStream(result.Filename, FileMode.Append);
|
|
|
|
|
stream.Write(bytes, 0, bytes.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TestFile
|
|
|
|
|
{
|
|
|
|
|
public TestFile(string filename)
|
|
|
|
|
{
|
|
|
|
|
Filename = filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Filename { get; }
|
|
|
|
|
|
|
|
|
|
public void AssertIsEqual(TestFile? other)
|
|
|
|
|
{
|
|
|
|
|
if (other == null) Assert.Fail("TestFile is null.");
|
|
|
|
|
if (other == this || other!.Filename == Filename) Assert.Fail("TestFile is compared to itself.");
|
|
|
|
|
|
|
|
|
|
using var stream1 = new FileStream(Filename, FileMode.Open, FileAccess.Read);
|
|
|
|
|
using var stream2 = new FileStream(other.Filename, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
|
|
|
|
var bytes1 = new byte[FileManager.ChunkSize];
|
|
|
|
|
var bytes2 = new byte[FileManager.ChunkSize];
|
|
|
|
|
|
|
|
|
|
var read1 = 0;
|
|
|
|
|
var read2 = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
read1 = stream1.Read(bytes1, 0, FileManager.ChunkSize);
|
|
|
|
|
read2 = stream2.Read(bytes2, 0, FileManager.ChunkSize);
|
|
|
|
|
|
|
|
|
|
if (read1 == 0 && read2 == 0) return;
|
|
|
|
|
Assert.That(read1, Is.EqualTo(read2), "Files are not of equal length.");
|
|
|
|
|
CollectionAssert.AreEqual(bytes1, bytes2, "Files are not binary-equal.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|