Skip to content

Commit

Permalink
use CppUnitTestingFramework and apply minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwansart committed Oct 3, 2023
1 parent 8350cc0 commit 2e8a4fa
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 251 deletions.
8 changes: 0 additions & 8 deletions SHA256SUMS

This file was deleted.

20 changes: 20 additions & 0 deletions cppcheck.cppcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="1">
<builddir>cppcheck-cppcheck-build-dir</builddir>
<platform>win64</platform>
<importproject>sha256sum.sln</importproject>
<analyze-all-vs-configs>true</analyze-all-vs-configs>
<check-headers>true</check-headers>
<check-unused-templates>true</check-unused-templates>
<max-ctu-depth>2</max-ctu-depth>
<max-template-recursion>100</max-template-recursion>
<vs-configurations>
<config>Debug</config>
<config>Release</config>
<config>Tests</config>
</vs-configurations>
<libraries>
<library>windows</library>
</libraries>
<project-name>cppcheck</project-name>
</project>
10 changes: 2 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int run(int argc, LPWSTR argv[])
// show application version
if (args.show_version)
{
wprintf(L"%ls version %ld.%ld.%ld\n", argv[0], MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
wprintf(L"%ls version %d.%d.%d\n", argv[0], MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
return SUCCESS;
}

Expand Down Expand Up @@ -49,8 +49,7 @@ int run(int argc, LPWSTR argv[])

if (hFind == INVALID_HANDLE_VALUE)
{
wprintf(L"failed to find files for argument '%ls' with error %d\n", current->file, GetLastError());
FindClose(hFind);
wprintf(L"failed to find files for argument '%ls' with error %lu\n", current->file, GetLastError());
return MAIN_FAILED_TO_FIND_FILES;
}

Expand All @@ -76,10 +75,5 @@ int run(int argc, LPWSTR argv[])

int wmain(int argc, LPWSTR argv[])
{
#ifdef _UNITTESTS
BOOL run_tests();
return run_tests();
#else
return run(argc, argv);
#endif // _UNITTESTS
}
22 changes: 11 additions & 11 deletions sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
if (!args->status)
{
wchar_t errorMsg[MAX_PATH + 50];
wsprintfW(errorMsg, L"failed to open file '%ls' with error: %d\r\n", file, GetLastError());
wsprintfW(errorMsg, L"failed to open file '%ls' with error: %lu\r\n", file, GetLastError());
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), errorMsg, lstrlenW(errorMsg), NULL, NULL);
}
status = CALC_HASH_FAILED_TO_OPEN_FILE;
Expand All @@ -71,7 +71,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("open an algorithm handle failed: %x\n", hashStatus);
printf("open an algorithm handle failed: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_OPEN_ALG_HANDLE;
goto Cleanup;
Expand All @@ -82,7 +82,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("hash buffer size allocation failed, err: %x\n", hashStatus);
printf("hash buffer size allocation failed, err: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_ALLOCATE_HASH_BUFFER_SIZE;
goto Cleanup;
Expand All @@ -105,7 +105,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("hash length calculation failed: %x\n", hashStatus);
printf("hash length calculation failed: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_CALC_HASH_LENGTH;
goto Cleanup;
Expand All @@ -128,7 +128,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("hash creation failed: %x\n", hashStatus);
printf("hash creation failed: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_CREATE_HASH;
goto Cleanup;
Expand All @@ -140,7 +140,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("read file failed: %d\n", GetLastError());
printf("read file failed: %lu\n", GetLastError());
}
status = CALC_HASH_FAILED_TO_READ;
goto Cleanup;
Expand All @@ -156,7 +156,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("data hashing failed: %x\n", hashStatus);
printf("data hashing failed: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_HASH;
goto Cleanup;
Expand All @@ -168,7 +168,7 @@ ErrorCode calc_hash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
{
if (!args->status)
{
printf("hash finalization failed: %x\n", hashStatus);
printf("hash finalization failed: %ld\n", hashStatus);
}
status = CALC_HASH_FAILED_TO_FINISH_HASH;
goto Cleanup;
Expand Down Expand Up @@ -299,7 +299,7 @@ ErrorCode check_sums(__in Args* args)
{
if (!args->status)
{
printf("failed to open file: %d\n", GetLastError());
printf("failed to open file: %lu\n", GetLastError());
}
status = CHECK_SUMS_FAILED_TO_OPEN_SUM_FILE;
goto Cleanup;
Expand Down Expand Up @@ -468,7 +468,7 @@ ErrorCode check_sums(__in Args* args)
{
if (!args->status)
{
printf("file read failed: %d\n", GetLastError());
printf("file read failed: %lu\n", GetLastError());
}
status = CHECK_SUMS_FAILED_TO_READ;
goto Cleanup;
Expand Down Expand Up @@ -523,7 +523,7 @@ ErrorCode check_sums(__in Args* args)
if (head != NULL)
{
current = head;
FileHash* next = current;
FileHash* next = NULL;
while (current != NULL)
{
next = current->next;
Expand Down
9 changes: 9 additions & 0 deletions sha256sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ typedef struct prog_args
BOOL text_mode;
} Args;

// this is required for CppUnitTestFramework
#ifdef __cplusplus
extern "C" {
#endif

ErrorCode parse_args(__out Args*, __in int, __in LPWSTR[]);

ErrorCode print_hash(__in Args* args, __in LPWSTR);
ErrorCode check_sums(__in Args*);

#ifdef __cplusplus
}
#endif
18 changes: 6 additions & 12 deletions sha256sum.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sha256sum", "sha256sum.vcxproj", "{0D52E9D4-DF97-4975-854E-3BC74D9A0731}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests\tests.vcxproj", "{8C494A81-1735-4AEF-804C-242C1AAE19A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
Tests|x64 = Tests|x64
Tests|x86 = Tests|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Debug|x64.ActiveCfg = Debug|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Debug|x64.Build.0 = Debug|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Debug|x86.ActiveCfg = Debug|Win32
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Debug|x86.Build.0 = Debug|Win32
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Release|x64.ActiveCfg = Release|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Release|x64.Build.0 = Release|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Release|x86.ActiveCfg = Release|Win32
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Release|x86.Build.0 = Release|Win32
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Tests|x64.ActiveCfg = Tests|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Tests|x64.Build.0 = Tests|x64
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Tests|x86.ActiveCfg = Tests|Win32
{0D52E9D4-DF97-4975-854E-3BC74D9A0731}.Tests|x86.Build.0 = Tests|Win32
{8C494A81-1735-4AEF-804C-242C1AAE19A7}.Debug|x64.ActiveCfg = Debug|x64
{8C494A81-1735-4AEF-804C-242C1AAE19A7}.Debug|x64.Build.0 = Debug|x64
{8C494A81-1735-4AEF-804C-242C1AAE19A7}.Release|x64.ActiveCfg = Release|x64
{8C494A81-1735-4AEF-804C-242C1AAE19A7}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 0 additions & 1 deletion sha256sum.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@
<ClCompile Include="args.c" />
<ClCompile Include="main.c" />
<ClCompile Include="sha256.c" />
<ClCompile Include="tests.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="sha256sum.h" />
Expand Down
3 changes: 0 additions & 3 deletions sha256sum.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
<ClCompile Include="sha256.c">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="tests.c">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sha256sum.h">
Expand Down
Loading

0 comments on commit 2e8a4fa

Please sign in to comment.