diff options
Diffstat (limited to 'tests/filesys/extended/dir-rm-cwd.c')
| -rw-r--r-- | tests/filesys/extended/dir-rm-cwd.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/filesys/extended/dir-rm-cwd.c b/tests/filesys/extended/dir-rm-cwd.c new file mode 100644 index 0000000..78e13de --- /dev/null +++ b/tests/filesys/extended/dir-rm-cwd.c | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | /* Tries to remove the current directory, which may succeed or | ||
| 2 | fail. The requirements in each case are different; refer to | ||
| 3 | the assignment for details. */ | ||
| 4 | |||
| 5 | #include <syscall.h> | ||
| 6 | #include "tests/lib.h" | ||
| 7 | #include "tests/main.h" | ||
| 8 | |||
| 9 | static int | ||
| 10 | wrap_open (const char *name) | ||
| 11 | { | ||
| 12 | static int fds[8], fd_cnt; | ||
| 13 | int fd, i; | ||
| 14 | |||
| 15 | CHECK ((fd = open (name)) > 1, "open \"%s\"", name); | ||
| 16 | for (i = 0; i < fd_cnt; i++) | ||
| 17 | if (fds[i] == fd) | ||
| 18 | fail ("fd returned is not unique"); | ||
| 19 | fds[fd_cnt++] = fd; | ||
| 20 | return fd; | ||
| 21 | } | ||
| 22 | |||
| 23 | void | ||
| 24 | test_main (void) | ||
| 25 | { | ||
| 26 | int root_fd, a_fd0; | ||
| 27 | char name[READDIR_MAX_LEN + 1]; | ||
| 28 | |||
| 29 | root_fd = wrap_open ("/"); | ||
| 30 | CHECK (mkdir ("a"), "mkdir \"a\""); | ||
| 31 | |||
| 32 | a_fd0 = wrap_open ("/a"); | ||
| 33 | CHECK (!readdir (a_fd0, name), "verify \"/a\" is empty"); | ||
| 34 | CHECK (inumber (root_fd) != inumber (a_fd0), | ||
| 35 | "\"/\" and \"/a\" must have different inumbers"); | ||
| 36 | |||
| 37 | CHECK (chdir ("a"), "chdir \"a\""); | ||
| 38 | |||
| 39 | msg ("try to remove \"/a\""); | ||
| 40 | if (remove ("/a")) | ||
| 41 | { | ||
| 42 | msg ("remove successful"); | ||
| 43 | |||
| 44 | CHECK (open ("/a") == -1, "open \"/a\" (must fail)"); | ||
| 45 | CHECK (open (".") == -1, "open \".\" (must fail)"); | ||
| 46 | CHECK (open ("..") == -1, "open \"..\" (must fail)"); | ||
| 47 | CHECK (!create ("x", 512), "create \"x\" (must fail)"); | ||
| 48 | } | ||
| 49 | else | ||
| 50 | { | ||
| 51 | int a_fd1, a_fd2, a_fd3; | ||
| 52 | |||
| 53 | msg ("remove failed"); | ||
| 54 | |||
| 55 | CHECK (!remove ("../a"), "try to remove \"../a\" (must fail)"); | ||
| 56 | CHECK (!remove (".././a"), "try to remove \".././a\" (must fail)"); | ||
| 57 | CHECK (!remove ("/./a"), "try to remove \"/./a\" (must fail)"); | ||
| 58 | |||
| 59 | a_fd1 = wrap_open ("/a"); | ||
| 60 | a_fd2 = wrap_open ("."); | ||
| 61 | CHECK (inumber (a_fd1) == inumber (a_fd2), | ||
| 62 | "\"/a\" and \".\" must have same inumber"); | ||
| 63 | CHECK (inumber (root_fd) != inumber (a_fd1), | ||
| 64 | "\"/\" and \"/a\" must have different inumbers"); | ||
| 65 | |||
| 66 | CHECK (chdir ("/a"), "chdir \"/a\""); | ||
| 67 | a_fd3 = wrap_open ("."); | ||
| 68 | CHECK (inumber (a_fd3) == inumber (a_fd1), | ||
| 69 | "\".\" must have same inumber as before"); | ||
| 70 | |||
| 71 | CHECK (chdir ("/"), "chdir \"/\""); | ||
| 72 | CHECK (!remove ("a"), "try to remove \"a\" (must fail: still open)"); | ||
| 73 | } | ||
| 74 | CHECK (!readdir (a_fd0, name), "verify \"/a\" is empty"); | ||
| 75 | } | ||
