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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
* ProFTPD - FTP server testsuite
* Copyright (c) 2011-2020 The ProFTPD Project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, The ProFTPD Project team and other respective
* copyright holders give permission to link this program with OpenSSL, and
* distribute the resulting executable, without including the source code for
* OpenSSL in the source distribution.
*/
/* Response API tests */
#include "tests.h"
extern pr_response_t *resp_list, *resp_err_list;
static pool *p = NULL;
static void set_up(void) {
if (p == NULL) {
p = permanent_pool = make_sub_pool(NULL);
}
init_netio();
init_inet();
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("netio", 1, 20);
pr_trace_set_levels("response", 1, 20);
}
}
static void tear_down(void) {
pr_response_register_handler(NULL);
if (session.c != NULL) {
pr_inet_close(p, session.c);
session.c = NULL;
}
pr_unregister_netio(PR_NETIO_STRM_CTRL);
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("netio", 0, 0);
pr_trace_set_levels("response", 0, 0);
}
if (p) {
destroy_pool(p);
p = permanent_pool = NULL;
}
}
START_TEST (response_pool_get_test) {
pool *res;
res = pr_response_get_pool();
ck_assert_msg(res == NULL, "Response pool not null as expected");
}
END_TEST
START_TEST (response_pool_set_test) {
pool *res;
pr_response_set_pool(p);
res = pr_response_get_pool();
ck_assert_msg(res == p, "Response pool not %p as expected", p);
}
END_TEST
START_TEST (response_add_test) {
int res;
const char *last_resp_code = NULL, *last_resp_msg = NULL;
char *resp_code = R_200, *resp_msg = "OK";
pr_response_set_pool(NULL);
mark_point();
pr_response_add(resp_code, "%s", resp_msg);
pr_response_set_pool(p);
mark_point();
pr_response_add(NULL, NULL);
mark_point();
pr_response_add(NULL, "%s", resp_msg);
mark_point();
pr_response_add(resp_code, "%s", resp_msg);
pr_response_add(NULL, "%s", resp_msg);
res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
ck_assert_msg(res == 0, "Failed to get last values: %d (%s)", errno,
strerror(errno));
ck_assert_msg(last_resp_code != NULL, "Last response code unexpectedly null");
ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
"Expected response code '%s', got '%s'", resp_code, last_resp_code);
ck_assert_msg(last_resp_msg != NULL, "Last response message unexpectedly null");
ck_assert_msg(strcmp(last_resp_msg, resp_msg) == 0,
"Expected response message '%s', got '%s'", resp_msg, last_resp_msg);
}
END_TEST
START_TEST (response_add_err_test) {
int res;
const char *last_resp_code = NULL, *last_resp_msg = NULL;
char *resp_code = R_450, *resp_msg = "Busy";
pr_response_set_pool(NULL);
mark_point();
pr_response_add(resp_code, "%s", resp_msg);
pr_response_set_pool(p);
mark_point();
pr_response_add_err(NULL, NULL);
mark_point();
pr_response_add_err(resp_code, "%s", resp_msg);
res = pr_response_get_last(p, &last_resp_code, &last_resp_msg);
ck_assert_msg(res == 0, "Failed to get last values: %d (%s)", errno,
strerror(errno));
ck_assert_msg(last_resp_code != NULL, "Last response code unexpectedly null");
ck_assert_msg(strcmp(last_resp_code, resp_code) == 0,
"Expected response code '%s', got '%s'", resp_code, last_resp_code);
ck_assert_msg(last_resp_msg != NULL, "Last response message unexpectedly null");
ck_assert_msg(strcmp(last_resp_msg, resp_msg) == 0,
"Expected response message '%s', got '%s'", resp_msg, last_resp_msg);
}
END_TEST
START_TEST (response_get_last_test) {
int res;
const char *resp_code = NULL, *resp_msg = NULL;
res = pr_response_get_last(NULL, NULL, NULL);
ck_assert_msg(res == -1, "Failed to handle null pool");
ck_assert_msg(errno == EINVAL, "Expected errno %d (%s), got %d (%s)",
EINVAL, strerror(EINVAL), errno, strerror(errno));
res = pr_response_get_last(p, NULL, NULL);
ck_assert_msg(res == -1, "Failed to handle null argument");
ck_assert_msg(errno == EINVAL, "Expected errno %d (%s), got %d (%s)",
EINVAL, strerror(EINVAL), errno, strerror(errno));
res = pr_response_get_last(p, &resp_code, &resp_msg);
ck_assert_msg(res == 0, "Failed to get last values: %d (%s)", errno,
strerror(errno));
ck_assert_msg(resp_code == NULL,
"Last response code not null as expected: %s", resp_code);
ck_assert_msg(resp_msg == NULL,
"Last response message not null as expected: %s", resp_msg);
}
END_TEST
START_TEST (response_block_test) {
int res;
res = pr_response_block(-1);
ck_assert_msg(res == -1, "Failed to handle invalid argument");
ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)",
EINVAL, strerror(errno), errno);
res = pr_response_block(TRUE);
ck_assert_msg(res == 0, "Failed to block responses: %s", strerror(errno));
res = pr_response_block(FALSE);
ck_assert_msg(res == 0, "Failed to unblock responses: %s", strerror(errno));
}
END_TEST
START_TEST (response_blocked_test) {
int res;
(void) pr_response_block(TRUE);
res = pr_response_blocked();
ck_assert_msg(res == TRUE, "Failed to detect blocked responses");
(void) pr_response_block(FALSE);
res = pr_response_blocked();
ck_assert_msg(res == FALSE, "Failed to detect unblocked responses");
}
END_TEST
START_TEST (response_clear_test) {
mark_point();
pr_response_clear(NULL);
pr_response_set_pool(p);
pr_response_add(R_200, "%s", "OK");
pr_response_clear(&resp_list);
}
END_TEST
static int response_netio_poll_cb(pr_netio_stream_t *nstrm) {
/* Always return >0, to indicate that we haven't timed out, AND that there
* is a writable fd available.
*/
return 7;
}
static int response_netio_write_cb(pr_netio_stream_t *nstrm, char *buf,
size_t buflen) {
return buflen;
}
static unsigned int resp_nlines = 0;
static char *resp_line = NULL;
static char *response_handler_cb(pool *cb_pool, const char *fmt, ...) {
char buf[PR_RESPONSE_BUFFER_SIZE] = {'\0'};
va_list msg;
va_start(msg, fmt);
vsnprintf(buf, sizeof(buf), fmt, msg);
va_end(msg);
buf[sizeof(buf)-1] = '\0';
resp_nlines++;
resp_line = pstrdup(cb_pool, buf);
return resp_line;
}
START_TEST (response_flush_test) {
int res, sockfd = -2;
conn_t *conn;
pr_netio_t *netio;
mark_point();
pr_response_flush(NULL);
netio = pr_alloc_netio2(p, NULL, "testsuite");
netio->poll = response_netio_poll_cb;
netio->write = response_netio_write_cb;
res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
strerror(errno));
conn = pr_inet_create_conn(p, sockfd, NULL, INPORT_ANY, FALSE);
session.c = conn;
pr_response_register_handler(response_handler_cb);
resp_nlines = 0;
resp_line = NULL;
pr_response_set_pool(p);
pr_response_add(R_200, "%s", "OK");
pr_response_add(R_DUP, "%s", "Still OK");
pr_response_add(R_DUP, "%s", "OK already!");
pr_response_flush(&resp_list);
pr_response_register_handler(NULL);
pr_inet_close(p, session.c);
session.c = NULL;
pr_unregister_netio(PR_NETIO_STRM_CTRL);
ck_assert_msg(resp_nlines == 3, "Expected 3 response lines flushed, got %u",
resp_nlines);
}
END_TEST
START_TEST (response_send_test) {
int res, sockfd = -2;
conn_t *conn;
pr_netio_t *netio;
netio = pr_alloc_netio2(p, NULL, "testsuite");
netio->poll = response_netio_poll_cb;
netio->write = response_netio_write_cb;
res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
strerror(errno));
conn = pr_inet_create_conn(p, sockfd, NULL, INPORT_ANY, FALSE);
session.c = conn;
pr_response_register_handler(response_handler_cb);
resp_nlines = 0;
resp_line = NULL;
pr_response_set_pool(p);
pr_response_send(R_200, "%s", "OK");
pr_response_register_handler(NULL);
pr_inet_close(p, session.c);
session.c = NULL;
pr_unregister_netio(PR_NETIO_STRM_CTRL);
ck_assert_msg(resp_nlines == 1, "Expected 1 response line flushed, got %u",
resp_nlines);
}
END_TEST
START_TEST (response_send_async_test) {
int res, sockfd = -2;
conn_t *conn;
pr_netio_t *netio;
netio = pr_alloc_netio2(p, NULL, "testsuite");
netio->poll = response_netio_poll_cb;
netio->write = response_netio_write_cb;
res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
strerror(errno));
conn = pr_inet_create_conn(p, sockfd, NULL, INPORT_ANY, FALSE);
session.c = conn;
pr_response_register_handler(response_handler_cb);
resp_nlines = 0;
resp_line = NULL;
pr_response_set_pool(p);
pr_response_send_async(R_200, "%s", "OK");
pr_response_register_handler(NULL);
pr_inet_close(p, session.c);
session.c = NULL;
pr_unregister_netio(PR_NETIO_STRM_CTRL);
ck_assert_msg(resp_nlines == 1, "Expected 1 response line flushed, got %u",
resp_nlines);
ck_assert_msg(resp_line != NULL, "Expected response line");
ck_assert_msg(strcmp(resp_line, "200 OK\r\n") == 0,
"Expected '200 OK', got '%s'", resp_line);
}
END_TEST
START_TEST (response_send_raw_test) {
int res, sockfd = -2;
conn_t *conn;
pr_netio_t *netio;
netio = pr_alloc_netio2(p, NULL, "testsuite");
netio->poll = response_netio_poll_cb;
netio->write = response_netio_write_cb;
res = pr_register_netio(netio, PR_NETIO_STRM_CTRL);
ck_assert_msg(res == 0, "Failed to register custom ctrl NetIO: %s",
strerror(errno));
conn = pr_inet_create_conn(p, sockfd, NULL, INPORT_ANY, FALSE);
session.c = conn;
pr_response_register_handler(response_handler_cb);
resp_nlines = 0;
resp_line = NULL;
pr_response_set_pool(p);
pr_response_send_raw("%s", "OK");
pr_response_register_handler(NULL);
pr_inet_close(p, session.c);
session.c = NULL;
pr_unregister_netio(PR_NETIO_STRM_CTRL);
ck_assert_msg(resp_nlines == 1, "Expected 1 response line flushed, got %u",
resp_nlines);
}
END_TEST
#if defined(TEST_BUG3711)
START_TEST (response_pool_bug3711_test) {
cmd_rec *cmd;
pool *resp_pool, *cmd_pool;
char *err_code = R_450, *err_msg = "Busy";
resp_pool = make_sub_pool(p);
cmd_pool = make_sub_pool(p);
cmd = pr_cmd_alloc(cmd_pool, 1, "foo");
pr_response_set_pool(cmd->pool);
pr_response_add_err(err_code, "%s", err_msg);
/* We expect segfaults here, so use the mark_point() function to get
* more accurate reporting of the problematic line of code in the
* error logs.
*/
mark_point();
/* We explicitly do NOT reset the Response API pool here, to emulate the
* behavior of Bug#3711.
*
* In the future, we could address this by proving a Pool API function
* that e.g. the Response API could use, to check whether the given
* pool is still a valid pool. To do this, the Pool API would keep a
* list of allocated pools, which would then be scanned. In practice such
* a list is maintained, albeit in a tree form. And there is tracking
* of the root trees for pools; permanent_pool is not the only root pool
* which can be created/used.
*/
destroy_pool(cmd_pool);
mark_point();
pr_response_add_err(err_code, "%s", err_msg);
mark_point();
pr_response_add_err(err_code, "%s", err_msg);
mark_point();
pr_response_add_err(err_code, "%s", err_msg);
}
END_TEST
#endif /* TEST_BUG3711 */
Suite *tests_get_response_suite(void) {
Suite *suite;
TCase *testcase;
#if defined(TEST_BUG3711)
int bug3711_signo = 0;
#endif /* TEST_BUG3711 */
suite = suite_create("response");
testcase = tcase_create("base");
tcase_add_checked_fixture(testcase, set_up, tear_down);
tcase_add_test(testcase, response_pool_get_test);
tcase_add_test(testcase, response_pool_set_test);
tcase_add_test(testcase, response_add_test);
tcase_add_test(testcase, response_add_err_test);
tcase_add_test(testcase, response_get_last_test);
tcase_add_test(testcase, response_block_test);
tcase_add_test(testcase, response_blocked_test);
tcase_add_test(testcase, response_clear_test);
tcase_add_test(testcase, response_flush_test);
tcase_add_test(testcase, response_send_test);
tcase_add_test(testcase, response_send_async_test);
tcase_add_test(testcase, response_send_raw_test);
#if defined(TEST_BUG3711)
/* We expect this test to fail due to a segfault; see Bug#3711.
*
* Note that on some platforms (e.g. Darwin), the test case should fail
* with a SIGBUS rather than SIGSEGV, hence the conditional here.
*/
#if defined(DARWIN9) || defined(DARWIN10) || defined(DARWIN11)
bug3711_signo = SIGBUS;
#else
bug3711_signo = SIGSEGV;
#endif
/* Disable this test for now; it's a reproduction recipe rather than
* a regression test, and only generates core files which can litter
* the filesystems of build/test machines needlessly.
*/
tcase_add_test_raise_signal(testcase, response_pool_bug3711_test,
bug3711_signo);
#endif /* TEST_BUG3711 */
suite_add_tcase(suite, testcase);
return suite;
}
|