forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_idbfs_sync.c
More file actions
167 lines (143 loc) · 3.74 KB
/
test_idbfs_sync.c
File metadata and controls
167 lines (143 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2013 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <stdio.h>
#include <emscripten.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
int result = 1;
void success()
{
REPORT_RESULT(result);
#ifdef FORCE_EXIT
emscripten_force_exit(0);
#endif
}
void test() {
int fd;
#if FIRST
// for each file, we first make sure it doesn't currently exist
// (we delete it at the end of !FIRST). We then test an empty
// file plus two files each with a small amount of content
struct stat st;
// the empty file
if ((stat("/working1/empty.txt", &st) != -1) || (errno != ENOENT))
result = -1000 - errno;
fd = open("/working1/empty.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1)
result = -2000 - errno;
else if (close(fd) != 0)
result = -3000 - errno;
// a file whose contents are just 'az'
if ((stat("/working1/waka.txt", &st) != -1) || (errno != ENOENT))
result = -4000 - errno;
fd = open("/working1/waka.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1)
result = -5000 - errno;
else
{
if (write(fd,"az",2) != 2)
result = -6000 - errno;
if (close(fd) != 0)
result = -7000 - errno;
}
// a file whose contents are random-ish string set by the test_browser.py file
if ((stat("/working1/moar.txt", &st) != -1) || (errno != ENOENT))
result = -8000 - errno;
fd = open("/working1/moar.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1)
result = -9000 - errno;
else
{
if (write(fd, SECRET, strlen(SECRET)) != strlen(SECRET))
result = -10000 - errno;
if (close(fd) != 0)
result = -11000 - errno;
}
#else
// does the empty file exist?
fd = open("/working1/empty.txt", O_RDONLY);
if (fd == -1)
result = -12000 - errno;
else if (close(fd) != 0)
result = -13000 - errno;
if (unlink("/working1/empty.txt") != 0)
result = -14000 - errno;
// does the 'az' file exist, and does it contain 'az'?
fd = open("/working1/waka.txt", O_RDONLY);
if (fd == -1)
result = -15000 - errno;
else
{
char bf[4];
int bytes_read = read(fd,&bf[0],sizeof(bf));
if (bytes_read != 2)
result = -16000;
else if ((bf[0] != 'a') || (bf[1] != 'z'))
result = -17000;
if (close(fd) != 0)
result = -18000 - errno;
if (unlink("/working1/waka.txt") != 0)
result = -19000 - errno;
}
// does the random-ish file exist and does it contain SECRET?
fd = open("/working1/moar.txt", O_RDONLY);
if (fd == -1)
result = -20000 - errno;
else
{
char bf[256];
int bytes_read = read(fd,&bf[0],sizeof(bf));
if (bytes_read != strlen(SECRET))
result = -21000;
else
{
bf[strlen(SECRET)] = 0;
if (strcmp(bf,SECRET) != 0)
result = -22000;
}
if (close(fd) != 0)
result = -23000 - errno;
if (unlink("/working1/moar.txt") != 0)
result = -24000 - errno;
}
#endif
#if EXTRA_WORK
EM_ASM(
for (var i = 0; i < 100; i++) {
FS.syncfs(function (err) {
assert(!err);
console.log('extra work');
});
}
);
#endif
// sync from memory state to persisted and then
// run 'success'
EM_ASM(
FS.syncfs(function (err) {
assert(!err);
ccall('success', 'v');
});
);
}
int main() {
EM_ASM(
FS.mkdir('/working1');
FS.mount(IDBFS, {}, '/working1');
// sync from persisted state into memory and then
// run the 'test' function
FS.syncfs(true, function (err) {
assert(!err);
ccall('test', 'v');
});
);
emscripten_exit_with_live_runtime();
return 0;
}