added test for SFX 7z archive (and partly implemented the feature, but we still have problems)

fixed tests to work on every machine
This commit is contained in:
KOLANICH
2013-10-12 20:14:06 +04:00
parent ee2d6216b7
commit 13ef1c3701
7 changed files with 73 additions and 26 deletions

View File

@@ -61,6 +61,17 @@ namespace SharpCompress.Test
ArchiveFileRead("7Zip.BZip2.7z"); ArchiveFileRead("7Zip.BZip2.7z");
} }
[TestMethod]
public void SevenZipArchive_LZMA2_SFX_PathRead()
{
ArchiveFileRead("7Zip.LZMA2.sfx.exe");
}
[TestMethod]
public void SevenZipArchive_LZMA2_SFX_StreamRead() {
ArchiveStreamRead("7Zip.LZMA2.sfx.exe");
}
[TestMethod] [TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))] [ExpectedException(typeof(IndexOutOfRangeException))]

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@@ -10,12 +11,13 @@ namespace SharpCompress.Test
{ {
public class TestBase public class TestBase
{ {
protected const string TEST_BASE_PATH = @"C:\Git\sharpcompress"; //protected const string TEST_BASE_PATH = @"C:\Git\sharpcompress";
protected static readonly string TEST_ARCHIVES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Archives"); protected static string TEST_BASE_PATH=null;
protected static readonly string ORIGINAL_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Original"); protected static string TEST_ARCHIVES_PATH;
protected static readonly string MISC_TEST_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "MiscTest"); protected static string ORIGINAL_FILES_PATH;
protected static readonly string SCRATCH_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch"); protected static string MISC_TEST_FILES_PATH;
protected static readonly string SCRATCH2_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch2"); protected static string SCRATCH_FILES_PATH;
protected static string SCRATCH2_FILES_PATH;
protected static IEnumerable<string> GetRarArchives() protected static IEnumerable<string> GetRarArchives()
{ {
@@ -152,10 +154,26 @@ namespace SharpCompress.Test
private static readonly object testLock = new object(); private static readonly object testLock = new object();
private TestContext ctx;
public TestContext TestContext {
get {
return ctx;
}
set {
ctx = value;
}
}
[TestInitialize] [TestInitialize]
public void TestSetup() public void TestSetup()
{ {
Monitor.Enter(testLock); Monitor.Enter(testLock);
TEST_BASE_PATH = Path.GetDirectoryName(Path.GetDirectoryName(ctx.TestDir));
TEST_ARCHIVES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Archives");
ORIGINAL_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Original");
MISC_TEST_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "MiscTest");
SCRATCH_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch");
SCRATCH2_FILES_PATH = Path.Combine(TEST_BASE_PATH, "TestArchives", "Scratch2");
} }
[TestCleanup] [TestCleanup]

View File

@@ -43,10 +43,12 @@ namespace SharpCompress.Archive
return TarArchive.Open(stream, options); return TarArchive.Open(stream, options);
} }
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
if (SevenZipArchive.IsSevenZipFile(stream))
{ {
stream.Seek(0, SeekOrigin.Begin); long offset = SevenZipArchive.FindSignature(stream);
return SevenZipArchive.Open(stream, options); if (offset > -1) {
stream.Seek(offset, SeekOrigin.Begin);
return SevenZipArchive.Open(stream, options);
}
} }
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
if (GZipArchive.IsGZipFile(stream)) if (GZipArchive.IsGZipFile(stream))

View File

@@ -145,7 +145,7 @@ namespace SharpCompress.Archive.SevenZip
{ {
if (database == null) if (database == null)
{ {
stream.Position = 0; stream.Seek(FindSignature(stream),SeekOrigin.Begin);
var reader = new ArchiveReader(); var reader = new ArchiveReader();
reader.Open(stream); reader.Open(stream);
database = reader.ReadDatabase(null); database = reader.ReadDatabase(null);
@@ -157,7 +157,7 @@ namespace SharpCompress.Archive.SevenZip
{ {
try try
{ {
return SignatureMatch(stream); return FindSignature(stream)>-1;
} }
catch catch
{ {
@@ -165,13 +165,27 @@ namespace SharpCompress.Archive.SevenZip
} }
} }
private static readonly byte[] SIGNATURE = new byte[] {(byte) '7', (byte) 'z', 0xBC, 0xAF, 0x27, 0x1C}; private static readonly byte[] SIGNATURE = {(byte) '7', (byte) 'z', 0xBC, 0xAF, 0x27, 0x1C};
const int MAX_BYTES_TO_ARCHIVE = 0x40000;
private static bool SignatureMatch(Stream stream) public static long FindSignature(Stream stream){
{
BinaryReader reader = new BinaryReader(stream); BinaryReader reader = new BinaryReader(stream);
byte[] signatureBytes = reader.ReadBytes(6); int j = 0;
return signatureBytes.BinaryEquals(SIGNATURE); long match=-1;
var maxPos = Math.Min(MAX_BYTES_TO_ARCHIVE+SIGNATURE.Length, stream.Length);
for (; stream.Position < maxPos; ) {
var bt = reader.ReadByte();
if (bt == SIGNATURE[j]){
if (j == SIGNATURE.Length-1){
match = stream.Position - j-1;
break;
}
j++;
continue;
}
j = 0;
}
return match;
} }
protected override IReader CreateReaderForSolidExtraction() protected override IReader CreateReaderForSolidExtraction()

View File

@@ -10,10 +10,10 @@ using System.Runtime.CompilerServices;
[assembly: AssemblyProduct("SharpCompress")] [assembly: AssemblyProduct("SharpCompress")]
[assembly: [assembly:
InternalsVisibleTo( InternalsVisibleTo(
"SharpCompress.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010005d6ae1b0f6875393da83c920a5b9408f5191aaf4e8b3c2c476ad2a11f5041ecae84ce9298bc4c203637e2fd3a80ad5378a9fa8da1363e98cea45c73969198a4b64510927c910001491cebbadf597b22448ad103b0a4007e339faf8fe8665dcdb70d65b27ac05b1977c0655fad06b372b820ecbdccf10a0f214fee0986dfeded" "SharpCompress.Test"
)] )]
[assembly: [assembly:
InternalsVisibleTo( InternalsVisibleTo(
"SharpCompress.Test.Portable, PublicKey=002400000480000094000000060200000024000052534131000400000100010005d6ae1b0f6875393da83c920a5b9408f5191aaf4e8b3c2c476ad2a11f5041ecae84ce9298bc4c203637e2fd3a80ad5378a9fa8da1363e98cea45c73969198a4b64510927c910001491cebbadf597b22448ad103b0a4007e339faf8fe8665dcdb70d65b27ac05b1977c0655fad06b372b820ecbdccf10a0f214fee0986dfeded" "SharpCompress.Test.Portable"
)] )]
#endif #endif

View File

@@ -941,6 +941,7 @@ namespace SharpCompress.Common.SevenZip
#region Public Methods #region Public Methods
const int HEADER_SIZE=0x20;
public void Open(Stream stream) public void Open(Stream stream)
{ {
Close(); Close();
@@ -949,13 +950,14 @@ namespace SharpCompress.Common.SevenZip
_streamEnding = stream.Length; _streamEnding = stream.Length;
// TODO: Check Signature! // TODO: Check Signature!
_header = new byte[0x20]; _header = new byte[HEADER_SIZE];
for (int offset = 0; offset < 0x20; ) int delta;
for (int offset = 0; offset < HEADER_SIZE; offset += delta)
{ {
int delta = stream.Read(_header, offset, 0x20 - offset); delta = stream.Read(_header, offset, HEADER_SIZE - offset);
if (delta == 0) if (delta == 0)
throw new EndOfStreamException(); throw new EndOfStreamException("ArchiveReader.Open : unable to read");
offset += delta;
} }
_stream = stream; _stream = stream;
@@ -995,7 +997,7 @@ namespace SharpCompress.Common.SevenZip
crc = CRC.Finish(crc); crc = CRC.Finish(crc);
if (crc != crcFromArchive) if (crc != crcFromArchive)
throw new InvalidOperationException(); throw new InvalidOperationException("Bad CRC");
db.StartPositionAfterHeader = _streamOrigin + 0x20; db.StartPositionAfterHeader = _streamOrigin + 0x20;

Binary file not shown.