Add tests for checksum creation

This commit is contained in:
Nathan Fisher 2023-08-09 15:28:02 -04:00
parent f0ee28d495
commit e159900f2e
9 changed files with 130 additions and 8 deletions

View file

@ -116,7 +116,7 @@ typedef struct {
} haggis_node; } haggis_node;
void haggis_node_deinit(haggis_node *node); void haggis_node_deinit(haggis_node *node);
haggis_node* haggis_create_node(char *file, haggis_hardlink_list *list); haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_list *list);
int haggis_extract_node(FILE *stram, haggis_node *node); int haggis_extract_node(FILE *stram, haggis_node *node);
int haggis_load_node(FILE *stream, haggis_node *node); int haggis_load_node(FILE *stream, haggis_node *node);
int haggis_store_node(FILE *stream, haggis_node *node); int haggis_store_node(FILE *stream, haggis_node *node);

View file

@ -31,6 +31,7 @@
*/ */
#include <limits.h> // PATH_MAX #include <limits.h> // PATH_MAX
#include <stddef.h>
#include <stdint.h> // uint<x>_t #include <stdint.h> // uint<x>_t
#if defined(__FreeBSD__) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__DragonFly__)
@ -72,7 +73,7 @@ int haggis_check_header(FILE *stream) {
return 1; return 1;
if (memcmp(buf, header, 7)) if (memcmp(buf, header, 7))
return 2; return 2;
return 1; return 0;
} }
void haggis_device_init(dev_t rdev, haggis_device *dev) { void haggis_device_init(dev_t rdev, haggis_device *dev) {
@ -158,6 +159,15 @@ int haggis_load_cksum(FILE *stream, haggis_checksum *cksum) {
return 0; return 0;
} }
void init_md5(haggis_file *f) {
MD5_CTX ctx;
f->cksum.tag = md5;
MD5Init(&ctx);
MD5Update(&ctx, f->data, (size_t)f->len.val);
MD5Final(f->cksum.sum.md5, &ctx);
}
int validate_md5(haggis_file *file) { int validate_md5(haggis_file *file) {
MD5_CTX ctx; MD5_CTX ctx;
u8 digest[16]; u8 digest[16];
@ -171,6 +181,15 @@ int validate_md5(haggis_file *file) {
} }
#if defined(__FreeBSD__) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__DragonFly__)
void init_sha1(haggis_file *f) {
SHA1_CTX ctx;
f->cksum.tag = sha1;
SHA1_Init(&ctx);
SHA1_Update(&ctx, f->data, (size_t)f->len.val);
SHA1_Final(f->cksum.sum.sha1, &ctx);
}
int validate_sha1(haggis_file *file) { int validate_sha1(haggis_file *file) {
SHA1_CTX ctx; SHA1_CTX ctx;
u8 digest[20]; u8 digest[20];
@ -183,6 +202,15 @@ int validate_sha1(haggis_file *file) {
return 0; return 0;
} }
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) #elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
void init_sha1(haggis_file *f) {
SHA1_CTX ctx;
f->cksum.tag = sha1;
SHA1_Init(&ctx);
SHA1_Update(&ctx, f->data, (size_t)f->len.val);
SHA1_Final(f->cksum.sum.sha1, &ctx);
}
int validate_sha1(haggis_file *file) { int validate_sha1(haggis_file *file) {
SHA1_CTX ctx; SHA1_CTX ctx;
u8 digest[20]; u8 digest[20];
@ -197,6 +225,15 @@ int validate_sha1(haggis_file *file) {
#endif /* if defined (__FreeBSD__) */ #endif /* if defined (__FreeBSD__) */
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__) #if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
void init_sha256(haggis_file *f) {
SHA256_CTX ctx;
f->cksum.tag = sha256;
SHA256_Init(&ctx);
SHA256_Update(&ctx, f->data, (size_t)f->len.val);
SHA256_Final(f->cksum.sum.sha256, &ctx);
}
int validate_sha256(haggis_file *file) { int validate_sha256(haggis_file *file) {
SHA256_CTX ctx; SHA256_CTX ctx;
u8 digest[32]; u8 digest[32];
@ -209,6 +246,15 @@ int validate_sha256(haggis_file *file) {
return 0; return 0;
} }
#elif defined(__linux__) || defined(__OpenBSD__) #elif defined(__linux__) || defined(__OpenBSD__)
void init_sha256(haggis_file *f) {
SHA2_CTX ctx;
f->cksum.tag = sha256;
SHA256_Init(&ctx);
SHA256_Update(&ctx, f->data, (size_t)f->len.val);
SHA256_Final(f->cksum.sum.sha256, &ctx);
}
int validate_sha256(haggis_file *file) { int validate_sha256(haggis_file *file) {
SHA2_CTX ctx; SHA2_CTX ctx;
u8 digest[32]; u8 digest[32];
@ -222,6 +268,22 @@ int validate_sha256(haggis_file *file) {
} }
#endif /* if defined (__FreeBSD__) */ #endif /* if defined (__FreeBSD__) */
void haggis_init_cksum(haggis_file *f, haggis_algorithm a) {
switch (a) {
case md5:
init_md5(f);
break;
case sha1:
init_sha1(f);
break;
case sha256:
init_sha256(f);
break;
case skip:
break;
}
}
int haggis_validate_cksum(haggis_file *file) { int haggis_validate_cksum(haggis_file *file) {
switch (file->cksum.tag) { switch (file->cksum.tag) {
case md5: case md5:
@ -236,7 +298,7 @@ int haggis_validate_cksum(haggis_file *file) {
return 0; return 0;
} }
int haggis_file_init(char *path, haggis_file *hf) { int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a) {
FILE *f; FILE *f;
long len; long len;
@ -260,6 +322,7 @@ int haggis_file_init(char *path, haggis_file *hf) {
return 1; return 1;
} }
fclose(f); fclose(f);
haggis_init_cksum(hf, a);
return 0; return 0;
} }
@ -455,7 +518,7 @@ void haggis_node_deinit(haggis_node *node) {
free(node); free(node);
} }
haggis_node* haggis_create_node(char *file, haggis_hardlink_list *list) { haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_list *list) {
struct stat *st = NULL; struct stat *st = NULL;
u16 mode; u16 mode;
char *target; char *target;
@ -500,7 +563,7 @@ haggis_node* haggis_create_node(char *file, haggis_hardlink_list *list) {
return node; return node;
} }
} }
res = haggis_file_init(file, &node->filetype.f_type.file); res = haggis_file_init(file, &node->filetype.f_type.file, a);
if (res != 0) { if (res != 0) {
haggis_node_deinit(node); haggis_node_deinit(node);
return NULL; return NULL;

View file

@ -34,6 +34,8 @@ CFLAGS += -I../include
LIBS += ../libhaggis.a LIBS += ../libhaggis.a
LIBS += -lmd LIBS += -lmd
tests += store_header
tests += check_header
tests += store_device tests += store_device
tests += load_device tests += load_device
tests += store_md5 tests += store_md5
@ -42,13 +44,15 @@ tests += store_sha1
tests += load_sha1 tests += load_sha1
tests += store_sha256 tests += store_sha256
tests += load_sha256 tests += load_sha256
tests += init_file_md5
tests += init_file_sha1
total != echo $(tests) | wc -w | awk '{ print $$1 }' total != echo $(tests) | wc -w | awk '{ print $$1 }'
.PHONY: test .PHONY: test
test: $(tests) test: $(tests)
@printf "\n\tRunning %i tests\n\n" $(total) @echo -e "\n\t\e[0;33mRunning $(total) tests\e[0m\n"
@idx=1 ; success=0 ; fail=0; for t in $(tests) ; \ @idx=1 ; success=0 ; fail=0; for t in $(tests) ; \
do printf "[%i/$(total)] \%-25s" $${idx} $${t} ; \ do printf "[%02i/$(total)] \%-25s" $${idx} $${t} ; \
idx=$$(expr $${idx} + 1) ; \ idx=$$(expr $${idx} + 1) ; \
./$${t} ; \ ./$${t} ; \
if [ $$? -eq 0 ] ; \ if [ $$? -eq 0 ] ; \

13
test/check_header.c Normal file
View file

@ -0,0 +1,13 @@
#include "haggis_private.h"
#include <stdio.h>
int main() {
FILE *f;
int ret;
f = fopen("output/header", "r");
ret = haggis_check_header(f);
fclose(f);
return ret;
}

View file

@ -50,7 +50,7 @@ int validate_md5(haggis_file *file);
int validate_sha1(haggis_file *file); int validate_sha1(haggis_file *file);
int validate_sha256(haggis_file *file); int validate_sha256(haggis_file *file);
int haggis_validate_cksum(haggis_file *file); int haggis_validate_cksum(haggis_file *file);
int haggis_file_init(char *path, haggis_file *hf); int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a);
int haggis_store_file(FILE *stream, haggis_file *file); int haggis_store_file(FILE *stream, haggis_file *file);
int haggis_load_file(FILE *stream, haggis_file *f); int haggis_load_file(FILE *stream, haggis_file *f);
void haggis_filename_init(char *target, haggis_filename *fname); void haggis_filename_init(char *target, haggis_filename *fname);

BIN
test/init_file Executable file

Binary file not shown.

14
test/init_file_md5.c Normal file
View file

@ -0,0 +1,14 @@
#include "haggis_private.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
int main() {
haggis_file hf;
char *f = "check_header.c";
const u8 buf[16] = { 127, 161, 170, 134, 33, 218, 100, 29, 250, 255, 15, 125, 109, 5, 216, 9 };
assert(haggis_file_init(f, &hf, md5) == 0);
assert(hf.cksum.tag == md5);
assert(memcmp(buf, hf.cksum.sum.md5, 16) == 0);
}

15
test/init_file_sha1.c Normal file
View file

@ -0,0 +1,15 @@
#include "haggis_private.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
int main() {
haggis_file hf;
char *f = "check_header.c";
const u8 buf[20] = { 175, 45, 7, 226, 140, 184, 187, 69, 59, 208, 246, 108, 187, 249, 63, 111, 56, 65, 1, 58 };
assert(haggis_file_init(f, &hf, sha1) == 0);
assert(hf.cksum.tag == sha1);
assert(memcmp(buf, hf.cksum.sum.sha1, 20) == 0);
}

13
test/store_header.c Normal file
View file

@ -0,0 +1,13 @@
#include "haggis_private.h"
#include <stdio.h>
int main() {
FILE *f;
int ret;
f = fopen("output/header", "w");
ret = haggis_store_header(f);
fclose(f);
return ret;
}