| 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 |
#include "tests.h" |
| 28 |
|
| 29 |
static pool *p = NULL; |
| 30 |
static const char *pidfile_path = "/tmp/prt-pidfile"; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static void set_up(void) { |
| 35 |
if (p == NULL) { |
| 36 |
p = permanent_pool = make_sub_pool(NULL); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void tear_down(void) { |
| 41 |
if (p) { |
| 42 |
destroy_pool(p); |
| 43 |
p = permanent_pool = NULL; |
| 44 |
} |
| 45 |
|
| 46 |
(void) unlink(pidfile_path); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
START_TEST (pidfile_set_test) { |
| 52 |
int res; |
| 53 |
const char *path; |
| 54 |
|
| 55 |
res = pr_pidfile_set(NULL); |
| 56 |
ck_assert_msg(res < 0, "Failed to handle null argument"); |
| 57 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)", |
| 58 |
errno, strerror(errno)); |
| 59 |
|
| 60 |
path = "foo"; |
| 61 |
res = pr_pidfile_set(path); |
| 62 |
ck_assert_msg(res < 0, "Failed to handle relative path"); |
| 63 |
ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)", |
| 64 |
errno, strerror(errno)); |
| 65 |
|
| 66 |
path = "/foo"; |
| 67 |
res = pr_pidfile_set(path); |
| 68 |
ck_assert_msg(res == 0, "Failed to handle path '%s': %s", path, |
| 69 |
strerror(errno)); |
| 70 |
|
| 71 |
path = pr_pidfile_get(); |
| 72 |
ck_assert_msg(strcmp(path, "/foo") == 0, "Expected path '/foo', got '%s'", |
| 73 |
path); |
| 74 |
} |
| 75 |
END_TEST |
| 76 |
|
| 77 |
START_TEST (pidfile_remove_test) { |
| 78 |
int res; |
| 79 |
|
| 80 |
res = pr_pidfile_remove(); |
| 81 |
ck_assert_msg(res < 0, "Removed nonexistent file unexpectedly"); |
| 82 |
ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)", |
| 83 |
errno, strerror(errno)); |
| 84 |
} |
| 85 |
END_TEST |
| 86 |
|
| 87 |
START_TEST (pidfile_write_test) { |
| 88 |
int res; |
| 89 |
|
| 90 |
res = pr_pidfile_set(pidfile_path); |
| 91 |
ck_assert_msg(res == 0, "Failed to set path '%s': %s", pidfile_path, |
| 92 |
strerror(errno)); |
| 93 |
|
| 94 |
res = pr_pidfile_write(); |
| 95 |
ck_assert_msg(res == 0, "Failed to write to path '%s': %s", pidfile_path, |
| 96 |
strerror(errno)); |
| 97 |
|
| 98 |
res = pr_pidfile_remove(); |
| 99 |
ck_assert_msg(res == 0, "Failed to remove path '%s': %s", pidfile_path, |
| 100 |
strerror(errno)); |
| 101 |
|
| 102 |
res = pr_pidfile_remove(); |
| 103 |
ck_assert_msg(res < 0, "Removed nonexistent file unexpectedly"); |
| 104 |
ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)", |
| 105 |
errno, strerror(errno)); |
| 106 |
} |
| 107 |
END_TEST |
| 108 |
|
| 109 |
Suite *tests_get_pidfile_suite(void) { |
| 110 |
Suite *suite; |
| 111 |
TCase *testcase; |
| 112 |
|
| 113 |
suite = suite_create("pidfile"); |
| 114 |
|
| 115 |
testcase = tcase_create("base"); |
| 116 |
|
| 117 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 118 |
|
| 119 |
tcase_add_test(testcase, pidfile_set_test); |
| 120 |
tcase_add_test(testcase, pidfile_remove_test); |
| 121 |
tcase_add_test(testcase, pidfile_write_test); |
| 122 |
|
| 123 |
suite_add_tcase(suite, testcase); |
| 124 |
|
| 125 |
return suite; |
| 126 |
} |