Skip to content

Commit

Permalink
Add tests for archive start offset
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Sep 10, 2024
1 parent 70eaa7d commit 55a5f3c
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ target_include_directories( ${TESTS_BASE_TARGET} INTERFACE
if( BIT7Z_TESTS_FILESYSTEM )
CPMAddPackage( NAME bit7z_test_data
GITHUB_REPOSITORY "rikyoz/bit7z-test-data"
VERSION 1.8
VERSION 1.10
DOWNLOAD_ONLY YES
GIT_PROGRESS ON )
if( bit7z_test_data_ADDED )
Expand Down
146 changes: 146 additions & 0 deletions tests/src/test_bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,4 +1357,150 @@ TEMPLATE_TEST_CASE( "BitInputArchive: Extract to raw data callback",
REQUIRE( totalSize == clouds.size );
REQUIRE( crcValue == clouds.crc32 );
}
}

// NOLINTNEXTLINE(*-err58-cpp)
TEMPLATE_TEST_CASE( "BitInputArchive: Reading the archive from the start of the input file",
"[bitinputarchive]", tstring, buffer_t, stream_t ) {
const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "single_file" };

#ifdef BIT7Z_BUILD_FOR_P7ZIP
const auto testArchive = GENERATE( as< TestInputFormat >(),
TestInputFormat{ "7z", BitFormat::SevenZip },
TestInputFormat{ "bz2", BitFormat::BZip2 },
TestInputFormat{ "gz", BitFormat::GZip },
TestInputFormat{ "iso", BitFormat::Iso },
TestInputFormat{ "lzh", BitFormat::Lzh },
TestInputFormat{ "lzma", BitFormat::Lzma },
TestInputFormat{ "tar", BitFormat::Tar },
TestInputFormat{ "wim", BitFormat::Wim },
TestInputFormat{ "xz", BitFormat::Xz },
TestInputFormat{ "zip", BitFormat::Zip } );
#else
const auto testArchive = GENERATE( as< TestInputFormat >(),
TestInputFormat{ "7z", BitFormat::SevenZip },
TestInputFormat{ "bz2", BitFormat::BZip2 },
TestInputFormat{ "gz", BitFormat::GZip },
TestInputFormat{ "iso", BitFormat::Iso },
TestInputFormat{ "lzh", BitFormat::Lzh },
TestInputFormat{ "lzma", BitFormat::Lzma },
TestInputFormat{ "rar4.rar", BitFormat::Rar },
TestInputFormat{ "rar5.rar", BitFormat::Rar5 },
TestInputFormat{ "tar", BitFormat::Tar },
TestInputFormat{ "wim", BitFormat::Wim },
TestInputFormat{ "xz", BitFormat::Xz },
TestInputFormat{ "zip", BitFormat::Zip } );
#endif

DYNAMIC_SECTION( "Archive format: " << testArchive.extension ) {
const fs::path arcFileName = fs::path{ clouds.name }.concat( "." + testArchive.extension );

TestType inputArchive{};
getInputArchive( arcFileName, inputArchive );
const Bit7zLibrary lib{ test::sevenzip_lib_path() };
REQUIRE_NOTHROW( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::FileStart, testArchive.format ) );
}
}

// NOLINTNEXTLINE(*-err58-cpp)
TEMPLATE_TEST_CASE( "BitInputArchive: Scanning a file for the archive start",
"[bitinputarchive]", tstring, buffer_t, stream_t ) {
const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "nested" };

const fs::path arcFileName = "multiple_nested2.tar";

TestType inputArchive{};
getInputArchive( arcFileName, inputArchive );
const Bit7zLibrary lib{ test::sevenzip_lib_path() };

#ifdef BIT7Z_AUTO_FORMAT
SECTION( "Detecting the format from the file extension (extension is correct)" ) {
const BitArchiveReader reader( lib, inputArchive, ArchiveStartOffset::None );
REQUIRE_NOTHROW( reader.detectedFormat() == BitFormat::Tar );
}
#endif

SECTION( "Opening the archive with the Zip format succeeds, "
"as 7-Zip will scan the input Tar archive and find the nested Zip archive" ) {
REQUIRE_NOTHROW( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::None, BitFormat::Zip ) );
}

SECTION( "Opening the archive with the 7z format succeeds, "
"as 7-Zip will scan the input Tar archive and find the nested 7z archive" ) {
REQUIRE_NOTHROW( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::None, BitFormat::SevenZip ) );
}

SECTION( "The BZip2 format doesn't support scanning the input file for the archive start,"
"so the opening must fail even though the Tar archive contains a BZip2 file") {
REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::None, BitFormat::BZip2 ) );
}
}

// NOLINTNEXTLINE(*-err58-cpp)
TEMPLATE_TEST_CASE( "BitInputArchive: Checking only the file start for the archive start",
"[bitinputarchive]", tstring, buffer_t, stream_t ) {
const TestDirectory testDir{ fs::path{ test_archives_dir } / "extraction" / "nested" };

const fs::path arcFileName = "multiple_nested2.tar";

TestType inputArchive{};
getInputArchive( arcFileName, inputArchive );
const Bit7zLibrary lib{ test::sevenzip_lib_path() };

#ifdef BIT7Z_AUTO_FORMAT
SECTION( "Detecting the format from the file extension (extension is correct)" ) {
const BitArchiveReader reader( lib, inputArchive, ArchiveStartOffset::FileStart );
REQUIRE_NOTHROW( reader.detectedFormat() == BitFormat::Tar );
}
#endif

SECTION( "Opening the Tar file as a Zip archive fails, as 7-Zip will check the format only at the file start" ) {
REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::FileStart, BitFormat::Zip ) );
}

SECTION( "Opening the Tar file as a 7z archive fails, as 7-Zip will check the format only at the file start" ) {
REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::FileStart, BitFormat::SevenZip ) );
}

SECTION( "Opening the Tar file as a BZip2 archive fails, as 7-Zip will check the format only at the file start" ) {
REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::FileStart, BitFormat::BZip2 ) );
}
}

// NOLINTNEXTLINE(*-err58-cpp)
TEMPLATE_TEST_CASE( "BitInputArchive: Reading a nested archive with wrong extension",
"[bitinputarchive]", tstring, buffer_t, stream_t ) {
const TestDirectory testDir{ fs::path{ test_archives_dir } / "detection" };

const fs::path arcFileName = "nested_wrong_extension.zip"; // 7z file with zip extension

TestType inputArchive{};
getInputArchive( arcFileName, inputArchive );
const Bit7zLibrary lib{ test::sevenzip_lib_path() };

SECTION( "Checking archive start at input file start" ){
#ifdef BIT7Z_AUTO_FORMAT
const BitArchiveReader reader( lib, inputArchive, ArchiveStartOffset::FileStart );
REQUIRE( reader.detectedFormat() == BitFormat::SevenZip );
REQUIRE_NOTHROW( reader.test() );
#else
REQUIRE_THROWS( BitArchiveReader( lib, inputArchive, ArchiveStartOffset::FileStart, BitFormat::Zip ) );
#endif
}

SECTION( "Checking archive start by scanning through the input file" ){
#ifdef BIT7Z_AUTO_FORMAT
const BitArchiveReader reader( lib, inputArchive, ArchiveStartOffset::None );
if ( reader.archivePath().empty() ) {
REQUIRE( reader.detectedFormat() == BitFormat::SevenZip );
REQUIRE_NOTHROW( reader.test() );
} else {
REQUIRE( reader.detectedFormat() == BitFormat::Zip );
REQUIRE_THROWS( reader.test() );
}
#else
const BitArchiveReader reader( lib, inputArchive, ArchiveStartOffset::None, BitFormat::Zip );
REQUIRE_THROWS( reader.test() );
#endif
}
}

0 comments on commit 55a5f3c

Please sign in to comment.