| 1 | #include <sys/types.h> |
| 2 | #include <sys/stat.h> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <check.h> |
| 10 | |
| 11 | #include "cfg.h" |
| 12 | #include "cfgfile_xml.h" |
| 13 | #include "log.h" |
| 14 | #include "stream.h" |
| 15 | |
| 16 | static void _test_cfgfile_rw(const char *cfgfile); |
| 17 | |
| 18 | Suite * cfgfile_xml_suite(void); |
| 19 | void setup_checked(void); |
| 20 | void teardown_checked(void); |
| 21 | |
| 22 | static void |
| 23 | _test_cfgfile_rw(const char *cfgfile) |
| 24 | { |
| 25 | const char *testfile = BUILDDIR "/check_cfgfile_xml.xml"; |
| 26 | FILE *fp; |
| 27 | int fd; |
| 28 | int ret; |
| 29 | |
| 30 | ck_assert_int_eq(cfg_set_program_config_file(cfgfile, NULL), 0); |
| 31 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 32 | |
| 33 | fd = open(testfile, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR); |
| 34 | assert(0 <= fd); |
| 35 | fp = fdopen(fd, "w"); |
| 36 | if (NULL == fp) { |
| 37 | unlink(testfile); |
| 38 | close(fd); |
| 39 | } |
| 40 | ck_assert_ptr_ne(fp, NULL); |
| 41 | cfgfile_xml_print(fp); |
| 42 | ck_assert_int_eq(fclose(fp), 0); |
| 43 | |
| 44 | ck_assert_int_eq(cfg_set_program_config_file(testfile, NULL), 0); |
| 45 | ret = cfg_file_reload(); |
| 46 | ck_assert_int_eq(unlink(testfile), 0); |
| 47 | ck_assert_int_eq(ret, 0); |
| 48 | } |
| 49 | |
| 50 | START_TEST(test_reload) |
| 51 | { |
| 52 | stream_t test_stream; |
| 53 | |
| 54 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-file_template.xml"); |
| 55 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-file_template.xml", |
| 56 | NULL), 0); |
| 57 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 58 | test_stream = stream_create(CFG_DEFAULT); |
| 59 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 60 | stream_destroy(&test_stream); |
| 61 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-full.xml"); |
| 62 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-full.xml", |
| 63 | NULL), 0); |
| 64 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 65 | test_stream = stream_create(CFG_DEFAULT); |
| 66 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 67 | stream_destroy(&test_stream); |
| 68 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-metadata.xml"); |
| 69 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-metadata.xml", |
| 70 | NULL), 0); |
| 71 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 72 | test_stream = stream_create(CFG_DEFAULT); |
| 73 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 74 | stream_destroy(&test_stream); |
| 75 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-minimal.xml"); |
| 76 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-minimal.xml", |
| 77 | NULL), 0); |
| 78 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 79 | test_stream = stream_create(CFG_DEFAULT); |
| 80 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 81 | stream_destroy(&test_stream); |
| 82 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-stdin.xml"); |
| 83 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-stdin.xml", |
| 84 | NULL), 0); |
| 85 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 86 | test_stream = stream_create(CFG_DEFAULT); |
| 87 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 88 | stream_destroy(&test_stream); |
| 89 | _test_cfgfile_rw(EXAMPLESDIR "/ezstream-video.xml"); |
| 90 | ck_assert_int_eq(cfg_set_program_config_file(EXAMPLESDIR "/ezstream-video.xml", |
| 91 | NULL), 0); |
| 92 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 93 | test_stream = stream_create(CFG_DEFAULT); |
| 94 | ck_assert_int_eq(stream_configure(test_stream), 0); |
| 95 | stream_destroy(&test_stream); |
| 96 | ck_assert_int_eq(cfg_file_reload(), 0); |
| 97 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad.xml", |
| 98 | NULL), 0); |
| 99 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 100 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad2.xml", |
| 101 | NULL), 0); |
| 102 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 103 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad3.xml", |
| 104 | NULL), 0); |
| 105 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 106 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad4.xml", |
| 107 | NULL), 0); |
| 108 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 109 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad5.xml", |
| 110 | NULL), 0); |
| 111 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 112 | ck_assert_int_eq(cfg_set_program_config_file(SRCDIR "/config-bad6.xml", |
| 113 | NULL), 0); |
| 114 | ck_assert_int_eq(cfg_file_reload(), -1); |
| 115 | } |
| 116 | END_TEST |
| 117 | |
| 118 | Suite * |
| 119 | cfgfile_xml_suite(void) |
| 120 | { |
| 121 | Suite *s; |
| 122 | TCase *tc_xmlcfg; |
| 123 | |
| 124 | s = suite_create("Config"); |
| 125 | |
| 126 | tc_xmlcfg = tcase_create("XmlConfigFile"); |
| 127 | tcase_add_checked_fixture(tc_xmlcfg, setup_checked, teardown_checked); |
| 128 | tcase_add_test(tc_xmlcfg, test_reload); |
| 129 | suite_add_tcase(s, tc_xmlcfg); |
| 130 | |
| 131 | return (s); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | setup_checked(void) |
| 136 | { |
| 137 | if (0 < cfg_init() || |
| 138 | 0 < cfg_set_program_name("check_cfgfile_xml", NULL) || |
| 139 | 0 < log_init(cfg_get_program_name()) || |
| 140 | 0 < stream_init()) |
| 141 | ck_abort_msg("setup_checked failed"); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | teardown_checked(void) |
| 146 | { |
| 147 | stream_exit(); |
| 148 | log_exit(); |
| 149 | cfg_exit(); |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | main(void) |
| 154 | { |
| 155 | int num_failed; |
| 156 | Suite *s; |
| 157 | SRunner *sr; |
| 158 | |
| 159 | s = cfgfile_xml_suite(); |
| 160 | sr = srunner_create(s); |
| 161 | |
| 162 | srunner_run_all(sr, CK_NORMAL); |
| 163 | num_failed = srunner_ntests_failed(sr); |
| 164 | srunner_free(sr); |
| 165 | |
| 166 | if (num_failed) |
| 167 | return (1); |
| 168 | return (0); |
| 169 | } |