Added several tests and fixed bugs in [load/store]_u16. Must pass the

union as a pointer or the compiler optimizes it away.
This commit is contained in:
Nathan Fisher 2023-08-09 23:02:37 -04:00
parent e159900f2e
commit 08bf2a4ccc
11 changed files with 98 additions and 9 deletions

View file

@ -37,8 +37,8 @@
#include "haggis.h"
int load_u16(FILE *stream, u16 num);
int store_u16(FILE *stream, u16 num);
int load_u16(FILE *stream, u16 *num);
int store_u16(FILE *stream, u16 *num);
int load_u32(FILE *stream, u32 num);
int store_u32(FILE *stream, u32 num);
int load_u64(FILE *stream, u64 num);

View file

@ -38,12 +38,12 @@
#include "haggis.h"
#if __BYTE_ORDER__ == __LITTLE_ENDIAN
int load_u16(FILE *stream, u16 num) {
return fread(num.bytes, 1, 2, stream);
int load_u16(FILE *stream, u16 *num) {
return fread(&num->bytes, 1, 2, stream);
}
int store_u16(FILE *stream, u16 num) {
return fwrite(num.bytes, 1, 2, stream);
int store_u16(FILE *stream, u16 *num) {
return fwrite(&num->bytes, 1, 2, stream);
}
int load_u32(FILE *stream, u32 num) {

View file

@ -351,6 +351,8 @@ int haggis_load_file(FILE *stream, haggis_file *f) {
return -1;
int res = fread(f->data, 1, (size_t)f->len.val, stream);
if (res != (size_t)f->len.val) {
fprintf(stderr, "Error reading file:\n length: %i\n expected: %lu\n",
res, f->len.val);
free(f->data);
return 1;
}
@ -643,7 +645,7 @@ int haggis_load_node(FILE *stream, haggis_node *node) {
res = load_u64(stream, node->mtime);
if (res != 8)
return 2;
res = load_u16(stream, mode);
res = load_u16(stream, &mode);
if (res != 2)
return 2;
tag = haggis_filetype_from_mode(mode);
@ -664,6 +666,6 @@ int haggis_store_node(FILE *stream, haggis_node *node) {
if (store_u32(stream, node->gid) != 4) return 2;
if (store_u64(stream, node->mtime) != 8) return 2;
mode = haggis_derive_mode(node->mode, &node->filetype);
if (store_u16(stream, mode) != 2) return 2;
if (store_u16(stream, &mode) != 2) return 2;
return haggis_store_filetype(stream, &node->filetype);
}

View file

@ -34,6 +34,8 @@ CFLAGS += -I../include
LIBS += ../libhaggis.a
LIBS += -lmd
tests += store_u16
tests += load_u16
tests += store_header
tests += check_header
tests += store_device
@ -46,6 +48,9 @@ tests += store_sha256
tests += load_sha256
tests += init_file_md5
tests += init_file_sha1
tests += init_file_sha256
tests += store_file_md5
#tests += load_file_md5
total != echo $(tests) | wc -w | awk '{ print $$1 }'
.PHONY: test

View file

@ -6,7 +6,8 @@
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 };
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);

17
test/init_file_sha256.c Normal file
View file

@ -0,0 +1,17 @@
#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[32] = { 254, 29, 31, 135, 226, 153, 9, 124, 189, 232, 10, 158,
250, 203, 77, 108, 230, 7, 159, 124, 6, 237, 169, 239, 70, 218, 173, 188,
153, 219, 121, 82 };
assert(haggis_file_init(f, &hf, sha256) == 0);
assert(hf.cksum.tag == sha256);
assert(memcmp(buf, hf.cksum.sum.sha256, 32) == 0);
}

BIN
test/load_file_md5 Executable file

Binary file not shown.

16
test/load_file_md5.c Normal file
View file

@ -0,0 +1,16 @@
#include "haggis_private.h"
#include <assert.h>
#include <md5.h>
#include <stdio.h>
int main() {
haggis_file hf;
char *f = "output/store_file_md5";
FILE *fd;
int res;
fd = fopen(f, "r");
res = haggis_load_file(fd, &hf);
fclose(fd);
return 1;
}

16
test/load_u16.c Normal file
View file

@ -0,0 +1,16 @@
#include "bytes.h"
#include "haggis_private.h"
#include <assert.h>
#include <stdio.h>
int main() {
u16 n;
FILE *fd;
n.val = 0;
fd = fopen("output/u16", "r");
assert(load_u16(fd, &n) == 2);
assert(n.val == 300);
fclose(fd);
}

17
test/store_file_md5.c Normal file
View file

@ -0,0 +1,17 @@
#include "haggis_private.h"
#include <assert.h>
#include <md5.h>
#include <stdio.h>
int main() {
haggis_file hf;
char *f = "load_device.c";
FILE *fd;
assert(haggis_file_init(f, &hf, md5) == 0);
fd = fopen("output/store_file_md5", "w");
assert(haggis_store_file(fd, &hf) == 0);
fclose(fd);
return 0;
}

15
test/store_u16.c Normal file
View file

@ -0,0 +1,15 @@
#include "bytes.h"
#include "haggis_private.h"
#include <assert.h>
#include <stdio.h>
int main() {
u16 n;
FILE *fd;
n.val = 300;
fd = fopen("output/u16", "w");
assert(store_u16(fd, &n) == 2);
fflush(fd);
fclose(fd);
}