Skip to content

Commit a9108f8

Browse files
authored
Use assert in test_unlink.cpp NFC (#23174)
This is shorter and the failures give more information.
1 parent 0c3c07f commit a9108f8

File tree

1 file changed

+20
-57
lines changed

1 file changed

+20
-57
lines changed

test/other/test_unlink.cpp

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string.h>
1111
#include <sys/stat.h>
1212
#include <unistd.h>
13+
#include <assert.h>
1314

1415
#ifdef WASMFS
1516
#include "../wasmfs/get_backend.h"
@@ -31,72 +32,42 @@ int main() {
3132

3233
// Create a file
3334
int fd = open(filename, O_RDWR | O_CREAT, 0777);
34-
if (fd == -1) {
35-
return 1;
36-
}
35+
assert(fd != -1);
3736
// Check it exists
38-
if (access(filename, F_OK) != 0) {
39-
return 1;
40-
}
37+
assert(access(filename, F_OK) == 0);
4138
// Delete the file
42-
if (unlinkat(AT_FDCWD, filename, 0)) {
43-
return 1;
44-
}
39+
assert(unlinkat(AT_FDCWD, filename, 0) == 0);
4540
// Check that it doesn't exist
46-
if (access(filename, F_OK) != -1) {
47-
return 1;
48-
}
41+
assert(access(filename, F_OK) == -1);
4942
// Check that we can still write to it
50-
if (write(fd, "hello", 5) != 5) {
51-
return 1;
52-
}
43+
assert(write(fd, "hello", 5) == 5);
5344
// And seek in it.
54-
if (lseek(fd, 0, SEEK_SET) != 0) {
55-
return 1;
56-
}
45+
assert(lseek(fd, 0, SEEK_SET) == 0);
5746
// And read from it.
5847
char buf[6] = {0};
5948
auto r = read(fd, buf, 5);
60-
if (r != 5) {
61-
return 1;
62-
}
63-
if (strcmp("hello", buf) != 0) {
64-
return 1;
65-
}
66-
if (close(fd)) {
67-
return 1;
68-
}
49+
assert(r==5);
50+
assert(strcmp("hello", buf) == 0);
51+
assert(close(fd)==0);
6952

7053
// Create a directory
71-
if (mkdir(dirname, 0700) != 0) {
72-
return 1;
73-
}
54+
assert(mkdir(dirname, 0700) == 0);
7455
// Open the directory
7556
DIR* d = opendir(dirname);
76-
if (d == NULL) {
77-
return 1;
78-
}
57+
assert(d != NULL);
7958
// Delete the directory
80-
if (unlinkat(AT_FDCWD, dirname, AT_REMOVEDIR)) {
81-
return 1;
82-
}
59+
assert(unlinkat(AT_FDCWD, dirname, AT_REMOVEDIR) == 0);
8360
// Check that it doesn't exist
84-
if (access(dirname, F_OK) != -1) {
85-
return 1;
86-
}
61+
assert(access(dirname, F_OK) == -1);
8762

8863
// The rest of this test does not yet pass with the node backend!
8964
#ifndef WASMFS_NODE_BACKEND
9065

9166
// Check that we can still read the directory, but that it is empty.
9267
errno = 0;
93-
if (readdir(d) != NULL || errno != 0) {
94-
return 1;
95-
}
68+
assert(readdir(d) == NULL && errno == 0);
9669
// Check that we *cannot* create a child.
97-
if (openat(dirfd(d), filename, O_CREAT | O_WRONLY, S_IRWXU) != -1) {
98-
return 1;
99-
}
70+
assert(openat(dirfd(d), filename, O_CREAT | O_WRONLY, S_IRWXU) == -1);
10071
printf("%s\n", strerror(errno));
10172
#ifdef __EMSCRIPTEN__
10273
// Linux allows "." and ".." to be accessed on unlinked directories, but this
@@ -105,27 +76,19 @@ int main() {
10576
// TODO: Consider supporting "." on unlinked files, if not ".."
10677

10778
// Check that we cannot still access "."
108-
if (openat(dirfd(d), ".", O_DIRECTORY | O_RDONLY) != -1) {
109-
return 1;
110-
}
79+
assert(openat(dirfd(d), ".", O_DIRECTORY | O_RDONLY) == -1);
11180
#ifdef WASMFS
11281
// Check that we cannot still access ".." on WasmFS.
113-
if (openat(dirfd(d), "..", O_DIRECTORY | O_RDONLY) != -1) {
114-
return 1;
115-
}
82+
assert(openat(dirfd(d), "..", O_DIRECTORY | O_RDONLY) == -1);
11683
#endif
11784
#else
11885
// Check that we can still access "." on Linux.
11986
int self = openat(dirfd(d), ".", O_DIRECTORY | O_RDONLY);
120-
if (self == -1) {
121-
return 1;
122-
}
87+
assert(self != -1);
12388
close(self);
12489
// Check that we can still access ".." on Linux.
12590
int parent = openat(dirfd(d), "..", O_DIRECTORY | O_RDONLY);
126-
if (parent == -1) {
127-
return 1;
128-
}
91+
assert(parent != -1);
12992
close(parent);
13093
#endif
13194

0 commit comments

Comments
 (0)