From 3c89b0a6e6dc5fa36ab44d747a33eb9a1cd9f726 Mon Sep 17 00:00:00 2001 From: Fabian Grutschus Date: Tue, 12 Nov 2024 18:33:30 +0100 Subject: [PATCH] Added tests for calculating/verification absolute and relative paths (#2) * Fixed build, fixed tests, fixed debug env, added test for relative and absolute paths --------- Co-authored-by: Fabian Grutschus --- sha256sum.vcxproj | 5 +- tests/sha256.cpp | 166 ++++++++++++++++++++++++++++++++++-- tests/tests.vcxproj | 23 ++--- tests/tests.vcxproj.filters | 12 +-- tests/tests.vcxproj.user | 12 ++- 5 files changed, 193 insertions(+), 25 deletions(-) diff --git a/sha256sum.vcxproj b/sha256sum.vcxproj index b661f91..b35acaa 100644 --- a/sha256sum.vcxproj +++ b/sha256sum.vcxproj @@ -31,7 +31,7 @@ Win32Proj {0d52e9d4-df97-4975-854e-3bc74d9a0731} sha256sum - 10.0.22000.0 + 10.0.22621.0 @@ -68,7 +68,7 @@ Application false - v140 + v143 true Unicode x64 @@ -148,6 +148,7 @@ Console true + Shlwapi.lib;Pathcch.lib;%(AdditionalDependencies) diff --git a/tests/sha256.cpp b/tests/sha256.cpp index a3dcbcd..fc51c49 100644 --- a/tests/sha256.cpp +++ b/tests/sha256.cpp @@ -1,6 +1,12 @@ #include "pch.h" #include "CppUnitTest.h" #include +#include +#define GetCurrentDir _getcwd +#include +#include +#include +#include using namespace Microsoft::VisualStudio::CppUnitTestFramework; @@ -9,7 +15,7 @@ TEST_CLASS(fCalcHash) { public: - TEST_METHOD(TestValidHash) + TEST_METHOD(TestValidHashCurrentPath) { Args args = { 0 }; LPWSTR file = L"CalcHashTestFile.txt"; @@ -18,7 +24,77 @@ TEST_CLASS(fCalcHash) ErrorCode act = CalcHash(&args, &hash, file); ErrorCode exp = SUCCESS; - Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual((int)exp, (int)act); + Assert::AreEqual(L"5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1", hash); + } + + TEST_METHOD(TestValidHashMissingFile) + { + Args args = { 0 }; + LPWSTR file = L"Missing.txt"; + + LPWSTR hash = NULL; + ErrorCode act = CalcHash(&args, &hash, file); + ErrorCode exp = CALC_HASH_FAILED_TO_OPEN_FILE; + + Assert::AreEqual((int)exp, (int)act); + } + + TEST_METHOD(TestValidHashAbsolutePath) + { + char cwd[FILENAME_MAX]; + if (!GetCurrentDir(cwd, sizeof(cwd))) + { + Assert::Fail(L"Can't get current path"); + return; + } + + std::string fullPath = std::string(cwd) + "\\CalcHashTestFile.txt"; + std::wstring wideFullPath(fullPath.begin(), fullPath.end()); + + Args args = { 0 }; + LPWSTR file = &wideFullPath[0]; + + LPWSTR hash = NULL; + ErrorCode act = CalcHash(&args, &hash, file); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)exp, (int)act); + Assert::AreEqual(L"5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1", hash); + } + + TEST_METHOD(TestValidHashRelativePath) + { + char tempCwd[FILENAME_MAX]; + if (!GetCurrentDir(tempCwd, sizeof(tempCwd))) + { + Assert::Fail(L"Can't get current path"); + return; + } + + std::string cwd(tempCwd); + + size_t lastSeparator = cwd.find_last_of('\\'); + + std::string lastPart; + if (lastSeparator != std::string::npos) { + lastPart = cwd.substr(lastSeparator + 1); + } + else { + lastPart = cwd; + } + + std::string fullPath = std::string(cwd) + "\\..\\" + lastPart + "\\CalcHashTestFile.txt"; + std::wstring wideFullPath(fullPath.begin(), fullPath.end()); + + Args args = { 0 }; + LPWSTR file = &wideFullPath[0]; + + LPWSTR hash = NULL; + ErrorCode act = CalcHash(&args, &hash, file); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)exp, (int)act); Assert::AreEqual(L"5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1", hash); } }; @@ -27,7 +103,7 @@ TEST_CLASS(fVerifyChecksums) { public: - TEST_METHOD(TestSuccess) + TEST_METHOD(TestSuccessCurrentPath) { Args args = { 0 }; args.sumFile = L"ShasumSuccess.txt"; @@ -36,7 +112,87 @@ TEST_CLASS(fVerifyChecksums) ErrorCode act = VerifyChecksums(&args); ErrorCode exp = SUCCESS; - Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual((int)exp, (int)act); + } + + TEST_METHOD(TestSuccessAbsolutePath) + { + char cwd[FILENAME_MAX]; + if (!GetCurrentDir(cwd, sizeof(cwd))) + { + Assert::Fail(L"Can't get current path"); + return; + } + + std::string expectedChecksum = "5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1"; + std::string checksumFilename = "ShasumSuccessAbsolute.txt"; + std::ofstream checksumFile(checksumFilename); + + if (!checksumFile) { + Assert::Fail(L"Count not create checksum file"); + return; + } + + checksumFile << expectedChecksum << " *" << cwd << "\\CalcHashTestFile.txt" << std::endl; + checksumFile.close(); + + std::string fullPath = std::string(cwd) + "\\" + checksumFilename; + + std::wstring wideFullPath(fullPath.begin(), fullPath.end()); + Args args = { 0 }; + args.sumFile = &wideFullPath[0]; + + LPWSTR hash = NULL; + ErrorCode act = VerifyChecksums(&args); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)exp, (int)act); + } + + TEST_METHOD(TestSuccessRelativePath) + { + char tempCwd[FILENAME_MAX]; + if (!GetCurrentDir(tempCwd, sizeof(tempCwd))) + { + Assert::Fail(L"Can't get current path"); + return; + } + + std::string cwd(tempCwd); + + size_t lastSeparator = cwd.find_last_of('\\'); + + std::string lastPart; + if (lastSeparator != std::string::npos) { + lastPart = cwd.substr(lastSeparator + 1); + } + else { + lastPart = cwd; + } + + std::string expectedChecksum = "5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1"; + std::string checksumFilename = "ShasumSuccessRelative.txt"; + std::ofstream checksumFile(checksumFilename); + + if (!checksumFile) { + Assert::Fail(L"Count not create checksum file"); + return; + } + + checksumFile << expectedChecksum << " *" << "..\\" << lastPart << "\\CalcHashTestFile.txt" << std::endl; + checksumFile.close(); + + std::string fullPath = std::string(cwd) + "\\" + checksumFilename; + + std::wstring wideFullPath(fullPath.begin(), fullPath.end()); + Args args = { 0 }; + args.sumFile = &wideFullPath[0]; + + LPWSTR hash = NULL; + ErrorCode act = VerifyChecksums(&args); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)exp, (int)act); } TEST_METHOD(TestFailure) @@ -48,7 +204,7 @@ TEST_CLASS(fVerifyChecksums) ErrorCode act = VerifyChecksums(&args); ErrorCode exp = CHECK_SUM_CHECKSUM_FAILED; - Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual((int)exp, (int)act); } }; } diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 972645d..bf48699 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -23,7 +23,7 @@ {8C494A81-1735-4AEF-804C-242C1AAE19A7} Win32Proj tests - 10.0 + 10.0.22621.0 NativeUnitTestProject @@ -101,13 +101,14 @@ NDEBUG;%(PreprocessorDefinitions) true pch.h + Default Windows true true - $(VCInstallDir)UnitTest\lib;$(TargetDir);C:\src\sha256sum.exe\x64\Release\;sha256.obj;%(AdditionalLibraryDirectories) - args.obj;%(AdditionalDependencies) + $(VCInstallDir)UnitTest\lib;$(TargetDir);$(ProjectDir)..\sha256sum\x64\Release;%(AdditionalLibraryDirectories) + args.obj;sha256.obj;Shlwapi.lib;Pathcch.lib;%(AdditionalDependencies) @@ -137,8 +138,8 @@ Windows - $(TargetDir);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - args.obj;sha256.obj;%(AdditionalDependencies) + $(TargetDir);$(VCInstallDir)UnitTest\lib;$(ProjectDir)..\sha256sum\x64\Debug;%(AdditionalLibraryDirectories) + args.obj;sha256.obj;Shlwapi.lib;Pathcch.lib;%(AdditionalDependencies) @@ -179,15 +180,15 @@ - + true - - + + true - - + + true - + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 49bf4e1..224212f 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -31,14 +31,14 @@ - + Resource Files - - + + Resource Files - - + + Resource Files - + \ No newline at end of file diff --git a/tests/tests.vcxproj.user b/tests/tests.vcxproj.user index 88a5509..4c7ac91 100644 --- a/tests/tests.vcxproj.user +++ b/tests/tests.vcxproj.user @@ -1,4 +1,14 @@  - + + OUTPUT_PATH=$(OutputPath) +$(LocalDebuggerEnvironment) + WindowsLocalDebugger + + + $(LocalDebuggerEnvironment) + WindowsLocalDebugger + + + \ No newline at end of file