From 77fe651bffa6a86611cea0b95218e5e8dc54597e Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Thu, 11 Jun 2015 23:50:31 +0200 Subject: [PATCH] Move config plausibility check to CFG and test it --- src/cfg.c | 44 +++++++++++++++++++++++++++++++++++++------- src/cfg.h | 2 ++ src/ezstream.c | 21 +++------------------ tests/check_cfg.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 25 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index acc302e..c5dca02 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -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 diff --git a/src/cfg.h b/src/cfg.h index 57795a1..69649ba 100644 --- a/src/cfg.h +++ b/src/cfg.h @@ -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 *); diff --git a/src/ezstream.c b/src/ezstream.c index 3a0e4f0..1c5e47a 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -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: missing", - cfg_get_program_config_file()); - return (ez_shutdown(2)); - } - if (!cfg_get_media_filename()) { - log_error("%s: missing", - cfg_get_program_config_file()); - return (ez_shutdown(2)); - } - if (CFG_STREAM_INVALID == cfg_get_stream_format()) { - log_error("%s: missing or unsupported value", - cfg_get_program_config_file()); - } if (NULL == (shout = stream_setup())) return (ez_shutdown(1)); diff --git a/tests/check_cfg.c b/tests/check_cfg.c index d4abc57..3680964 100644 --- a/tests/check_cfg.c +++ b/tests/check_cfg.c @@ -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);