Debian Codegraph @

tests/api/scoreboard.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2020 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 /* Scoreboard API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static const char *test_dir = "/tmp/prt-scoreboard/";
32 static const char *test_file = "/tmp/prt-scoreboard/test.dat";
33 static const char *test_mutex = "/tmp/prt-scoreboard/test.dat.lck";
34 static const char *test_file2 = "/tmp/prt-scoreboard-mutex.dat";
35
36 static void set_up(void) {
37 (void) unlink(test_file);
38 (void) unlink(test_file2);
39 (void) unlink(test_mutex);
40 (void) rmdir(test_dir);
41
42 if (p == NULL) {
43 p = permanent_pool = make_sub_pool(NULL);
44 }
45
46 ServerType = SERVER_STANDALONE;
47 init_netaddr();
48
49 if (getenv("TEST_VERBOSE") != NULL) {
50 pr_trace_set_levels("lock", 1, 20);
51 pr_trace_set_levels("scoreboard", 1, 20);
52 }
53 }
54
55 static void tear_down(void) {
56 (void) unlink(test_file);
57 (void) unlink(test_file2);
58 (void) unlink(test_mutex);
59 (void) rmdir(test_dir);
60
61 if (getenv("TEST_VERBOSE") != NULL) {
62 pr_trace_set_levels("lock", 0, 0);
63 pr_trace_set_levels("scoreboard", 0, 0);
64 }
65
66 if (p) {
67 destroy_pool(p);
68 p = permanent_pool = NULL;
69 }
70 }
71
72 START_TEST (scoreboard_get_test) {
73 const char *ok, *res;
74
75 ok = PR_RUN_DIR "/proftpd.scoreboard";
76
77 res = pr_get_scoreboard();
78 ck_assert_msg(res != NULL, "Failed to get scoreboard path: %s",
79 strerror(errno));
80 ck_assert_msg(strcmp(res, ok) == 0,
81 "Expected scoreboard path '%s', got '%s'", ok, res);
82
83 ok = PR_RUN_DIR "/proftpd.scoreboard.lck";
84
85 res = pr_get_scoreboard_mutex();
86 ck_assert_msg(res != NULL, "Failed to get scoreboard mutex path: %s",
87 strerror(errno));
88 ck_assert_msg(strcmp(res, ok) == 0,
89 "Expected scoreboard mutex path '%s', got '%s'", ok, res);
90 }
91 END_TEST
92
93 START_TEST (scoreboard_set_test) {
94 int fd, res;
95 const char *path;
96
97 mark_point();
98 res = pr_set_scoreboard(NULL);
99 ck_assert_msg(res < 0, "Failed to handle null argument");
100 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
101
102 mark_point();
103 res = pr_set_scoreboard("foo");
104 ck_assert_msg(res < 0, "Failed to handle non-path argument");
105 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
106 errno);
107
108 mark_point();
109 res = pr_set_scoreboard("foo/");
110 ck_assert_msg(res < 0, "Failed to handle relative path argument");
111 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
112 errno);
113
114 mark_point();
115 res = pr_set_scoreboard("/foo");
116 ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
117 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
118 errno);
119
120 mark_point();
121 res = pr_set_scoreboard("/tmp");
122 ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
123 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
124 errno);
125
126 mark_point();
127 res = pr_set_scoreboard("/tmp/");
128 ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
129 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
130 errno);
131
132 mark_point();
133 res = pr_set_scoreboard("/tmp/foo/bar");
134 ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
135 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
136 strerror(errno), errno);
137
138 res = mkdir(test_dir, 0777);
139 ck_assert_msg(res == 0,
140 "Failed to create tmp directory '%s': %s", test_dir, strerror(errno));
141 res = chmod(test_dir, 0777);
142 ck_assert_msg(res == 0,
143 "Failed to create set 0777 perms on '%s': %s", test_dir, strerror(errno));
144
145 mark_point();
146 fd = open("/tmp/foo", O_CREAT|O_RDONLY, 0400);
147 if (fd < 0) {
148 return;
149 }
150 (void) close(fd);
151
152 res = pr_set_scoreboard("/tmp/foo/bar");
153 ck_assert_msg(res < 0, "Failed to handle non-directory path argument");
154 ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
155 strerror(errno), errno);
156 (void) unlink("/tmp/foo");
157
158 mark_point();
159 res = pr_set_scoreboard(test_dir);
160 ck_assert_msg(res < 0, "Failed to handle nonexistent file argument");
161 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
162
163 mark_point();
164 res = pr_set_scoreboard("/tmp/prt-scoreboard/bar");
165 ck_assert_msg(res < 0, "Failed to handle world-writable path argument");
166 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
167
168 res = chmod(test_dir, 0775);
169 ck_assert_msg(res == 0, "Failed to set 0775 perms on '%s': %s", test_dir,
170 strerror(errno));
171
172 mark_point();
173 res = pr_set_scoreboard("/tmp/prt-scoreboard/bar");
174 ck_assert_msg(res == 0, "Failed to set scoreboard: %s", strerror(errno));
175 (void) rmdir(test_dir);
176
177 path = pr_get_scoreboard();
178 ck_assert_msg(path != NULL, "Failed to get scoreboard path: %s",
179 strerror(errno));
180 ck_assert_msg(strcmp("/tmp/prt-scoreboard/bar", path) == 0,
181 "Expected '%s', got '%s'", "/tmp/prt-scoreboard/bar", path);
182
183 (void) rmdir(test_dir);
184 }
185 END_TEST
186
187 START_TEST (scoreboard_set_mutex_test) {
188 int res;
189 const char *path;
190
191 res = pr_set_scoreboard_mutex(NULL);
192 ck_assert_msg(res == -1, "Failed to handle null argument");
193 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
194
195 res = pr_set_scoreboard_mutex("/tmp");
196 ck_assert_msg(res == 0, "Failed to set scoreboard mutex: %s",
197 strerror(errno));
198
199 path = pr_get_scoreboard_mutex();
200 ck_assert_msg(path != NULL, "Failed to get scoreboard mutex path: %s",
201 strerror(errno));
202 ck_assert_msg(strcmp("/tmp", path) == 0,
203 "Expected '%s', got '%s'", "/tmp", path);
204 }
205 END_TEST
206
207 START_TEST (scoreboard_open_close_test) {
208 int res;
209 const char *symlink_path = "/tmp/prt-scoreboard/symlink";
210
211 res = mkdir(test_dir, 0775);
212 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
213 strerror(errno));
214
215 res = chmod(test_dir, 0775);
216 ck_assert_msg(res == 0, "Failed to set perms on '%s': %s", test_dir,
217 strerror(errno));
218
219 res = pr_set_scoreboard(test_file);
220 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
221 strerror(errno));
222
223 if (symlink(symlink_path, test_file) == 0) {
224
225 res = pr_open_scoreboard(O_RDWR);
226 if (res == 0) {
227 (void) unlink(symlink_path);
228
229 ck_abort_msg("Unexpectedly opened symlink scoreboard");
230 }
231
232 if (errno != EPERM) {
233 int xerrno = errno;
234
235 (void) unlink(symlink_path);
236
237 ck_abort_msg("Failed to set errno to EPERM (got %d)", xerrno);
238 }
239
240 (void) unlink(symlink_path);
241
242 res = pr_set_scoreboard(test_file);
243 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
244 strerror(errno));
245 }
246
247 res = pr_open_scoreboard(O_RDONLY);
248 ck_assert_msg(res < 0, "Unexpectedly opened scoreboard using O_RDONLY");
249
250 if (errno != EINVAL) {
251 int xerrno = errno;
252
253 (void) unlink(symlink_path);
254
255 ck_abort_msg("Failed to set errno to EINVAL (got %d)", xerrno);
256 }
257
258 (void) unlink(test_mutex);
259 (void) unlink(test_file);
260 res = pr_open_scoreboard(O_RDWR);
261 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
262
263 /* Try opening the scoreboard again; it should be OK, since we are the
264 * opener.
265 */
266 res = pr_open_scoreboard(O_RDWR);
267 ck_assert_msg(res == 0, "Failed to open scoreboard again: %s", strerror(errno));
268
269 /* Now that we have a scoreboard, try opening it again using O_RDONLY. */
270 mark_point();
271 pr_close_scoreboard(FALSE);
272
273 res = pr_open_scoreboard(O_RDONLY);
274 ck_assert_msg(res < 0, "Unexpectedly opened scoreboard using O_RDONLY");
275 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
276 strerror(errno), errno);
277
278 mark_point();
279 res = pr_open_scoreboard(O_RDWR);
280 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
281
282 res = pr_close_scoreboard(FALSE);
283 ck_assert_msg(res == 0, "Failed to close scoreboard: %s", strerror(errno));
284
285 /* Close the already-closed scoreboard, to assert that it is a no-op. */
286 mark_point();
287 res = pr_close_scoreboard(FALSE);
288 ck_assert_msg(res == 0, "Failed to close scoreboard: %s", strerror(errno));
289
290 (void) unlink(test_mutex);
291 (void) unlink(test_file);
292 (void) rmdir(test_dir);
293 }
294 END_TEST
295
296 START_TEST (scoreboard_open_invalid_header_test) {
297 int fd, res;
298 pr_scoreboard_header_t sch;
299
300 res = mkdir(test_dir, 0775);
301 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
302 strerror(errno));
303
304 res = chmod(test_dir, 0775);
305 ck_assert_msg(res == 0, "Failed to set perms on '%s': %s", test_dir,
306 strerror(errno));
307
308 (void) unlink(test_file);
309 res = pr_set_scoreboard(test_file);
310 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
311 strerror(errno));
312
313 /* Bad magic */
314 mark_point();
315 fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
316 if (fd < 0) {
317 return;
318 }
319
320 memset(&sch, 0, sizeof(sch));
321 (void) write(fd, &sch, sizeof(sch));
322 (void) close(fd);
323 res = pr_open_scoreboard(O_RDWR);
324 ck_assert_msg(res == PR_SCORE_ERR_BAD_MAGIC,
325 "Failed to handle invalid header magic; expected %d, got %d",
326 PR_SCORE_ERR_BAD_MAGIC, res);
327 (void) unlink(test_file);
328
329 /* Version too old */
330 mark_point();
331 fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
332 if (fd < 0) {
333 return;
334 }
335
336 memset(&sch, 0, sizeof(sch));
337 sch.sch_magic = PR_SCOREBOARD_MAGIC;
338 sch.sch_version = 1;
339 (void) write(fd, &sch, sizeof(sch));
340 (void) close(fd);
341 res = pr_open_scoreboard(O_RDWR);
342 ck_assert_msg(res == PR_SCORE_ERR_OLDER_VERSION,
343 "Failed to handle too-old header version; expected %d, got %d",
344 PR_SCORE_ERR_OLDER_VERSION, res);
345 (void) unlink(test_file);
346
347 /* Version too new */
348 mark_point();
349 fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
350 if (fd < 0) {
351 return;
352 }
353
354 memset(&sch, 0, sizeof(sch));
355 sch.sch_magic = PR_SCOREBOARD_MAGIC;
356 sch.sch_version = PR_SCOREBOARD_VERSION + 1;
357 (void) write(fd, &sch, sizeof(sch));
358 (void) close(fd);
359 res = pr_open_scoreboard(O_RDWR);
360 ck_assert_msg(res == PR_SCORE_ERR_NEWER_VERSION,
361 "Failed to handle too-new header version; expected %d, got %d",
362 PR_SCORE_ERR_NEWER_VERSION, res);
363 (void) unlink(test_file);
364
365 (void) unlink(test_mutex);
366 (void) rmdir(test_dir);
367 }
368 END_TEST
369
370 START_TEST (scoreboard_lock_test) {
371 int fd = -1, lock_type = -1, res;
372
373 res = pr_lock_scoreboard(fd, lock_type);
374 ck_assert_msg(res < 0, "Failed to handle bad lock type");
375 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
376 strerror(errno), errno);
377
378 lock_type = F_RDLCK;
379 res = pr_lock_scoreboard(fd, lock_type);
380 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
381 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
382 strerror(errno), errno);
383
384 fd = open(test_file2, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
385 ck_assert_msg(fd >= 0, "Failed to open '%s': %s", test_file2, strerror(errno));
386
387 res = pr_lock_scoreboard(fd, lock_type);
388 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
389
390 lock_type = F_WRLCK;
391 res = pr_lock_scoreboard(fd, lock_type);
392 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
393
394 lock_type = F_UNLCK;
395 res = pr_lock_scoreboard(fd, lock_type);
396 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
397
398 lock_type = F_WRLCK;
399 res = pr_lock_scoreboard(fd, lock_type);
400 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
401
402 /* Note: apparently attempt to lock (again) a file on which a lock
403 * (of the same type) is already held will succeed. Huh.
404 */
405 res = pr_lock_scoreboard(fd, lock_type);
406 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
407
408 lock_type = F_RDLCK;
409 res = pr_lock_scoreboard(fd, lock_type);
410 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
411
412 lock_type = F_UNLCK;
413 res = pr_lock_scoreboard(fd, lock_type);
414 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
415
416 (void) unlink(test_file2);
417 }
418 END_TEST
419
420 START_TEST (scoreboard_delete_test) {
421 int res;
422 struct stat st;
423
424 res = mkdir(test_dir, 0775);
425 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
426 strerror(errno));
427
428 res = chmod(test_dir, 0775);
429 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
430 strerror(errno));
431
432 res = pr_set_scoreboard(test_file);
433 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
434 strerror(errno));
435
436 res = pr_open_scoreboard(O_RDWR);
437 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
438
439 res = stat(pr_get_scoreboard(), &st);
440 ck_assert_msg(res == 0, "Failed to stat scoreboard: %s", strerror(errno));
441
442 pr_delete_scoreboard();
443
444 res = stat(pr_get_scoreboard(), &st);
445 ck_assert_msg(res < 0, "Unexpectedly found deleted scoreboard");
446
447 res = stat(pr_get_scoreboard_mutex(), &st);
448 ck_assert_msg(res < 0, "Unexpectedly found deleted scoreboard mutex");
449
450 (void) unlink(test_mutex);
451 (void) unlink(test_file);
452 (void) rmdir(test_dir);
453 }
454 END_TEST
455
456 START_TEST (scoreboard_restore_test) {
457 int res;
458 const char *path;
459
460 res = mkdir(test_dir, 0775);
461 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
462 strerror(errno));
463
464 res = chmod(test_dir, 0775);
465 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
466 strerror(errno));
467
468 res = pr_set_scoreboard(test_file);
469 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
470 strerror(errno));
471
472 mark_point();
473 res = pr_restore_scoreboard();
474 ck_assert_msg(res < 0, "Unexpectedly restored scoreboard");
475 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
476 strerror(errno), errno);
477
478 res = pr_open_scoreboard(O_RDWR);
479 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
480
481 mark_point();
482 res = pr_restore_scoreboard();
483 ck_assert_msg(res < 0,
484 "Restoring scoreboard before rewind succeeded unexpectedly");
485
486 mark_point();
487 res = pr_rewind_scoreboard();
488 ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));
489 res = pr_restore_scoreboard();
490 ck_assert_msg(res == 0, "Failed to restore scoreboard: %s", strerror(errno));
491
492 mark_point();
493 path = pr_get_scoreboard();
494 res = pr_set_scoreboard("none");
495 ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
496 res = pr_restore_scoreboard();
497 ck_assert_msg(res == 0, "Failed to restore scoreboard: %s", strerror(errno));
498 res = pr_set_scoreboard(path);
499 ck_assert_msg(res == 0, "Failed to enable scoreboarding: %s", strerror(errno));
500
501 (void) unlink(test_mutex);
502 (void) unlink(test_file);
503 (void) rmdir(test_dir);
504 }
505 END_TEST
506
507 START_TEST (scoreboard_rewind_test) {
508 int res;
509 const char *path;
510
511 res = mkdir(test_dir, 0775);
512 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
513 strerror(errno));
514
515 res = chmod(test_dir, 0775);
516 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
517 strerror(errno));
518
519 mark_point();
520 res = pr_set_scoreboard(test_file);
521 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
522 strerror(errno));
523
524 res = pr_rewind_scoreboard();
525 ck_assert_msg(res < 0, "Unexpectedly rewound scoreboard");
526 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
527 strerror(errno), errno);
528
529 mark_point();
530 res = pr_open_scoreboard(O_RDWR);
531 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
532 res = pr_rewind_scoreboard();
533 ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));
534
535 mark_point();
536 path = pr_get_scoreboard();
537 res = pr_set_scoreboard("none");
538 ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
539 res = pr_rewind_scoreboard();
540 ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));
541 res = pr_set_scoreboard(path);
542 ck_assert_msg(res == 0, "Failed to enable scoreboarding: %s", strerror(errno));
543
544 (void) unlink(test_mutex);
545 (void) unlink(test_file);
546 (void) rmdir(test_dir);
547 }
548 END_TEST
549
550 START_TEST (scoreboard_scrub_test) {
551 uid_t euid;
552 int res;
553
554 res = mkdir(test_dir, 0775);
555 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
556 strerror(errno));
557
558 res = chmod(test_dir, 0775);
559 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
560 strerror(errno));
561
562 res = pr_set_scoreboard(test_file);
563 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
564 strerror(errno));
565
566 res = pr_scoreboard_scrub();
567 ck_assert_msg(res < 0, "Unexpectedly scrubbed scoreboard");
568
569 euid = geteuid();
570 if (euid != 0) {
571 if (errno != EPERM &&
572 errno != ENOENT) {
573 ck_abort_msg("Failed to set errno to EPERM/ENOENT, got %d [%s] (euid = %lu)",
574 errno, strerror(errno), (unsigned long) euid);
575 }
576
577 } else {
578 if (errno != ENOENT) {
579 ck_abort_msg("Failed to set errno to ENOENT, got %d [%s] (euid = %lu)", errno,
580 strerror(errno), (unsigned long) euid);
581 }
582 }
583
584 res = pr_open_scoreboard(O_RDWR);
585 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
586
587 res = pr_scoreboard_scrub();
588 ck_assert_msg(res == 0, "Failed to scrub scoreboard: %s", strerror(errno));
589
590 (void) unlink(test_mutex);
591 (void) unlink(test_file);
592 (void) rmdir(test_dir);
593 }
594 END_TEST
595
596 START_TEST (scoreboard_get_daemon_pid_test) {
597 int res;
598 pid_t daemon_pid;
599
600 res = mkdir(test_dir, 0775);
601 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
602 strerror(errno));
603
604 res = chmod(test_dir, 0775);
605 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
606 strerror(errno));
607
608 res = pr_set_scoreboard(test_file);
609 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
610 strerror(errno));
611
612 res = pr_open_scoreboard(O_RDWR);
613 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
614
615 daemon_pid = pr_scoreboard_get_daemon_pid();
616 if (daemon_pid != getpid()) {
617 ck_abort_msg("Expected %lu, got %lu", (unsigned long) getpid(),
618 (unsigned long) daemon_pid);
619 }
620
621 pr_delete_scoreboard();
622
623 ServerType = SERVER_INETD;
624
625 res = pr_open_scoreboard(O_RDWR);
626 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
627
628 daemon_pid = pr_scoreboard_get_daemon_pid();
629 if (daemon_pid != 0) {
630 ck_abort_msg("Expected %lu, got %lu", (unsigned long) 0,
631 (unsigned long) daemon_pid);
632 }
633
634 (void) unlink(test_mutex);
635 (void) unlink(test_file);
636 (void) rmdir(test_dir);
637 }
638 END_TEST
639
640 START_TEST (scoreboard_get_daemon_uptime_test) {
641 int res;
642 time_t daemon_uptime, now;
643
644 res = mkdir(test_dir, 0775);
645 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
646 strerror(errno));
647
648 res = chmod(test_dir, 0775);
649 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
650 strerror(errno));
651
652 res = pr_set_scoreboard(test_file);
653 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
654 strerror(errno));
655
656 res = pr_open_scoreboard(O_RDWR);
657 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
658
659 daemon_uptime = pr_scoreboard_get_daemon_uptime();
660 now = time(NULL);
661
662 if (daemon_uptime > now) {
663 ck_abort_msg("Expected %lu, got %lu", (unsigned long) now,
664 (unsigned long) daemon_uptime);
665 }
666
667 pr_delete_scoreboard();
668
669 ServerType = SERVER_INETD;
670
671 res = pr_open_scoreboard(O_RDWR);
672 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
673
674 daemon_uptime = pr_scoreboard_get_daemon_uptime();
675 if (daemon_uptime != 0) {
676 ck_abort_msg("Expected %lu, got %lu", (unsigned long) 0,
677 (unsigned long) daemon_uptime);
678 }
679
680 (void) unlink(test_mutex);
681 (void) unlink(test_file);
682 (void) rmdir(test_dir);
683 }
684 END_TEST
685
686 START_TEST (scoreboard_entry_add_test) {
687 int res;
688
689 res = mkdir(test_dir, 0775);
690 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
691 strerror(errno));
692
693 res = chmod(test_dir, 0775);
694 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
695 strerror(errno));
696
697 res = pr_set_scoreboard(test_file);
698 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
699 strerror(errno));
700
701 res = pr_scoreboard_entry_add();
702 ck_assert_msg(res < 0, "Unexpectedly added entry to scoreboard");
703 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
704 strerror(errno), errno);
705
706 res = pr_open_scoreboard(O_RDWR);
707 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
708
709 res = pr_scoreboard_entry_add();
710 ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
711 strerror(errno));
712
713 res = pr_scoreboard_entry_add();
714 ck_assert_msg(res < 0, "Unexpectedly added entry to scoreboard");
715 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
716 strerror(errno), errno);
717
718 (void) unlink(test_mutex);
719 (void) unlink(test_file);
720 (void) rmdir(test_dir);
721 }
722 END_TEST
723
724 START_TEST (scoreboard_entry_del_test) {
725 int res;
726
727 res = mkdir(test_dir, 0775);
728 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
729 strerror(errno));
730
731 res = chmod(test_dir, 0775);
732 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
733 strerror(errno));
734
735 res = pr_set_scoreboard(test_file);
736 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
737 strerror(errno));
738
739 res = pr_scoreboard_entry_del(FALSE);
740 ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
741 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
742 strerror(errno), errno);
743
744 res = pr_open_scoreboard(O_RDWR);
745 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
746
747 res = pr_scoreboard_entry_del(FALSE);
748 ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
749 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
750 strerror(errno), errno);
751
752 res = pr_scoreboard_entry_add();
753 ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
754 strerror(errno));
755
756 res = pr_scoreboard_entry_del(FALSE);
757 ck_assert_msg(res == 0, "Failed to delete entry from scoreboard: %s",
758 strerror(errno));
759
760 res = pr_scoreboard_entry_del(FALSE);
761 ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
762 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
763 strerror(errno), errno);
764
765 (void) unlink(test_mutex);
766 (void) unlink(test_file);
767 (void) rmdir(test_dir);
768 }
769 END_TEST
770
771 START_TEST (scoreboard_entry_read_test) {
772 int res;
773 pr_scoreboard_entry_t *score;
774
775 res = mkdir(test_dir, 0775);
776 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
777 strerror(errno));
778
779 res = chmod(test_dir, 0775);
780 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
781 strerror(errno));
782
783 res = pr_set_scoreboard(test_file);
784 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
785 strerror(errno));
786
787 score = pr_scoreboard_entry_read();
788 ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");
789 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
790 strerror(errno), errno);
791
792 res = pr_open_scoreboard(O_RDWR);
793 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
794
795 /* We expect NULL here because the scoreboard file should be empty. */
796 score = pr_scoreboard_entry_read();
797 ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");
798
799 res = pr_scoreboard_entry_add();
800 ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
801 strerror(errno));
802
803 score = pr_scoreboard_entry_read();
804 ck_assert_msg(score != NULL, "Failed to read scoreboard entry: %s",
805 strerror(errno));
806
807 if (score->sce_pid != getpid()) {
808 ck_abort_msg("Failed to read expected scoreboard entry (expected PID %lu, got %lu)",
809 (unsigned long) getpid(), (unsigned long) score->sce_pid);
810 }
811
812 score = pr_scoreboard_entry_read();
813 ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");
814
815 (void) unlink(test_mutex);
816 (void) unlink(test_file);
817 (void) rmdir(test_dir);
818 }
819 END_TEST
820
821 START_TEST (scoreboard_entry_get_test) {
822 register unsigned int i;
823 int res;
824 const char *val;
825 int scoreboard_fields[] = {
826 PR_SCORE_USER,
827 PR_SCORE_CLIENT_ADDR,
828 PR_SCORE_CLIENT_NAME,
829 PR_SCORE_CLASS,
830 PR_SCORE_CWD,
831 PR_SCORE_CMD,
832 PR_SCORE_CMD_ARG,
833 PR_SCORE_PROTOCOL,
834 -1
835 };
836
837 res = mkdir(test_dir, 0775);
838 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
839 strerror(errno));
840
841 res = chmod(test_dir, 0775);
842 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
843 strerror(errno));
844
845 res = pr_set_scoreboard(test_file);
846 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
847 strerror(errno));
848
849 val = pr_scoreboard_entry_get(-1);
850 ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
851 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
852 strerror(errno), errno);
853
854 res = pr_open_scoreboard(O_RDWR);
855 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
856
857 val = pr_scoreboard_entry_get(-1);
858 ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
859 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
860 strerror(errno), errno);
861
862 res = pr_scoreboard_entry_add();
863 ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
864 strerror(errno));
865
866 val = pr_scoreboard_entry_get(-1);
867 ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
868 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
869 strerror(errno), errno);
870
871 for (i = 0; scoreboard_fields[i] != -1; i++) {
872 val = pr_scoreboard_entry_get(scoreboard_fields[i]);
873 ck_assert_msg(val != NULL, "Failed to read scoreboard field %d: %s",
874 scoreboard_fields[i], strerror(errno));
875 }
876
877 (void) unlink(test_mutex);
878 (void) unlink(test_file);
879 (void) rmdir(test_dir);
880 }
881 END_TEST
882
883 START_TEST (scoreboard_entry_update_test) {
884 int num, res;
885 const char *val;
886 pid_t pid = getpid();
887 const pr_netaddr_t *addr;
888 time_t now;
889 off_t len;
890 unsigned long elapsed;
891
892 res = mkdir(test_dir, 0775);
893 ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
894 strerror(errno));
895
896 res = chmod(test_dir, 0775);
897 ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
898 strerror(errno));
899
900 res = pr_set_scoreboard(test_file);
901 ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
902 strerror(errno));
903
904 res = pr_scoreboard_entry_update(pid, 0);
905 ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
906 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
907 strerror(errno), errno);
908
909 res = pr_open_scoreboard(O_RDWR);
910 ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
911
912 res = pr_scoreboard_entry_update(pid, 0);
913 ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
914 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
915 strerror(errno), errno);
916
917 res = pr_scoreboard_entry_add();
918 ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
919 strerror(errno));
920
921 res = pr_scoreboard_entry_update(pid, -1);
922 ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
923 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
924 strerror(errno), errno);
925
926 val = "cwd";
927 res = pr_scoreboard_entry_update(pid, PR_SCORE_CWD, val, NULL);
928 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CWD: %s", strerror(errno));
929
930 val = pr_scoreboard_entry_get(PR_SCORE_CWD);
931 ck_assert_msg(val != NULL, "Failed to get entry PR_SCORE_CWD: %s",
932 strerror(errno));
933 ck_assert_msg(strcmp(val, "cwd") == 0, "Expected 'cwd', got '%s'", val);
934
935 val = "user";
936 res = pr_scoreboard_entry_update(pid, PR_SCORE_USER, val, NULL);
937 ck_assert_msg(res == 0, "Failed to update PR_SCORE_USER: %s", strerror(errno));
938
939 addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
940 ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
941 strerror(errno));
942
943 res = pr_scoreboard_entry_update(pid, PR_SCORE_CLIENT_ADDR, addr, NULL);
944 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLIENT_ADDR: %s",
945 strerror(errno));
946
947 val = "remote_name";
948 res = pr_scoreboard_entry_update(pid, PR_SCORE_CLIENT_NAME, val, NULL);
949 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLIENT_NAME: %s",
950 strerror(errno));
951
952 val = "session_class";
953 res = pr_scoreboard_entry_update(pid, PR_SCORE_CLASS, val, NULL);
954 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLASS: %s", strerror(errno));
955
956 val = "USER";
957 res = pr_scoreboard_entry_update(pid, PR_SCORE_CMD, "%s", val, NULL, NULL);
958 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CMD: %s", strerror(errno));
959
960 val = "foo bar";
961 res = pr_scoreboard_entry_update(pid, PR_SCORE_CMD_ARG, "%s", val, NULL,
962 NULL);
963 ck_assert_msg(res == 0, "Failed to update PR_SCORE_CMD_ARG: %s",
964 strerror(errno));
965
966 num = 77;
967 res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_PORT, num, NULL);
968 ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_PORT: %s",
969 strerror(errno));
970
971 res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_ADDR, addr, num, NULL);
972 ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_ADDR: %s",
973 strerror(errno));
974
975 val = "label";
976 res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_LABEL, val, NULL);
977 ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_LABEL: %s",
978 strerror(errno));
979
980 now = 1;
981 res = pr_scoreboard_entry_update(pid, PR_SCORE_BEGIN_IDLE, now, NULL);
982 ck_assert_msg(res == 0, "Failed to update PR_SCORE_BEGIN_IDLE: %s",
983 strerror(errno));
984
985 now = 2;
986 res = pr_scoreboard_entry_update(pid, PR_SCORE_BEGIN_SESSION, now, NULL);
987 ck_assert_msg(res == 0, "Failed to update PR_SCORE_BEGIN_SESSION: %s",
988 strerror(errno));
989
990 len = 7;
991 res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_DONE, len, NULL);
992 ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_DONE: %s",
993 strerror(errno));
994
995 len = 8;
996 res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_SIZE, len, NULL);
997 ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_SIZE: %s",
998 strerror(errno));
999
1000 len = 9;
1001 res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_LEN, len, NULL);
1002 ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_LEN: %s",
1003 strerror(errno));
1004
1005 elapsed = 1;
1006 res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_ELAPSED, elapsed, NULL);
1007 ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_ELAPSED: %s",
1008 strerror(errno));
1009
1010 val = "protocol";
1011 res = pr_scoreboard_entry_update(pid, PR_SCORE_PROTOCOL, val, NULL);
1012 ck_assert_msg(res == 0, "Failed to update PR_SCORE_PROTOCOL: %s",
1013 strerror(errno));
1014
1015 (void) unlink(test_mutex);
1016 (void) unlink(test_file);
1017 (void) rmdir(test_dir);
1018 }
1019 END_TEST
1020
1021 START_TEST (scoreboard_entry_kill_test) {
1022 int res;
1023 pr_scoreboard_entry_t sce;
1024
1025 mark_point();
1026 res = pr_scoreboard_entry_kill(NULL, 0);
1027 ck_assert_msg(res < 0, "Failed to handle null arguments");
1028 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1029 strerror(errno), errno);
1030
1031 mark_point();
1032 sce.sce_pid = 1;
1033 res = pr_scoreboard_entry_kill(&sce, 0);
1034 ck_assert_msg(res < 0, "Failed to handle bad PID");
1035 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1036 strerror(errno), errno);
1037
1038 mark_point();
1039 sce.sce_pid = getpid();
1040 res = pr_scoreboard_entry_kill(&sce, 0);
1041 ck_assert_msg(res == 0, "Failed to send signal 0 to PID %lu: %s",
1042 (unsigned long) sce.sce_pid, strerror(errno));
1043
1044 mark_point();
1045 res = pr_set_scoreboard("none");
1046 ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
1047 sce.sce_pid = getpid();
1048 res = pr_scoreboard_entry_kill(&sce, 0);
1049 ck_assert_msg(res == 0, "Failed to send signal: %s", strerror(errno));
1050 }
1051 END_TEST
1052
1053 START_TEST (scoreboard_entry_lock_test) {
1054 int fd = -1, lock_type = -1, res;
1055
1056 res = pr_scoreboard_entry_lock(fd, lock_type);
1057 ck_assert_msg(res < 0, "Failed to handle bad lock type");
1058 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1059 strerror(errno), errno);
1060
1061 lock_type = F_RDLCK;
1062 res = pr_scoreboard_entry_lock(fd, lock_type);
1063 ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
1064 ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
1065 strerror(errno), errno);
1066
1067 fd = open(test_file2, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
1068 ck_assert_msg(fd >= 0, "Failed to open '%s': %s", test_file2, strerror(errno));
1069
1070 res = pr_scoreboard_entry_lock(fd, lock_type);
1071 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1072
1073 lock_type = F_WRLCK;
1074 res = pr_scoreboard_entry_lock(fd, lock_type);
1075 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1076
1077 lock_type = F_UNLCK;
1078 res = pr_scoreboard_entry_lock(fd, lock_type);
1079 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1080
1081 lock_type = F_WRLCK;
1082 res = pr_scoreboard_entry_lock(fd, lock_type);
1083 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1084
1085 /* Note: apparently attempt to lock (again) a file on which a lock
1086 * (of the same type) is already held will succeed. Huh.
1087 */
1088 res = pr_scoreboard_entry_lock(fd, lock_type);
1089 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1090
1091 lock_type = F_RDLCK;
1092 res = pr_scoreboard_entry_lock(fd, lock_type);
1093 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1094
1095 lock_type = F_UNLCK;
1096 res = pr_scoreboard_entry_lock(fd, lock_type);
1097 ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));
1098
1099 (void) unlink(test_file2);
1100 }
1101 END_TEST
1102
1103 START_TEST (scoreboard_disabled_test) {
1104 register unsigned int i = 0;
1105 const char *paths[4] = {
1106 "/dev/null",
1107 "none",
1108 "off",
1109 NULL
1110 };
1111 const char *path;
1112
1113 for (path = paths[i]; path != NULL; path = paths[i++]) {
1114 int res;
1115 const char *field, *ok;
1116 pid_t scoreboard_pid;
1117 time_t scoreboard_uptime;
1118 pr_scoreboard_entry_t *score;
1119
1120 res = pr_set_scoreboard(path);
1121 ck_assert_msg(res == 0, "Failed set to scoreboard to '%s': %s", path,
1122 strerror(errno));
1123
1124 ok = PR_RUN_DIR "/proftpd.scoreboard";
1125
1126 path = pr_get_scoreboard();
1127 ck_assert_msg(path != NULL, "Failed to get scoreboard path: %s",
1128 strerror(errno));
1129 ck_assert_msg(strcmp(path, ok) == 0,
1130 "Expected path '%s', got '%s'", ok, path);
1131
1132 res = pr_open_scoreboard(O_RDONLY);
1133 ck_assert_msg(res == 0, "Failed to open '%s' scoreboard: %s", path,
1134 strerror(errno));
1135
1136 res = pr_scoreboard_scrub();
1137 ck_assert_msg(res == 0, "Failed to scrub '%s' scoreboard: %s", path,
1138 strerror(errno));
1139
1140 scoreboard_pid = pr_scoreboard_get_daemon_pid();
1141 ck_assert_msg(scoreboard_pid == 0,
1142 "Expected to get scoreboard PID 0, got %lu",
1143 (unsigned long) scoreboard_pid);
1144
1145 scoreboard_uptime = pr_scoreboard_get_daemon_uptime();
1146 ck_assert_msg(scoreboard_uptime == 0,
1147 "Expected to get scoreboard uptime 0, got %lu",
1148 (unsigned long) scoreboard_uptime);
1149
1150 res = pr_scoreboard_entry_add();
1151 ck_assert_msg(res == 0, "Failed to add entry to '%s' scoreboard: %s", path,
1152 strerror(errno));
1153
1154 score = pr_scoreboard_entry_read();
1155 ck_assert_msg(score == NULL, "Expected null entry");
1156
1157 field = pr_scoreboard_entry_get(PR_SCORE_CMD_ARG);
1158 ck_assert_msg(field == NULL, "Expected null CMD_ARG field");
1159
1160 res = pr_scoreboard_entry_update(getpid(), PR_SCORE_CWD, "foo", NULL);
1161 ck_assert_msg(res == 0, "Failed to update CWD field: %s", strerror(errno));
1162
1163 res = pr_scoreboard_entry_del(FALSE);
1164 ck_assert_msg(res == 0, "Failed to delete entry from '%s' scoreboard: %s",
1165 path, strerror(errno));
1166
1167 res = pr_close_scoreboard(FALSE);
1168 ck_assert_msg(res == 0, "Failed to close '%s' scoreboard: %s", path,
1169 strerror(errno));
1170
1171 /* Internal hack: even calling pr_set_scoreboard() with a NULL
1172 * argument will set the Scoreboard API internal flag back to true.
1173 */
1174 pr_set_scoreboard(NULL);
1175 }
1176 }
1177 END_TEST
1178
1179 Suite *tests_get_scoreboard_suite(void) {
1180 Suite *suite;
1181 TCase *testcase;
1182
1183 suite = suite_create("scoreboard");
1184
1185 testcase = tcase_create("base");
1186 tcase_add_checked_fixture(testcase, set_up, tear_down);
1187
1188 tcase_add_test(testcase, scoreboard_get_test);
1189 tcase_add_test(testcase, scoreboard_set_test);
1190 tcase_add_test(testcase, scoreboard_set_mutex_test);
1191 tcase_add_test(testcase, scoreboard_open_close_test);
1192 tcase_add_test(testcase, scoreboard_open_invalid_header_test);
1193 tcase_add_test(testcase, scoreboard_lock_test);
1194 tcase_add_test(testcase, scoreboard_delete_test);
1195 tcase_add_test(testcase, scoreboard_restore_test);
1196 tcase_add_test(testcase, scoreboard_rewind_test);
1197 tcase_add_test(testcase, scoreboard_scrub_test);
1198 tcase_add_test(testcase, scoreboard_get_daemon_pid_test);
1199 tcase_add_test(testcase, scoreboard_get_daemon_uptime_test);
1200 tcase_add_test(testcase, scoreboard_entry_add_test);
1201 tcase_add_test(testcase, scoreboard_entry_del_test);
1202 tcase_add_test(testcase, scoreboard_entry_read_test);
1203 tcase_add_test(testcase, scoreboard_entry_get_test);
1204 tcase_add_test(testcase, scoreboard_entry_update_test);
1205 tcase_add_test(testcase, scoreboard_entry_kill_test);
1206 tcase_add_test(testcase, scoreboard_entry_lock_test);
1207 tcase_add_test(testcase, scoreboard_disabled_test);
1208
1209 suite_add_tcase(suite, testcase);
1210 return suite;
1211 }