Skip to content

Commit

Permalink
Merge branch 'develop' into feature/wide-strs-support
Browse files Browse the repository at this point in the history
  • Loading branch information
dnzbk committed Nov 1, 2024
2 parents 0f89ecf + 3ad27d0 commit 611c68b
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ nzbget-setup.nsi eol=crlf
windows/package-info.json eol=crlf
windows/resources/resource.h eol=crlf
windows/resources/nzbget.rc eol=crlf
tests/testdata/**/* binary

# Configure GitHub's language detector
lib/* linguist-vendored linguist-language=C++
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ add_subdirectory(main)
add_subdirectory(extension)
add_subdirectory(nntp)
add_subdirectory(system)
#add_subdirectory(postprocess)
add_subdirectory(postprocess)
15 changes: 8 additions & 7 deletions tests/postprocess/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
file(GLOB PostprocessTestsSrc
main.cpp
DirectUnpackTest.cpp
DupeMatcherTest.cpp
# DirectUnpackTest.cpp
# DupeMatcherTest.cpp
ParCheckerTest.cpp
ParRenamerTest.cpp
RarReaderTest.cpp
RarReaderTest.cpp
RarRenamerTest.cpp
../suite/TestUtil.cpp
${CMAKE_SOURCE_DIR}/daemon/postprocess/DirectUnpack.cpp
${CMAKE_SOURCE_DIR}/daemon/postprocess/DupeMatcher.cpp
# ${CMAKE_SOURCE_DIR}/daemon/postprocess/DirectUnpack.cpp
# ${CMAKE_SOURCE_DIR}/daemon/postprocess/DupeMatcher.cpp
${CMAKE_SOURCE_DIR}/daemon/postprocess/ParChecker.cpp
${CMAKE_SOURCE_DIR}/daemon/postprocess/ParParser.cpp
${CMAKE_SOURCE_DIR}/daemon/postprocess/ParRenamer.cpp
Expand All @@ -20,15 +19,17 @@ file(GLOB PostprocessTestsSrc
${CMAKE_SOURCE_DIR}/daemon/util/Util.cpp
${CMAKE_SOURCE_DIR}/daemon/util/Thread.cpp
${CMAKE_SOURCE_DIR}/daemon/util/Log.cpp
${CMAKE_SOURCE_DIR}/daemon/util/ScriptController.cpp
#${CMAKE_SOURCE_DIR}/daemon/util/ScriptController.cpp
${CMAKE_SOURCE_DIR}/daemon/util/FileSystem.cpp
${CMAKE_SOURCE_DIR}/daemon/queue/DownloadInfo.cpp
${CMAKE_SOURCE_DIR}/daemon/queue/DiskState.cpp
)

add_executable(PostprocessTests ${PostprocessTestsSrc})
target_link_libraries(PostprocessTests PRIVATE ${LIBS})
target_include_directories(PostprocessTests PRIVATE ${INCLUDES})
target_include_directories(PostprocessTests PRIVATE ${INCLUDES} ../suite)

file(COPY ../testdata/dupematcher1 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ../testdata/parchecker DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ../testdata/rarrenamer DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME PostprocessTests COMMAND $<TARGET_FILE:PostprocessTests> --log_level=message)
3 changes: 2 additions & 1 deletion tests/postprocess/DirectUnpackTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2017-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down
17 changes: 9 additions & 8 deletions tests/postprocess/DupeMatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -14,42 +15,42 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


#include "nzbget.h"

#include <boost/test/unit_test.hpp>
#include <filesystem>

#include "FileSystem.h"
#include "DupeMatcher.h"
#include "TestUtil.h"

BOOST_AUTO_TEST_CASE(DupeMatcherTest)
{
const std::string testDataDir = std::filesystem::current_path().string() + "/rarrenamer";
const std::string testDataDir = TestUtil::WorkingDir() + "/rarrenamer";
const std::string workingDir = testDataDir + "/DupeMatcher";
std::filesystem::create_directory(workingDir);
FileSystem::CreateDirectory(workingDir.c_str());

CString errmsg;

// prepare directories

std::string dupe1(workingDir + "/dupe1");
BOOST_CHECK(FileSystem::ForceDirectories(dupe1.c_str(), errmsg));
std::filesystem::copy(dupe1, testDataDir + "/parchecker");
FileSystem::CopyFile(dupe1.c_str(), (testDataDir + "/parchecker").c_str());

std::string dupe2(workingDir + "/dupe2");
BOOST_CHECK(FileSystem::ForceDirectories(dupe2.c_str(), errmsg));
std::filesystem::copy(dupe2, testDataDir + "/parchecker");
FileSystem::CopyFile(dupe2.c_str(), (testDataDir + "/parchecker").c_str());
FileSystem::DeleteFile((dupe2 + "/testfile.nfo").c_str());

std::string rardupe1(testDataDir + "/dupematcher1");
std::string rardupe2(testDataDir + "/dupematcher2");

std::string nondupe(workingDir + "/nondupe");
BOOST_CHECK(FileSystem::ForceDirectories(nondupe.c_str(), errmsg));
std::filesystem::copy(nondupe, testDataDir + "/parchecker");
FileSystem::CopyFile(nondupe.c_str(), (testDataDir + "/parchecker").c_str());
remove((nondupe + "/testfile.dat").c_str());

// now test
Expand Down
107 changes: 71 additions & 36 deletions tests/postprocess/ParCheckerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand All @@ -25,61 +26,72 @@
#include "Options.h"
#include "ParChecker.h"
#include "TestUtil.h"
#include "FileSystem.h"
#include "YEncode.h"

class ParCheckerMock: public ParChecker
static std::string currDir = FileSystem::GetCurrentDirectory().Str();
static std::string testDataDir = currDir + "/" + "parchecker";

class ParCheckerMock : public ParChecker
{
public:
ParCheckerMock();
ParCheckerMock(std::string workingDir);
void Execute();
void CorruptFile(const char* filename, int offset);
~ParCheckerMock()
{
CString errmsg;
BOOST_CHECK(FileSystem::DeleteDirectoryWithContent(m_workingDir.c_str(), errmsg));
}

protected:
virtual bool RequestMorePars(int blockNeeded, int* blockFound) { return false; }
virtual EFileStatus FindFileCrc(const char* filename, uint32* crc, SegmentList* segments);
bool RequestMorePars(int blockNeeded, int* blockFound) override { return false; }
EFileStatus FindFileCrc(const char* filename, uint32* crc, SegmentList* segments) override;

private:
uint32 CalcFileCrc(const char* filename);
std::string m_workingDir;
};

ParCheckerMock::ParCheckerMock()
ParCheckerMock::ParCheckerMock(std::string workingDir) : m_workingDir(std::move(workingDir))
{
TestUtil::PrepareWorkingDir("parchecker");
SetDestDir(TestUtil::WorkingDir().c_str());
SetDestDir(m_workingDir.c_str());
BOOST_REQUIRE(FileSystem::CreateDirectory(m_workingDir.c_str()));
TestUtil::CopyAllFiles(m_workingDir.c_str(), testDataDir.c_str());
}

void ParCheckerMock::Execute()
{
TestUtil::DisableCout();
ParChecker::Execute();
TestUtil::EnableCout();
}

void ParCheckerMock::CorruptFile(const char* filename, int offset)
{
std::string fullfilename(TestUtil::WorkingDir() + "/" + filename);
std::string fullfilename(m_workingDir + "/" + filename);

FILE* file = fopen(fullfilename.c_str(), FOPEN_RBP);
BOOST_CHECK(file != nullptr);
BOOST_REQUIRE(file != nullptr);

fseek(file, offset, SEEK_SET);
char b = 0;
int written = fwrite(&b, 1, 1, file);
BOOST_CHECK(written == 1);
BOOST_REQUIRE(written == 1);

fclose(file);
}

ParCheckerMock::EFileStatus ParCheckerMock::FindFileCrc(const char* filename, uint32* crc, SegmentList* segments)
{
std::ifstream sm((TestUtil::WorkingDir() + "/crc.txt").c_str());
std::string crcFileName = m_workingDir + "/crc.txt";
std::ifstream sm(crcFileName);
std::string smfilename, smcrc;
while (!sm.eof())
{
sm >> smfilename >> smcrc;
if (smfilename == filename)
{
*crc = strtoul(smcrc.c_str(), nullptr, 16);
uint32 realCrc = CalcFileCrc((TestUtil::WorkingDir() + "/" + filename).c_str());
uint32 realCrc = CalcFileCrc((m_workingDir + "/" + filename).c_str());
return *crc == realCrc ? ParChecker::fsSuccess : ParChecker::fsUnknown;
}
}
Expand All @@ -89,7 +101,7 @@ ParCheckerMock::EFileStatus ParCheckerMock::FindFileCrc(const char* filename, ui
uint32 ParCheckerMock::CalcFileCrc(const char* filename)
{
FILE* infile = fopen(filename, FOPEN_RB);
BOOST_CHECK(infile);
BOOST_REQUIRE(infile);

CharBuffer buffer(1024 * 64);
Crc32 downloadCrc;
Expand All @@ -112,7 +124,7 @@ BOOST_AUTO_TEST_CASE(RepairNoNeedTest)
cmdOpts.push_back("ParRepair=no");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
ParCheckerMock parChecker(currDir + "/" + "RepairNoNeedTest");
parChecker.Execute();

BOOST_CHECK(parChecker.GetStatus() == ParChecker::psRepairNotNeeded);
Expand All @@ -125,7 +137,7 @@ BOOST_AUTO_TEST_CASE(RepairPossibleTest)
cmdOpts.push_back("ParRepair=no");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
ParCheckerMock parChecker(currDir + "/" + "RepairPossibleTest");
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.Execute();

Expand All @@ -139,21 +151,21 @@ BOOST_AUTO_TEST_CASE(RepairSuccessfulTest)
cmdOpts.push_back("ParRepair=yes");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
ParCheckerMock parChecker(currDir + "/" + "RepairSuccessfulTest");
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.Execute();

BOOST_CHECK(parChecker.GetStatus() == ParChecker::psRepaired);
BOOST_CHECK(parChecker.GetParFull() == true);
}

BOOST_AUTO_TEST_CASE(RepairFailed)
BOOST_AUTO_TEST_CASE(RepairFailedTest)
{
Options::CmdOptList cmdOpts;
cmdOpts.push_back("ParRepair=no");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
ParCheckerMock parChecker(currDir + "/" + "RepairSuccessfulTest");
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.CorruptFile("testfile.dat", 30000);
parChecker.CorruptFile("testfile.dat", 40000);
Expand All @@ -173,7 +185,9 @@ BOOST_AUTO_TEST_CASE(QuickVerificationRepairNotNeededTest)
cmdOpts.push_back("ParRepair=no");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
YEncode::init();

ParCheckerMock parChecker(currDir + "/" + "QuickVerificationRepairNotNeededTest");
parChecker.SetParQuick(true);
parChecker.Execute();

Expand All @@ -187,7 +201,9 @@ BOOST_AUTO_TEST_CASE(QuickVerificationRepairSuccessfulTest)
cmdOpts.push_back("ParRepair=yes");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
YEncode::init();

ParCheckerMock parChecker(currDir + "/" + "QuickVerificationRepairSuccessfulTest");
parChecker.SetParQuick(true);
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.Execute();
Expand All @@ -202,7 +218,9 @@ BOOST_AUTO_TEST_CASE(QuickFullVerificationRepairSuccessfulTest)
cmdOpts.push_back("ParRepair=yes");
Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
YEncode::init();

ParCheckerMock parChecker(currDir + "/" + "QuickFullVerificationRepairSuccessfulTest");
parChecker.SetParQuick(true);
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.CorruptFile("testfile.nfo", 100);
Expand All @@ -214,28 +232,45 @@ BOOST_AUTO_TEST_CASE(QuickFullVerificationRepairSuccessfulTest)
BOOST_CHECK(parChecker.GetParFull() == true);
}

BOOST_AUTO_TEST_CASE(IgnoringExtensionsTest)
BOOST_AUTO_TEST_CASE(ParIgnoreExtDatTest)
{
Options::CmdOptList cmdOpts;
cmdOpts.push_back("ParRepair=yes");

ParChecker::EStatus expectedStatus;

// SECTION("ParIgnoreExt")
// {
// cmdOpts.push_back("ParIgnoreExt=.dat");
// expectedStatus = ParChecker::psRepairNotNeeded;
// }
cmdOpts.push_back("ExtCleanupDisk=.dat");
expectedStatus = ParChecker::psFailed;

Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker(currDir + "/" + "ParIgnoreExtDatTest");
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.CorruptFile("testfile.dat", 30000);
parChecker.CorruptFile("testfile.dat", 40000);
parChecker.CorruptFile("testfile.dat", 50000);
parChecker.CorruptFile("testfile.dat", 60000);
parChecker.CorruptFile("testfile.dat", 70000);
parChecker.CorruptFile("testfile.dat", 80000);

parChecker.Execute();

BOOST_CHECK(parChecker.GetStatus() == expectedStatus);
}

BOOST_AUTO_TEST_CASE(ExtCleanupDiskDatTest)
{
Options::CmdOptList cmdOpts;
cmdOpts.push_back("ParRepair=yes");

ParChecker::EStatus expectedStatus;

// SECTION("ExtCleanupDisk")
// {
// cmdOpts.push_back("ExtCleanupDisk=.dat");
// expectedStatus = ParChecker::psFailed;
// }
cmdOpts.push_back("ExtCleanupDisk=.dat");
expectedStatus = ParChecker::psFailed;

Options options(&cmdOpts, nullptr);

ParCheckerMock parChecker;
ParCheckerMock parChecker(currDir + "/" + "ExtCleanupDiskDatTest");
parChecker.CorruptFile("testfile.dat", 20000);
parChecker.CorruptFile("testfile.dat", 30000);
parChecker.CorruptFile("testfile.dat", 40000);
Expand Down
Loading

0 comments on commit 611c68b

Please sign in to comment.