Added tests for loading and storing integers, fixed load_file_md5 test

so that it now passes. Required fixing integer storage.
This commit is contained in:
Nathan Fisher 2023-08-09 23:56:59 -04:00
parent 08bf2a4ccc
commit 2acf8844dd
17 changed files with 133 additions and 58 deletions

View file

@ -79,4 +79,4 @@ clean:
rm -rf *.a *.so src/*.o
$(MAKE) -C test clean
.PHONY: all clean install
.PHONY: all clean install test

View file

@ -39,9 +39,9 @@
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);
int store_u64(FILE *stream, u64 num);
int load_u32(FILE *stream, u32 *num);
int store_u32(FILE *stream, u32 *num);
int load_u64(FILE *stream, u64 *num);
int store_u64(FILE *stream, u64 *num);
#endif

View file

@ -46,84 +46,84 @@ int store_u16(FILE *stream, u16 *num) {
return fwrite(&num->bytes, 1, 2, stream);
}
int load_u32(FILE *stream, u32 num) {
return fread(num.bytes, 1, 4, stream);
int load_u32(FILE *stream, u32 *num) {
return fread(&num->bytes, 1, 4, stream);
}
int store_u32(FILE *stream, u32 num) {
return fwrite(num.bytes, 1, 4, stream);
int store_u32(FILE *stream, u32 *num) {
return fwrite(&num->bytes, 1, 4, stream);
}
int load_u64(FILE *stream, u64 num) {
return fread(num.bytes, 1, 8, stream);
int load_u64(FILE *stream, u64 *num) {
return fread(&num->bytes, 1, 8, stream);
}
int store_u64(FILE *stream, u64 num) {
return fwrite(num.bytes, 1, 8, stream);
int store_u64(FILE *stream, u64 *num) {
return fwrite(&num->bytes, 1, 8, stream);
}
#else
int load_u16(FILE *stream, u16 num) {
int load_u16(FILE *stream, u16 *num) {
u8 buf[2];
int res = fread(buf, 1, 2, stream);
if (res != 2)
return res;
num.bytes[0] = buf[1];
num.bytes[1] = buf[0];
num->bytes[0] = buf[1];
num->bytes[1] = buf[0];
return res;
}
int store_u16(FILE * stream, u16 num) {
int store_u16(FILE * stream, u16 *num) {
u8 buf[2];
buf[0] = num.bytes[1];
buf[1] = num.bytes[0];
buf[0] = num->bytes[1];
buf[1] = num->bytes[0];
return fwrite(buf, 1, 2, stream);
}
int load_u32(FILE *stream, u32 num) {
int load_u32(FILE *stream, u32 *num) {
u8 buf[4];
int res = fread(buf, 1, 4, stream);
if (res != 4)
return res;
num.bytes[0] = buf[3];
num.bytes[1] = buf[2];
num.bytes[2] = buf[1];
num.bytes[3] = buf[0];
num->bytes[0] = buf[3];
num->bytes[1] = buf[2];
num->bytes[2] = buf[1];
num->bytes[3] = buf[0];
}
int store_u32(FILE *stream, u32 num) {
int store_u32(FILE *stream, u32 *num) {
u8 buf[4];
buf[0] = num.bytes[3];
buf[1] = num.bytes[2];
buf[2] = num.bytes[1];
buf[3] = num.bytes[0];
buf[0] = num->bytes[3];
buf[1] = num->bytes[2];
buf[2] = num->bytes[1];
buf[3] = num->bytes[0];
return fwrite(buf, 1, 2, stream);
}
int load_u64(FILE *stream, u64 num) {
int load_u64(FILE *stream, u64 *num) {
u8 buf[8];
int res = fread(buf, 1, 8, stream);
if (res != 8)
return res;
num.bytes[0] = buf[7];
num.bytes[1] = buf[6];
num.bytes[2] = buf[5];
num.bytes[3] = buf[4];
num.bytes[4] = buf[3];
num.bytes[5] = buf[2];
num.bytes[6] = buf[1];
num.bytes[7] = buf[0];
num->bytes[0] = buf[7];
num->bytes[1] = buf[6];
num->bytes[2] = buf[5];
num->bytes[3] = buf[4];
num->bytes[4] = buf[3];
num->bytes[5] = buf[2];
num->bytes[6] = buf[1];
num->bytes[7] = buf[0];
}
int store_u64(FILE *stream, u64 num) {
int store_u64(FILE *stream, u64 *num) {
u8 buf[8];
buf[0] = num.bytes[7];
buf[1] = num.bytes[6];
buf[2] = num.bytes[5];
buf[3] = num.bytes[4];
buf[4] = num.bytes[3];
buf[5] = num.butes[2];
buf[6] = num.bytes[1];
buf[7] = num.bytes[0];
buf[0] = num->bytes[7];
buf[1] = num->bytes[6];
buf[2] = num->bytes[5];
buf[3] = num->bytes[4];
buf[4] = num->bytes[3];
buf[5] = num->butes[2];
buf[6] = num->bytes[1];
buf[7] = num->bytes[0];
return fwrite(buf, 1, 2, stream);
}
#endif

View file

@ -331,7 +331,7 @@ void haggis_file_deinit(haggis_file *f) {
}
int haggis_store_file(FILE *stream, haggis_file *file) {
if (store_u64(stream, file->len) != 8)
if (store_u64(stream, &file->len) != 8)
return 1;
if (haggis_store_cksum(stream, &file->cksum) != 0)
return 1;
@ -342,7 +342,7 @@ int haggis_store_file(FILE *stream, haggis_file *file) {
}
int haggis_load_file(FILE *stream, haggis_file *f) {
if (load_u64(stream, f->len) != 8)
if (load_u64(stream, &f->len) != 8)
return 1;
if (haggis_load_cksum(stream, &f->cksum) != 0)
return 1;
@ -636,13 +636,13 @@ int haggis_load_node(FILE *stream, haggis_node *node) {
res = haggis_load_filename(stream, &node->name);
if (res)
return res;
res = load_u32(stream, node->uid);
res = load_u32(stream, &node->uid);
if (res != 4)
return 2;
res = load_u32(stream, node->gid);
res = load_u32(stream, &node->gid);
if (res != 4)
return 2;
res = load_u64(stream, node->mtime);
res = load_u64(stream, &node->mtime);
if (res != 8)
return 2;
res = load_u16(stream, &mode);
@ -662,9 +662,9 @@ int haggis_store_node(FILE *stream, haggis_node *node) {
if (haggis_store_filename(stream, &node->name) != (size_t)(node->name.len.val) + 2)
return 2;
if (store_u32(stream, node->uid) != 4) return 2;
if (store_u32(stream, node->gid) != 4) return 2;
if (store_u64(stream, node->mtime) != 8) return 2;
if (store_u32(stream, &node->uid) != 4) return 2;
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;
return haggis_store_filetype(stream, &node->filetype);

View file

@ -36,6 +36,10 @@ LIBS += -lmd
tests += store_u16
tests += load_u16
tests += store_u32
tests += load_u32
tests += store_u64
tests += load_u64
tests += store_header
tests += check_header
tests += store_device
@ -50,7 +54,7 @@ tests += init_file_md5
tests += init_file_sha1
tests += init_file_sha256
tests += store_file_md5
#tests += load_file_md5
tests += load_file_md5
total != echo $(tests) | wc -w | awk '{ print $$1 }'
.PHONY: test

Binary file not shown.

View file

@ -12,5 +12,5 @@ int main() {
fd = fopen(f, "r");
res = haggis_load_file(fd, &hf);
fclose(fd);
return 1;
return res;
}

View file

@ -15,4 +15,5 @@ int main() {
for (i = 0; i < 16; i++) {
assert(cksum.sum.md5[i] == (uint8_t)i);
}
fclose(f);
}

View file

@ -15,5 +15,6 @@ int main() {
for (i = 0; i < 20; i++) {
assert(cksum.sum.sha1[i] == (uint8_t)i);
}
fclose(f);
}

View file

@ -15,6 +15,6 @@ int main() {
for (i = 0; i < 32; i++) {
assert(cksum.sum.sha1[i] == (uint8_t)i);
}
fclose(f);
}

16
test/load_u32.c Normal file
View file

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

16
test/load_u64.c Normal file
View file

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

View file

@ -10,6 +10,7 @@ int main() {
dev.minor.val = 69;
f = fopen("output/device", "w");
ret = haggis_store_device(f, &dev);
fflush(f);
fclose(f);
return ret;
}

View file

@ -11,6 +11,7 @@ int main() {
assert(haggis_file_init(f, &hf, md5) == 0);
fd = fopen("output/store_file_md5", "w");
assert(haggis_store_file(fd, &hf) == 0);
fflush(fd);
fclose(fd);
return 0;
}

View file

@ -7,6 +7,7 @@ int main() {
f = fopen("output/header", "w");
ret = haggis_store_header(f);
fflush(f);
fclose(f);
return ret;
}

17
test/store_u32.c Normal file
View file

@ -0,0 +1,17 @@
#include "bytes.h"
#include "haggis_private.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
int main() {
u32 n;
FILE *fd;
n.val = 123456;
fd = fopen("output/u32", "w");
assert(store_u32(fd, &n) == 4);
fflush(fd);
fclose(fd);
}

17
test/store_u64.c Normal file
View file

@ -0,0 +1,17 @@
#include "bytes.h"
#include "haggis_private.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
int main() {
u64 n;
FILE *fd;
n.val = 1234567890;
fd = fopen("output/u64", "w");
assert(store_u64(fd, &n) == 8);
fflush(fd);
fclose(fd);
}