Feat: Add tests

This commit is contained in:
Dorian Zedler 2022-06-29 19:03:12 +02:00
parent 9194475153
commit 331ed1750f
Signed by: dorian
GPG Key ID: 989DE36109AFA354
6 changed files with 81 additions and 16 deletions

11
.vscode/settings.json vendored
View File

@ -2,7 +2,14 @@
"C_Cpp.clang_format_fallbackStyle": "Google",
"files.associations": {
"functional": "c",
"helper.h": "c"
"helper.h": "c",
"string": "c",
"vector": "c",
"memory": "c",
"optional": "c",
"string_view": "c",
"system_error": "c",
"variant": "c"
},
"editor.formatOnSave": true,
"editor.formatOnSave": true
}

View File

@ -14,6 +14,6 @@
* @param size a pointer where the size of the result should be stored
* @return the raw data
*/
static uint8_t* read_file(const char* path, size_t* size);
uint8_t* read_file(const char* path, size_t* size);
#endif // IO_H

View File

@ -25,4 +25,6 @@ void md2_hash(size_t len, const uint8_t buf[], uint8_t out[16]);
*/
void md2_checksum(size_t len, uint8_t* buf);
void encodeHash(uint8_t hash[16], char* stringHash);
#endif // MD2_H

View File

@ -1,6 +1,6 @@
#include "io.h"
static uint8_t* read_file(const char* path, size_t* size) {
uint8_t* read_file(const char* path, size_t* size) {
// Read the contents of the file specified by path into a heap-allocated
// buffer and return a pointer to that buffer.
FILE* f = fopen(path, "r");

View File

@ -1,7 +1,52 @@
#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);
@ -20,14 +65,19 @@ int main(int argc, char** argv) {
"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];
md2_hash(3, "abc", out);
const char* test = "abcdefghijklmnopqrstuvwxyz";
len = strlen(test);
md2_hash(len, test, out);
printf("Hash: ");
for (int i = 0; i < 16; i++) {
printf("%02x", out[i]);
}
printf("\n");
const char hash[16];
encodeHash(out, &hash);
printf("%s\n", hash);*/
return 0;
}

View File

@ -48,7 +48,7 @@ void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
uint8_t originalPadding = paddingNeeded;
len += paddingNeeded;
printf("len: %d\n", len);
// printf("len: %d\n", len);
// +16 for the checksum
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
@ -58,14 +58,14 @@ void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
newBuf[len - paddingNeeded] = originalPadding;
paddingNeeded--;
}
printf("buf with padding: ");
printBuf(len + 16, newBuf);
// printf("buf with padding: ");
// printBuf(len + 16, newBuf);
// === step 2 ===
md2_checksum(len, newBuf);
printf("buf with cecksum: ");
printBuf(len + 16, newBuf);
// printf("buf with cecksum: ");
// printBuf(len + 16, newBuf);
// === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
@ -88,11 +88,17 @@ void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
t = (t + j) % 256;
}
}
printf("messageDigestBuf: \n");
printBuf(16, messageDigestBuf);
// printf("messageDigestBuf: \n");
// printBuf(16, messageDigestBuf);
memcpy(out, messageDigestBuf, 16);
free(messageDigestBuf);
free(newBuf);
}
void encodeHash(uint8_t hash[16], char* stringHash) {
for (int i = 0; i < 16; i++) {
sprintf(stringHash + (2 * i), "%02x", hash[i]);
}
}