Debian Codegraph @

tests/api/display.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2015-2017 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 /* Display API tests
26 */
27
28 #include "tests.h"
29
30 extern pr_response_t *resp_list, *resp_err_list;
31
32 static pool *p = NULL;
33
34 static const char *display_test_file = "/tmp/prt-display.txt";
35
36 static const char *display_lines[] = {
37 "Hello, %U\n",
38 "Environment: %{env:FOO} (%{env:NO_FOO})\n",
39 "Variable: %{BAR}\n",
40 "Time: %{time:%Y%m%d}\n",
41 NULL
42 };
43
44 /* Fixtures */
45
46 static void set_up(void) {
47 (void) unlink(display_test_file);
48
49 if (p == NULL) {
50 p = session.pool = permanent_pool = make_sub_pool(NULL);
51 }
52
53 init_dirtree();
54 init_fs();
55 init_netio();
56 init_inet();
57
58 if (getenv("TEST_VERBOSE") != NULL) {
59 pr_trace_set_levels("netio", 1, 20);
60 pr_trace_set_levels("response", 1, 20);
61 }
62 }
63
64 static void tear_down(void) {
65 pr_response_register_handler(NULL);
66
67 if (session.c != NULL) {
68 pr_inet_close(p, session.c);
69 session.c = NULL;
70 }
71
72 pr_unregister_netio(PR_NETIO_STRM_CTRL);
73
74 if (getenv("TEST_VERBOSE") != NULL) {
75 pr_trace_set_levels("netio", 0, 0);
76 pr_trace_set_levels("response", 0, 0);
77 }
78
79 (void) unlink(display_test_file);
80
81 if (p) {
82 destroy_pool(p);
83 p = session.pool = permanent_pool = NULL;
84 }
85 }
86
87 static int write_file(const char *path, const char **lines,
88 unsigned int nlines) {
89 register unsigned int i;
90 FILE *fh;
91 int res;
92
93 /* Write out a test Display file. */
94 fh = fopen(path, "w+");
95 if (fh == NULL) {
96 return -1;
97 }
98
99 for (i = 0; i < nlines; i++) {
100 const char *line;
101 size_t line_len;
102
103 line = lines[i];
104 line_len = strlen(line);
105 fwrite(line, line_len, 1, fh);
106 }
107
108 res = fclose(fh);
109 return res;
110 }
111
112 /* Tests */
113
114 START_TEST (display_file_test) {
115 int res;
116 const char *path = NULL, *resp_code = NULL;
117 const char *last_resp_code = NULL, *last_resp_msg = NULL;
118 pr_class_t *conn_class;
119
120 res = pr_display_file(NULL, NULL, NULL, 0);
121 ck_assert_msg(res < 0, "Failed to handle null arguments");
122 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
123 strerror(errno), errno);
124
125 path = display_test_file;
126 res = pr_display_file(path, NULL, NULL, 0);
127 ck_assert_msg(res < 0, "Failed to handle null resp_code argument");
128 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
129 strerror(errno), errno);
130
131 mark_point();
132 resp_code = R_200;
133 path = "/";
134 res = pr_display_file(path, NULL, resp_code, 0);
135 ck_assert_msg(res < 0, "Failed to handle directory");
136 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
137 strerror(errno), errno);
138
139 mark_point();
140 path = display_test_file;
141 res = pr_display_file(path, NULL, resp_code, 0);
142 ck_assert_msg(res < 0, "Failed to handle nonexistent file");
143 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
144 strerror(errno), errno);
145
146 res = write_file(path, display_lines, 4);
147 ck_assert_msg(res == 0, "Failed to write display file: %s", strerror(errno));
148
149 pr_response_set_pool(p);
150
151 conn_class = pcalloc(p, sizeof(pr_class_t));
152 conn_class->cls_pool = p;
153 conn_class->cls_name = "foo.bar";
154 session.conn_class = conn_class;
155
156 mark_point();
157 res = pr_display_file(path, NULL, resp_code, 0);
158 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
159
160 mark_point();
161 res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
162 ck_assert_msg(res == 0, "Failed to get last response values: %s (%d)",
163 strerror(errno), errno);
164
165 ck_assert_msg(last_resp_code != NULL,
166 "Last response code is unexpectedly null");
167 ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
168 "Expected response code '%s', got '%s'", resp_code, last_resp_code);
169
170 ck_assert_msg(last_resp_msg != NULL,
171 "Last response message is unexpectedly null");
172
173 /* Send the display file NOW */
174 mark_point();
175 res = pr_display_file(path, NULL, resp_code, PR_DISPLAY_FL_SEND_NOW);
176 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
177
178 mark_point();
179 res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
180 ck_assert_msg(res == 0, "Failed to get last response values: %s (%d)",
181 strerror(errno), errno);
182
183 ck_assert_msg(last_resp_code != NULL,
184 "Last response code is unexpectedly null");
185 ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
186 "Expected response code '%s', got '%s'", resp_code, last_resp_code);
187
188 ck_assert_msg(last_resp_msg != NULL,
189 "Last response message is unexpectedly null");
190
191 /* Send the display file NOW, with no EOM */
192
193 mark_point();
194 res = pr_display_file(path, NULL, resp_code,
195 PR_DISPLAY_FL_SEND_NOW|PR_DISPLAY_FL_NO_EOM);
196 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
197
198 mark_point();
199 res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
200 ck_assert_msg(res == 0, "Failed to get last response values: %s (%d)",
201 strerror(errno), errno);
202
203 ck_assert_msg(last_resp_code != NULL,
204 "Last response code is unexpectedly null");
205 ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
206 "Expected response code '%s', got '%s'", resp_code, last_resp_code);
207
208 ck_assert_msg(last_resp_msg != NULL,
209 "Last response message is unexpectedly null");
210
211 /* With MultilineRFC2228 on */
212 mark_point();
213 res = pr_display_file(path, NULL, resp_code,
214 PR_DISPLAY_FL_SEND_NOW|PR_DISPLAY_FL_NO_EOM);
215 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
216
217 mark_point();
218 res = pr_display_file(path, NULL, resp_code, PR_DISPLAY_FL_SEND_NOW);
219 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
220
221 /* With session.auth_mech */
222 session.auth_mech = "testsuite";
223
224 mark_point();
225 res = pr_display_file(path, NULL, resp_code,
226 PR_DISPLAY_FL_SEND_NOW|PR_DISPLAY_FL_NO_EOM);
227 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
228
229 mark_point();
230 res = pr_display_file(path, NULL, resp_code, PR_DISPLAY_FL_SEND_NOW);
231 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
232 }
233 END_TEST
234
235 START_TEST (display_fh_test) {
236 pr_fh_t *fh;
237 int res;
238 const char *path = NULL, *resp_code = NULL;
239 const char *last_resp_code = NULL, *last_resp_msg = NULL;
240
241 res = pr_display_fh(NULL, NULL, NULL, 0);
242 ck_assert_msg(res < 0, "Failed to handle null arguments");
243 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
244 strerror(errno), errno);
245
246 (void) unlink(display_test_file);
247 res = write_file(display_test_file, display_lines, 4);
248 ck_assert_msg(res == 0, "Failed to write display file: %s", strerror(errno));
249
250 path = display_test_file;
251 fh = pr_fsio_open(path, O_RDONLY);
252 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", path, strerror(errno));
253
254 mark_point();
255 res = pr_display_fh(fh, NULL, NULL, 0);
256 ck_assert_msg(res < 0, "Failed to handle null resp_code argument");
257 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
258 strerror(errno), errno);
259
260 resp_code = R_200;
261 pr_response_set_pool(p);
262
263 mark_point();
264 res = pr_display_fh(fh, NULL, resp_code, 0);
265 ck_assert_msg(res == 0, "Failed to display file: %s", strerror(errno));
266
267 mark_point();
268 res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
269 ck_assert_msg(res == 0, "Failed to get last response values: %s (%d)",
270 strerror(errno), errno);
271
272 ck_assert_msg(last_resp_code != NULL,
273 "Last response code is unexpectedly null");
274 ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
275 "Expected response code '%s', got '%s'", resp_code, last_resp_code);
276
277 ck_assert_msg(last_resp_msg != NULL,
278 "Last response message is unexpectedly null");
279 }
280 END_TEST
281
282 Suite *tests_get_display_suite(void) {
283 Suite *suite;
284 TCase *testcase;
285
286 suite = suite_create("display");
287
288 testcase = tcase_create("base");
289 tcase_add_checked_fixture(testcase, set_up, tear_down);
290
291 tcase_add_test(testcase, display_file_test);
292 tcase_add_test(testcase, display_fh_test);
293
294 suite_add_tcase(suite, testcase);
295 return suite;
296 }