5
5
* found in the LICENSE file.
6
6
*/
7
7
8
+ #include <assert.h>
9
+ #include <stdbool.h>
10
+ #include <stdlib.h>
8
11
#include <stdio.h>
9
12
#include <emscripten.h>
10
13
#include <fcntl.h>
13
16
#include <errno.h>
14
17
#include <string.h>
15
18
16
- int result = 1 ;
19
+ EM_JS_DEPS ( deps , "$callUserCallback" ) ;
17
20
18
- void report_result () {
19
- REPORT_RESULT (result );
20
- #ifdef FORCE_EXIT
21
+ bool test_complete = false;
22
+
23
+ void finish () {
24
+ printf ("finish\n" );
25
+ test_complete = true;
21
26
emscripten_force_exit (0 );
22
- #endif
27
+ }
28
+
29
+ void cleanup () {
30
+ // If the test failed, then delete test files from IndexedDB so that the test
31
+ // runner will not leak test state to subsequent tests that reuse this same
32
+ // file.
33
+ printf ("cleaning up test files\n" );
34
+ unlink ("/working1/empty.txt" );
35
+ unlink ("/working1/waka.txt" );
36
+ unlink ("/working1/moar.txt" );
37
+ rmdir ("/working1/dir" );
38
+ EM_ASM (FS .syncfs (function (){})); // And persist deleted changes
23
39
}
24
40
25
41
void test () {
26
- int fd ;
42
+ int fd , res ;
27
43
struct stat st ;
28
44
29
45
#if FIRST
46
+ printf ("running test FIRST half ..\n" );
47
+
48
+ // Run cleanup first in case a previous test failed half way through.
49
+ cleanup ();
30
50
31
51
// for each file, we first make sure it doesn't currently exist
32
52
// (we delete it at the end of !FIRST). We then test an empty
33
53
// file plus two files each with a small amount of content
34
54
35
55
// the empty file
36
- if ((stat ("/working1/empty.txt" , & st ) != -1 ) || (errno != ENOENT ))
37
- result = -1000 - errno ;
56
+ res = stat ("/working1/empty.txt" , & st );
57
+ assert (res == -1 && errno == ENOENT );
58
+
38
59
fd = open ("/working1/empty.txt" , O_RDWR | O_CREAT , 0666 );
39
- if (fd == -1 )
40
- result = -2000 - errno ;
41
- else if (close (fd ) != 0 )
42
- result = -3000 - errno ;
60
+ assert (fd != -1 );
61
+ res = close (fd );
62
+ assert (res == 0 );
43
63
44
64
// a file whose contents are just 'az'
45
- if (( stat ("/working1/waka.txt" , & st ) != -1 ) || ( errno != ENOENT ))
46
- result = -4000 - errno ;
65
+ res = stat ("/working1/waka.txt" , & st );
66
+ assert ( res == -1 && errno == ENOENT ) ;
47
67
fd = open ("/working1/waka.txt" , O_RDWR | O_CREAT , 0666 );
48
- if (fd == -1 )
49
- result = -5000 - errno ;
50
- else {
51
- if (write (fd ,"az" ,2 ) != 2 )
52
- result = -6000 - errno ;
53
- if (close (fd ) != 0 )
54
- result = -7000 - errno ;
55
- }
68
+ assert (fd != -1 );
69
+ res = write (fd , "az" , 2 );
70
+ assert (res == 2 );
71
+ res = close (fd );
72
+ assert (res == 0 );
56
73
57
74
// a file whose contents are random-ish string set by the test_browser.py file
58
- if (( stat ("/working1/moar.txt" , & st ) != -1 ) || ( errno != ENOENT ))
59
- result = -8000 - errno ;
75
+ res = stat ("/working1/moar.txt" , & st );
76
+ assert ( res == -1 && errno == ENOENT ) ;
60
77
fd = open ("/working1/moar.txt" , O_RDWR | O_CREAT , 0666 );
61
- if (fd == -1 )
62
- result = -9000 - errno ;
63
- else {
64
- if (write (fd , SECRET , strlen (SECRET )) != strlen (SECRET ))
65
- result = -10000 - errno ;
66
- if (close (fd ) != 0 )
67
- result = -11000 - errno ;
68
- }
78
+ assert (fd != -1 );
79
+ res = write (fd , SECRET , strlen (SECRET ));
80
+ assert (res == strlen (SECRET ));
81
+ res = close (fd );
82
+ assert (res == 0 );
69
83
70
84
// a directory
71
- if (( stat ("/working1/dir" , & st ) != -1 ) || ( errno != ENOENT ))
72
- result = -12000 - errno ;
73
- else if ( mkdir ("/working1/dir" , 0777 ) != 0 )
74
- result = -13000 - errno ;
85
+ res = stat ("/working1/dir" , & st );
86
+ assert ( res == -1 && errno == ENOENT ) ;
87
+ res = mkdir ("/working1/dir" , 0777 );
88
+ assert ( res == 0 ) ;
75
89
76
90
#else
91
+ printf ("running test SECOND half ..\n" );
77
92
78
93
// does the empty file exist?
79
94
fd = open ("/working1/empty.txt" , O_RDONLY );
80
- if (fd == -1 )
81
- result = -14000 - errno ;
82
- else if (close (fd ) != 0 )
83
- result = -15000 - errno ;
84
- if (unlink ("/working1/empty.txt" ) != 0 )
85
- result = -16000 - errno ;
95
+ assert (fd != -1 );
96
+ res = close (fd );
97
+ assert (res == 0 );
98
+ res = unlink ("/working1/empty.txt" );
99
+ assert (res == 0 );
86
100
87
101
// does the 'az' file exist, and does it contain 'az'?
88
102
fd = open ("/working1/waka.txt" , O_RDONLY );
89
- if (fd == -1 )
90
- result = -17000 - errno ;
91
- else {
103
+ assert (fd != -1 );
104
+ {
92
105
char bf [4 ];
93
106
int bytes_read = read (fd ,& bf [0 ],sizeof (bf ));
94
- if (bytes_read != 2 )
95
- result = -18000 ;
96
- else if ((bf [0 ] != 'a' ) || (bf [1 ] != 'z' ))
97
- result = -19000 ;
98
- if (close (fd ) != 0 )
99
- result = -20000 - errno ;
100
- if (unlink ("/working1/waka.txt" ) != 0 )
101
- result = -21000 - errno ;
107
+ assert (bytes_read == 2 );
108
+ assert (bf [0 ] == 'a' && bf [1 ] == 'z' );
102
109
}
110
+ res = close (fd );
111
+ assert (res == 0 );
112
+ res = unlink ("/working1/waka.txt" );
113
+ assert (res == 0 );
103
114
104
115
// does the random-ish file exist and does it contain SECRET?
105
116
fd = open ("/working1/moar.txt" , O_RDONLY );
106
- if (fd == -1 ) {
107
- result = -22000 - errno ;
108
- } else {
117
+ assert (fd != -1 );
118
+ {
109
119
char bf [256 ];
110
120
int bytes_read = read (fd ,& bf [0 ],sizeof (bf ));
111
- if (bytes_read != strlen (SECRET )) {
112
- result = -23000 ;
113
- } else {
114
- bf [strlen (SECRET )] = 0 ;
115
- if (strcmp (bf ,SECRET ) != 0 )
116
- result = -24000 ;
117
- }
118
- if (close (fd ) != 0 )
119
- result = -25000 - errno ;
120
- if (unlink ("/working1/moar.txt" ) != 0 )
121
- result = -26000 - errno ;
121
+ assert (bytes_read == strlen (SECRET ));
122
+ bf [strlen (SECRET )] = 0 ;
123
+ assert (strcmp (bf , SECRET ) == 0 );
122
124
}
125
+ res = close (fd );
126
+ assert (res == 0 );
127
+ res = unlink ("/working1/moar.txt" );
128
+ assert (res == 0 );
123
129
124
130
// does the directory exist?
125
- if (stat ("/working1/dir" , & st ) != 0 ) {
126
- result = -27000 - errno ;
127
- } else {
128
- if (!S_ISDIR (st .st_mode ))
129
- result = -28000 ;
130
- if (rmdir ("/working1/dir" ) != 0 )
131
- result = -29000 - errno ;
132
- }
131
+ res = stat ("/working1/dir" , & st );
132
+ assert (res == 0 );
133
+ assert (S_ISDIR (st .st_mode ));
134
+ res = rmdir ("/working1/dir" );
135
+ assert (res == 0 );
133
136
134
137
#endif
135
138
136
- // If the test failed, then delete test files from IndexedDB so that the test
137
- // runner will not leak test state to subsequent tests that reuse this same
138
- // file.
139
- if (result != 1 ) {
140
- unlink ("/working1/empty.txt" );
141
- unlink ("/working1/waka.txt" );
142
- unlink ("/working1/moar.txt" );
143
- rmdir ("/working1/dir" );
144
- EM_ASM (FS .syncfs (function (){})); // And persist deleted changes
145
- }
139
+ printf ("done test ..\n" );
146
140
147
141
#if EXTRA_WORK && !FIRST
148
142
EM_ASM (
@@ -156,27 +150,29 @@ void test() {
156
150
#endif
157
151
158
152
#ifdef IDBFS_AUTO_PERSIST
159
- report_result ();
153
+ finish ();
160
154
#else
161
155
// sync from memory state to persisted and then
162
- // run 'report_result '
163
- EM_ASM (
156
+ // run 'finish '
157
+ EM_ASM ({
164
158
// Ensure IndexedDB is closed at exit.
165
- Module ['onExit' ] = function () {
159
+ var orig = Module ['onExit' ];
160
+ Module ['onExit' ] = (status ) = > {
166
161
assert (Object .keys (IDBFS .dbs ).length == 0 );
162
+ orig (status );
167
163
};
168
- FS .syncfs (function (err ) {
164
+ FS .syncfs ((err ) = > {
169
165
assert (!err );
170
- ccall ('report_result' , 'v' );
166
+ callUserCallback (_finish );
167
+ });
171
168
});
172
- );
173
169
#endif
174
170
}
175
171
176
172
int main () {
177
173
EM_ASM (
178
174
FS .mkdir ('/working1' );
179
- FS .mount (IDBFS , {
175
+ FS .mount (IDBFS , {
180
176
#ifdef IDBFS_AUTO_PERSIST
181
177
autoPersist : true
182
178
#endif
@@ -191,7 +187,7 @@ int main() {
191
187
// run the 'test' function
192
188
FS .syncfs (true, function (err ) {
193
189
assert (!err );
194
- ccall ( 'test' , 'v' );
190
+ callUserCallback ( _test );
195
191
});
196
192
);
197
193
0 commit comments