Debian Codegraph @

tests/api/ctrls.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2020-2023 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 /* Controls API tests */
26
27 #include "tests.h"
28 #include "mod_ctrls.h"
29
30 #if defined(PR_USE_CTRLS)
31 static pool *p = NULL;
32
33 static const char *tmpfile_path = "/tmp/prt-ctrls.dat";
34
35 static void set_up(void) {
36 (void) unlink(tmpfile_path);
37
38 if (p == NULL) {
39 p = permanent_pool = make_sub_pool(NULL);
40 }
41
42 if (getenv("TEST_VERBOSE") != NULL) {
43 pr_trace_set_levels("ctrls", 1, 20);
44 pr_trace_set_levels("json", 1, 20);
45 }
46
47 init_ctrls2("/tmp/test.sock");
48 }
49
50 static void tear_down(void) {
51 if (getenv("TEST_VERBOSE") != NULL) {
52 pr_trace_set_levels("ctrls", 0, 0);
53 pr_trace_set_levels("json", 0, 0);
54 }
55
56 if (p != NULL) {
57 destroy_pool(p);
58 p = permanent_pool = NULL;
59 }
60
61 (void) unlink(tmpfile_path);
62 }
63
64 static int devnull_fd(void) {
65 int fd;
66
67 fd = open("/dev/null", O_RDWR);
68 if (fd < 0) {
69 fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
70 return -1;
71 }
72
73 return fd;
74 }
75
76 static int tmpfile_fd(void) {
77 int fd;
78
79 fd = open(tmpfile_path, O_CREAT|O_RDWR, 0600);
80 if (fd < 0) {
81 fprintf(stderr, "Error opening %s: %s\n", tmpfile_path, strerror(errno));
82 return -1;
83 }
84
85 (void) unlink(tmpfile_path);
86 return fd;
87 }
88
89 static int reset_fd(int fd) {
90 (void) close(fd);
91 return tmpfile_fd();
92 }
93
94 static int rewind_fd(int fd) {
95 if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
96 return -1;
97 }
98
99 return 0;
100 }
101
102 /* Largely copied from mod_ctrls. */
103 static int listen_unix(const char *path) {
104 int fd = -1, socklen = 0;
105 struct sockaddr_un sock;
106 #if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
107 !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
108 int opt = 1;
109 socklen_t optlen = sizeof(opt);
110 #endif /* !LOCAL_CREDS */
111
112 fd = socket(AF_UNIX, SOCK_STREAM, 0);
113 if (fd < 0) {
114 fprintf(stderr, "error creating local socket: %s\n", strerror(errno));
115 return -1;
116 }
117
118 (void) unlink(path);
119 memset(&sock, 0, sizeof(sock));
120 sock.sun_family = AF_UNIX;
121 sstrncpy(sock.sun_path, path, sizeof(sock.sun_path));
122
123 socklen = sizeof(sock);
124 if (bind(fd, (struct sockaddr *) &sock, socklen) < 0) {
125 fprintf(stderr, "error binding local socket to path '%s': %s\n", path,
126 strerror(errno));
127 (void) close(fd);
128 return -1;
129 }
130
131 if (listen(fd, 5) < 0) {
132 fprintf(stderr, "error listening on local socket: %s\n", strerror(errno));
133 (void) close(fd);
134 return -1;
135 }
136
137 #if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
138 !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
139 /* Set the LOCAL_CREDS socket option. */
140 if (setsockopt(fd, 0, LOCAL_CREDS, &opt, optlen) < 0) {
141 fprintf(stderr, "error enabling LOCAL_CREDS: %s\n", strerror(errno));
142 }
143 #endif /* !LOCAL_CREDS */
144
145 return fd;
146 }
147
148 /* Tests */
149
150 START_TEST (ctrls_alloc_free_test) {
151 int res;
152 pr_ctrls_t *ctrl, *ctrl2, *ctrl3;
153
154 mark_point();
155 res = pr_ctrls_free(NULL);
156 ck_assert_msg(res < 0, "Failed to handle null ctrl");
157 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
158 strerror(errno), errno);
159
160 mark_point();
161 ctrl = pr_ctrls_alloc();
162 ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
163 res = pr_ctrls_free(ctrl);
164 ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
165
166 mark_point();
167 ctrl = pr_ctrls_alloc();
168 ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
169 res = pr_ctrls_free(ctrl);
170 ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
171
172 /* LIFO order */
173 mark_point();
174 ctrl = pr_ctrls_alloc();
175 ctrl2 = pr_ctrls_alloc();
176 ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
177 ctrl2->ctrls_tmp_pool = make_sub_pool(p);
178 ctrl3 = pr_ctrls_alloc();
179 ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
180 ctrl3->ctrls_tmp_pool = make_sub_pool(p);
181
182 res = pr_ctrls_free(ctrl3);
183 ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
184 res = pr_ctrls_free(ctrl2);
185 ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
186 res = pr_ctrls_free(ctrl);
187 ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
188
189 /* FIFO order */
190 mark_point();
191 ctrl = pr_ctrls_alloc();
192 ctrl2 = pr_ctrls_alloc();
193 ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
194 ctrl2->ctrls_tmp_pool = make_sub_pool(p);
195 ctrl3 = pr_ctrls_alloc();
196 ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
197 ctrl3->ctrls_tmp_pool = make_sub_pool(p);
198
199 res = pr_ctrls_free(ctrl);
200 ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
201 res = pr_ctrls_free(ctrl2);
202 ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
203 res = pr_ctrls_free(ctrl3);
204 ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
205 }
206 END_TEST
207
208 START_TEST (ctrls_unregister_test) {
209 int res;
210
211 mark_point();
212 res = pr_ctrls_unregister(NULL, NULL);
213 ck_assert_msg(res < 0, "Failed to handle lack of registered actions");
214 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
215 strerror(errno), errno);
216 }
217 END_TEST
218
219 static int ctrls_test_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
220 return 0;
221 }
222
223 static int ctrls_test2_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
224 return 0;
225 }
226
227 START_TEST (ctrls_register_test) {
228 int res;
229 const char *action = NULL, *desc = NULL;
230 module m;
231
232 mark_point();
233 res = pr_ctrls_register(NULL, NULL, NULL, NULL);
234 ck_assert_msg(res < 0, "Failed to handle null action");
235 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
236 strerror(errno), errno);
237
238 mark_point();
239 action = "test";
240 res = pr_ctrls_register(NULL, action, NULL, NULL);
241 ck_assert_msg(res < 0, "Failed to handle null desc");
242 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
243 strerror(errno), errno);
244
245 mark_point();
246 desc = "desc";
247 res = pr_ctrls_register(NULL, action, desc, NULL);
248 ck_assert_msg(res < 0, "Failed to handle null callback");
249 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
250 strerror(errno), errno);
251
252 mark_point();
253 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
254 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
255
256 mark_point();
257 res = pr_ctrls_unregister(NULL, action);
258 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
259 strerror(errno));
260
261 mark_point();
262 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
263 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
264
265 m.name = "test";
266 res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
267 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
268
269 mark_point();
270 res = pr_ctrls_unregister(NULL, action);
271 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
272 strerror(errno));
273 }
274 END_TEST
275
276 START_TEST (ctrls_add_arg_test) {
277 int res;
278 pr_ctrls_t *ctrl;
279 char buf[4];
280 size_t buflen;
281
282 mark_point();
283 res = pr_ctrls_add_arg(NULL, NULL, 0);
284 ck_assert_msg(res < 0, "Failed to handle null ctrl");
285 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
286 strerror(errno), errno);
287
288 mark_point();
289 ctrl = pr_ctrls_alloc();
290 res = pr_ctrls_add_arg(ctrl, NULL, 0);
291 ck_assert_msg(res < 0, "Failed to handle null arg");
292 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
293 strerror(errno), errno);
294
295 mark_point();
296 ctrl = pr_ctrls_alloc();
297
298 /* Provide an arg that uses unprintable ASCII. */
299 buf[0] = 'a';
300 buf[1] = 'b';
301 buf[2] = -120;
302 buflen = 3;
303
304 res = pr_ctrls_add_arg(ctrl, buf, buflen);
305 ck_assert_msg(res < 0, "Failed to handle bad arg");
306 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
307 strerror(errno), errno);
308
309 mark_point();
310 buf[0] = 'a';
311 buf[1] = 'B';
312 buf[2] = '1';
313 buflen = 3;
314
315 res = pr_ctrls_add_arg(ctrl, buf, buflen);
316 ck_assert_msg(res == 0, "Failed to add ctrl arg: %s", strerror(errno));
317 }
318 END_TEST
319
320 START_TEST (ctrls_add_response_test) {
321 int res;
322 pr_ctrls_t *ctrl;
323
324 mark_point();
325 res = pr_ctrls_add_response(NULL, NULL);
326 ck_assert_msg(res < 0, "Failed to handle null ctrl");
327 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
328 strerror(errno), errno);
329
330 mark_point();
331 ctrl = pr_ctrls_alloc();
332 res = pr_ctrls_add_response(ctrl, NULL);
333 ck_assert_msg(res < 0, "Failed to handle null fmt");
334 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
335 strerror(errno), errno);
336
337 mark_point();
338 res = pr_ctrls_add_response(ctrl, "%s", "foo");
339 ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
340 }
341 END_TEST
342
343 START_TEST (ctrls_copy_args_test) {
344 int res;
345 pr_ctrls_t *src_ctrl, *dst_ctrl;
346
347 mark_point();
348 res = pr_ctrls_copy_args(NULL, NULL);
349 ck_assert_msg(res < 0, "Failed to handle null src ctrl");
350 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
351 strerror(errno), errno);
352
353 mark_point();
354 src_ctrl = pr_ctrls_alloc();
355 res = pr_ctrls_copy_args(src_ctrl, NULL);
356 ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
357 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
358 strerror(errno), errno);
359
360 mark_point();
361 res = pr_ctrls_copy_args(src_ctrl, src_ctrl);
362 ck_assert_msg(res < 0, "Failed to handle same src/dst ctrl");
363 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
364 strerror(errno), errno);
365
366 mark_point();
367 dst_ctrl = pr_ctrls_alloc();
368 res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
369 ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
370
371 mark_point();
372 res = pr_ctrls_add_arg(src_ctrl, "foo", 3);
373 ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
374
375 res = pr_ctrls_add_arg(src_ctrl, "bar", 3);
376 ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
377
378 res = pr_ctrls_add_arg(src_ctrl, "baz", 3);
379 ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
380
381 res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
382 ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
383 }
384 END_TEST
385
386 START_TEST (ctrls_copy_resps_test) {
387 int res;
388 pr_ctrls_t *src_ctrl, *dst_ctrl;
389
390 mark_point();
391 res = pr_ctrls_copy_resps(NULL, NULL);
392 ck_assert_msg(res < 0, "Failed to handle null src ctrl");
393 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
394 strerror(errno), errno);
395
396 mark_point();
397 src_ctrl = pr_ctrls_alloc();
398 res = pr_ctrls_copy_resps(src_ctrl, NULL);
399 ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
400 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
401 strerror(errno), errno);
402
403 mark_point();
404 res = pr_ctrls_copy_resps(src_ctrl, src_ctrl);
405 ck_assert_msg(res < 0, "Failed to handle same src/dst ctrl");
406 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
407 strerror(errno), errno);
408
409 mark_point();
410 dst_ctrl = pr_ctrls_alloc();
411 res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
412 ck_assert_msg(res < 0, "Failed to handle src ctrl with no responses");
413 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
414 strerror(errno), errno);
415
416 mark_point();
417 res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
418 ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
419
420 res = pr_ctrls_add_response(dst_ctrl, "%s", "bar");
421 ck_assert_msg(res == 0, "Failed to add dst ctrl response: %s", strerror(errno));
422
423 res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
424 ck_assert_msg(res < 0, "Failed to handle dst ctrl with responses");
425 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
426 strerror(errno), errno);
427
428 mark_point();
429 src_ctrl = pr_ctrls_alloc();
430 res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
431 ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
432
433 dst_ctrl = pr_ctrls_alloc();
434 res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
435 ck_assert_msg(res == 0, "Failed to copy ctrl responses: %s", strerror(errno));
436 }
437 END_TEST
438
439 START_TEST (ctrls_send_request_test) {
440 int fd, res;
441 char *action = "foo";
442 unsigned int msgargc = 1;
443 char *msgargv[] = { "bar", NULL };
444
445 mark_point();
446 res = pr_ctrls_send_request(NULL, -1, NULL, 0, NULL);
447 ck_assert_msg(res < 0, "Failed to handle null pool");
448 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
449 strerror(errno), errno);
450
451 mark_point();
452 res = pr_ctrls_send_request(p, -1, NULL, 0, NULL);
453 ck_assert_msg(res < 0, "Failed to handle invalid fd");
454 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
455 strerror(errno), errno);
456
457 mark_point();
458 fd = 7;
459 res = pr_ctrls_send_request(p, fd, NULL, 0, NULL);
460 ck_assert_msg(res < 0, "Failed to handle null action");
461 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
462 strerror(errno), errno);
463
464 mark_point();
465 res = pr_ctrls_send_request(p, fd, action, msgargc, NULL);
466 ck_assert_msg(res < 0, "Failed to handle mismatched argc/argv");
467 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
468 strerror(errno), errno);
469
470 mark_point();
471 fd = 7777;
472 res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
473 ck_assert_msg(res < 0, "Failed to handle invalid fd");
474 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
475 strerror(errno), errno);
476
477 fd = devnull_fd();
478 if (fd < 0) {
479 return;
480 }
481
482 mark_point();
483 res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
484 ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
485
486 (void) close(fd);
487 }
488 END_TEST
489
490 START_TEST (ctrls_send_response_test) {
491 int fd, res, status;
492 unsigned int msgargc = 1;
493 char *msgargv[] = { "foo", NULL };
494
495 mark_point();
496 res = pr_ctrls_send_response(NULL, -1, 0, 0, NULL);
497 ck_assert_msg(res < 0, "Failed to handle null pool");
498 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
499 strerror(errno), errno);
500
501 mark_point();
502 res = pr_ctrls_send_response(p, -1, 0, 0, NULL);
503 ck_assert_msg(res < 0, "Failed to handle invalid fd");
504 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
505 strerror(errno), errno);
506
507 mark_point();
508 fd = devnull_fd();
509 if (fd < 0) {
510 return;
511 }
512 res = pr_ctrls_send_response(p, fd, 0, 0, NULL);
513 ck_assert_msg(res == 0, "Failed to send zero ctrl messages: %s",
514 strerror(errno));
515
516 mark_point();
517 res = pr_ctrls_send_response(p, fd, 0, msgargc, NULL);
518 ck_assert_msg(res < 0, "Failed to handle missing argv");
519 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
520 strerror(errno), errno);
521 (void) close(fd);
522
523 mark_point();
524 fd = 7777;
525 status = -24;
526 res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
527 ck_assert_msg(res < 0, "Failed to handle invalid fd");
528 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
529 strerror(errno), errno);
530
531 fd = devnull_fd();
532 if (fd < 0) {
533 return;
534 }
535
536 mark_point();
537 status = -24;
538 res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
539 ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
540
541 (void) close(fd);
542 }
543 END_TEST
544
545 START_TEST (ctrls_flush_response_test) {
546 int res;
547 pr_ctrls_t *ctrl;
548 pr_ctrls_cl_t *cl;
549
550 mark_point();
551 res = pr_ctrls_flush_response(NULL);
552 ck_assert_msg(res < 0, "Failed to handle null ctrl");
553 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
554 strerror(errno), errno);
555
556 mark_point();
557 ctrl = pr_ctrls_alloc();
558 res = pr_ctrls_flush_response(ctrl);
559 ck_assert_msg(res == 0, "Failed to flush ctrl with no responses: %s",
560 strerror(errno));
561
562 mark_point();
563 res = pr_ctrls_add_response(ctrl, "%s", "foo");
564 ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
565 res = pr_ctrls_flush_response(ctrl);
566 ck_assert_msg(res < 0, "Failed to handle ctrl with no client");
567 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
568 strerror(errno), errno);
569
570 mark_point();
571 cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
572 cl->cl_fd = devnull_fd();
573 if (cl->cl_fd < 0) {
574 return;
575 }
576
577 ctrl->ctrls_cl = cl;
578 res = pr_ctrls_flush_response(ctrl);
579 ck_assert_msg(res == 0, "Failed to flush ctrl responses: %s", strerror(errno));
580
581 mark_point();
582 (void) close(cl->cl_fd);
583 res = pr_ctrls_flush_response(ctrl);
584 ck_assert_msg(res < 0, "Failed to handle bad fd");
585 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
586 strerror(errno), errno);
587 }
588 END_TEST
589
590 START_TEST (ctrls_recv_request_invalid_test) {
591 int fd, res;
592 uint32_t msglen;
593 char *msg = NULL;
594 pr_ctrls_cl_t *cl;
595
596 mark_point();
597 res = pr_ctrls_recv_request(NULL);
598 ck_assert_msg(res < 0, "Failed to handle null client");
599 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
600 strerror(errno), errno);
601
602 mark_point();
603 cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
604 res = pr_ctrls_recv_request(cl);
605 ck_assert_msg(res < 0, "Failed to handle client without ctrls list");
606 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
607 strerror(errno), errno);
608
609 mark_point();
610 cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
611 cl->cl_fd = -1;
612 res = pr_ctrls_recv_request(cl);
613 ck_assert_msg(res < 0, "Failed to handle client without fd");
614 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
615 strerror(errno), errno);
616
617 mark_point();
618 fd = tmpfile_fd();
619 if (fd < 0) {
620 return;
621 }
622
623 cl->cl_fd = fd;
624 (void) close(cl->cl_fd);
625 res = pr_ctrls_recv_request(cl);
626 ck_assert_msg(res < 0, "Failed to handle client with bad fd");
627 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
628 strerror(errno), errno);
629
630 mark_point();
631 fd = tmpfile_fd();
632 if (fd < 0) {
633 return;
634 }
635
636 cl->cl_fd = fd;
637 (void) write(fd, "a", 1);
638 rewind_fd(fd);
639 res = pr_ctrls_recv_request(cl);
640 ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
641 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
642 strerror(errno), errno);
643
644 fd = reset_fd(fd);
645 if (fd < 0) {
646 return;
647 }
648 cl->cl_fd = fd;
649
650 mark_point();
651 msglen = 2;
652 (void) write(fd, &msglen, sizeof(msglen));
653 (void) write(fd, "a", 1);
654 rewind_fd(fd);
655 res = pr_ctrls_recv_request(cl);
656 ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
657 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
658 strerror(errno), errno);
659
660 fd = reset_fd(fd);
661 if (fd < 0) {
662 return;
663 }
664 cl->cl_fd = fd;
665
666 mark_point();
667 msglen = 2;
668 (void) write(fd, &msglen, sizeof(msglen));
669 (void) write(fd, "aa", 2);
670 rewind_fd(fd);
671 res = pr_ctrls_recv_request(cl);
672 ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
673 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
674 strerror(errno), errno);
675
676 fd = reset_fd(fd);
677 if (fd < 0) {
678 return;
679 }
680 cl->cl_fd = fd;
681
682 mark_point();
683 msg = "{}";
684 msglen = strlen(msg);
685 (void) write(fd, &msglen, sizeof(msglen));
686 (void) write(fd, msg, msglen);
687 rewind_fd(fd);
688 res = pr_ctrls_recv_request(cl);
689 ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'action')");
690 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
691 strerror(errno), errno);
692
693 fd = reset_fd(fd);
694 if (fd < 0) {
695 return;
696 }
697 cl->cl_fd = fd;
698
699 mark_point();
700 msg = "{\"action\":\"foo\"}";
701 msglen = strlen(msg);
702 (void) write(fd, &msglen, sizeof(msglen));
703 (void) write(fd, msg, msglen);
704 rewind_fd(fd);
705 res = pr_ctrls_recv_request(cl);
706 ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'args')");
707 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
708 strerror(errno), errno);
709
710 fd = reset_fd(fd);
711 if (fd < 0) {
712 return;
713 }
714 cl->cl_fd = fd;
715
716 mark_point();
717 msg = "{\"action\":\"test\",\"args\":[1,2,3,4,5,6,7,8,9,10]}";
718 msglen = strlen(msg);
719 (void) write(fd, &msglen, sizeof(msglen));
720 (void) write(fd, msg, msglen);
721 rewind_fd(fd);
722 res = pr_ctrls_recv_request(cl);
723 ck_assert_msg(res < 0, "Failed to handle unknown action");
724 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
725 strerror(errno), errno);
726
727 (void) close(fd);
728 }
729 END_TEST
730
731 START_TEST (ctrls_recv_request_valid_test) {
732 int fd, res;
733 uint32_t msglen = 0;
734 char *msg = NULL;
735 const char *action, *desc;
736 pr_ctrls_cl_t *cl;
737 pr_ctrls_t *ctrl;
738 module m;
739
740 mark_point();
741 m.name = "test";
742 action = "test";
743 desc = "desc";
744 res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
745 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
746
747 mark_point();
748 fd = tmpfile_fd();
749 if (fd < 0) {
750 return;
751 }
752
753 cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
754 cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
755 cl->cl_fd = fd;
756
757 msg = "{\"action\":\"test\",\"args\":[]}";
758 msglen = strlen(msg);
759 (void) write(fd, &msglen, sizeof(msglen));
760 (void) write(fd, msg, msglen);
761 rewind_fd(fd);
762 res = pr_ctrls_recv_request(cl);
763 ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
764 ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
765 cl->cl_ctrls->nelts);
766 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
767 ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
768 "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
769 ck_assert_msg(ctrl->ctrls_cb_args == NULL,
770 "Expected no callback args, got %p", ctrl->ctrls_cb_args);
771
772 mark_point();
773 fd = reset_fd(fd);
774 if (fd < 0) {
775 return;
776 }
777 clear_array(cl->cl_ctrls);
778 cl->cl_fd = fd;
779
780 msg = "{\"action\":\"test\",\"args\":[\"a\"]}";
781 msglen = strlen(msg);
782 (void) write(fd, &msglen, sizeof(msglen));
783 (void) write(fd, msg, msglen);
784 rewind_fd(fd);
785 res = pr_ctrls_recv_request(cl);
786 ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
787 ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
788 cl->cl_ctrls->nelts);
789 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
790 ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
791 "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
792 ck_assert_msg(ctrl->ctrls_cb_args != NULL,
793 "Expected callback args, got %p", ctrl->ctrls_cb_args);
794
795 mark_point();
796 fd = reset_fd(fd);
797 if (fd < 0) {
798 return;
799 }
800 clear_array(cl->cl_ctrls);
801 cl->cl_fd = fd;
802
803 msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
804 msglen = strlen(msg);
805 (void) write(fd, &msglen, sizeof(msglen));
806 (void) write(fd, msg, msglen);
807 rewind_fd(fd);
808 res = pr_ctrls_recv_request(cl);
809 ck_assert_msg(res == 0, "Failed to handle valid request: %s",
810 strerror(errno));
811 ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
812 cl->cl_ctrls->nelts);
813 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
814 ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
815 "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
816 ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
817 ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
818 "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
819
820 /* next_action present */
821
822 mark_point();
823 res = pr_ctrls_register(&m, action, desc, ctrls_test2_cb);
824 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
825
826 mark_point();
827 fd = reset_fd(fd);
828 if (fd < 0) {
829 return;
830 }
831 clear_array(cl->cl_ctrls);
832 cl->cl_fd = fd;
833
834 msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
835 msglen = strlen(msg);
836 (void) write(fd, &msglen, sizeof(msglen));
837 (void) write(fd, msg, msglen);
838 rewind_fd(fd);
839 res = pr_ctrls_recv_request(cl);
840 ck_assert_msg(res == 0, "Failed to handle valid request: %s",
841 strerror(errno));
842 ck_assert_msg(cl->cl_ctrls->nelts == 2, "Expected 2 ctrl, got %d",
843 cl->cl_ctrls->nelts);
844
845 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
846 ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
847 "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
848 ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
849 ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
850 "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
851
852 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[1];
853 ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
854 "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
855 ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
856 ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
857 "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
858
859 (void) pr_ctrls_unregister(&m, action);
860 (void) close(fd);
861 }
862 END_TEST
863
864 START_TEST (ctrls_recv_response_test) {
865 int fd, res, status;
866 uint32_t msglen;
867 char *msg = NULL;
868
869 mark_point();
870 res = pr_ctrls_recv_response(NULL, -1, NULL, NULL);
871 ck_assert_msg(res < 0, "Failed to handle null pool");
872 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
873 strerror(errno), errno);
874
875 mark_point();
876 res = pr_ctrls_recv_response(p, -1, NULL, NULL);
877 ck_assert_msg(res < 0, "Failed to handle invalid fd");
878 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
879 strerror(errno), errno);
880
881 mark_point();
882 fd = tmpfile_fd();
883 if (fd < 0) {
884 return;
885 }
886
887 res = pr_ctrls_recv_response(p, fd, NULL, NULL);
888 ck_assert_msg(res < 0, "Failed to handle null status");
889 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
890 strerror(errno), errno);
891 (void) close(fd);
892
893 mark_point();
894 fd = tmpfile_fd();
895 if (fd < 0) {
896 return;
897 }
898
899 (void) write(fd, "a", 1);
900 rewind_fd(fd);
901 res = pr_ctrls_recv_response(p, fd, &status, NULL);
902 ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
903 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
904 strerror(errno), errno);
905
906 fd = reset_fd(fd);
907 if (fd < 0) {
908 return;
909 }
910
911 mark_point();
912 msglen = 2;
913 (void) write(fd, &msglen, sizeof(msglen));
914 (void) write(fd, "a", 1);
915 rewind_fd(fd);
916 res = pr_ctrls_recv_response(p, fd, &status, NULL);
917 ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
918 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
919 strerror(errno), errno);
920
921 fd = reset_fd(fd);
922 if (fd < 0) {
923 return;
924 }
925
926 mark_point();
927 msglen = 2;
928 (void) write(fd, &msglen, sizeof(msglen));
929 (void) write(fd, "aa", 2);
930 rewind_fd(fd);
931 res = pr_ctrls_recv_response(p, fd, &status, NULL);
932 ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
933 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
934 strerror(errno), errno);
935
936 fd = reset_fd(fd);
937 if (fd < 0) {
938 return;
939 }
940
941 mark_point();
942 msg = "{}";
943 msglen = strlen(msg);
944 (void) write(fd, &msglen, sizeof(msglen));
945 (void) write(fd, msg, msglen);
946 rewind_fd(fd);
947 res = pr_ctrls_recv_response(p, fd, &status, NULL);
948 ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'status')");
949 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
950 strerror(errno), errno);
951
952 fd = reset_fd(fd);
953 if (fd < 0) {
954 return;
955 }
956
957 mark_point();
958 msg = "{\"status\":0}";
959 msglen = strlen(msg);
960 (void) write(fd, &msglen, sizeof(msglen));
961 (void) write(fd, msg, msglen);
962 rewind_fd(fd);
963 res = pr_ctrls_recv_response(p, fd, &status, NULL);
964 ck_assert_msg(res < 0,
965 "Failed to handle invalid message (missing 'responses')");
966 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
967 strerror(errno), errno);
968
969 fd = reset_fd(fd);
970 if (fd < 0) {
971 return;
972 }
973
974 mark_point();
975 msg = "{\"status\":0,\"responses\":[1]}";
976 msglen = strlen(msg);
977 (void) write(fd, &msglen, sizeof(msglen));
978 (void) write(fd, msg, msglen);
979 rewind_fd(fd);
980 res = pr_ctrls_recv_response(p, fd, &status, NULL);
981 ck_assert_msg(res < 0,
982 "Failed to handle invalid message (non-text response)");
983 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
984 strerror(errno), errno);
985
986 fd = reset_fd(fd);
987 if (fd < 0) {
988 return;
989 }
990
991 mark_point();
992 msg = "{\"status\":0,\"responses\":[\"ok\"]}";
993 msglen = strlen(msg);
994 (void) write(fd, &msglen, sizeof(msglen));
995 (void) write(fd, msg, msglen);
996 rewind_fd(fd);
997 res = pr_ctrls_recv_response(p, fd, &status, NULL);
998 ck_assert_msg(res == 1, "Failed to handle valid response: %s",
999 strerror(errno));
1000
1001 (void) close(fd);
1002 }
1003 END_TEST
1004
1005 START_TEST (ctrls_issock_unix_test) {
1006 int res;
1007 mode_t mode;
1008
1009 mark_point();
1010 mode = 0;
1011 res = pr_ctrls_issock_unix(mode);
1012 ck_assert_msg(res < 0, "Failed to handle invalid mode");
1013 ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
1014 strerror(errno), errno);
1015
1016 #if defined(S_ISFIFO)
1017 mark_point();
1018 mode = 0;
1019 mode |= S_IFIFO;
1020 res = pr_ctrls_issock_unix(mode);
1021 if (res < 0) {
1022 ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
1023 }
1024 #endif /* S_ISFIFO */
1025
1026 #if defined(S_ISSOCK)
1027 mark_point();
1028 mode = 0;
1029 mode |= S_IFSOCK;
1030 res = pr_ctrls_issock_unix(mode);
1031 if (res < 0) {
1032 ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
1033 }
1034 #endif /* S_ISSOCK */
1035 }
1036 END_TEST
1037
1038 START_TEST (ctrls_get_registered_actions_test) {
1039 int res;
1040 pr_ctrls_t *ctrl;
1041 const char *action, *desc;
1042 module m;
1043
1044 mark_point();
1045 res = pr_get_registered_actions(NULL, 0);
1046 ck_assert_msg(res < 0, "Failed to handle null ctrl");
1047 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1048 strerror(errno), errno);
1049
1050 mark_point();
1051 ctrl = pr_ctrls_alloc();
1052 res = pr_get_registered_actions(ctrl, 0);
1053 ck_assert_msg(res == 0, "Failed to handle lack of registered actions: %s",
1054 strerror(errno));
1055
1056 mark_point();
1057 pr_block_ctrls();
1058 res = pr_get_registered_actions(ctrl, 0);
1059 ck_assert_msg(res < 0, "Failed to handle blocked actions");
1060 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1061 strerror(errno), errno);
1062 pr_unblock_ctrls();
1063
1064 mark_point();
1065 action = "test";
1066 desc = "desc";
1067 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1068 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1069
1070 m.name = "test";
1071 res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1072 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1073
1074 mark_point();
1075 res = pr_get_registered_actions(ctrl, 0);
1076 ck_assert_msg(res == 0, "Failed to handle invalid flags: %s", strerror(errno));
1077
1078 mark_point();
1079 res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ALL);
1080 ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ALL flag: %s",
1081 strerror(errno));
1082
1083 mark_point();
1084 res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ENABLED);
1085 ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ENABLED flag: %s",
1086 strerror(errno));
1087
1088 mark_point();
1089 res = pr_get_registered_actions(ctrl, CTRLS_GET_DESC);
1090 ck_assert_msg(res == 2, "Failed to handle GET_DESC flag: %s", strerror(errno));
1091
1092 mark_point();
1093 res = pr_ctrls_unregister(NULL, action);
1094 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
1095 strerror(errno));
1096 }
1097 END_TEST
1098
1099 START_TEST (ctrls_set_registered_actions_test) {
1100 int res;
1101 const char *action, *desc;
1102
1103 mark_point();
1104 res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1105 ck_assert_msg(res < 0, "Failed to handle no registered actions");
1106 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1107 strerror(errno), errno);
1108
1109 mark_point();
1110 res = pr_set_registered_actions(NULL, NULL, FALSE, 24);
1111 ck_assert_msg(res < 0, "Failed to handle invalid flag");
1112 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1113 strerror(errno), errno);
1114
1115 mark_point();
1116 action = "test";
1117 desc = "desc";
1118 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1119 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1120
1121 mark_point();
1122 res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1123 ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
1124 strerror(errno));
1125
1126 mark_point();
1127 pr_block_ctrls();
1128 res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1129 ck_assert_msg(res < 0, "Failed to handle blocked actions");
1130 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1131 strerror(errno), errno);
1132 pr_unblock_ctrls();
1133
1134 mark_point();
1135 res = pr_set_registered_actions(NULL, action, FALSE, 0);
1136 ck_assert_msg(res == 0, "Failed to handle action '%s': %s", action,
1137 strerror(errno));
1138
1139 mark_point();
1140 res = pr_set_registered_actions(NULL, "all", FALSE, 0);
1141 ck_assert_msg(res == 0, "Failed to handle action 'all': %s", strerror(errno));
1142
1143 mark_point();
1144 res = pr_ctrls_unregister(NULL, action);
1145 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
1146 strerror(errno));
1147 }
1148 END_TEST
1149
1150 START_TEST (ctrls_check_actions_test) {
1151 int res;
1152 const char *action, *desc;
1153
1154 mark_point();
1155 res = pr_ctrls_check_actions();
1156 ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
1157 strerror(errno));
1158
1159 mark_point();
1160 action = "test";
1161 desc = "desc";
1162 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1163 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1164
1165 mark_point();
1166 res = pr_ctrls_check_actions();
1167 ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
1168 strerror(errno));
1169
1170 /* Register a duplicate action name, then check. */
1171
1172 mark_point();
1173 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1174 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1175
1176 mark_point();
1177 res = pr_ctrls_check_actions();
1178 ck_assert_msg(res == 0, "Failed to handle registered actions: %s",
1179 strerror(errno));
1180
1181 mark_point();
1182 res = pr_set_registered_actions(NULL, action, FALSE, PR_CTRLS_ACT_SOLITARY);
1183 ck_assert_msg(res == 0, "Failed to set SOLITARY action flag: %s",
1184 strerror(errno));
1185
1186 mark_point();
1187 res = pr_ctrls_check_actions();
1188 ck_assert_msg(res < 0, "Failed to handle duplicate SOLITARY actions");
1189 ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
1190 strerror(errno), errno);
1191
1192 mark_point();
1193 res = pr_ctrls_unregister(NULL, action);
1194 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
1195 strerror(errno));
1196 }
1197 END_TEST
1198
1199 START_TEST (ctrls_run_ctrls_test) {
1200 int fd, res;
1201 uint32_t msglen = 0;
1202 char *msg = NULL;
1203 const char *action, *desc;
1204 pr_ctrls_cl_t *cl;
1205 pr_ctrls_t *ctrl;
1206 module m, m2;
1207 time_t now;
1208
1209 mark_point();
1210 res = pr_run_ctrls(NULL, NULL);
1211 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1212
1213 mark_point();
1214 pr_block_ctrls();
1215 res = pr_run_ctrls(NULL, NULL);
1216 ck_assert_msg(res < 0, "Failed to handle blocked ctrls");
1217 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1218 strerror(errno), errno);
1219 pr_unblock_ctrls();
1220
1221 mark_point();
1222 action = "test";
1223 desc = "desc";
1224 m.name = "test";
1225 m2.name = "test2";
1226 res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1227 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1228
1229 mark_point();
1230 res = pr_run_ctrls(NULL, NULL);
1231 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1232
1233 mark_point();
1234 res = pr_run_ctrls(&m2, NULL);
1235 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1236
1237 mark_point();
1238 res = pr_run_ctrls(&m, NULL);
1239 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1240
1241 /* XXX TODO More test cases to fill in here. */
1242 /* Note that pr_run_ctrls() makes a lot of assumptions about recv_response,
1243 * recv_request having been called previously. Not great. How to deal
1244 * with that?
1245 */
1246
1247 mark_point();
1248 fd = tmpfile_fd();
1249 if (fd < 0) {
1250 return;
1251 }
1252
1253 cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1254 cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1255 cl->cl_fd = fd;
1256
1257 msg = "{\"action\":\"test\",\"args\":[\"FOO\"]}";
1258 msglen = strlen(msg);
1259 (void) write(fd, &msglen, sizeof(msglen));
1260 (void) write(fd, msg, msglen);
1261 rewind_fd(fd);
1262 res = pr_ctrls_recv_request(cl);
1263 ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1264 ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
1265 cl->cl_ctrls->nelts);
1266 ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1267
1268 mark_point();
1269 res = pr_run_ctrls(&m2, NULL);
1270 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1271
1272 mark_point();
1273 res = pr_run_ctrls(&m, NULL);
1274 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1275
1276 mark_point();
1277 cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1278 ctrl->ctrls_flags |= PR_CTRLS_ACT_DISABLED;
1279 res = pr_run_ctrls(&m, NULL);
1280 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1281
1282 mark_point();
1283 cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1284 ctrl->ctrls_flags &= ~PR_CTRLS_ACT_DISABLED;
1285 ctrl->ctrls_flags &= ~PR_CTRLS_FL_REQUESTED;
1286 res = pr_run_ctrls(&m, NULL);
1287 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1288
1289 mark_point();
1290 cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1291 ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1292 now = time(NULL);
1293 ctrl->ctrls_when = now + 10;
1294 res = pr_run_ctrls(&m, NULL);
1295 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1296
1297 mark_point();
1298 cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1299 ctrl->ctrls_flags &= PR_CTRLS_FL_PENDING;
1300 ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1301 ctrl->ctrls_when = now - 10;
1302 res = pr_run_ctrls(&m, "test2");
1303 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1304
1305 mark_point();
1306 cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1307 ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1308 res = pr_run_ctrls(&m, "test");
1309 ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1310
1311 mark_point();
1312 res = pr_ctrls_unregister(NULL, action);
1313 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
1314 strerror(errno));
1315
1316 (void) close(fd);
1317 }
1318 END_TEST
1319
1320 START_TEST (ctrls_reset_ctrls_test) {
1321 int res;
1322 const char *action, *desc;
1323
1324 mark_point();
1325 res = pr_ctrls_reset();
1326 ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
1327
1328 mark_point();
1329 action = "test";
1330 desc = "desc";
1331 res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1332 ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1333
1334 mark_point();
1335 res = pr_ctrls_reset();
1336 ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
1337
1338 mark_point();
1339 res = pr_ctrls_unregister(NULL, action);
1340 ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
1341 strerror(errno));
1342 }
1343 END_TEST
1344
1345 START_TEST (ctrls_accept_test) {
1346 int fd, res;
1347
1348 mark_point();
1349 res = pr_ctrls_accept(-1, NULL, NULL, NULL, 0);
1350 ck_assert_msg(res < 0, "Failed to handle bad fd");
1351 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1352 strerror(errno), errno);
1353
1354 mark_point();
1355 fd = devnull_fd();
1356 if (fd < 0) {
1357 return;
1358 }
1359
1360 res = pr_ctrls_accept(fd, NULL, NULL, NULL, 5);
1361 ck_assert_msg(res < 0, "Failed to handle no clients");
1362 ck_assert_msg(errno = ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1363 strerror(errno), errno);
1364
1365 (void) close(fd);
1366 }
1367 END_TEST
1368
1369 START_TEST (ctrls_connect_test) {
1370 int fd, res;
1371 const char *socket_path;
1372
1373 mark_point();
1374 res = pr_ctrls_connect(NULL);
1375 ck_assert_msg(res < 0, "Failed to handle null path");
1376 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1377 strerror(errno), errno);
1378
1379 mark_point();
1380 socket_path = "/tmp/foo.sock";
1381 res = pr_ctrls_connect(socket_path);
1382 ck_assert_msg(res < 0, "Failed to handle nonexistent socket path");
1383 ck_assert_msg(errno == ECONNREFUSED || errno == ENOENT,
1384 "Expected ECONNREFUSED (%d) or ENOENT (%d), got %s (%d)", ECONNREFUSED,
1385 ENOENT, strerror(errno), errno);
1386
1387 mark_point();
1388 fd = listen_unix(socket_path);
1389 if (fd < 0) {
1390 return;
1391 }
1392
1393 res = pr_ctrls_connect(socket_path);
1394 ck_assert_msg(res >= 0, "Failed to connect to local socket: %s",
1395 strerror(errno));
1396
1397 (void) close(res);
1398 (void) close(fd);
1399 (void) unlink(socket_path);
1400 }
1401 END_TEST
1402
1403 START_TEST (ctrls_check_group_acl_test) {
1404 int res;
1405 gid_t gid;
1406 ctrls_group_acl_t *group_acl;
1407
1408 mark_point();
1409 res = pr_ctrls_check_group_acl(0, NULL);
1410 ck_assert_msg(res < 0, "Failed to handle null acl");
1411 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1412 strerror(errno), errno);
1413
1414 mark_point();
1415 group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1416 res = pr_ctrls_check_group_acl(0, group_acl);
1417 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1418
1419 mark_point();
1420 group_acl->allow = TRUE;
1421 res = pr_ctrls_check_group_acl(0, group_acl);
1422 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1423
1424 mark_point();
1425 group_acl->ngids = 1;
1426 res = pr_ctrls_check_group_acl(0, group_acl);
1427 ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
1428 group_acl->allow, res);
1429
1430 group_acl->allow = FALSE;
1431 res = pr_ctrls_check_group_acl(0, group_acl);
1432 ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
1433 group_acl->allow, res);
1434
1435 mark_point();
1436 gid = 1;
1437 group_acl->allow = TRUE;
1438 group_acl->gids = palloc(p, sizeof(gid_t) * 2);
1439 ((gid_t *) group_acl->gids)[0] = gid;
1440 res = pr_ctrls_check_group_acl(0, group_acl);
1441 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1442
1443 res = pr_ctrls_check_group_acl(gid, group_acl);
1444 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1445 }
1446 END_TEST
1447
1448 START_TEST (ctrls_check_user_acl_test) {
1449 int res;
1450 uid_t uid;
1451 ctrls_user_acl_t *user_acl;
1452
1453 mark_point();
1454 res = pr_ctrls_check_user_acl(0, NULL);
1455 ck_assert_msg(res < 0, "Failed to handle null acl");
1456 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1457 strerror(errno), errno);
1458
1459 mark_point();
1460 user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1461 res = pr_ctrls_check_user_acl(0, user_acl);
1462 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1463
1464 mark_point();
1465 user_acl->allow = TRUE;
1466 res = pr_ctrls_check_user_acl(0, user_acl);
1467 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1468
1469 mark_point();
1470 user_acl->nuids = 1;
1471 res = pr_ctrls_check_user_acl(0, user_acl);
1472 ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
1473 user_acl->allow, res);
1474
1475 user_acl->allow = FALSE;
1476 res = pr_ctrls_check_user_acl(0, user_acl);
1477 ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
1478 user_acl->allow, res);
1479
1480 mark_point();
1481 uid = 1;
1482 user_acl->allow = TRUE;
1483 user_acl->uids = palloc(p, sizeof(uid_t) * 2);
1484 ((uid_t *) user_acl->uids)[0] = uid;
1485 res = pr_ctrls_check_user_acl(0, user_acl);
1486 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1487
1488 res = pr_ctrls_check_user_acl(uid, user_acl);
1489 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1490 }
1491 END_TEST
1492
1493 START_TEST (ctrls_init_acl_test) {
1494 int res;
1495 ctrls_acl_t *acl;
1496
1497 mark_point();
1498 res = pr_ctrls_init_acl(NULL);
1499 ck_assert_msg(res < 0, "Failed to handle null acl");
1500 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1501 strerror(errno), errno);
1502
1503 mark_point();
1504 acl = pcalloc(p, sizeof(ctrls_acl_t));
1505 res = pr_ctrls_init_acl(acl);
1506 ck_assert_msg(res == 0, "Failed to init acl: %s", strerror(errno));
1507 }
1508 END_TEST
1509
1510 static int test_action_cb(pr_ctrls_t *ctl, int reqargc, char **reqargv) {
1511 return 0;
1512 }
1513
1514 START_TEST (ctrls_check_acl_test) {
1515 int res;
1516 pr_ctrls_t *ctrl;
1517 pr_ctrls_cl_t *cl;
1518 ctrls_acl_t *acl;
1519 const char *action;
1520 ctrls_acttab_t acttab[] = {
1521 { "test", "desc", NULL, test_action_cb },
1522 { NULL, NULL, NULL, NULL }
1523 };
1524
1525 mark_point();
1526 res = pr_ctrls_check_acl(NULL, NULL, NULL);
1527 ck_assert_msg(res < 0, "Failed to handle null ctrl");
1528 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1529 strerror(errno), errno);
1530
1531 mark_point();
1532 ctrl = pr_ctrls_alloc();
1533 res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1534 ck_assert_msg(res < 0, "Failed to handle ctrl without client");
1535 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1536 strerror(errno), errno);
1537
1538 mark_point();
1539 cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1540 cl->cl_uid = cl->cl_gid = 1;
1541 ctrl->ctrls_cl = cl;
1542 res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1543 ck_assert_msg(res < 0, "Failed to handle null acttab");
1544 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1545 strerror(errno), errno);
1546
1547 mark_point();
1548 res = pr_ctrls_check_acl(ctrl, acttab, NULL);
1549 ck_assert_msg(res < 0, "Failed to handle null action");
1550 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1551 strerror(errno), errno);
1552
1553 mark_point();
1554 action = "foobar";
1555 res = pr_ctrls_check_acl(ctrl, acttab, action);
1556 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1557
1558 mark_point();
1559 action = "test";
1560 res = pr_ctrls_check_acl(ctrl, acttab, action);
1561 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1562
1563 mark_point();
1564 acl = pcalloc(p, sizeof(ctrls_acl_t));
1565 res = pr_ctrls_init_acl(acl);
1566 acttab[0].act_acl = acl;
1567 res = pr_ctrls_check_acl(ctrl, acttab, action);
1568 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
1569
1570 mark_point();
1571 acl->acl_groups.ngids = 1;
1572 res = pr_ctrls_check_acl(ctrl, acttab, action);
1573 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1574
1575 acl->acl_users.nuids = 1;
1576 res = pr_ctrls_check_acl(ctrl, acttab, action);
1577 ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
1578 }
1579 END_TEST
1580
1581 START_TEST (ctrls_parse_acl_test) {
1582 char **res;
1583 const char *names;
1584
1585 mark_point();
1586 res = pr_ctrls_parse_acl(NULL, NULL);
1587 ck_assert_msg(res == NULL, "Failed to handle null pool");
1588 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1589 strerror(errno), errno);
1590
1591 mark_point();
1592 res = pr_ctrls_parse_acl(p, NULL);
1593 ck_assert_msg(res == NULL, "Failed to handle null ACL text");
1594 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1595 strerror(errno), errno);
1596
1597 mark_point();
1598 names = "foo";
1599 res = pr_ctrls_parse_acl(p, names);
1600 ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
1601 strerror(errno));
1602 ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
1603 ck_assert_msg(res[1] == NULL, "Expected NULL, got %p", res[1]);
1604
1605 mark_point();
1606 names = "foo,'Bar',BAZ";
1607 res = pr_ctrls_parse_acl(p, names);
1608 ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
1609 strerror(errno));
1610 ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
1611 ck_assert_msg(strcmp(res[1], "'Bar'") == 0, "Expected 'Bar', got '%s'", res[1]);
1612 ck_assert_msg(strcmp(res[2], "BAZ") == 0, "Expected 'BAZ', got '%s'", res[2]);
1613 ck_assert_msg(res[3] == NULL, "Expected NULL, got %p", res[3]);
1614 }
1615 END_TEST
1616
1617 START_TEST (ctrls_set_group_acl_test) {
1618 int res;
1619 ctrls_group_acl_t *group_acl;
1620 const char *allow;
1621 char *grouplist;
1622
1623 mark_point();
1624 res = pr_ctrls_set_group_acl(NULL, NULL, NULL, NULL);
1625 ck_assert_msg(res < 0, "Failed to handle null pool");
1626 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1627 strerror(errno), errno);
1628
1629 mark_point();
1630 res = pr_ctrls_set_group_acl(p, NULL, NULL, NULL);
1631 ck_assert_msg(res < 0, "Failed to handle null group_acl");
1632 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1633 strerror(errno), errno);
1634
1635 mark_point();
1636 group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1637 res = pr_ctrls_set_group_acl(p, group_acl, NULL, NULL);
1638 ck_assert_msg(res < 0, "Failed to handle null allow");
1639 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1640 strerror(errno), errno);
1641
1642 mark_point();
1643 allow = "allow";
1644 res = pr_ctrls_set_group_acl(p, group_acl, allow, NULL);
1645 ck_assert_msg(res < 0, "Failed to handle null grouplist");
1646 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1647 strerror(errno), errno);
1648
1649 mark_point();
1650 grouplist = "foo,bar,baz,wheel";
1651 res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1652 ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
1653 ck_assert_msg(group_acl->allow == TRUE, "Expected TRUE, got %d",
1654 group_acl->allow);
1655 /* Note that we expect zero here, because of name/GID lookup failures. */
1656 ck_assert_msg(group_acl->ngids == 0, "Expected 0, got %d",
1657 group_acl->ngids);
1658 ck_assert_msg(group_acl->gids != NULL, "Got NULL unexpectedly");
1659
1660 mark_point();
1661 group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1662 allow = "deny";
1663 grouplist = "foo,*";
1664 res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1665 ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
1666 ck_assert_msg(group_acl->allow == FALSE, "Expected FALSE, got %d",
1667 group_acl->allow);
1668 ck_assert_msg(group_acl->ngids == 1, "Expected 1, got %d",
1669 group_acl->ngids);
1670 ck_assert_msg(group_acl->gids == NULL, "Expected NULL, got %p",
1671 group_acl->gids);
1672 }
1673 END_TEST
1674
1675 START_TEST (ctrls_set_user_acl_test) {
1676 int res;
1677 ctrls_user_acl_t *user_acl;
1678 const char *allow;
1679 char *userlist;
1680
1681 mark_point();
1682 res = pr_ctrls_set_user_acl(NULL, NULL, NULL, NULL);
1683 ck_assert_msg(res < 0, "Failed to handle null pool");
1684 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1685 strerror(errno), errno);
1686
1687 mark_point();
1688 res = pr_ctrls_set_user_acl(p, NULL, NULL, NULL);
1689 ck_assert_msg(res < 0, "Failed to handle null user_acl");
1690 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1691 strerror(errno), errno);
1692
1693 mark_point();
1694 user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1695 res = pr_ctrls_set_user_acl(p, user_acl, NULL, NULL);
1696 ck_assert_msg(res < 0, "Failed to handle null allow");
1697 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1698 strerror(errno), errno);
1699
1700 mark_point();
1701 allow = "allow";
1702 res = pr_ctrls_set_user_acl(p, user_acl, allow, NULL);
1703 ck_assert_msg(res < 0, "Failed to handle null userlist");
1704 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1705 strerror(errno), errno);
1706
1707 mark_point();
1708 userlist = "foo,bar,baz,root";
1709 res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1710 ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
1711 ck_assert_msg(user_acl->allow == TRUE, "Expected TRUE, got %d",
1712 user_acl->allow);
1713 /* Note that we expect zero here, because of name/UID lookup failures. */
1714 ck_assert_msg(user_acl->nuids == 0, "Expected 0, got %d", user_acl->nuids);
1715 ck_assert_msg(user_acl->uids != NULL, "Got NULL unexpectedly");
1716
1717 mark_point();
1718 user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1719 allow = "deny";
1720 userlist = "foo,*";
1721 res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1722 ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
1723 ck_assert_msg(user_acl->allow == FALSE, "Expected FALSE, got %d",
1724 user_acl->allow);
1725 ck_assert_msg(user_acl->nuids == 1, "Expected 1, got %d", user_acl->nuids);
1726 ck_assert_msg(user_acl->uids == NULL, "Expected NULL, got %p",
1727 user_acl->uids);
1728 }
1729 END_TEST
1730
1731 START_TEST (ctrls_set_module_acls_test) {
1732 char *res, *list;
1733 const char *allow, *type;
1734 ctrls_acl_t *acl;
1735 char *good_actions[] = { "test", NULL };
1736 char *bad_actions[] = { "bar", "baz", NULL };
1737 char *all_actions[] = { "all", NULL };
1738 ctrls_acttab_t acttab[] = {
1739 { "test", "desc", NULL, test_action_cb },
1740 { NULL, NULL, NULL, NULL }
1741 };
1742
1743 mark_point();
1744 res = pr_ctrls_set_module_acls(NULL, NULL, NULL, NULL, NULL, NULL);
1745 ck_assert_msg(res == NULL, "Failed to handle null acttab");
1746 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1747 strerror(errno), errno);
1748
1749 mark_point();
1750 res = pr_ctrls_set_module_acls(acttab, NULL, NULL, NULL, NULL, NULL);
1751 ck_assert_msg(res == NULL, "Failed to handle null acl_pool");
1752 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1753 strerror(errno), errno);
1754
1755 mark_point();
1756 res = pr_ctrls_set_module_acls(acttab, p, NULL, NULL, NULL, NULL);
1757 ck_assert_msg(res == NULL, "Failed to handle null actions");
1758 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1759 strerror(errno), errno);
1760
1761 mark_point();
1762 res = pr_ctrls_set_module_acls(acttab, p, good_actions, NULL, NULL, NULL);
1763 ck_assert_msg(res == NULL, "Failed to handle null allow");
1764 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1765 strerror(errno), errno);
1766
1767 mark_point();
1768 allow = "allow";
1769 res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, NULL, NULL);
1770 ck_assert_msg(res == NULL, "Failed to handle null type");
1771 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1772 strerror(errno), errno);
1773
1774 mark_point();
1775 type = "test";
1776 res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, NULL);
1777 ck_assert_msg(res == NULL, "Failed to handle null list");
1778 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1779 strerror(errno), errno);
1780
1781 mark_point();
1782 list = "foo,bar,baz";
1783 res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1784 ck_assert_msg(res == NULL, "Failed to handle invalid type '%s'", type);
1785 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1786 strerror(errno), errno);
1787
1788 mark_point();
1789 type = "user";
1790 res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1791 ck_assert_msg(res != NULL, "Failed to handle invalid action");
1792 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1793 strerror(errno), errno);
1794 ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
1795
1796 mark_point();
1797 acl = pcalloc(p, sizeof(ctrls_acl_t));
1798 ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
1799 "Failed to initialize acl: %s", strerror(errno));
1800
1801 acttab[0].act_acl = acl;
1802 type = "group";
1803 res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1804 ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
1805
1806 mark_point();
1807 type = "user";
1808 res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1809 ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
1810
1811 mark_point();
1812 res = pr_ctrls_set_module_acls(acttab, p, all_actions, allow, type, list);
1813 ck_assert_msg(res == NULL, "Failed to handle all actions: %s", strerror(errno));
1814 }
1815 END_TEST
1816
1817 START_TEST (ctrls_set_module_acls2_test) {
1818 int res;
1819 char *list;
1820 const char *allow, *type, *bad_action = NULL;
1821 ctrls_acl_t *acl;
1822 char *good_actions[] = { "test", NULL };
1823 char *bad_actions[] = { "bar", "baz", NULL };
1824 char *all_actions[] = { "all", NULL };
1825 ctrls_acttab_t acttab[] = {
1826 { "test", "desc", NULL, test_action_cb },
1827 { NULL, NULL, NULL, NULL }
1828 };
1829
1830 mark_point();
1831 res = pr_ctrls_set_module_acls2(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1832 ck_assert_msg(res < 0, "Failed to handle null acttab");
1833 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1834 strerror(errno), errno);
1835
1836 mark_point();
1837 res = pr_ctrls_set_module_acls2(acttab, NULL, NULL, NULL, NULL, NULL, NULL);
1838 ck_assert_msg(res < 0, "Failed to handle null acl_pool");
1839 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1840 strerror(errno), errno);
1841
1842 mark_point();
1843 res = pr_ctrls_set_module_acls2(acttab, p, NULL, NULL, NULL, NULL, NULL);
1844 ck_assert_msg(res < 0, "Failed to handle null actions");
1845 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1846 strerror(errno), errno);
1847
1848 mark_point();
1849 res = pr_ctrls_set_module_acls2(acttab, p, good_actions, NULL, NULL, NULL,
1850 NULL);
1851 ck_assert_msg(res < 0, "Failed to handle null allow");
1852 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1853 strerror(errno), errno);
1854
1855 mark_point();
1856 allow = "allow";
1857 res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, NULL, NULL,
1858 NULL);
1859 ck_assert_msg(res < 0, "Failed to handle null type");
1860 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1861 strerror(errno), errno);
1862
1863 mark_point();
1864 type = "test";
1865 res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, NULL,
1866 NULL);
1867 ck_assert_msg(res < 0, "Failed to handle null list");
1868 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1869 strerror(errno), errno);
1870
1871 mark_point();
1872 list = "foo,bar,baz";
1873 res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1874 NULL);
1875 ck_assert_msg(res < 0, "Failed to handle null bad_action");
1876 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1877 strerror(errno), errno);
1878
1879 mark_point();
1880 res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1881 &bad_action);
1882 ck_assert_msg(res < 0, "Failed to handle invalid type '%s'", type);
1883 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1884 strerror(errno), errno);
1885
1886 mark_point();
1887 type = "user";
1888 res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1889 &bad_action);
1890 ck_assert_msg(res < 0, "Failed to handle invalid action");
1891 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1892 strerror(errno), errno);
1893 ck_assert_msg(strcmp(bad_action, "bar") == 0,
1894 "Expected 'bar', got '%s'", bad_action);
1895
1896 mark_point();
1897 acl = pcalloc(p, sizeof(ctrls_acl_t));
1898 ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
1899 "Failed to initialize acl: %s", strerror(errno));
1900
1901 acttab[0].act_acl = acl;
1902 type = "group";
1903 res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1904 &bad_action);
1905 ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
1906
1907 mark_point();
1908 type = "user";
1909 res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1910 &bad_action);
1911 ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
1912
1913 mark_point();
1914 res = pr_ctrls_set_module_acls2(acttab, p, all_actions, allow, type, list,
1915 &bad_action);
1916 ck_assert_msg(res == 0, "Failed to handle all actions: %s", strerror(errno));
1917 }
1918 END_TEST
1919
1920 START_TEST (ctrls_unregister_module_actions_test) {
1921 char *res;
1922 char *good_actions[] = { "test", NULL };
1923 char *bad_actions[] = { "bar", "baz", NULL };
1924 ctrls_acl_t *acl;
1925 ctrls_acttab_t acttab[] = {
1926 { "test", "desc", NULL, test_action_cb },
1927 { NULL, NULL, NULL, NULL }
1928 };
1929 module m;
1930
1931 mark_point();
1932 res = pr_ctrls_unregister_module_actions(NULL, NULL, NULL);
1933 ck_assert_msg(res == NULL, "Failed to handle null acttab");
1934 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1935 strerror(errno), errno);
1936
1937 mark_point();
1938 res = pr_ctrls_unregister_module_actions(acttab, NULL, NULL);
1939 ck_assert_msg(res == NULL, "Failed to handle null actions");
1940 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1941 strerror(errno), errno);
1942
1943 mark_point();
1944 res = pr_ctrls_unregister_module_actions(acttab, good_actions, NULL);
1945 ck_assert_msg(res == NULL, "Failed to handle null module");
1946 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1947 strerror(errno), errno);
1948
1949 mark_point();
1950 m.name = "test";
1951 res = pr_ctrls_unregister_module_actions(acttab, bad_actions, &m);
1952 ck_assert_msg(res != NULL, "Failed to handle invalid action");
1953 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1954 strerror(errno), errno);
1955 ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
1956
1957 mark_point();
1958 acl = pcalloc(p, sizeof(ctrls_acl_t));
1959 ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
1960 "Failed to initialize acl: %s", strerror(errno));
1961 acttab[0].act_acl = acl;
1962 res = pr_ctrls_unregister_module_actions(acttab, good_actions, &m);
1963 ck_assert_msg(res == NULL, "Failed to handle valid action: %s",
1964 strerror(errno));
1965 }
1966 END_TEST
1967
1968 START_TEST (ctrls_unregister_module_actions2_test) {
1969 int res;
1970 const char *bad_action = NULL;
1971 char *good_actions[] = { "test", NULL };
1972 char *bad_actions[] = { "bar", "baz", NULL };
1973 ctrls_acl_t *acl;
1974 ctrls_acttab_t acttab[] = {
1975 { "test", "desc", NULL, test_action_cb },
1976 { NULL, NULL, NULL, NULL }
1977 };
1978 module m;
1979
1980 mark_point();
1981 res = pr_ctrls_unregister_module_actions2(NULL, NULL, NULL, NULL);
1982 ck_assert_msg(res < 0, "Failed to handle null acttab");
1983 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1984 strerror(errno), errno);
1985
1986 mark_point();
1987 res = pr_ctrls_unregister_module_actions2(acttab, NULL, NULL, NULL);
1988 ck_assert_msg(res < 0, "Failed to handle null actions");
1989 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1990 strerror(errno), errno);
1991
1992 mark_point();
1993 res = pr_ctrls_unregister_module_actions2(acttab, good_actions, NULL, NULL);
1994 ck_assert_msg(res < 0, "Failed to handle null module");
1995 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1996 strerror(errno), errno);
1997
1998 mark_point();
1999 m.name = "test";
2000 res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m, NULL);
2001 ck_assert_msg(res < 0, "Failed to handle null bad_action");
2002 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2003 strerror(errno), errno);
2004
2005 mark_point();
2006 res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m,
2007 &bad_action);
2008 ck_assert_msg(res < 0, "Failed to handle invalid action");
2009 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2010 strerror(errno), errno);
2011 ck_assert_msg(bad_action != NULL, "Expected bad_action, got NULL");
2012 ck_assert_msg(strcmp(bad_action, "bar") == 0,
2013 "Expected 'bar', got '%s'", bad_action);
2014
2015 mark_point();
2016 acl = pcalloc(p, sizeof(ctrls_acl_t));
2017 ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2018 "Failed to initialize acl: %s", strerror(errno));
2019 acttab[0].act_acl = acl;
2020 res = pr_ctrls_unregister_module_actions2(acttab, good_actions, &m,
2021 &bad_action);
2022 ck_assert_msg(res == 0, "Failed to handle valid action: %s", strerror(errno));
2023 }
2024 END_TEST
2025
2026 START_TEST (ctrls_set_logfd_test) {
2027 int fd, res;
2028
2029 mark_point();
2030 fd = 0;
2031 res = pr_ctrls_set_logfd(fd);
2032 ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
2033 strerror(errno));
2034
2035 mark_point();
2036 fd = -1;
2037 res = pr_ctrls_set_logfd(fd);
2038 ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
2039 strerror(errno));
2040 }
2041 END_TEST
2042
2043 START_TEST (ctrls_log_test) {
2044 int fd, res;
2045
2046 mark_point();
2047 fd = -1;
2048 pr_ctrls_set_logfd(fd);
2049 res = pr_ctrls_log(NULL, NULL);
2050 ck_assert_msg(res == 0, "Failed to handle bad logfd: %s", strerror(errno));
2051
2052 mark_point();
2053 fd = devnull_fd();
2054 if (fd < 0) {
2055 return;
2056 }
2057
2058 pr_ctrls_set_logfd(fd);
2059 res = pr_ctrls_log(NULL, NULL);
2060 ck_assert_msg(res == 0, "Failed to handle bad module_version: %s",
2061 strerror(errno));
2062
2063 mark_point();
2064 res = pr_ctrls_log("test", NULL);
2065 ck_assert_msg(res == 0, "Failed to handle null fmt: %s", strerror(errno));
2066
2067 mark_point();
2068 res = pr_ctrls_log("test", "%s", "foo bar baz");
2069 ck_assert_msg(res == 0, "Failed to handle valid fmt: %s", strerror(errno));
2070
2071 (void) close(fd);
2072 }
2073 END_TEST
2074
2075 #endif /* PR_USE_CTRLS */
2076
2077 Suite *tests_get_ctrls_suite(void) {
2078 Suite *suite;
2079 TCase *testcase;
2080
2081 suite = suite_create("ctrls");
2082 testcase = tcase_create("base");
2083
2084 #if defined(PR_USE_CTRLS)
2085 tcase_add_checked_fixture(testcase, set_up, tear_down);
2086
2087 tcase_add_test(testcase, ctrls_alloc_free_test);
2088 tcase_add_test(testcase, ctrls_unregister_test);
2089 tcase_add_test(testcase, ctrls_register_test);
2090 tcase_add_test(testcase, ctrls_add_arg_test);
2091 tcase_add_test(testcase, ctrls_add_response_test);
2092 tcase_add_test(testcase, ctrls_copy_args_test);
2093 tcase_add_test(testcase, ctrls_copy_resps_test);
2094 tcase_add_test(testcase, ctrls_send_request_test);
2095 tcase_add_test(testcase, ctrls_send_response_test);
2096 tcase_add_test(testcase, ctrls_flush_response_test);
2097 tcase_add_test(testcase, ctrls_recv_request_invalid_test);
2098 tcase_add_test(testcase, ctrls_recv_request_valid_test);
2099 tcase_add_test(testcase, ctrls_recv_response_test);
2100
2101 tcase_add_test(testcase, ctrls_issock_unix_test);
2102 tcase_add_test(testcase, ctrls_get_registered_actions_test);
2103 tcase_add_test(testcase, ctrls_set_registered_actions_test);
2104 tcase_add_test(testcase, ctrls_check_actions_test);
2105 tcase_add_test(testcase, ctrls_run_ctrls_test);
2106 tcase_add_test(testcase, ctrls_reset_ctrls_test);
2107 tcase_add_test(testcase, ctrls_accept_test);
2108 tcase_add_test(testcase, ctrls_connect_test);
2109
2110 /* mod_ctrls */
2111 tcase_add_test(testcase, ctrls_check_group_acl_test);
2112 tcase_add_test(testcase, ctrls_check_user_acl_test);
2113 tcase_add_test(testcase, ctrls_init_acl_test);
2114 tcase_add_test(testcase, ctrls_check_acl_test);
2115 tcase_add_test(testcase, ctrls_parse_acl_test);
2116 tcase_add_test(testcase, ctrls_set_group_acl_test);
2117 tcase_add_test(testcase, ctrls_set_user_acl_test);
2118 tcase_add_test(testcase, ctrls_set_module_acls_test);
2119 tcase_add_test(testcase, ctrls_set_module_acls2_test);
2120 tcase_add_test(testcase, ctrls_unregister_module_actions_test);
2121 tcase_add_test(testcase, ctrls_unregister_module_actions2_test);
2122 tcase_add_test(testcase, ctrls_set_logfd_test);
2123 tcase_add_test(testcase, ctrls_log_test);
2124 #endif /* PR_USE_CTRLS */
2125
2126 suite_add_tcase(suite, testcase);
2127 return suite;
2128 }