YAIP/tests/config.test.c
Jonathan Hodgson bb62ed3b1f Starts work on https as well as some moving about
I now start the listener in the main.c file rather than proxy given that
I didn't feel proxy was the right place if a normal (non-proxied)
request came in. webserver.{c,h} and proxy.{c,h} had some changes
relating to this.

The config changed slightly - we now create a folder in ~/.config/
called yaip. This is where certificates and so on will be stored along
with the user configuration

I created a helper function to get files inside this directory (it
changes based on xdg_config_home) and updated relevant tests.

In ssl.{c,h} I have started work. If they don't exist, the tool now
creates and stores a key and certificate for the CA that this tool will
need to pretend to be. I still need to write tests for this.
2022-01-20 16:53:35 +00:00

169 lines
4.6 KiB
C

#ifndef CONFIG_TEST
#define CONFIG_TEST
#include "munit/munit.h"
#include "../src/config.h"
#include <unistd.h> //This has getcwd
#include <stdlib.h> //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( "/testing/xdg/directory/yaip", 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/yaip" );
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 conffile[] = "/testing/xdg/directory/yaip/proxy.conf";
char certfile[] = "/testing/xdg/directory/yaip/cert.pem";
char keyfile[] = "/testing/xdg/directory/yaip/key.pem";
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, conffile);
munit_assert_string_equal(conf->certfile, certfile);
munit_assert_string_equal(conf->keyfile, keyfile);
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 */