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