Skip to content

Commit cd1f6cc

Browse files
authored
Cleanup test/fs tests. NFC (#24831)
Mostly converting to C and using EM_JS_DEPS macro. Also, added lzfs test test_other.py so it can be tested outside the browser tests.
1 parent aac42d0 commit cd1f6cc

13 files changed

+224
-216
lines changed

test/fs/test_fopen_write.cpp renamed to test/fs/test_fopen_write.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include <string.h>
1010
#include <emscripten/emscripten.h>
1111

12-
void create_file()
13-
{
12+
void create_file() {
1413
FILE *file = fopen("hello_file.txt", "wb");
1514
assert(file);
1615
const char *data = "Hello world";
@@ -24,8 +23,7 @@ void create_file()
2423
fclose(file);
2524
}
2625

27-
void read_file()
28-
{
26+
void read_file() {
2927
FILE *file = fopen("hello_file.txt", "rb");
3028
char buffer[128] = {};
3129
size_t read = fread(buffer, 1, sizeof(buffer), file);
@@ -34,8 +32,7 @@ void read_file()
3432
assert(!strcmp(buffer, "Hello data!"));
3533
}
3634

37-
int main()
38-
{
35+
int main() {
3936
create_file();
4037
read_file();
4138
return 0;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2017 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <assert.h>
7+
#include <errno.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <sys/stat.h>
12+
#include <unistd.h>
13+
14+
char* makeLongStr(const char* str, int strCount) {
15+
char* result = malloc((strCount * strlen(str)) + 1);
16+
result[0] = '\0';
17+
18+
for (int i = 0; i < strCount; ++i) {
19+
strcat(result, str);
20+
}
21+
22+
return result;
23+
}
24+
25+
void doPositiveTest(const char* dirName) {
26+
printf("positive test case: %s\n", dirName);
27+
28+
char fullDirName[4096];
29+
sprintf(fullDirName, "/%s", dirName);
30+
int ret = mkdir(fullDirName, 0777);
31+
assert(ret == 0);
32+
ret = chdir(fullDirName);
33+
assert(ret == 0);
34+
35+
char buf[4096];
36+
char* cwd = getcwd(buf, sizeof(buf));
37+
assert(cwd != NULL);
38+
printf("getcwd -> %s\n", cwd);
39+
}
40+
41+
void doNegativeTest(const char* dirName) {
42+
printf("negative test case: %s\n", dirName);
43+
44+
int ret = mkdir(dirName, 0777);
45+
assert(ret == 0);
46+
ret = chdir(dirName);
47+
assert(ret == 0);
48+
49+
unsigned char buf[4096];
50+
unsigned char filler = 0xFE;
51+
memset(buf, filler, sizeof(buf));
52+
53+
size_t allowedBufferSize = strlen(dirName) / 2;
54+
char* cwd = getcwd((char*)buf, allowedBufferSize);
55+
assert(cwd == 0);
56+
assert(errno == ERANGE);
57+
58+
for (size_t i = allowedBufferSize; i < sizeof(buf); i++) {
59+
assert(buf[i] == filler);
60+
}
61+
}
62+
63+
int main() {
64+
// Short non-ascii name
65+
doPositiveTest(u8"абвгд");
66+
67+
// Long non-ascii name
68+
char longStr[4096] = "abcde";
69+
strcat(longStr, makeLongStr(u8"абвгд", 25));
70+
assert(strlen(longStr) == 255);
71+
doPositiveTest(longStr);
72+
73+
// The negative test passes a half-sized buffer to getcwd and makes sure that it fails.
74+
doNegativeTest(makeLongStr(u8"abcde", 10));
75+
}

test/fs/test_getcwd_with_non_ascii_name.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
positive test case: абвгд
2-
ret2 = /абвгд
2+
getcwd -> /абвгд
33
positive test case: abcdeабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгд
4-
ret2 = /abcdeабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгд
4+
getcwd -> /abcdeабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгдабвгд
55
negative test case: abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde

test/fs/test_getdents64.cpp renamed to test/fs/test_getdents64.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@
88
#include <stdio.h>
99
#include <unistd.h>
1010
#include <assert.h>
11+
#include <stdbool.h>
1112
#include <stdlib.h>
1213
#include <sys/stat.h>
1314
#include <sys/syscall.h>
1415

15-
#define BUF_SIZE (sizeof(dirent)*2)
16+
#define BUF_SIZE (sizeof(struct dirent)*2)
1617

17-
int main(int argc, char *argv[])
18-
{
18+
int main(int argc, char *argv[]) {
1919
int fd = open(".", O_RDONLY | O_DIRECTORY);
2020
assert(fd > 0);
2121

22-
printf("sizeof(dirent): %zu, sizeof(buffer): %zu\n", sizeof(dirent), BUF_SIZE);
22+
printf("sizeof(dirent): %zu, sizeof(buffer): %zu\n", sizeof(struct dirent), BUF_SIZE);
2323

2424
bool first = true;
2525

26-
for(;;)
27-
{
26+
while (1) {
2827
char buf[BUF_SIZE];
29-
int nread = getdents(fd, (dirent*)buf, BUF_SIZE);
28+
int nread = getdents(fd, (struct dirent*)buf, BUF_SIZE);
3029
assert(nread != -1);
3130
if (nread == 0)
3231
break;
@@ -39,9 +38,8 @@ int main(int argc, char *argv[])
3938
printf("--------------- nread=%d ---------------\n", nread);
4039
printf("i-node# file type d_reclen d_off d_name\n");
4140
int bpos = 0;
42-
while(bpos < nread)
43-
{
44-
dirent *d = (dirent *)(buf + bpos);
41+
while (bpos < nread) {
42+
struct dirent *d = (struct dirent *)(buf + bpos);
4543
printf("%8ld ", (long)d->d_ino);
4644
char d_type = *(buf + bpos + d->d_reclen - 1);
4745
printf("%-10s ", (d_type == DT_REG) ? "regular" :
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2017 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <assert.h>
7+
#include <dirent.h>
8+
#include <string.h>
9+
#include <stdio.h>
10+
#include <sys/stat.h>
11+
#include <fcntl.h>
12+
#include <unistd.h>
13+
14+
void touch(const char* fileName) {
15+
int fd = open(fileName, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP);
16+
assert(fd);
17+
close(fd);
18+
}
19+
20+
void doTest(const char* uniqueDirName, const char* fileName) {
21+
printf("test in %s: %s\n", uniqueDirName, fileName);
22+
23+
int ret = mkdir(uniqueDirName, 0777);
24+
assert(ret == 0);
25+
26+
char buf[1024];
27+
sprintf(buf, "%s/%s", uniqueDirName, fileName);
28+
touch(buf);
29+
30+
int fd = open(uniqueDirName, O_RDONLY | O_DIRECTORY);
31+
assert(fd > 0);
32+
33+
while (1) {
34+
struct dirent d;
35+
int nread = getdents(fd, &d, sizeof(d));
36+
assert(nread != -1);
37+
if (nread == 0) {
38+
break;
39+
}
40+
41+
if (strcmp(d.d_name, ".") == 0 || strcmp(d.d_name, "..") == 0) {
42+
continue;
43+
}
44+
45+
printf("d.d_name = %s\n", d.d_name);
46+
47+
// If it needed to be, the name was truncated.
48+
assert(strlen(d.d_name) <= 255);
49+
}
50+
}
51+
52+
int main() {
53+
// Non-ascii file name.
54+
doTest("test_dir", u8"абвгд");
55+
56+
#ifndef WASMFS // The JS FS truncates filenames automatically, which is incorrect. Wasmfs and Linux do not.
57+
// File name exceeds the limit of 255 chars and is truncated.
58+
char longName[300];
59+
memset(longName, 1, sizeof(longName));
60+
doTest("test_dir2", longName);
61+
#endif
62+
}

test/fs/test_getdents64_special_cases.cpp

Lines changed: 0 additions & 61 deletions
This file was deleted.

test/fs/test_lz4fs.cpp renamed to test/fs/test_lz4fs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
#define TOTAL_SIZE (10*1024*128)
1616

17+
EM_JS_DEPS(deps, "$ccall");
18+
1719
double before_it_all;
1820

19-
extern "C" void EMSCRIPTEN_KEEPALIVE finish() {
21+
void EMSCRIPTEN_KEEPALIVE finish() {
2022
// load some file data, SYNCHRONOUSLY :)
2123
char buffer[100];
2224
int num;
@@ -130,7 +132,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE finish() {
130132
#if LOAD_MANUALLY
131133
result = 1;
132134
#else
133-
result = 2;
135+
result = 0;
134136
#endif
135137
emscripten_force_exit(result);
136138
}

test/fs/test_workerfs_package.cpp renamed to test/fs/test_workerfs_package.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <emscripten.h>
1111

12-
extern "C" {
12+
EM_JS_DEPS(deps, "$ccall");
1313

1414
void EMSCRIPTEN_KEEPALIVE finish() {
1515
// load some file data, SYNCHRONOUSLY :)
@@ -39,8 +39,6 @@ void EMSCRIPTEN_KEEPALIVE finish() {
3939
REPORT_RESULT(1);
4040
}
4141

42-
}
43-
4442
int main() {
4543
// Load the metadata and data of our file package. When they arrive, load the contents of the package into our filesystem.
4644
// The data arrives as a Blob, which could in other cases arrive from any other way a Blob can arrive:

0 commit comments

Comments
 (0)