Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cwansart committed Oct 3, 2023
1 parent 2e8a4fa commit d0d7c64
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 157 deletions.
30 changes: 14 additions & 16 deletions args.c
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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 <file>
// checks for -c or --check and checks the following argument
// fails when there is no other argument after -c
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'*')
{
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -227,18 +227,18 @@ 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);
}
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" ";
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions sha256sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,24 @@ 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
#ifdef __cplusplus
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
}
Expand Down
1 change: 1 addition & 0 deletions tests/CalcHashTestFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file for the CalcHash test
1 change: 1 addition & 0 deletions tests/ShasumFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a2 *CalcHashTestFile.txt
1 change: 1 addition & 0 deletions tests/ShasumSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5825c4a88eddd074eb3c12b23dedc0eb4d7d5f2356a61a4078a0bd3ccf69c7a1 *CalcHashTestFile.txt
Loading

0 comments on commit d0d7c64

Please sign in to comment.