Implemented Ar Reader/Writer and UnitTests
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("UnitTests")]
|
|
||||||
|
|
||||||
namespace AMWD.Common.Packing.Ar
|
namespace AMWD.Common.Packing.Ar
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ namespace AMWD.Common.Packing.Ar
|
|||||||
byte[] magic = new byte[2];
|
byte[] magic = new byte[2];
|
||||||
inStream.Read(magic, 0, magic.Length);
|
inStream.Read(magic, 0, magic.Length);
|
||||||
|
|
||||||
if (magic[0] != 0x60 || magic[1] != 0x0A)
|
if (magic[0] != 0x60 || magic[1] != 0x0A) // `\n
|
||||||
throw new FormatException("Invalid file magic");
|
throw new FormatException("Invalid file magic");
|
||||||
|
|
||||||
return new ArFileInfoExtended
|
return new ArFileInfoExtended
|
||||||
|
|||||||
@@ -217,6 +217,126 @@ namespace UnitTests.Common.Packing.Ar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentException))]
|
||||||
|
public void ShouldThrowExceptionOnMissingRead()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stream = new OverrideStream();
|
||||||
|
stream.CanReadOR = false;
|
||||||
|
stream.CanSeekOR = true;
|
||||||
|
stream.CanWriteOR = true;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var reader = new ArReader(stream);
|
||||||
|
|
||||||
|
// Assert - ArgumentException
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentException))]
|
||||||
|
public void ShouldThrowExceptionOnMissingSeek()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stream = new OverrideStream();
|
||||||
|
stream.CanReadOR = true;
|
||||||
|
stream.CanSeekOR = false;
|
||||||
|
stream.CanWriteOR = true;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var reader = new ArReader(stream);
|
||||||
|
|
||||||
|
// Assert - ArgumentException
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentException))]
|
||||||
|
public void ShouldThrowExceptionOnMissingWrite()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stream = new OverrideStream();
|
||||||
|
stream.CanReadOR = true;
|
||||||
|
stream.CanSeekOR = true;
|
||||||
|
stream.CanWriteOR = false;
|
||||||
|
|
||||||
|
var reader = new ArReader(inStream);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
reader.ReadFile("abcd.tmp", stream);
|
||||||
|
|
||||||
|
// Assert - ArgumentException
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(FormatException))]
|
||||||
|
public void ShouldThrowExceptionOnInvalidArchive()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
inStream.Seek(8, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_ = new ArReader(inStream);
|
||||||
|
|
||||||
|
// Assert - FormatException
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(FormatException))]
|
||||||
|
public void ShouldThrowExceptionOnInvalidMagic()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
inStream.Seek(0, SeekOrigin.End);
|
||||||
|
inStream.Write(Encoding.ASCII.GetBytes($"{"foo.bar",-16}{"123456789",-12}123 456 100644 {"0",-10}´\n"));
|
||||||
|
inStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_ = new ArReader(inStream);
|
||||||
|
|
||||||
|
// Assert - FormatException
|
||||||
|
Assert.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldWriteNothingToStreamForMissingFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var reader = new ArReader(inStream);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
using var ms = new MemoryStream();
|
||||||
|
|
||||||
|
reader.ReadFile("foo.bar", ms);
|
||||||
|
ms.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(0, ms.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldWriteNothingToDiskForMissingFile()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string tmpFile = Path.GetTempFileName();
|
||||||
|
var reader = new ArReader(inStream);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
reader.ReadFile("foo.bar", tmpFile);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(0, new FileInfo(tmpFile).Length);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
File.Delete(tmpFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class OverrideStream : MemoryStream
|
private class OverrideStream : MemoryStream
|
||||||
{
|
{
|
||||||
public override bool CanWrite => CanWriteOR;
|
public override bool CanWrite => CanWriteOR;
|
||||||
@@ -226,6 +346,10 @@ namespace UnitTests.Common.Packing.Ar
|
|||||||
public override bool CanSeek => CanSeekOR;
|
public override bool CanSeek => CanSeekOR;
|
||||||
|
|
||||||
public bool CanSeekOR { get; set; }
|
public bool CanSeekOR { get; set; }
|
||||||
|
|
||||||
|
public override bool CanRead => CanReadOR;
|
||||||
|
|
||||||
|
public bool CanReadOR { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user