Debian Codegraph @

tests/api/pidfile.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2014 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Pidfile API tests. */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30 static const char *pidfile_path = "/tmp/prt-pidfile";
31
32 /* Fixtures */
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 /* Tests */
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 }