kernel: some boottime sanitychecks
[minix.git] / test / test58.c
blob6ce9a8af589fccf52e173c6630c3eede2b36aeaf
1 /* This tests the behavior of Minix when the current working dir (cwd) doesn't
2 * actually exist and we either:
3 * - create a new file
4 * - make a new directory
5 * - make a special file (mknod)
6 * - create a hard link
7 * - create a symbolic link, or
8 * - rename a file
9 * In each case, `a component of the path does not name an existing file', and
10 * the operation should fail with ENOENT. These tests should actually be
11 * distributed over the other tests that actually test the specific system
12 * calls.
15 #include <stdio.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <sys/stat.h>
24 int subtest = -1;
25 #define MAX_ERROR 999 /* Effectively no limit. This is necessary as this
26 * test tries to undo errors and should therefore not
27 * preemptively exit, as that would leave the FS
28 * in a corrupted state. */
29 #include "common.c"
31 #define TEST_PATH "a/b/c"
32 #define INTEGR_MSG "You might want to check fs integrity\n"
34 void do_test(void);
36 void do_test(void)
38 int r, fd;
39 int s[2];
40 char buf[1], testroot[PATH_MAX+1], renamebuf[PATH_MAX+1];
42 subtest = 1;
43 if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) == -1) e(1);
44 if (system("mkdir -p " TEST_PATH) == -1) e(2);
45 if (realpath(".", testroot) == NULL) e(3);
47 r = fork();
48 if (r == -1) e(4);
49 else if (r == 0) { /* Child */
50 /* Change child's cwd to TEST_PATH */
51 if (chdir(TEST_PATH) == -1) e(5);
53 /* Signal parent we're ready for the test */
54 buf[0] = 'a';
55 if (write(s[0], buf, sizeof(buf)) != sizeof(buf)) e(6);
57 /* Wait for parent to remove my cwd */
58 if (read(s[0], buf, sizeof(buf)) != sizeof(buf)) e(7);
60 /* Try to create a file */
61 if ((fd = open("testfile", O_RDWR | O_CREAT)) != -1) {
62 e(8);
63 /* Uh oh. We created a file?! Try to remove it. */
64 (void) close(fd);
65 if (unlink("testfile") != 0) {
66 /* This is not good. We created a file, but we can
67 * never access it; we have a spurious inode.
69 e(9);
70 printf(INTEGR_MSG);
71 exit(errct);
74 if (errno != ENOENT) e(10);
76 /* Try to create a dir */
77 errno = 0;
78 if (mkdir("testdir", 0777) == 0) {
79 e(11);
80 /* Uh oh. This shouldn't have been possible. Try to undo. */
81 if (rmdir("testdir") != 0) {
82 /* Not good. */
83 e(12);
84 printf(INTEGR_MSG);
85 exit(errct);
88 if (errno != ENOENT) e(13);
90 /* Try to create a special file */
91 errno = 0;
92 if (mknod("testnode", 0777 | S_IFIFO, 0) == 0) {
93 e(14);
94 /* Impossible. Try to make it unhappen. */
95 if (unlink("testnode") != 0) {
96 /* Not good. */
97 e(15);
98 printf(INTEGR_MSG);
99 exit(errct);
102 if (errno != ENOENT) e(16);
104 /* Try to rename a file */
105 errno = 0;
106 /* First create a file in the test dir */
107 snprintf(renamebuf, PATH_MAX, "%s/oldname", testroot);
108 if ((fd = open(renamebuf, O_RDWR | O_CREAT)) == -1) e(17);
109 if (close(fd) != 0) e(18);
111 /* Now try to rename that file to an entry in the current, non-existing
112 * working directory.
114 if (rename(renamebuf, "testrename") == 0) {
115 e(19);
116 /* This shouldn't have been possible. Revert the name change.
118 if (rename("testrename", renamebuf) != 0) {
119 /* Failed */
120 e(20);
121 printf(INTEGR_MSG);
122 exit(errct);
126 /* Try to create a hard link to that file */
127 errno = 0;
128 if (link(renamebuf, "testhlink") == 0) {
129 e(21);
130 /* Try to undo the hard link to prevent fs corruption. */
131 if (unlink("testhlink") != 0) {
132 /* Failed. */
133 e(22);
134 printf(INTEGR_MSG);
135 exit(errct);
138 if (errno != ENOENT) e(23);
140 /* Try to create a symlink */
141 errno = 0;
142 if (symlink(testroot, "testslink") == 0) {
143 e(24);
144 /* Try to remove the symlink to prevent fs corruption. */
145 if (unlink("testslink") != 0) {
146 /* Failed. */
147 e(25);
148 printf(INTEGR_MSG);
149 exit(errct);
152 if (errno != ENOENT) e(26);
154 exit(errct);
155 } else { /* Parent */
156 int status;
158 /* Wait for the child to enter the TEST_PATH dir */
159 if (read(s[1], buf, sizeof(buf)) != sizeof(buf)) e(27);
161 /* Delete TEST_PATH */
162 if (rmdir(TEST_PATH) != 0) e(28);
164 /* Tell child we removed its cwd */
165 buf[0] = 'b';
166 if (write(s[1], buf, sizeof(buf)) != sizeof(buf)) e(29);
168 wait(&status);
169 errct += WEXITSTATUS(status); /* Count errors */
173 int main(int argc, char* argv[])
175 start(58);
176 do_test();
177 quit();
178 return(-1); /* Unreachable */