diff --git a/Makefile b/Makefile index 730a62e..6b35824 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,9 @@ CFILES = $(wildcard src/*.c) OBJFILES = $(CFILES:.c=.o) -OUT = proxy +TESTFILES = $(wildcard tests/*.c) +TESTOUT = $(TESTFILES:.c=) +OUT = yaip CFLAGS = -Wall LDLIBS = -lsqlite3 CC = gcc @@ -22,7 +24,14 @@ $(OUT): $(OBJFILES) %.o: %.c $(CC) $(CFLAGS) -c -o $@ $^ +tests/%.test: tests/%.test.c tests/munit/munit.c + $(CC) $? -o $@ + + +test-%: tests/%.test + $< + .PHONY: clean clean: - rm -f $(OBJFILES) $(OUT) + rm -f $(OBJFILES) $(OUT) $(TESTOUT) diff --git a/src/config.c b/src/config.c index 2a46276..a36293e 100644 --- a/src/config.c +++ b/src/config.c @@ -14,18 +14,19 @@ static bool path_exists(const char *path) { */ static char* resolveTilde(const char *path) { static glob_t globbuf; + static char *ret = NULL; int res = glob(path, GLOB_TILDE, NULL, &globbuf); /* no match, or many wildcard matches are bad */ if (res == GLOB_NOMATCH) - return strdup(path); + ret = strdup(path); else if (res != 0) { printf("glob() failed\n"); - return NULL; } else { - return strdup(globbuf.gl_pathv[0]); + ret = strdup(globbuf.gl_pathv[0]); } globfree(&globbuf); + return ret; } /* @@ -36,7 +37,7 @@ Config* configDefaults(){ conf->database = "proxy.sqlite"; conf->port = 8080; conf->localConfig = "proxy.conf"; - conf->userConfig = getDefaultUserConfigLoc(); + //conf->userConfig = getDefaultUserConfigLoc(); return conf; } @@ -51,7 +52,7 @@ char* getConfigDir(){ char* getDefaultUserConfigLoc(){ char *configDir = getConfigDir(); - char *configFile[strlen(configDir) + 11]; + char configFile[strlen(configDir) + 11]; memset(configFile, '\0', strlen(configDir) + 11); @@ -59,6 +60,6 @@ char* getDefaultUserConfigLoc(){ strcat( configFile, configDir ); - return configFile; + return strdup(configFile); } diff --git a/tests/all.test.c b/tests/all.test.c new file mode 100644 index 0000000..d413b3b --- /dev/null +++ b/tests/all.test.c @@ -0,0 +1,28 @@ +// This stops the main definintion in the sub suites +#define MAINTEST + +#include "munit/munit.h" +#include "config.test.c" +#include +#include + + + + +int main (int argc, char* argv[]) { + + MunitSuite all_suites[] = { + config_test_suite, + { NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE } + }; + + MunitSuite all_test_suite = { + "", /* name */ + NULL, /* tests */ + all_suites, /* suites */ + 1, /* iterations */ + MUNIT_SUITE_OPTION_NONE /* options */ + }; + + return munit_suite_main(&all_test_suite, NULL, argc, argv); +} diff --git a/tests/config.test.c b/tests/config.test.c new file mode 100644 index 0000000..1cebe4d --- /dev/null +++ b/tests/config.test.c @@ -0,0 +1,113 @@ +#include "munit/munit.h" +#include "../src/config.c" +#include //This has getcwd +#include //This has getenv + +/* +* Ensures that path_exists returns true on current working dir (should to exist) +*/ +MunitResult path_that_exists(const MunitParameter params[], + void* user_data_or_fixture){ + char cwd[500] = {'\0'}; + getcwd(cwd,500); + munit_assert_true( path_exists( cwd ) ); + return MUNIT_OK; +} + +MunitResult path_that_doesnt_exist(const MunitParameter params[], + void* user_data_or_fixture){ + char cwd[500] = {'\0'}; + getcwd(cwd,500); + strcat( cwd, "thisshoudldnotexist" ); + munit_assert_false( path_exists( cwd ) ); + return MUNIT_OK; +} + +MunitResult tilde_resolves_correctly(const MunitParameter params[], + void* user_data_or_fixture){ + munit_assert_string_equal( getenv("HOME"), resolveTilde("~") ); + return MUNIT_OK; +} + + +MunitResult check_config_dir_with_xdg(const MunitParameter params[], + void* user_data_or_fixture){ + char directory[] = "/testing/xdg/directory"; + setenv( "XDG_CONFIG_HOME",directory, 1 ); + munit_assert_string_equal( directory, getConfigDir() ); + return MUNIT_OK; +} + +MunitResult check_config_dir_without_xdg(const MunitParameter params[], + void* user_data_or_fixture){ + unsetenv( "XDG_CONFIG_HOME" ); + char dir[500] = {'\0'}; + strcpy( dir, getenv("HOME") ); + strcat( dir, "/.config" ); + munit_assert_string_equal( dir, getConfigDir() ); + return MUNIT_OK; +} + + +MunitTest tests[] = { + { + "/path_exists/yes", /* name */ + path_that_exists, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + { + "/path_exists/no", /* name */ + path_that_doesnt_exist, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + { + "/TildeResolves", /* name */ + tilde_resolves_correctly, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + { + "/ConfigDir/WithXDG", /* name */ + check_config_dir_with_xdg, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + { + "/ConfigDir/WithoutXDG", /* name */ + check_config_dir_without_xdg, /* test */ + NULL, /* setup */ + NULL, /* tear_down */ + MUNIT_TEST_OPTION_NONE, /* options */ + NULL /* parameters */ + }, + /* Mark the end of the array with an entry where the test + * function is NULL */ + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +MunitSuite config_test_suite = { + "/config", /* name */ + tests, /* tests */ + NULL, /* suites */ + 1, /* iterations */ + MUNIT_SUITE_OPTION_NONE /* options */ +}; + +#ifndef MAINTEST +#define MAINTEST + +int main (int argc, char* argv[]) { + return munit_suite_main(&config_test_suite, NULL, argc, argv); +} + +#endif /* ifndef MAINTEST */