#ifndef CONFIG_TEST #define CONFIG_TEST #include "munit/munit.h" #include "../src/config.h" #include //This has getcwd #include //This has getenv /* * Ensures that path_exists returns true on current working dir (should to exist) */ MunitResult pathThatExists(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 pathThatDoesntExist(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 tildeResolvesCorrectly(const MunitParameter params[], void* user_data_or_fixture){ munit_assert_string_equal( getenv("HOME"), resolveTilde("~") ); return MUNIT_OK; } MunitResult checkConfigDirWithXdg(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 checkConfigDirWithoutXdg(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; } MunitResult checkDefaults(const MunitParameter params[], void* user_data_or_fixture){ char directory[] = "/testing/xdg/directory"; char file[] = "/testing/xdg/directory/proxy.conf"; setenv( "XDG_CONFIG_HOME",directory, 1 ); Config *conf = configDefaults(); munit_assert_string_equal(conf->database, "proxy.sqlite"); munit_assert_int(conf->port, ==, 8080); munit_assert_string_equal(conf->localConfig, "proxy.conf"); munit_assert_string_equal(conf->userConfig, file); return MUNIT_OK; } MunitResult checkSetConfig(const MunitParameter params[], void* user_data_or_fixture){ Config *conf = configDefaults(); setConfig(conf, "database", "test.blar"); munit_assert_string_equal(conf->database, "test.blar"); setConfig(conf, "localConfig", "test.blar"); munit_assert_string_equal(conf->localConfig, "test.blar"); setConfig(conf, "userConfig", "test.blar"); munit_assert_string_equal(conf->userConfig, "test.blar"); setConfig(conf, "port", "1234"); munit_assert_int(conf->port, ==, 1234); return MUNIT_OK; } static MunitTest config_tests[] = { { "/path_exists/yes", /* name */ pathThatExists, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/path_exists/no", /* name */ pathThatDoesntExist, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/TildeResolves", /* name */ tildeResolvesCorrectly, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/ConfigDir/WithXDG", /* name */ checkConfigDirWithoutXdg, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/ConfigDir/WithoutXDG", /* name */ checkConfigDirWithoutXdg, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/ConfigDir/Defaults", /* name */ checkDefaults, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/ConfigDir/SetConfigs", /* name */ checkSetConfig, /* 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 */ config_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 */ #endif /* ifndef CONFIG_TEST */