| 1 |
#include <sys/file.h> |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include <check.h> |
| 7 |
|
| 8 |
#include "util.h" |
| 9 |
#include "xalloc.h" |
| 10 |
|
| 11 |
Suite * util_suite(void); |
| 12 |
|
| 13 |
START_TEST(test_util_get_progname) |
| 14 |
{ |
| 15 |
|
| 16 |
ck_assert_str_ne(util_get_progname(NULL), ""); |
| 17 |
} |
| 18 |
END_TEST |
| 19 |
|
| 20 |
START_TEST(test_util_write_pid_file) |
| 21 |
{ |
| 22 |
FILE *pidfile_2; |
| 23 |
|
| 24 |
ck_assert_int_eq(util_write_pid_file(NULL), 0); |
| 25 |
ck_assert_int_ne(util_write_pid_file("/nonexistent/path/check_util-write_pid_file.pid"), 0); |
| 26 |
|
| 27 |
pidfile_2 = fopen(BUILDDIR "/check_util-write_pid_file2.pid", "w"); |
| 28 |
ck_assert_ptr_ne(pidfile_2, NULL); |
| 29 |
ck_assert_int_eq(flock(fileno(pidfile_2), LOCK_EX), 0); |
| 30 |
ck_assert_int_ne(util_write_pid_file(BUILDDIR "/check_util-write_pid_file2.pid"), 0); |
| 31 |
ck_assert_int_eq(flock(fileno(pidfile_2), LOCK_UN), 0); |
| 32 |
ck_assert_int_eq(fclose(pidfile_2), 0); |
| 33 |
|
| 34 |
unlink(BUILDDIR "/check_util-write_pid_file.pid"); |
| 35 |
ck_assert_int_eq(util_write_pid_file(BUILDDIR "/check_util-write_pid_file.pid"), 0); |
| 36 |
ck_assert_int_eq(util_write_pid_file(BUILDDIR "/check_util-write_pid_file.pid"), 0); |
| 37 |
ck_assert_int_eq(unlink(BUILDDIR "/check_util-write_pid_file.pid"), 0); |
| 38 |
ck_assert_int_ne(access(BUILDDIR "/check_util-write_pid_file.pid", F_OK), 0); |
| 39 |
} |
| 40 |
END_TEST |
| 41 |
|
| 42 |
START_TEST(test_util_strrcmp) |
| 43 |
{ |
| 44 |
ck_assert_int_ne(util_strrcmp(".m3u", "playlist.m3u"), 0); |
| 45 |
ck_assert_int_ne(util_strrcasecmp(".m3u", "playlist.m3u"), 0); |
| 46 |
|
| 47 |
ck_assert_int_ne(util_strrcmp("playlist.m3u", ".MEU"), 0); |
| 48 |
ck_assert_int_ne(util_strrcasecmp("playlist.m3u", ".MEU"), 0); |
| 49 |
|
| 50 |
ck_assert_int_ne(util_strrcmp("playlist.m3u", ".M3u"), 0); |
| 51 |
ck_assert_int_eq(util_strrcasecmp("playlist.m3u", ".M3u"), 0); |
| 52 |
|
| 53 |
ck_assert_int_eq(util_strrcmp("playlist.m3u", ".m3u"), 0); |
| 54 |
ck_assert_int_eq(util_strrcasecmp("playlist.m3u", ".m3u"), 0); |
| 55 |
} |
| 56 |
END_TEST |
| 57 |
|
| 58 |
START_TEST(test_util_utf8sanity) |
| 59 |
{ |
| 60 |
const char *test_str = "TESTing\t1234 !@#{}\n"; |
| 61 |
char *str; |
| 62 |
|
| 63 |
str = util_char2utf8(test_str); |
| 64 |
ck_assert_str_eq(str, test_str); |
| 65 |
xfree(str); |
| 66 |
|
| 67 |
str = util_utf82char(test_str); |
| 68 |
ck_assert_str_eq(str, test_str); |
| 69 |
xfree(str); |
| 70 |
} |
| 71 |
END_TEST |
| 72 |
|
| 73 |
START_TEST(test_util_expand_words) |
| 74 |
{ |
| 75 |
ck_assert_ptr_eq(util_expand_words("", NULL), NULL); |
| 76 |
} |
| 77 |
END_TEST |
| 78 |
|
| 79 |
START_TEST(test_util_shellquote) |
| 80 |
{ |
| 81 |
char *str; |
| 82 |
|
| 83 |
str = util_shellquote("testing 1 2 3", 0); |
| 84 |
ck_assert_str_eq(str, "'testing 1 2 3'"); |
| 85 |
xfree(str); |
| 86 |
|
| 87 |
str = util_shellquote("testing 1 2 3", 9); |
| 88 |
ck_assert_str_eq(str, "'testing'"); |
| 89 |
xfree(str); |
| 90 |
|
| 91 |
str = util_shellquote("testing'1 2 3", 11); |
| 92 |
ck_assert_str_eq(str, "'testing'"); |
| 93 |
xfree(str); |
| 94 |
|
| 95 |
str = util_shellquote("foo'bar", 0); |
| 96 |
ck_assert_str_eq(str, "'foo'\\''bar'"); |
| 97 |
xfree(str); |
| 98 |
|
| 99 |
str = util_shellquote("foo\\bar", 0); |
| 100 |
ck_assert_str_eq(str, "'foo\\bar'"); |
| 101 |
xfree(str); |
| 102 |
|
| 103 |
str = util_shellquote("''''", 0); |
| 104 |
ck_assert_str_eq(str, "''\\'''\\'''\\'''\\'''"); |
| 105 |
xfree(str); |
| 106 |
|
| 107 |
str = util_shellquote("$(echo TEST)", 0); |
| 108 |
ck_assert_str_eq(str, "'$(echo TEST)'"); |
| 109 |
xfree(str); |
| 110 |
|
| 111 |
str = util_shellquote("`echo TEST`", 1000000); |
| 112 |
ck_assert_str_eq(str, "'`echo TEST`'"); |
| 113 |
xfree(str); |
| 114 |
} |
| 115 |
END_TEST |
| 116 |
|
| 117 |
Suite * |
| 118 |
util_suite(void) |
| 119 |
{ |
| 120 |
Suite *s; |
| 121 |
TCase *tc_util; |
| 122 |
|
| 123 |
s = suite_create("Util"); |
| 124 |
|
| 125 |
tc_util = tcase_create("Util"); |
| 126 |
tcase_add_test(tc_util, test_util_get_progname); |
| 127 |
tcase_add_test(tc_util, test_util_write_pid_file); |
| 128 |
tcase_add_test(tc_util, test_util_strrcmp); |
| 129 |
tcase_add_test(tc_util, test_util_utf8sanity); |
| 130 |
tcase_add_test(tc_util, test_util_expand_words); |
| 131 |
tcase_add_test(tc_util, test_util_shellquote); |
| 132 |
suite_add_tcase(s, tc_util); |
| 133 |
|
| 134 |
return (s); |
| 135 |
} |
| 136 |
|
| 137 |
int |
| 138 |
main(void) |
| 139 |
{ |
| 140 |
int num_failed; |
| 141 |
Suite *s; |
| 142 |
SRunner *sr; |
| 143 |
|
| 144 |
s = util_suite(); |
| 145 |
sr = srunner_create(s); |
| 146 |
|
| 147 |
srunner_run_all(sr, CK_NORMAL); |
| 148 |
num_failed = srunner_ntests_failed(sr); |
| 149 |
srunner_free(sr); |
| 150 |
|
| 151 |
if (num_failed) |
| 152 |
return (1); |
| 153 |
return (0); |
| 154 |
} |