1
0
mirror of https://gitlab.xiph.org/xiph/ezstream.git synced 2024-12-04 14:46:31 -05:00

Move config plausibility check to CFG and test it

This commit is contained in:
Moritz Grimm 2015-06-11 23:50:31 +02:00
parent 744511ffe2
commit 77fe651bff
4 changed files with 72 additions and 25 deletions

View File

@ -169,6 +169,36 @@ cfg_reload(void)
return (0);
}
int
cfg_check(const char **errstrp)
{
if (!cfg_get_server_hostname()) {
if (NULL != errstrp)
*errstrp = "server hostname missing";
return (-1);
}
if (!cfg_get_server_password()) {
if (NULL != errstrp)
*errstrp = "server password missing";
return (-1);
}
if (!cfg_get_media_filename()) {
if (NULL != errstrp)
*errstrp = "media filename missing";
return (-1);
}
if (CFG_STREAM_INVALID == cfg_get_stream_format()) {
if (NULL != errstrp)
*errstrp = "stream format missing or unsupported";
return (-1);
}
return (0);
}
int
cfg_stream_str2fmt(const char *str, enum cfg_stream_format *fmt_p)
{
@ -630,7 +660,7 @@ cfg_get_server_protocol_str(void)
const char *
cfg_get_server_hostname(void)
{
return (cfg.server.hostname);
return (cfg.server.hostname[0] ? cfg.server.hostname : NULL);
}
unsigned int
@ -648,31 +678,31 @@ cfg_get_server_user(void)
const char *
cfg_get_server_password(void)
{
return (cfg.server.password);
return (cfg.server.password[0] ? cfg.server.password : NULL);
}
const char *
cfg_get_server_ca_dir(void)
{
return (cfg.server.ca_dir);
return (cfg.server.ca_dir[0] ? cfg.server.ca_dir : NULL);
}
const char *
cfg_get_server_ca_file(void)
{
return (cfg.server.ca_file);
return (cfg.server.ca_file[0] ? cfg.server.ca_file : NULL);
}
const char *
cfg_get_server_client_cert(void)
{
return (cfg.server.client_cert);
return (cfg.server.client_cert[0] ? cfg.server.client_cert : NULL);
}
const char *
cfg_get_server_client_key(void)
{
return (cfg.server.client_key);
return (cfg.server.client_key[0] ? cfg.server.client_key : NULL);
}
unsigned int
@ -768,7 +798,7 @@ cfg_get_media_type(void)
const char *
cfg_get_media_filename(void)
{
return (cfg.media.filename);
return (cfg.media.filename[0] ? cfg.media.filename : NULL);
}
int

View File

@ -69,6 +69,8 @@ void cfg_save(void);
void cfg_pop(void);
void cfg_clear(void);
int cfg_check(const char **);
int cfg_reload(void);
int cfg_stream_str2fmt(const char *, enum cfg_stream_format *);

View File

@ -1009,6 +1009,7 @@ int
main(int argc, char *argv[])
{
int ret;
const char *errstr;
shout_t *shout;
extern char *optarg;
extern int optind;
@ -1027,26 +1028,10 @@ main(int argc, char *argv[])
return (ez_shutdown(ret));
shout_init();
if (!cfg_get_server_hostname() ||
!cfg_get_server_port()){
log_error("%s: missing server configuration",
cfg_get_program_config_file());
if (0 > cfg_check(&errstr)) {
log_error("%s: %s", cfg_get_program_config_file(), errstr);
return (ez_shutdown(2));
}
if (!cfg_get_server_password()) {
log_error("%s: <sourcepassword> missing",
cfg_get_program_config_file());
return (ez_shutdown(2));
}
if (!cfg_get_media_filename()) {
log_error("%s: <filename> missing",
cfg_get_program_config_file());
return (ez_shutdown(2));
}
if (CFG_STREAM_INVALID == cfg_get_stream_format()) {
log_error("%s: <format> missing or unsupported value",
cfg_get_program_config_file());
}
if (NULL == (shout = stream_setup()))
return (ez_shutdown(1));

View File

@ -119,6 +119,35 @@ START_TEST(test_stash)
}
END_TEST
START_TEST(test_check)
{
const char *errstr = NULL;
ck_assert_int_eq(cfg_check(NULL), -1);
ck_assert_int_eq(cfg_check(&errstr), -1);
ck_assert_str_eq(errstr, "server hostname missing");
ck_assert_int_eq(cfg_set_server_hostname("localhost", NULL), 0);
ck_assert_int_eq(cfg_check(NULL), -1);
ck_assert_int_eq(cfg_check(&errstr), -1);
ck_assert_str_eq(errstr, "server password missing");
ck_assert_int_eq(cfg_set_server_password("secret", NULL), 0);
ck_assert_int_eq(cfg_check(NULL), -1);
ck_assert_int_eq(cfg_check(&errstr), -1);
ck_assert_str_eq(errstr, "media filename missing");
ck_assert_int_eq(cfg_set_media_filename(SRCDIR "/playlist.txt", NULL),
0);
ck_assert_int_eq(cfg_check(NULL), -1);
ck_assert_int_eq(cfg_check(&errstr), -1);
ck_assert_str_eq(errstr, "stream format missing or unsupported");
ck_assert_int_eq(cfg_set_stream_format(CFG_SFMT_VORBIS, NULL), 0);
ck_assert_int_eq(cfg_check(NULL), 0);
}
END_TEST
START_TEST(test_stream_str2fmt)
{
enum cfg_stream_format fmt;
@ -749,6 +778,7 @@ cfg_suite(void)
tc_core = tcase_create("Core");
tcase_add_checked_fixture(tc_core, setup_checked, teardown_checked);
tcase_add_test(tc_core, test_stash);
tcase_add_test(tc_core, test_check);
tcase_add_test(tc_core, test_stream_str2fmt);
tcase_add_test(tc_core, test_stream_fmt2str);
tcase_add_test(tc_core, test_file_check);