diff --git a/makefile b/makefile index 9d42ef3..5166f03 100644 --- a/makefile +++ b/makefile @@ -15,15 +15,17 @@ HEADERS = purr.h mmap_file.h OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o mmap_file.o TEST = tests -TOBJS = tests.o formats.o urls.o +TOBJS = tests.o formats.o urls.o mmap_file.o all: $(FINAL) check: $(TEST) ./tests -$(OBJS): $(HEADERS) -$(OBJS): CFLAGS += $(WARN) $(INC) +$(OBJS) $(TOBJS): $(HEADERS) +$(OBJS) $(TOBJS): CFLAGS += $(WARN) +encrypt.o: CFLAGS += $(INC) + purr: $(OBJS) $(LIBS) tests: $(TOBJS) $(LIBS) diff --git a/mmap_file.h b/mmap_file.h index c725d76..16718cf 100644 --- a/mmap_file.h +++ b/mmap_file.h @@ -4,7 +4,7 @@ #include #include -#define RESET_MMAP(file) do{(file).offset = 0; (file).cursor = 0}while(0); +#define RESET_MMAP(file) do{(file).offset = 0; (file).cursor = 0;}while(0); #define ERROR_MMAP(file) ((file).data == MAP_FAILED || (file).data == NULL) #define CLOSE_MMAP(file) do{if((file).data != MAP_FAILED && (file).data != NULL) munmap((file).data, (file).size);}while(0); diff --git a/tests.c b/tests.c index 3750620..ca5e494 100644 --- a/tests.c +++ b/tests.c @@ -1,8 +1,11 @@ #include +#include #include #include +#include #include "purr.h" +#include "mmap_file.h" static int compare_strings(const char *expected, const char *result, const char *function) { @@ -81,5 +84,21 @@ int main() assert(err == 0); } + { + /* mmap_file.c */ + int write_size = 1024 * 1024; + struct mmap_file f = {.size = 2 * write_size, .prot = PROT_MEM, .flags = MAP_MEM}; + f.data = mmap(NULL, f.size, f.prot, f.flags, -1, 0); + assert(!ERROR_MMAP(f)); + uint8_t *data = malloc(write_size); + getrandom(data, write_size, 0); + assert(data); + int written = write_into_mmap(&f, data, write_size); + assert(f.offset == written); + RESET_MMAP(f); + read_from_mmap(&f, write_size); + rv = compare_arrays(data, f.data, write_size, "{write_into,read_from}_mmap") ? 1 : rv; + } + return rv; }