File: scoreboard.c

package info (click to toggle)
proftpd-dfsg 1.3.8.c%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 56,576 kB
  • sloc: perl: 286,353; ansic: 241,458; sh: 16,680; php: 11,586; makefile: 1,092; xml: 93
file content (1210 lines) | stat: -rw-r--r-- 38,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
/*
 * ProFTPD - FTP server testsuite
 * Copyright (c) 2008-2020 The ProFTPD Project team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, The ProFTPD Project team and other respective
 * copyright holders give permission to link this program with OpenSSL, and
 * distribute the resulting executable, without including the source code for
 * OpenSSL in the source distribution.
 */

/* Scoreboard API tests */

#include "tests.h"

static pool *p = NULL;

static const char *test_dir = "/tmp/prt-scoreboard/";
static const char *test_file = "/tmp/prt-scoreboard/test.dat";
static const char *test_mutex = "/tmp/prt-scoreboard/test.dat.lck";
static const char *test_file2 = "/tmp/prt-scoreboard-mutex.dat";

static void set_up(void) {
  (void) unlink(test_file);
  (void) unlink(test_file2);
  (void) unlink(test_mutex);
  (void) rmdir(test_dir);

  if (p == NULL) {
    p = permanent_pool = make_sub_pool(NULL);
  }

  ServerType = SERVER_STANDALONE;
  init_netaddr();

  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("lock", 1, 20);
    pr_trace_set_levels("scoreboard", 1, 20);
  }
}

static void tear_down(void) {
  (void) unlink(test_file);
  (void) unlink(test_file2);
  (void) unlink(test_mutex);
  (void) rmdir(test_dir);

  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("lock", 0, 0);
    pr_trace_set_levels("scoreboard", 0, 0);
  }

  if (p) {
    destroy_pool(p);
    p = permanent_pool = NULL;
  } 
}

START_TEST (scoreboard_get_test) {
  const char *ok, *res;

  ok = PR_RUN_DIR "/proftpd.scoreboard";

  res = pr_get_scoreboard();
  ck_assert_msg(res != NULL, "Failed to get scoreboard path: %s",
    strerror(errno));
  ck_assert_msg(strcmp(res, ok) == 0,
    "Expected scoreboard path '%s', got '%s'", ok, res);

  ok = PR_RUN_DIR "/proftpd.scoreboard.lck";

  res = pr_get_scoreboard_mutex();
  ck_assert_msg(res != NULL, "Failed to get scoreboard mutex path: %s",
    strerror(errno));
  ck_assert_msg(strcmp(res, ok) == 0,
    "Expected scoreboard mutex path '%s', got '%s'", ok, res);
}
END_TEST

START_TEST (scoreboard_set_test) {
  int fd, res;
  const char *path;

  mark_point();
  res = pr_set_scoreboard(NULL);
  ck_assert_msg(res < 0, "Failed to handle null argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");

  mark_point();
  res = pr_set_scoreboard("foo");
  ck_assert_msg(res < 0, "Failed to handle non-path argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  mark_point();
  res = pr_set_scoreboard("foo/");
  ck_assert_msg(res < 0, "Failed to handle relative path argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  mark_point();
  res = pr_set_scoreboard("/foo");
  ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  mark_point();
  res = pr_set_scoreboard("/tmp");
  ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  mark_point();
  res = pr_set_scoreboard("/tmp/");
  ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  mark_point();
  res = pr_set_scoreboard("/tmp/foo/bar");
  ck_assert_msg(res < 0, "Failed to handle nonexistent path argument");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
    strerror(errno), errno);

  res = mkdir(test_dir, 0777);
  ck_assert_msg(res == 0,
    "Failed to create tmp directory '%s': %s", test_dir, strerror(errno));
  res = chmod(test_dir, 0777);
  ck_assert_msg(res == 0,
    "Failed to create set 0777 perms on '%s': %s", test_dir, strerror(errno));

  mark_point();
  fd = open("/tmp/foo", O_CREAT|O_RDONLY, 0400);
  if (fd < 0) {
    return;
  }
  (void) close(fd);

  res = pr_set_scoreboard("/tmp/foo/bar");
  ck_assert_msg(res < 0, "Failed to handle non-directory path argument");
  ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
    strerror(errno), errno);
  (void) unlink("/tmp/foo");

  mark_point();
  res = pr_set_scoreboard(test_dir);
  ck_assert_msg(res < 0, "Failed to handle nonexistent file argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");

  mark_point();
  res = pr_set_scoreboard("/tmp/prt-scoreboard/bar");
  ck_assert_msg(res < 0, "Failed to handle world-writable path argument");
  ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set 0775 perms on '%s': %s", test_dir,
    strerror(errno));

  mark_point();
  res = pr_set_scoreboard("/tmp/prt-scoreboard/bar");
  ck_assert_msg(res == 0, "Failed to set scoreboard: %s", strerror(errno));
  (void) rmdir(test_dir);

  path = pr_get_scoreboard();
  ck_assert_msg(path != NULL, "Failed to get scoreboard path: %s",
    strerror(errno));  
  ck_assert_msg(strcmp("/tmp/prt-scoreboard/bar", path) == 0,
    "Expected '%s', got '%s'", "/tmp/prt-scoreboard/bar", path);

  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_set_mutex_test) {
  int res;
  const char *path;

  res = pr_set_scoreboard_mutex(NULL);
  ck_assert_msg(res == -1, "Failed to handle null argument");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");

  res = pr_set_scoreboard_mutex("/tmp");
  ck_assert_msg(res == 0, "Failed to set scoreboard mutex: %s", strerror(errno));

  path = pr_get_scoreboard_mutex();
  ck_assert_msg(path != NULL, "Failed to get scoreboard mutex path: %s",
    strerror(errno));  
  ck_assert_msg(strcmp("/tmp", path) == 0,
    "Expected '%s', got '%s'", "/tmp", path);
}
END_TEST

START_TEST (scoreboard_open_close_test) {
  int res;
  const char *symlink_path = "/tmp/prt-scoreboard/symlink";

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  if (symlink(symlink_path, test_file) == 0) {

    res = pr_open_scoreboard(O_RDWR);
    if (res == 0) {
      (void) unlink(symlink_path);

      ck_abort_msg("Unexpectedly opened symlink scoreboard");
    }

    if (errno != EPERM) {
      int xerrno = errno;

      (void) unlink(symlink_path);

      ck_abort_msg("Failed to set errno to EPERM (got %d)", xerrno);
    }

    (void) unlink(symlink_path);

    res = pr_set_scoreboard(test_file);
    ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
      strerror(errno));
  }

  res = pr_open_scoreboard(O_RDONLY);
  ck_assert_msg(res < 0, "Unexpectedly opened scoreboard using O_RDONLY");

  if (errno != EINVAL) {
    int xerrno = errno;

    (void) unlink(symlink_path);

    ck_abort_msg("Failed to set errno to EINVAL (got %d)", xerrno);
  }

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  /* Try opening the scoreboard again; it should be OK, since we are the
   * opener.
   */
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard again: %s", strerror(errno));

  /* Now that we have a scoreboard, try opening it again using O_RDONLY. */
  mark_point();
  pr_close_scoreboard(FALSE);

  res = pr_open_scoreboard(O_RDONLY);
  ck_assert_msg(res < 0, "Unexpectedly opened scoreboard using O_RDONLY");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = pr_close_scoreboard(FALSE);
  ck_assert_msg(res == 0, "Failed to close scoreboard: %s", strerror(errno));

  /* Close the already-closed scoreboard, to assert that it is a no-op. */
  mark_point();
  res = pr_close_scoreboard(FALSE);
  ck_assert_msg(res == 0, "Failed to close scoreboard: %s", strerror(errno));

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_open_invalid_header_test) {
  int fd, res;
  pr_scoreboard_header_t sch;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s': %s", test_dir,
    strerror(errno));

  (void) unlink(test_file);
  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  /* Bad magic */
  mark_point();
  fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
  if (fd < 0) {
    return;
  }

  memset(&sch, 0, sizeof(sch));
  (void) write(fd, &sch, sizeof(sch));
  (void) close(fd);
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == PR_SCORE_ERR_BAD_MAGIC,
    "Failed to handle invalid header magic; expected %d, got %d",
    PR_SCORE_ERR_BAD_MAGIC, res);
  (void) unlink(test_file);

  /* Version too old */
  mark_point();
  fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
  if (fd < 0) {
    return;
  }

  memset(&sch, 0, sizeof(sch));
  sch.sch_magic = PR_SCOREBOARD_MAGIC;
  sch.sch_version = 1;
  (void) write(fd, &sch, sizeof(sch));
  (void) close(fd);
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == PR_SCORE_ERR_OLDER_VERSION,
    "Failed to handle too-old header version; expected %d, got %d",
    PR_SCORE_ERR_OLDER_VERSION, res);
  (void) unlink(test_file);

  /* Version too new */
  mark_point();
  fd = open(test_file, O_CREAT|O_RDWR, PR_SCOREBOARD_MODE);
  if (fd < 0) {
    return;
  }

  memset(&sch, 0, sizeof(sch));
  sch.sch_magic = PR_SCOREBOARD_MAGIC;
  sch.sch_version = PR_SCOREBOARD_VERSION + 1;
  (void) write(fd, &sch, sizeof(sch));
  (void) close(fd);
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == PR_SCORE_ERR_NEWER_VERSION,
    "Failed to handle too-new header version; expected %d, got %d",
    PR_SCORE_ERR_NEWER_VERSION, res);
  (void) unlink(test_file);

  (void) unlink(test_mutex);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_lock_test) {
  int fd = -1, lock_type = -1, res;

  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res < 0, "Failed to handle bad lock type");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  lock_type = F_RDLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
    strerror(errno), errno);

  fd = open(test_file2, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
  ck_assert_msg(fd >= 0, "Failed to open '%s': %s", test_file2, strerror(errno));

  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_WRLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_UNLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_WRLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  /* Note: apparently attempt to lock (again) a file on which a lock
   * (of the same type) is already held will succeed.  Huh.
   */
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_RDLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_UNLCK;
  res = pr_lock_scoreboard(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  (void) unlink(test_file2);
}
END_TEST

START_TEST (scoreboard_delete_test) {
  int res;
  struct stat st;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = stat(pr_get_scoreboard(), &st);
  ck_assert_msg(res == 0, "Failed to stat scoreboard: %s", strerror(errno));

  pr_delete_scoreboard();

  res = stat(pr_get_scoreboard(), &st);
  ck_assert_msg(res < 0, "Unexpectedly found deleted scoreboard");

  res = stat(pr_get_scoreboard_mutex(), &st);
  ck_assert_msg(res < 0, "Unexpectedly found deleted scoreboard mutex");

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_restore_test) {
  int res;
  const char *path;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  mark_point();
  res = pr_restore_scoreboard();
  ck_assert_msg(res < 0, "Unexpectedly restored scoreboard");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  mark_point();
  res = pr_restore_scoreboard();
  ck_assert_msg(res < 0,
    "Restoring scoreboard before rewind succeeded unexpectedly");

  mark_point();
  res = pr_rewind_scoreboard();
  ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));
  res = pr_restore_scoreboard();
  ck_assert_msg(res == 0, "Failed to restore scoreboard: %s", strerror(errno));

  mark_point();
  path = pr_get_scoreboard();
  res = pr_set_scoreboard("none");
  ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
  res = pr_restore_scoreboard();
  ck_assert_msg(res == 0, "Failed to restore scoreboard: %s", strerror(errno));
  res = pr_set_scoreboard(path);
  ck_assert_msg(res == 0, "Failed to enable scoreboarding: %s", strerror(errno));

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_rewind_test) {
  int res;
  const char *path;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  mark_point();
  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_rewind_scoreboard();
  ck_assert_msg(res < 0, "Unexpectedly rewound scoreboard");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));
  res = pr_rewind_scoreboard();
  ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));

  mark_point();
  path = pr_get_scoreboard();
  res = pr_set_scoreboard("none");
  ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
  res = pr_rewind_scoreboard();
  ck_assert_msg(res == 0, "Failed to rewind scoreboard: %s", strerror(errno));
  res = pr_set_scoreboard(path);
  ck_assert_msg(res == 0, "Failed to enable scoreboarding: %s", strerror(errno));

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_scrub_test) {
  uid_t euid;
  int res;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_scoreboard_scrub();
  ck_assert_msg(res < 0, "Unexpectedly scrubbed scoreboard");

  euid = geteuid();
  if (euid != 0) {
    if (errno != EPERM &&
        errno != ENOENT) {
      ck_abort_msg("Failed to set errno to EPERM/ENOENT, got %d [%s] (euid = %lu)",
        errno, strerror(errno), (unsigned long) euid);
    }

  } else {
    if (errno != ENOENT) {
      ck_abort_msg("Failed to set errno to ENOENT, got %d [%s] (euid = %lu)", errno,
        strerror(errno), (unsigned long) euid);
    }
  }

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = pr_scoreboard_scrub();
  ck_assert_msg(res == 0, "Failed to scrub scoreboard: %s", strerror(errno));

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_get_daemon_pid_test) {
  int res;
  pid_t daemon_pid;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  daemon_pid = pr_scoreboard_get_daemon_pid();
  if (daemon_pid != getpid()) {
    ck_abort_msg("Expected %lu, got %lu", (unsigned long) getpid(),
      (unsigned long) daemon_pid);
  }

  pr_delete_scoreboard();

  ServerType = SERVER_INETD;

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  daemon_pid = pr_scoreboard_get_daemon_pid();
  if (daemon_pid != 0) {
    ck_abort_msg("Expected %lu, got %lu", (unsigned long) 0,
      (unsigned long) daemon_pid);
  }

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_get_daemon_uptime_test) {
  int res;
  time_t daemon_uptime, now;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  daemon_uptime = pr_scoreboard_get_daemon_uptime();
  now = time(NULL);

  if (daemon_uptime > now) {
    ck_abort_msg("Expected %lu, got %lu", (unsigned long) now,
      (unsigned long) daemon_uptime);
  }

  pr_delete_scoreboard();

  ServerType = SERVER_INETD;

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  daemon_uptime = pr_scoreboard_get_daemon_uptime();
  if (daemon_uptime != 0) {
    ck_abort_msg("Expected %lu, got %lu", (unsigned long) 0,
      (unsigned long) daemon_uptime);
  }

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_add_test) {
  int res;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res < 0, "Unexpectedly added entry to scoreboard");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
    strerror(errno));

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res < 0, "Unexpectedly added entry to scoreboard");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
    strerror(errno), errno);

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_del_test) {
  int res;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_scoreboard_entry_del(FALSE);
  ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = pr_scoreboard_entry_del(FALSE);
  ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
    strerror(errno), errno);

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
    strerror(errno));

  res = pr_scoreboard_entry_del(FALSE);
  ck_assert_msg(res == 0, "Failed to delete entry from scoreboard: %s",
    strerror(errno));

  res = pr_scoreboard_entry_del(FALSE);
  ck_assert_msg(res < 0, "Unexpectedly deleted entry from scoreboard");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
    strerror(errno), errno);

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_read_test) {
  int res;
  pr_scoreboard_entry_t *score;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  score = pr_scoreboard_entry_read();
  ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  /* We expect NULL here because the scoreboard file should be empty. */
  score = pr_scoreboard_entry_read();
  ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
    strerror(errno));

  score = pr_scoreboard_entry_read();
  ck_assert_msg(score != NULL, "Failed to read scoreboard entry: %s",
    strerror(errno));

  if (score->sce_pid != getpid()) {
    ck_abort_msg("Failed to read expected scoreboard entry (expected PID %lu, got %lu)",
      (unsigned long) getpid(), (unsigned long) score->sce_pid);
  }

  score = pr_scoreboard_entry_read();
  ck_assert_msg(score == NULL, "Unexpectedly read scoreboard entry");

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_get_test) {
  register unsigned int i;
  int res;
  const char *val;
  int scoreboard_fields[] = {
    PR_SCORE_USER,
    PR_SCORE_CLIENT_ADDR,
    PR_SCORE_CLIENT_NAME,
    PR_SCORE_CLASS,
    PR_SCORE_CWD,
    PR_SCORE_CMD,
    PR_SCORE_CMD_ARG,
    PR_SCORE_PROTOCOL,
    -1
  };

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  val = pr_scoreboard_entry_get(-1);
  ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  val = pr_scoreboard_entry_get(-1);
  ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
    strerror(errno), errno);

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
    strerror(errno));

  val = pr_scoreboard_entry_get(-1);
  ck_assert_msg(val == NULL, "Unexpectedly read value from scoreboard entry");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
    strerror(errno), errno);

  for (i = 0; scoreboard_fields[i] != -1; i++) {
    val = pr_scoreboard_entry_get(scoreboard_fields[i]);
    ck_assert_msg(val != NULL, "Failed to read scoreboard field %d: %s",
      scoreboard_fields[i], strerror(errno));
  }

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_update_test) {
  int num, res;
  const char *val;
  pid_t pid = getpid();
  const pr_netaddr_t *addr;
  time_t now;
  off_t len;
  unsigned long elapsed;

  res = mkdir(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to create directory '%s': %s", test_dir,
    strerror(errno));

  res = chmod(test_dir, 0775);
  ck_assert_msg(res == 0, "Failed to set perms on '%s' to 0775': %s", test_dir,
    strerror(errno));

  res = pr_set_scoreboard(test_file);
  ck_assert_msg(res == 0, "Failed to set scoreboard to '%s': %s", test_file,
    strerror(errno));

  res = pr_scoreboard_entry_update(pid, 0);
  ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  res = pr_open_scoreboard(O_RDWR);
  ck_assert_msg(res == 0, "Failed to open scoreboard: %s", strerror(errno));

  res = pr_scoreboard_entry_update(pid, 0);
  ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
    strerror(errno), errno);

  res = pr_scoreboard_entry_add();
  ck_assert_msg(res == 0, "Failed to add entry to scoreboard: %s",
    strerror(errno));

  res = pr_scoreboard_entry_update(pid, -1);
  ck_assert_msg(res < 0, "Unexpectedly updated scoreboard entry");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
    strerror(errno), errno);

  val = "cwd";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_CWD, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CWD: %s", strerror(errno));
 
  val = pr_scoreboard_entry_get(PR_SCORE_CWD); 
  ck_assert_msg(val != NULL, "Failed to get entry PR_SCORE_CWD: %s",
    strerror(errno));
  ck_assert_msg(strcmp(val, "cwd") == 0, "Expected 'cwd', got '%s'", val);

  val = "user";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_USER, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_USER: %s", strerror(errno));

  addr = pr_netaddr_get_addr(p, "127.0.0.1", NULL);
  ck_assert_msg(addr != NULL, "Failed to resolve '127.0.0.1': %s",
    strerror(errno));

  res = pr_scoreboard_entry_update(pid, PR_SCORE_CLIENT_ADDR, addr, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLIENT_ADDR: %s",
    strerror(errno));

  val = "remote_name";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_CLIENT_NAME, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLIENT_NAME: %s",
    strerror(errno));

  val = "session_class";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_CLASS, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CLASS: %s", strerror(errno));

  val = "USER";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_CMD, "%s", val, NULL, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CMD: %s", strerror(errno));

  val = "foo bar";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_CMD_ARG, "%s", val, NULL,
    NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_CMD_ARG: %s",
    strerror(errno));

  num = 77;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_PORT, num, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_PORT: %s",
    strerror(errno));

  res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_ADDR, addr, num, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_ADDR: %s",
    strerror(errno));

  val = "label";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_SERVER_LABEL, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_SERVER_LABEL: %s",
    strerror(errno));
 
  now = 1;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_BEGIN_IDLE, now, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_BEGIN_IDLE: %s",
    strerror(errno));

  now = 2;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_BEGIN_SESSION, now, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_BEGIN_SESSION: %s",
    strerror(errno));

  len = 7;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_DONE, len, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_DONE: %s",
    strerror(errno));

  len = 8;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_SIZE, len, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_SIZE: %s",
    strerror(errno));

  len = 9;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_LEN, len, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_LEN: %s",
    strerror(errno));

  elapsed = 1;
  res = pr_scoreboard_entry_update(pid, PR_SCORE_XFER_ELAPSED, elapsed, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_XFER_ELAPSED: %s",
    strerror(errno));

  val = "protocol";
  res = pr_scoreboard_entry_update(pid, PR_SCORE_PROTOCOL, val, NULL);
  ck_assert_msg(res == 0, "Failed to update PR_SCORE_PROTOCOL: %s",
    strerror(errno));

  (void) unlink(test_mutex);
  (void) unlink(test_file);
  (void) rmdir(test_dir);
}
END_TEST

START_TEST (scoreboard_entry_kill_test) {
  int res;
  pr_scoreboard_entry_t sce;

  mark_point();
  res = pr_scoreboard_entry_kill(NULL, 0);
  ck_assert_msg(res < 0, "Failed to handle null arguments");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  mark_point();
  sce.sce_pid = 1;
  res = pr_scoreboard_entry_kill(&sce, 0);
  ck_assert_msg(res < 0, "Failed to handle bad PID");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
    strerror(errno), errno);

  mark_point();
  sce.sce_pid = getpid();
  res = pr_scoreboard_entry_kill(&sce, 0);
  ck_assert_msg(res == 0, "Failed to send signal 0 to PID %lu: %s",
    (unsigned long) sce.sce_pid, strerror(errno));

  mark_point();
  res = pr_set_scoreboard("none");
  ck_assert_msg(res == 0, "Failed to disable scoreboarding: %s", strerror(errno));
  sce.sce_pid = getpid();
  res = pr_scoreboard_entry_kill(&sce, 0);
  ck_assert_msg(res == 0, "Failed to send signal: %s", strerror(errno));
}
END_TEST

START_TEST (scoreboard_entry_lock_test) {
  int fd = -1, lock_type = -1, res;

  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res < 0, "Failed to handle bad lock type");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
    strerror(errno), errno);

  lock_type = F_RDLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res < 0, "Failed to handle bad file descriptor");
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
    strerror(errno), errno);

  fd = open(test_file2, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
  ck_assert_msg(fd >= 0, "Failed to open '%s': %s", test_file2, strerror(errno));

  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_WRLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_UNLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_WRLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  /* Note: apparently attempt to lock (again) a file on which a lock
   * (of the same type) is already held will succeed.  Huh.
   */
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_RDLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  lock_type = F_UNLCK;
  res = pr_scoreboard_entry_lock(fd, lock_type);
  ck_assert_msg(res == 0, "Failed to lock fd %d: %s", fd, strerror(errno));

  (void) unlink(test_file2);
}
END_TEST

START_TEST (scoreboard_disabled_test) {
  register unsigned int i = 0;
  const char *paths[4] = {
    "/dev/null",
    "none",
    "off",
    NULL
  };
  const char *path;

  for (path = paths[i]; path != NULL; path = paths[i++]) { 
    int res;
    const char *field, *ok;
    pid_t scoreboard_pid;
    time_t scoreboard_uptime;
    pr_scoreboard_entry_t *score;

    res = pr_set_scoreboard(path);
    ck_assert_msg(res == 0, "Failed set to scoreboard to '%s': %s", path,
      strerror(errno));

    ok = PR_RUN_DIR "/proftpd.scoreboard";

    path = pr_get_scoreboard();
    ck_assert_msg(path != NULL, "Failed to get scoreboard path: %s",
      strerror(errno));
    ck_assert_msg(strcmp(path, ok) == 0,
      "Expected path '%s', got '%s'", ok, path);

    res = pr_open_scoreboard(O_RDONLY);
    ck_assert_msg(res == 0, "Failed to open '%s' scoreboard: %s", path,
      strerror(errno));

    res = pr_scoreboard_scrub();
    ck_assert_msg(res == 0, "Failed to scrub '%s' scoreboard: %s", path,
      strerror(errno));

    scoreboard_pid = pr_scoreboard_get_daemon_pid();
    ck_assert_msg(scoreboard_pid == 0,
      "Expected to get scoreboard PID 0, got %lu",
      (unsigned long) scoreboard_pid);

    scoreboard_uptime = pr_scoreboard_get_daemon_uptime();
    ck_assert_msg(scoreboard_uptime == 0,
      "Expected to get scoreboard uptime 0, got %lu",
      (unsigned long) scoreboard_uptime);

    res = pr_scoreboard_entry_add();
    ck_assert_msg(res == 0, "Failed to add entry to '%s' scoreboard: %s", path,
      strerror(errno));

    score = pr_scoreboard_entry_read();
    ck_assert_msg(score == NULL, "Expected null entry");

    field = pr_scoreboard_entry_get(PR_SCORE_CMD_ARG);
    ck_assert_msg(field == NULL, "Expected null CMD_ARG field");

    res = pr_scoreboard_entry_update(getpid(), PR_SCORE_CWD, "foo", NULL);
    ck_assert_msg(res == 0, "Failed to update CWD field: %s", strerror(errno));

    res = pr_scoreboard_entry_del(FALSE);
    ck_assert_msg(res == 0, "Failed to delete entry from '%s' scoreboard: %s",
      path, strerror(errno));

    res = pr_close_scoreboard(FALSE);
    ck_assert_msg(res == 0, "Failed to close '%s' scoreboard: %s", path,
      strerror(errno));

    /* Internal hack: even calling pr_set_scoreboard() with a NULL
     * argument will set the Scoreboard API internal flag back to true.
     */
    pr_set_scoreboard(NULL);
  }
}
END_TEST

Suite *tests_get_scoreboard_suite(void) {
  Suite *suite;
  TCase *testcase;

  suite = suite_create("scoreboard");

  testcase = tcase_create("base");
  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, scoreboard_get_test);
  tcase_add_test(testcase, scoreboard_set_test);
  tcase_add_test(testcase, scoreboard_set_mutex_test);
  tcase_add_test(testcase, scoreboard_open_close_test);
  tcase_add_test(testcase, scoreboard_open_invalid_header_test);
  tcase_add_test(testcase, scoreboard_lock_test);
  tcase_add_test(testcase, scoreboard_delete_test);
  tcase_add_test(testcase, scoreboard_restore_test);
  tcase_add_test(testcase, scoreboard_rewind_test);
  tcase_add_test(testcase, scoreboard_scrub_test);
  tcase_add_test(testcase, scoreboard_get_daemon_pid_test);
  tcase_add_test(testcase, scoreboard_get_daemon_uptime_test);
  tcase_add_test(testcase, scoreboard_entry_add_test);
  tcase_add_test(testcase, scoreboard_entry_del_test);
  tcase_add_test(testcase, scoreboard_entry_read_test);
  tcase_add_test(testcase, scoreboard_entry_get_test);
  tcase_add_test(testcase, scoreboard_entry_update_test);
  tcase_add_test(testcase, scoreboard_entry_kill_test);
  tcase_add_test(testcase, scoreboard_entry_lock_test);
  tcase_add_test(testcase, scoreboard_disabled_test);

  suite_add_tcase(suite, testcase);
  return suite;
}