| 1 | #include <check.h> |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include "cfg.h" |
| 6 | #include "log.h" |
| 7 | #include "mdata.h" |
| 8 | |
| 9 | Suite * mdata_suite(void); |
| 10 | void setup_checked(void); |
| 11 | void teardown_checked(void); |
| 12 | |
| 13 | mdata_t md; |
| 14 | |
| 15 | START_TEST(test_mdata_md) |
| 16 | { |
| 17 | ck_assert_ptr_eq(mdata_get_filename(md), NULL); |
| 18 | ck_assert_ptr_eq(mdata_get_name(md), NULL); |
| 19 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 20 | ck_assert_ptr_eq(mdata_get_album(md), NULL); |
| 21 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 22 | ck_assert_ptr_eq(mdata_get_songinfo(md), NULL); |
| 23 | ck_assert_int_lt(mdata_get_length(md), 0); |
| 24 | } |
| 25 | END_TEST |
| 26 | |
| 27 | START_TEST(test_mdata_parse_file) |
| 28 | { |
| 29 | ck_assert_int_ne(mdata_parse_file(md, SRCDIR "/nonexistent"), 0); |
| 30 | ck_assert_ptr_eq(mdata_get_filename(md), NULL); |
| 31 | ck_assert_ptr_eq(mdata_get_name(md), NULL); |
| 32 | |
| 33 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test01-artist+album+title.ogg"), 0); |
| 34 | ck_assert_str_eq(mdata_get_filename(md), SRCDIR "/test01-artist+album+title.ogg"); |
| 35 | ck_assert_str_eq(mdata_get_name(md), "test01-artist+album+title"); |
| 36 | ck_assert_str_eq(mdata_get_artist(md), "test artist"); |
| 37 | ck_assert_str_eq(mdata_get_album(md), "test album"); |
| 38 | ck_assert_str_eq(mdata_get_title(md), "test title"); |
| 39 | ck_assert_str_eq(mdata_get_songinfo(md), "test artist - test title - test album"); |
| 40 | ck_assert_int_eq(mdata_get_length(md), 1); |
| 41 | |
| 42 | mdata_set_normalize_strings(md, 1); |
| 43 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test02-whitespace.ogg"), 0); |
| 44 | ck_assert_str_eq(mdata_get_artist(md), "test artist"); |
| 45 | ck_assert_str_eq(mdata_get_album(md), "test album"); |
| 46 | ck_assert_str_eq(mdata_get_title(md), "test title"); |
| 47 | mdata_set_normalize_strings(md, 0); |
| 48 | ck_assert_int_eq(mdata_refresh(md), 0); |
| 49 | ck_assert_str_eq(mdata_get_artist(md), " test artist "); |
| 50 | ck_assert_str_eq(mdata_get_album(md), " test album "); |
| 51 | ck_assert_str_eq(mdata_get_title(md), " test title "); |
| 52 | |
| 53 | mdata_set_normalize_strings(md, 1); |
| 54 | |
| 55 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test11-artist+album.ogg"), 0); |
| 56 | ck_assert_str_eq(mdata_get_artist(md), "test artist"); |
| 57 | ck_assert_str_eq(mdata_get_album(md), "test album"); |
| 58 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 59 | ck_assert_str_eq(mdata_get_songinfo(md), "test artist - test album"); |
| 60 | |
| 61 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test13-album+title.ogg"), 0); |
| 62 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 63 | ck_assert_str_eq(mdata_get_album(md), "test album"); |
| 64 | ck_assert_str_eq(mdata_get_title(md), "test title"); |
| 65 | ck_assert_str_eq(mdata_get_songinfo(md), "test title - test album"); |
| 66 | |
| 67 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test12-artist.ogg"), 0); |
| 68 | ck_assert_str_eq(mdata_get_artist(md), "test artist"); |
| 69 | ck_assert_ptr_eq(mdata_get_album(md), NULL); |
| 70 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 71 | ck_assert_str_eq(mdata_get_songinfo(md), "test artist"); |
| 72 | |
| 73 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test14-album.ogg"), 0); |
| 74 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 75 | ck_assert_str_eq(mdata_get_album(md), "test album"); |
| 76 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 77 | ck_assert_str_eq(mdata_get_songinfo(md), "test album"); |
| 78 | |
| 79 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test15-title.ogg"), 0); |
| 80 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 81 | ck_assert_ptr_eq(mdata_get_album(md), NULL); |
| 82 | ck_assert_str_eq(mdata_get_title(md), "test title"); |
| 83 | ck_assert_str_eq(mdata_get_songinfo(md), "test title"); |
| 84 | |
| 85 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test16-nometa.ogg"), 0); |
| 86 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 87 | ck_assert_ptr_eq(mdata_get_album(md), NULL); |
| 88 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 89 | ck_assert_ptr_eq(mdata_get_songinfo(md), NULL); |
| 90 | |
| 91 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/.test17ogg"), 0); |
| 92 | ck_assert_str_eq(mdata_get_name(md), "[unknown]"); |
| 93 | |
| 94 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test18-emptymeta.ogg"), 0); |
| 95 | ck_assert_ptr_eq(mdata_get_artist(md), NULL); |
| 96 | ck_assert_ptr_eq(mdata_get_album(md), NULL); |
| 97 | ck_assert_ptr_eq(mdata_get_title(md), NULL); |
| 98 | ck_assert_ptr_eq(mdata_get_songinfo(md), NULL); |
| 99 | |
| 100 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test19-onlywhitespace.ogg"), 0); |
| 101 | ck_assert_str_eq(mdata_get_artist(md), ""); |
| 102 | ck_assert_str_eq(mdata_get_album(md), ""); |
| 103 | ck_assert_str_eq(mdata_get_title(md), ""); |
| 104 | ck_assert_ptr_eq(mdata_get_songinfo(md), NULL); |
| 105 | } |
| 106 | END_TEST |
| 107 | |
| 108 | START_TEST(test_mdata_run_program) |
| 109 | { |
| 110 | ck_assert_int_ne(mdata_run_program(md, SRCDIR "/nonexistent"), 0); |
| 111 | ck_assert_ptr_eq(mdata_get_filename(md), NULL); |
| 112 | |
| 113 | ck_assert_int_ne(mdata_run_program(md, SRCDIR "/test01-artist+album+title.ogg"), 0); |
| 114 | ck_assert_ptr_eq(mdata_get_filename(md), NULL); |
| 115 | |
| 116 | mdata_set_normalize_strings(md, 1); |
| 117 | ck_assert_int_eq(mdata_run_program(md, SRCDIR "/test-meta01.sh"), 0); |
| 118 | ck_assert_str_eq(mdata_get_filename(md), SRCDIR "/test-meta01.sh"); |
| 119 | ck_assert_str_eq(mdata_get_name(md), "[unknown]"); |
| 120 | ck_assert_str_eq(mdata_get_artist(md), "artist"); |
| 121 | ck_assert_str_eq(mdata_get_album(md), "album"); |
| 122 | ck_assert_str_eq(mdata_get_title(md), "title"); |
| 123 | ck_assert_str_eq(mdata_get_songinfo(md), "songinfo"); |
| 124 | mdata_set_normalize_strings(md, 0); |
| 125 | ck_assert_int_eq(mdata_refresh(md), 0); |
| 126 | ck_assert_str_eq(mdata_get_artist(md), " artist "); |
| 127 | ck_assert_str_eq(mdata_get_album(md), " album "); |
| 128 | ck_assert_str_eq(mdata_get_title(md), " title "); |
| 129 | ck_assert_str_eq(mdata_get_songinfo(md), " songinfo "); |
| 130 | |
| 131 | mdata_set_normalize_strings(md, 1); |
| 132 | |
| 133 | ck_assert_int_ne(mdata_run_program(md, SRCDIR "/test-meta02-error.sh"), 0); |
| 134 | ck_assert_str_eq(mdata_get_filename(md), SRCDIR "/test-meta01.sh"); |
| 135 | |
| 136 | ck_assert_int_eq(mdata_run_program(md, SRCDIR "/test-meta03-huge.sh"), 0); |
| 137 | |
| 138 | ck_assert_int_ne(mdata_run_program(md, SRCDIR "/test-meta04-kill.sh"), 0); |
| 139 | |
| 140 | ck_assert_int_eq(mdata_run_program(md, SRCDIR "/test-meta05-empty.sh"), 0); |
| 141 | } |
| 142 | END_TEST |
| 143 | |
| 144 | START_TEST(test_mdata_strformat) |
| 145 | { |
| 146 | char buf[BUFSIZ]; |
| 147 | int ret; |
| 148 | |
| 149 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test01-artist+album+title.ogg"), 0); |
| 150 | ret = mdata_strformat(md, buf, sizeof(buf), "@a@/@b@/@t@/@T@/@s@"); |
| 151 | ck_assert_int_eq(ret, (int)strlen(buf)); |
| 152 | ck_assert_str_eq(buf, |
| 153 | "test artist/test album/test title" |
| 154 | "/" SRCDIR "/test01-artist+album+title.ogg" |
| 155 | "/test artist - test title - test album"); |
| 156 | |
| 157 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test12-artist.ogg"), 0); |
| 158 | ret = mdata_strformat(md, buf, sizeof(buf), "@a@/@b@/@t@/@T@/@s@"); |
| 159 | ck_assert_int_eq(ret, (int)strlen(buf)); |
| 160 | ck_assert_str_eq(buf, |
| 161 | "test artist//" |
| 162 | "/" SRCDIR "/test12-artist.ogg" |
| 163 | "/test artist"); |
| 164 | |
| 165 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test15-title.ogg"), 0); |
| 166 | ret = mdata_strformat(md, buf, sizeof(buf), "@a@/@b@/@t@/@T@/@s@"); |
| 167 | ck_assert_int_eq(ret, (int)strlen(buf)); |
| 168 | ck_assert_str_eq(buf, |
| 169 | "//test title" |
| 170 | "/" SRCDIR "/test15-title.ogg" |
| 171 | "/test title"); |
| 172 | |
| 173 | ck_assert_int_eq(mdata_parse_file(md, SRCDIR "/test16-nometa.ogg"), 0); |
| 174 | ret = mdata_strformat(md, buf, sizeof(buf), "@a@/@b@/@t@/@T@/@s@"); |
| 175 | ck_assert_int_eq(ret, (int)strlen(buf)); |
| 176 | ck_assert_str_eq(buf, |
| 177 | "//" |
| 178 | "/" SRCDIR "/test16-nometa.ogg" |
| 179 | "/"); |
| 180 | |
| 181 | ck_assert_int_lt(mdata_strformat(md, buf, sizeof(buf), NULL), 0); |
| 182 | } |
| 183 | END_TEST |
| 184 | |
| 185 | Suite * |
| 186 | mdata_suite(void) |
| 187 | { |
| 188 | Suite *s; |
| 189 | TCase *tc_mdata; |
| 190 | |
| 191 | s = suite_create("MData"); |
| 192 | |
| 193 | tc_mdata = tcase_create("MData"); |
| 194 | tcase_add_checked_fixture(tc_mdata, setup_checked, teardown_checked); |
| 195 | tcase_add_test(tc_mdata, test_mdata_md); |
| 196 | tcase_add_test(tc_mdata, test_mdata_parse_file); |
| 197 | tcase_add_test(tc_mdata, test_mdata_run_program); |
| 198 | tcase_add_test(tc_mdata, test_mdata_strformat); |
| 199 | suite_add_tcase(s, tc_mdata); |
| 200 | |
| 201 | return (s); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | setup_checked(void) |
| 206 | { |
| 207 | if (0 < cfg_init() || |
| 208 | 0 < cfg_set_program_name("check_mdata", NULL) || |
| 209 | 0 < log_init(cfg_get_program_name())) |
| 210 | ck_abort_msg("setup_checked failed"); |
| 211 | |
| 212 | md = mdata_create(); |
| 213 | ck_assert_ptr_ne(md, NULL); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | teardown_checked(void) |
| 218 | { |
| 219 | mdata_destroy(&md); |
| 220 | mdata_destroy(&md); |
| 221 | ck_assert_ptr_eq(md, NULL); |
| 222 | |
| 223 | log_exit(); |
| 224 | cfg_exit(); |
| 225 | } |
| 226 | |
| 227 | int |
| 228 | main(void) |
| 229 | { |
| 230 | int num_failed; |
| 231 | Suite *s; |
| 232 | SRunner *sr; |
| 233 | |
| 234 | s = mdata_suite(); |
| 235 | sr = srunner_create(s); |
| 236 | |
| 237 | srunner_run_all(sr, CK_NORMAL); |
| 238 | num_failed = srunner_ntests_failed(sr); |
| 239 | srunner_free(sr); |
| 240 | |
| 241 | if (num_failed) |
| 242 | return (1); |
| 243 | return (0); |
| 244 | } |