Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions tests/other/test_unlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

Expand All @@ -13,13 +16,10 @@ int main() {
const char *dirname = "test";

// Create a file
FILE *f = fopen(filename, "wb");
FILE* f = fopen(filename, "w+");
if (f == NULL) {
return 1;
}
if (fclose(f)) {
return 1;
}
// Check it exists
if (access(filename, F_OK) != 0) {
return 1;
Expand All @@ -32,9 +32,30 @@ int main() {
if (access(filename, F_OK) != -1) {
return 1;
}
// Check that we can still write to it
if (fwrite("hello", 1, 5, f) != 5) {
return 1;
}
// And seek in it.
if (fseek(f, 0, SEEK_SET) != 0) {
return 1;
}
// And read from it.
char buf[6] = {0};
if (fread(buf, 1, 5, f) != 5 || strcmp("hello", buf) != 0) {
return 1;
}
if (fclose(f)) {
return 1;
}

// Create a directory
if (mkdir(dirname, 0700)) {
if (mkdir(dirname, 0700) != 0) {
return 1;
}
// Open the directory
DIR* d = opendir(dirname);
if (d == NULL) {
return 1;
}
// Delete the directory
Expand All @@ -45,6 +66,17 @@ int main() {
if (access(dirname, F_OK) != -1) {
return 1;
}
// Check that we can still read the directory, but that it is empty.
errno = 0;
if (readdir(d) != NULL || errno != 0) {
return 1;
}
// Check that we *cannot* create a child
if (openat(dirfd(d), filename, O_CREAT | O_WRONLY, S_IRWXU) != -1) {
return 1;
}

closedir(d);

printf("ok\n");

Expand Down