Skip to content

Commit 2c08293

Browse files
committed
[test] Simplify test/unistd/curdir.c. NFC
Also move some of the getcwd tests from `test/wasmfs/wasmfs_chdir.c` so they are verified not just under wasmfs.
1 parent 71e31f4 commit 2c08293

File tree

3 files changed

+44
-84
lines changed

3 files changed

+44
-84
lines changed

test/unistd/curdir.c

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
10+
#include <string.h>
911
#include <errno.h>
1012
#include <unistd.h>
1113
#include <fcntl.h>
@@ -23,90 +25,65 @@ int main() {
2325
);
2426

2527
char buffer[256];
28+
29+
// Try to pass a size of 0.
30+
getcwd(buffer, 0);
31+
printf("errno: %s\n", strerror(errno));
32+
assert(errno == EINVAL);
33+
errno = 0;
34+
35+
// Try to write to a buffer that is too small
36+
getcwd(buffer, 1);
37+
printf("errno: %s\n", strerror(errno));
38+
assert(errno == ERANGE);
39+
errno = 0;
40+
2641
printf("getcwd: %s\n", getcwd(buffer, 256));
27-
printf("errno: %d\n", errno);
42+
printf("errno: %s\n", strerror(errno));
2843
errno = 0;
2944
printf("\n");
3045

3146
printf("chdir(file): %d\n", chdir("/file"));
32-
printf("errno: %d\n", errno);
33-
if (!errno) {
34-
errno = 0;
35-
printf("getcwd: %s\n", getcwd(buffer, 256));
36-
printf("errno: %d\n", errno);
37-
}
47+
printf("errno: %s\n", strerror(errno));
48+
assert(errno != 0);
3849
errno = 0;
3950
printf("\n");
4051

41-
printf("chdir(dir): %d\n", chdir("/dir"));
42-
printf("errno: %d\n", errno);
43-
if (!errno) {
44-
errno = 0;
45-
printf("getcwd: %s\n", getcwd(buffer, 256));
46-
printf("errno: %d\n", errno);
47-
}
48-
errno = 2;
49-
printf("\n");
50-
5152
printf("chdir(\"\"): %d\n", chdir(""));
52-
printf("errno: %d\n", errno);
53-
if (!errno) {
54-
errno = 0;
55-
printf("getcwd: %s\n", getcwd(buffer, 256));
56-
printf("errno: %d\n", errno);
57-
}
58-
errno = 2;
53+
printf("errno: %s\n", strerror(errno));
54+
assert(errno != 0);
55+
errno = 0;
5956
printf("\n");
6057

6158
printf("chdir(device): %d\n", chdir("/device"));
62-
printf("errno: %d\n", errno);
63-
if (!errno) {
64-
errno = 0;
65-
printf("getcwd: %s\n", getcwd(buffer, 256));
66-
printf("errno: %d\n", errno);
67-
}
59+
printf("errno: %s\n", strerror(errno));
60+
assert(errno != 0);
6861
errno = 0;
6962
printf("\n");
7063

7164
printf("chdir(folder): %d\n", chdir("/folder"));
72-
printf("errno: %d\n", errno);
73-
if (!errno) {
74-
errno = 0;
75-
printf("getcwd: %s\n", getcwd(buffer, 256));
76-
printf("errno: %d\n", errno);
77-
}
78-
errno = 0;
65+
printf("errno: %s\n", strerror(errno));
66+
assert(errno == 0);
67+
printf("getcwd: %s\n", getcwd(buffer, 256));
7968
printf("\n");
8069

8170
printf("chdir(nonexistent): %d\n", chdir("/nonexistent"));
82-
printf("errno: %d\n", errno);
83-
if (!errno) {
84-
errno = 0;
85-
printf("getcwd: %s\n", getcwd(buffer, 256));
86-
printf("errno: %d\n", errno);
87-
}
71+
printf("errno: %s\n", strerror(errno));
72+
assert(errno != 0);
8873
errno = 0;
8974
printf("\n");
9075

9176
printf("chdir(link): %d\n", chdir("/link"));
92-
printf("errno: %d\n", errno);
93-
if (!errno) {
94-
errno = 0;
95-
printf("getcwd: %s\n", getcwd(buffer, 256));
96-
printf("errno: %d\n", errno);
97-
}
98-
errno = 0;
77+
printf("errno: %s\n", strerror(errno));
78+
assert(errno == 0);
79+
printf("getcwd: %s\n", getcwd(buffer, 256));
9980
printf("\n");
10081

10182
errno = 0;
10283
printf("fchdir(/): %d\n", fchdir(open("/", O_RDONLY, 0777)));
103-
printf("errno: %d\n", errno);
104-
if (!errno) {
105-
errno = 0;
106-
printf("getcwd: %s\n", getcwd(buffer, 256));
107-
printf("errno: %d\n", errno);
108-
errno = 0;
109-
}
84+
printf("errno: %s\n", strerror(errno));
85+
assert(errno == 0);
86+
printf("getcwd: %s\n", getcwd(buffer, 256));
11087

11188
return 0;
11289
}

test/unistd/curdir.out

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1+
errno: Invalid argument
2+
errno: Result not representable
13
getcwd: /
2-
errno: 0
4+
errno: No error information
35

46
chdir(file): -1
5-
errno: 54
6-
7-
chdir(dir): -1
8-
errno: 44
7+
errno: Not a directory
98

109
chdir(""): -1
11-
errno: 44
10+
errno: No such file or directory
1211

1312
chdir(device): -1
14-
errno: 54
13+
errno: Not a directory
1514

1615
chdir(folder): 0
17-
errno: 0
16+
errno: No error information
1817
getcwd: /folder
19-
errno: 0
2018

2119
chdir(nonexistent): -1
22-
errno: 44
20+
errno: No such file or directory
2321

2422
chdir(link): 0
25-
errno: 0
23+
errno: No error information
2624
getcwd: /folder
27-
errno: 0
2825

2926
fchdir(/): 0
30-
errno: 0
27+
errno: No error information
3128
getcwd: /
32-
errno: 0

test/wasmfs/wasmfs_chdir.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,4 @@ int main() {
8585
ret = getcwd(cwd, sizeof(cwd));
8686
assert(ret == cwd);
8787
printf("Current working dir is still: %s\n", cwd);
88-
89-
// Try to pass a size of 0.
90-
errno = 0;
91-
getcwd(cwd, 0);
92-
printf("Errno: %s\n", strerror(errno));
93-
assert(errno == EINVAL);
94-
95-
// Try to write to a buffer of size 1.
96-
errno = 0;
97-
char smallBuffer[1];
98-
getcwd(smallBuffer, 1);
99-
assert(errno == ERANGE);
100-
return 0;
10188
}

0 commit comments

Comments
 (0)