gra-projekt/Implementierung/src/main.c
2022-06-29 19:03:12 +02:00

83 lines
2.4 KiB
C

#include <stdbool.h>
#include "helper.h"
#include "io.h"
#include "md2.h"
// Returns true when val is approx. equal to exp.
static bool runTest(const char* message, const char* expectedHash) {
uint8_t out[16];
md2_hash(strlen(message), message, out);
char hash[32];
encodeHash(out, hash);
bool ok = !strcmp(hash, expectedHash);
printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash,
expectedHash);
return ok;
}
unsigned runTests(void) {
unsigned failed = 0;
// Note: cases with n<2 are not tested here.
failed += !runTest("", "8350e5a3e24c153df2275c9f80692773");
failed += !runTest("a", "32ec01ec4a6dac72c0ab96fb34c0b5d1");
failed += !runTest("abc", "da853b0d3f88d99b30283a69e6ded6bb");
failed += !runTest("message digest", "ab4f496bfb2a530b219ff33031fe06b0");
failed += !runTest("jebdjcslfhwfdig", "e1b69085c6f6e36cb8fe8d98ed3f2c35");
failed += !runTest("0123456789abcde", "d95629645108a20ab4d70e8545e0723b");
failed += !runTest("0123456789abcdef", "12c8dfa285f14e1af8c5254e7092d0d3");
failed += !runTest("0123456789abcdefg", "e4d0efded5ef7b6843a5ba47e1171347");
failed += !runTest("abcdefghijklmnopqrstuvwxyz",
"4e8ddff3650292ab5a4108c3aa47940b");
failed +=
!runTest("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"da33def2a42df13975352846c30338cd");
failed += !runTest(
"123456789012345678901234567890123456789012345678901234567890123456789012"
"34567890",
"d5976f79d83d3a0dc9806c3c66f3efd8");
if (failed)
printf("%u tests FAILED\n", failed);
else
printf("All tests PASSED\n");
return failed;
}
int main(int argc, char** argv) {
struct configuration c;
enum argumentParseResult result = parseArguments(argc, argv, &c);
switch (result) {
case RESULT_EXIT_SUCCESS:
return EXIT_SUCCESS;
case RESULT_EXIT_FAILURE:
return EXIT_FAILURE;
default:
break;
}
printf(
"Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, "
"benchmark cycles: %d\n",
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
runTests();
/*size_t len;
uint8_t* data = read_file(c.filename, &len);
uint8_t out[16];
const char* test = "abcdefghijklmnopqrstuvwxyz";
len = strlen(test);
md2_hash(len, test, out);
printf("Hash: ");
const char hash[16];
encodeHash(out, &hash);
printf("%s\n", hash);*/
return 0;
}