diff --git a/args.c b/args.c index 1cdeb3f..2ccb6f2 100644 --- a/args.c +++ b/args.c @@ -1,34 +1,32 @@ #include "sha256sum.h" -void usage(__in LPWSTR prog, __in_opt LPWSTR message) +void PrintUsage(__in LPWSTR prog, __in_opt LPWSTR message) { -#ifndef _UNITTESTS if (NULL != message) { wprintf(L"%ls\n", message); } wprintf(L"Usage: %ls [-c sha256sums_file] [file...]\n", prog); -#endif } -ErrorCode parse_args(__out Args* args, __in int argc, __in LPWSTR argv[]) +ErrorCode ParseArgs(__out Args* args, __in int argc, __in LPWSTR argv[]) { ErrorCode status = SUCCESS; FileList* current = NULL; // default values args->files = NULL; - args->sum_file = NULL; + args->sumFile = NULL; args->quiet = FALSE; args->status = FALSE; args->warn = FALSE; - args->show_version = FALSE; - args->text_mode = FALSE; + args->showVersion = FALSE; + args->textMode = FALSE; // check if there are any argments given if (argc < 2) { - usage(argv[0], L"too few arguments"); + PrintUsage(argv[0], L"too few arguments"); status = PARSE_ARGS_MISSING_PARAMETER; goto Cleanup; } @@ -38,14 +36,14 @@ ErrorCode parse_args(__out Args* args, __in int argc, __in LPWSTR argv[]) // -v, --version if (wcscmp(argv[i], L"-v") == 0 || wcscmp(argv[i], L"--version") == 0) { - args->show_version = TRUE; + args->showVersion = TRUE; goto Cleanup; } // -t, --text if (wcscmp(argv[i], L"-t") == 0 || wcscmp(argv[i], L"--text") == 0) { - args->text_mode = TRUE; + args->textMode = TRUE; goto Cleanup; } @@ -55,28 +53,28 @@ ErrorCode parse_args(__out Args* args, __in int argc, __in LPWSTR argv[]) // do nothing, since this only works with binary continue; } - + // -q, --quiet if (wcscmp(argv[i], L"-q") == 0 || wcscmp(argv[i], L"--quiet") == 0) { args->quiet = TRUE; continue; } - + // -s, --status if (wcscmp(argv[i], L"-s") == 0 || wcscmp(argv[i], L"--status") == 0) { args->status = TRUE; continue; } - + // -w, --warn if (wcscmp(argv[i], L"-w") == 0 || wcscmp(argv[i], L"--warn") == 0) { args->warn = TRUE; continue; } - + // -c, --check // checks for -c or --check and checks the following argument // fails when there is no other argument after -c @@ -85,13 +83,13 @@ ErrorCode parse_args(__out Args* args, __in int argc, __in LPWSTR argv[]) // check if there is another argument after -c if (i + 1 < argc) { - args->sum_file = argv[i + 1]; + args->sumFile = argv[i + 1]; ++i; // skip next argument since we used it here continue; } else { - usage(argv[0], L"missing SHA256SUMS file"); + PrintUsage(argv[0], L"missing SHA256SUMS file"); status = PARSE_ARGS_MISSING_SHASUMS_FILE; goto Cleanup; } diff --git a/main.c b/main.c index d70f0e4..b5150c9 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ int run(int argc, LPWSTR argv[]) { Args args = { 0 }; - ErrorCode parse_result = parse_args(&args, argc, argv); + ErrorCode parse_result = ParseArgs(&args, argc, argv); // error handling from argument parsing switch (parse_result) @@ -19,23 +19,23 @@ int run(int argc, LPWSTR argv[]) } // show application version - if (args.show_version) + if (args.showVersion) { wprintf(L"%ls version %d.%d.%d\n", argv[0], MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION); return SUCCESS; } // since WinAPI only reads binary we don't support text mode - if (args.text_mode) + if (args.textMode) { wprintf(L"%ls only supports binary mode\n", argv[0]); return SUCCESS; } // run check on checksum file - if (args.sum_file != NULL) + if (args.sumFile != NULL) { - return check_sums(&args); + return VerifyChecksums(&args); } // handle all FILE parameters @@ -55,7 +55,7 @@ int run(int argc, LPWSTR argv[]) do { - ErrorCode printHashStatus = print_hash(&args, findFileData.cFileName); + ErrorCode printHashStatus = PrintHash(&args, findFileData.cFileName); if (printHashStatus != SUCCESS) { FindClose(hFind); diff --git a/sha256.c b/sha256.c index 4afb4db..b0b2e2d 100644 --- a/sha256.c +++ b/sha256.c @@ -16,7 +16,7 @@ typedef struct file_hash_t struct file_hash_t* next; } FileHash; -void rem_binary_prefix(LPWSTR str) +void RemoveBinaryPrefix(LPWSTR str) { if (str[0] == L'*') { @@ -28,7 +28,7 @@ void rem_binary_prefix(LPWSTR str) } } -ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file) +ErrorCode CalcHash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file) { ErrorCode status = SUCCESS; HANDLE hFile; @@ -44,7 +44,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file) PBYTE pbHashObject = NULL; PBYTE pbHash = NULL; - rem_binary_prefix(file); + RemoveBinaryPrefix(file); // open file hFile = CreateFileW(file, // File name @@ -227,10 +227,10 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file) return status; } -ErrorCode print_hash(__in Args* args, __in LPWSTR file) +ErrorCode PrintHash(__in Args* args, __in LPWSTR file) { LPWSTR hash = NULL; - ErrorCode ok = calc_hash(args, &hash, file); + ErrorCode ok = CalcHash(args, &hash, file); if (hash != NULL && ok == SUCCESS) { wprintf(L"%ls *%ls\n", hash, file); @@ -238,7 +238,7 @@ ErrorCode print_hash(__in Args* args, __in LPWSTR file) return ok; } -ErrorCode parse_line(__in Args* args, __out FileHash* fh, __in int line_num, __in LPWSTR line) +ErrorCode ParseLine(__in Args* args, __out FileHash* fh, __in int line_num, __in LPWSTR line) { LPWSTR tokContext = NULL; LPWSTR delim = L" "; @@ -275,7 +275,7 @@ ErrorCode parse_line(__in Args* args, __out FileHash* fh, __in int line_num, __i return SUCCESS; } -ErrorCode check_sums(__in Args* args) +ErrorCode VerifyChecksums(__in Args* args) { ErrorCode status = SUCCESS; HANDLE hFile = NULL; @@ -288,7 +288,7 @@ ErrorCode check_sums(__in Args* args) FileHash* current = NULL; // open file - hFile = CreateFileW(args->sum_file, // File name + hFile = CreateFileW(args->sumFile, // File name GENERIC_READ, // Open for reading 0, // No sharing NULL, // Default security @@ -371,7 +371,7 @@ ErrorCode check_sums(__in Args* args) } else { - ErrorCode parseResult = parse_line(args, fh, lineNum, wideBuffer); + ErrorCode parseResult = ParseLine(args, fh, lineNum, wideBuffer); if (parseResult != SUCCESS) { free(fh); @@ -442,7 +442,7 @@ ErrorCode check_sums(__in Args* args) } else { - ErrorCode parseResult = parse_line(args, fh, lineNum, wideBuffer); + ErrorCode parseResult = ParseLine(args, fh, lineNum, wideBuffer); if (parseResult != SUCCESS) { free(fh); @@ -478,7 +478,7 @@ ErrorCode check_sums(__in Args* args) while (current != NULL) { LPWSTR file_hash = NULL; - ErrorCode calcResult = calc_hash(args, &file_hash, current->file); + ErrorCode calcResult = CalcHash(args, &file_hash, current->file); if (calcResult != SUCCESS) { status = calcResult; diff --git a/sha256sum.h b/sha256sum.h index 7f8ca84..96a4102 100644 --- a/sha256sum.h +++ b/sha256sum.h @@ -54,12 +54,12 @@ typedef struct file_list typedef struct prog_args { FileList* files; - LPWSTR sum_file; + LPWSTR sumFile; BOOL quiet; BOOL status; BOOL warn; - BOOL show_version; - BOOL text_mode; + BOOL showVersion; + BOOL textMode; } Args; // this is required for CppUnitTestFramework @@ -67,10 +67,11 @@ typedef struct prog_args extern "C" { #endif -ErrorCode parse_args(__out Args*, __in int, __in LPWSTR[]); +ErrorCode ParseArgs(__out Args*, __in int, __in LPWSTR[]); -ErrorCode print_hash(__in Args* args, __in LPWSTR); -ErrorCode check_sums(__in Args*); +ErrorCode CalcHash(__in Args*, __out LPWSTR*, __in LPWSTR); +ErrorCode PrintHash(__in Args*, __in LPWSTR); +ErrorCode VerifyChecksums(__in Args*); #ifdef __cplusplus } diff --git a/tests/CalcHashTestFile.txt b/tests/CalcHashTestFile.txt new file mode 100644 index 0000000..afbccbb --- /dev/null +++ b/tests/CalcHashTestFile.txt @@ -0,0 +1 @@ +This is a test file for the CalcHash test diff --git a/tests/ShasumFailure.txt b/tests/ShasumFailure.txt new file mode 100644 index 0000000..fd69dba --- /dev/null +++ b/tests/ShasumFailure.txt @@ -0,0 +1 @@ +5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a2 *CalcHashTestFile.txt \ No newline at end of file diff --git a/tests/ShasumSuccess.txt b/tests/ShasumSuccess.txt new file mode 100644 index 0000000..3e1f9e3 --- /dev/null +++ b/tests/ShasumSuccess.txt @@ -0,0 +1 @@ +5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1 *CalcHashTestFile.txt \ No newline at end of file diff --git a/tests/args.cpp b/tests/args.cpp new file mode 100644 index 0000000..58a37fb --- /dev/null +++ b/tests/args.cpp @@ -0,0 +1,115 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace args { +TEST_CLASS(fParseArgs) +{ +public: + + TEST_METHOD(TestNoArgs) + { + LPWSTR argv[] = { L"prog" }; + int argc = 1; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = PARSE_ARGS_MISSING_PARAMETER; + + Assert::AreEqual((int)act, (int)exp); + } + + TEST_METHOD(TestCheckWithoutSumFile) + { + LPWSTR argv[] = { L"prog", L"-c" }; + int argc = 2; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = PARSE_ARGS_MISSING_SHASUMS_FILE; + + Assert::AreEqual((int)act, (int)exp); + } + + TEST_METHOD(TestCheckWithSumFile) + { + LPWSTR argv[] = { L"prog", L"-c", L"SHA256SUMS" }; + int argc = 3; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.sumFile, L"SHA256SUMS"); + } + + TEST_METHOD(TestFiles) + { + LPWSTR argv[] = { L"prog", L"file1", L"file2" }; + int argc = 3; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.files->file, L"file1"); + Assert::AreEqual(args.files->next->file, L"file2"); + } + + TEST_METHOD(TestVersion) + { + LPWSTR argv[] = { L"prog", L"-v" }; + int argc = 2; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.showVersion, TRUE); + } + + TEST_METHOD(TestTextMode) + { + LPWSTR argv[] = { L"prog", L"-t" }; + int argc = 2; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.textMode, TRUE); + } + + TEST_METHOD(TestStatus) + { + LPWSTR argv[] = { L"prog", L"-s" }; + int argc = 2; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.status, TRUE); + } + + TEST_METHOD(TestWarn) + { + LPWSTR argv[] = { L"prog", L"-w" }; + int argc = 2; + Args args = { 0 }; + + ErrorCode act = ParseArgs(&args, argc, argv); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(args.warn, TRUE); + } +}; +} diff --git a/tests/sha256.cpp b/tests/sha256.cpp new file mode 100644 index 0000000..a3dcbcd --- /dev/null +++ b/tests/sha256.cpp @@ -0,0 +1,54 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace sha256 { +TEST_CLASS(fCalcHash) +{ +public: + + TEST_METHOD(TestValidHash) + { + Args args = { 0 }; + LPWSTR file = L"CalcHashTestFile.txt"; + + LPWSTR hash = NULL; + ErrorCode act = CalcHash(&args, &hash, file); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + Assert::AreEqual(L"5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1", hash); + } +}; + +TEST_CLASS(fVerifyChecksums) +{ +public: + + TEST_METHOD(TestSuccess) + { + Args args = { 0 }; + args.sumFile = L"ShasumSuccess.txt"; + + LPWSTR hash = NULL; + ErrorCode act = VerifyChecksums(&args); + ErrorCode exp = SUCCESS; + + Assert::AreEqual((int)act, (int)exp); + } + + TEST_METHOD(TestFailure) + { + Args args = { 0 }; + args.sumFile = L"ShasumFailure.txt"; + + LPWSTR hash = NULL; + ErrorCode act = VerifyChecksums(&args); + ErrorCode exp = CHECK_SUM_CHECKSUM_FAILED; + + Assert::AreEqual((int)act, (int)exp); + } +}; +} diff --git a/tests/tests.cpp b/tests/tests.cpp deleted file mode 100644 index 5844056..0000000 --- a/tests/tests.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include "pch.h" -#include "CppUnitTest.h" -#include "../sha256sum.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace sha256sum { - TEST_CLASS(args) - { - public: - - TEST_METHOD(TestNoArgs) - { - LPWSTR argv[] = { L"prog" }; - int argc = 1; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = PARSE_ARGS_MISSING_PARAMETER; - - Assert::AreEqual((int)act, (int)exp); - } - - TEST_METHOD(TestCheckWithoutSumFile) - { - LPWSTR argv[] = { L"prog", L"-c" }; - int argc = 2; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = PARSE_ARGS_MISSING_SHASUMS_FILE; - - Assert::AreEqual((int)act, (int)exp); - } - - TEST_METHOD(TestCheckWithSumFile) - { - LPWSTR argv[] = { L"prog", L"-c", L"SHA256SUMS" }; - int argc = 3; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.sum_file, L"SHA256SUMS"); - } - - TEST_METHOD(TestFiles) - { - LPWSTR argv[] = { L"prog", L"file1", L"file2" }; - int argc = 3; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.files->file, L"file1"); - Assert::AreEqual(args.files->next->file, L"file2"); - } - - TEST_METHOD(TestVersion) - { - LPWSTR argv[] = { L"prog", L"-v" }; - int argc = 2; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.show_version, TRUE); - } - - TEST_METHOD(TestTextMode) - { - LPWSTR argv[] = { L"prog", L"-t" }; - int argc = 2; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.text_mode, TRUE); - } - - TEST_METHOD(TestStatus) - { - LPWSTR argv[] = { L"prog", L"-s" }; - int argc = 2; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.status, TRUE); - } - - TEST_METHOD(TestWarn) - { - LPWSTR argv[] = { L"prog", L"-w" }; - int argc = 2; - Args args = { 0 }; - - ErrorCode act = parse_args(&args, argc, argv); - ErrorCode exp = SUCCESS; - - Assert::AreEqual((int)act, (int)exp); - Assert::AreEqual(args.warn, TRUE); - } - }; -} diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 1647d51..972645d 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -85,6 +85,7 @@ true $(ProjectDir)..\;$(IncludePath) + true false @@ -137,7 +138,7 @@ Windows $(TargetDir);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - args.obj;%(AdditionalDependencies) + args.obj;sha256.obj;%(AdditionalDependencies) @@ -166,7 +167,8 @@ Create Create - + + @@ -176,6 +178,17 @@ {0d52e9d4-df97-4975-854e-3bc74d9a0731} + + + true + + + true + + + true + + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 5ea1792..49bf4e1 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -15,16 +15,30 @@ - + Source Files Source Files + + Source Files + Header Files + + + Resource Files + + + Resource Files + + + Resource Files + + \ No newline at end of file