Skip to content

Commit

Permalink
Move tests, fix overflow with I/O due to non null terminated magic st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
ocsmit committed Jul 18, 2023
1 parent 927368a commit 764b3b7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 20 deletions.
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ build: $(OBJECTS)
# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

test: $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@echo
@echo ------------------ TEST ------------------
@./test
@echo
-@$(MAKE) -s clean
test:
make -C ./tests

.PHONY: clean check
.SILENT: clean
Expand Down
6 changes: 3 additions & 3 deletions src/bitarr_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ BitArray* BitArray_open(FILE *fp)
{
uint8_t l, width;
uint32_t n;
char magic_number[strlen(BIT_MAGIC_NUMBER)]; // 3
char magic_number[strlen(BIT_MAGIC_NUMBER)+1];

// Verify it is a correct file
fread(&magic_number, sizeof(char), strlen(BIT_MAGIC_NUMBER), fp);
fread(magic_number, sizeof(char), strlen(BIT_MAGIC_NUMBER), fp);
if (strcmp(magic_number, BIT_MAGIC_NUMBER) != 0) {
fprintf(stderr, "Incorrect file, magic string does not match\n");
exit(FILE_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion src/bitarr_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <string.h>
#include "bitarr.h"

static const char BIT_MAGIC_NUMBER[] = { 'B', 'I', 'T' };
static const char BIT_MAGIC_NUMBER[] = { 'B', 'I', 'T', '\0'};

/**
* @brief Save BitArray to disk
Expand Down
49 changes: 49 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
CC = gcc
CFLAGS = -g -Wall -O0 -std=c11 -Wextra -Wwrite-strings \
-Wno-parentheses -Wpedantic -Warray-bounds -Wconversion -Wstrict-prototypes -Wnewline-eof

# Test for leaks with llvm
# Apple clang does not support sanitize
ifeq ($(CHECKLEAK),1)
CC=/opt/homebrew/opt/llvm/bin/clang
CFLAGS += -fsanitize=address -fsanitize=leak
endif
#LDFLAGS = -lm

SOURCES=$(wildcard ./*.c ../src/*.c)
OBJECTS=$(patsubst %.c, %.o, $(SOURCES)) # list *.c -> *.o
TARGET = bin

all: test

# The first target defined in the makefile is the one
# used when make is invoked with no argument. Given the definitions
# above, this Makefile file will build the one named TARGET and
# assume that it depends on all the named OBJECTS files.
$(TARGET) : $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)


# Phony means not a "real" target, it doesn't build anything
# The phony target "clean" is used to remove all compiled object files.

test: $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@echo
@echo ------------------ TEST ------------------
@./test
@echo
-@$(MAKE) -s clean

.PHONY: clean check
.SILENT: clean
clean:
rm -rf build $(OBJECTS) $(TESTS) $(TARGET) test
find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print` # Remove XCode junk


check:
@echo Files with pontentially dangerous functions.
@egrep '[^_.>a-zA-Z0-9] (str(n?cpy|n?cat|xfrm|n?dup|str|pbrk|tok|_)\
|stpn?cpy|a?sn?printf|byte_)' $(SOURCES) || true
Binary file added tests/data/bitarr_valid.bit
Binary file not shown.
23 changes: 14 additions & 9 deletions src/tests.c → tests/tests.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#include "tests.h"
#include "assert.h"
#include <stdio.h>
#include "bitarr.h"
#include "bitops.h"
#include "bitarr_io.h"
#include "../src/bitarr.h"
#include "../src/bitops.h"
#include "../src/bitarr_io.h"



BEGIN_TESTING
// Data
// -- Data --------------------------------------------------------------------
unsigned int A[10] = { 20, 18, 22, 22, 16, 21, 11, 22, 21, 21 };

/*
* Both of binary representations of b have been flipped since when reading individual bits the most from array A, the least significant bit will be read first from each int.
* Both of binary representations of b have been flipped since when reading
* individual bits the most from array A, the least significant bit will be read
* first from each int.
*
* 394338780 := 1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,1,0,0,
* 177586 := 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,0,1,0,
Expand All @@ -22,11 +25,13 @@ unsigned int B_sig_ordered[64] = {
0,0,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,0,1,1,1,
0,1,0,0,1,1,0,1,1,0,1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

unsigned int correct_W[2] = { 3943389780, 177586} ;

BitArray* bit_arr = BitArray_init(A, (sizeof(A)/sizeof(A[0])), 5);

static char bit_arr_fp[] = "./data/bitarr_test.bit";

// ----------------------------------------------------------------------------


TEST("BitArray calloc")
{
Expand Down Expand Up @@ -103,15 +108,15 @@ TEST("bits read range")

TEST("Write to disk")
{
FILE *fp = fopen("test.bit", "wb");
FILE *fp = fopen(bit_arr_fp, "wb");
BitArray_save(bit_arr, fp);
fclose(fp);
printf("✔ BitArray disk write\n");
}

TEST("Read from disk")
{
FILE *fp = fopen("test.bit", "rb");
FILE *fp = fopen(bit_arr_fp, "rb");
BitArray* bit_arr_read = BitArray_open(fp);
fclose(fp);

Expand Down
File renamed without changes.

0 comments on commit 764b3b7

Please sign in to comment.