1 #include <stdio.h>
2
3 #include <fspred.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #define TDIR "/bin"
10 #define TEXE "/bin/ls"
11 #define TDEV "/dev/zero"
12 #define TPIPE "/tmp/myfifo123"
13 #define TMOUNTP "/dev"
14 #define TDEVM "devfs"
15 #define TEXIST "/nonexistent"
16
17 #define NTESTS 6
18
19 static int
test_is_dir(void)20 test_is_dir(void)
21 {
22 int pass = 0;
23
24 pass = is_dir("%s", TDIR);
25 pass = pass && !is_dir("%s", TEXIST);
26 pass = pass && !is_dir("%s", TEXE);
27
28 return pass;
29 }
30
31 static int
test_is_program(void)32 test_is_program(void)
33 {
34 int pass;
35
36 pass = is_program(TEXE);
37 pass = pass && !is_program("%s", TEXIST);
38 pass = pass && !is_program(TDIR);
39
40 return pass;
41 }
42
43 static int
test_is_device(void)44 test_is_device(void)
45 {
46 int pass;
47
48 pass = is_device(TDEV);
49 pass = pass && !is_device("%s", TEXIST);
50 pass = pass && !is_device(TDIR);
51
52 return pass;
53 }
54
55
56 static int
test_is_named_pipe(void)57 test_is_named_pipe(void)
58 {
59 int pass;
60
61 /* Need to make a named pipe */
62 unlink(TPIPE);
63 if (mkfifo(TPIPE, 0600) == -1) {
64 perror("mkfifo");
65 return EXIT_FAILURE;
66 }
67
68 pass = is_named_pipe(TPIPE);
69 pass = pass && !is_named_pipe("%s", TEXIST);
70 pass = pass && !is_named_pipe(TEXE);
71
72 return pass;
73 }
74
75 static int
test_is_mountpoint_mounted(void)76 test_is_mountpoint_mounted(void)
77 {
78 int pass;
79
80 pass = is_mountpoint_mounted(TMOUNTP);
81 pass = pass && !is_mountpoint_mounted(TEXIST);
82
83 return pass;
84 }
85
86 static int
test_is_device_mounted(void)87 test_is_device_mounted(void)
88 {
89 int pass;
90
91 pass = is_device_mounted(TDEVM);
92 pass = pass && !is_device_mounted(TEXIST);
93
94 return pass;
95 }
96
97 typedef struct {
98 const char *name;
99 int (*fn)(void);
100 } fspred_test;
101
102 fspred_test all_tests[NTESTS] = {
103 "is_dir", test_is_dir,
104 "is_program", test_is_program,
105 "is_device", test_is_device,
106 "is_named_pipe", test_is_named_pipe,
107 "is_mountpoint_mounted", test_is_mountpoint_mounted,
108 "is_device_mounted", test_is_device_mounted
109 };
110
111 int
main(void)112 main(void)
113 {
114 int pass = 1;
115 int ret;
116
117 for (int i = 0; i < NTESTS; i++) {
118 ret = all_tests[i].fn();
119 printf("%s ..... %s\n", all_tests[i].name,
120 (ret) ? "pass" : "fail");
121
122 if (ret == 0)
123 pass = 0;
124 }
125
126 /* XXX Any reasonable way of testing is_any_slice_mounted? */
127
128 return (pass) ? EXIT_SUCCESS : EXIT_FAILURE;
129 }
130