diff --git a/tests/Makefile.am b/tests/Makefile.am index b48f447..a5d6301 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -8,6 +8,7 @@ TESTS = \ check_mdata \ check_playlist \ check_stream \ + check_util \ check_xalloc check_PROGRAMS = $(TESTS) @@ -46,6 +47,11 @@ check_stream_SOURCES = \ check_stream_DEPENDENCIES = $(top_builddir)/src/libezstream.la check_stream_LDADD = $(check_stream_DEPENDENCIES) @CHECK_LIBS@ +check_util_SOURCES = \ + check_util.c +check_util_DEPENDENCIES = $(top_builddir)/src/libezstream.la +check_util_LDADD = $(check_util_DEPENDENCIES) @CHECK_LIBS@ + check_xalloc_SOURCES = \ check_xalloc.c check_xalloc_DEPENDENCIES = $(top_builddir)/src/libezstream.la @@ -57,6 +63,7 @@ AM_CPPFLAGS = @EZ_CPPFLAGS@ \ -I$(top_builddir)/src AM_CFLAGS = @EZ_CFLAGS@ @CHECK_CFLAGS@ \ -DSRCDIR="\"$(srcdir)\"" \ + -DBUILDDIR="\"$(builddir)\"" \ -DEXAMPLESDIR="\"$(top_srcdir)/examples\"" AM_LDFLAGS = @EZ_LDFLAGS@ diff --git a/tests/check_util.c b/tests/check_util.c new file mode 100644 index 0000000..cfcf9c1 --- /dev/null +++ b/tests/check_util.c @@ -0,0 +1,149 @@ +#include + +#include +#include + +#include + +#include "util.h" +#include "xalloc.h" + +Suite * util_suite(void); + +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_ne(access(BUILDDIR "/check_util-write_pid_file.pid", F_OK), 0); + ck_assert_int_eq(util_write_pid_file(BUILDDIR "/check_util-write_pid_file.pid"), 0); + ck_assert_int_eq(access(BUILDDIR "/check_util-write_pid_file.pid", F_OK), 0); + ck_assert_int_eq(util_write_pid_file(BUILDDIR "/check_util-write_pid_file.pid"), 0); + ck_assert_int_eq(access(BUILDDIR "/check_util-write_pid_file.pid", F_OK), 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_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) +{ + unsigned 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); +}