@@ -42,7 +42,7 @@ void check_file(int fd, const char* content) {
4242void test_url_relative () {
4343 printf ("Running %s...\n" , __FUNCTION__ );
4444
45- backend_t backend2 = wasmfs_create_fetch_backend ("test.txt" );
45+ backend_t backend2 = wasmfs_create_fetch_backend ("test.txt" , 0 );
4646 int fd = wasmfs_create_file ("/file_rel" , 0777 , backend2 );
4747 check_file (fd , "fetch 2" );
4848 assert (close (fd ) == 0 );
@@ -56,7 +56,7 @@ void test_url_absolute() {
5656 char url [200 ];
5757 snprintf (url , sizeof (url ), "%s%s" , url_orig , file_name );
5858
59- backend_t backend = wasmfs_create_fetch_backend (url );
59+ backend_t backend = wasmfs_create_fetch_backend (url , 0 );
6060 int fd = wasmfs_create_file (file_name , 0777 , backend );
6161 check_file (fd , "fetch 2" );
6262 assert (close (fd ) == 0 );
@@ -69,7 +69,7 @@ void test_directory_abs() {
6969 char url [200 ];
7070 snprintf (url , sizeof (url ), "%s%s" , url_orig , dir_path );
7171
72- backend_t backend = wasmfs_create_fetch_backend (url );
72+ backend_t backend = wasmfs_create_fetch_backend (url , 0 );
7373 int res = wasmfs_create_directory (dir_path , 0777 , backend );
7474 if (errno )
7575 perror ("wasmfs_create_directory" );
@@ -100,7 +100,7 @@ void test_directory_abs() {
100100
101101void test_default () {
102102 printf ("Running %s...\n" , __FUNCTION__ );
103- backend_t backend = wasmfs_create_fetch_backend ("data.dat" );
103+ backend_t backend = wasmfs_create_fetch_backend ("data.dat" , 0 );
104104
105105 // Create a file in that backend.
106106 int fd = wasmfs_create_file ("/testfile" , 0777 , backend );
@@ -136,7 +136,7 @@ void test_small_reads() {
136136 char expected [] = "hello" ;
137137 size_t size = 5 ;
138138
139- backend_t backend = wasmfs_create_fetch_backend ("small.dat" );
139+ backend_t backend = wasmfs_create_fetch_backend ("small.dat" , 0 );
140140 int fd = wasmfs_create_file ("/testfile3" , 0777 , backend );
141141 char buf [size + 1 ];
142142 for (size_t i = 0 ; i < size ; i ++ ) {
@@ -150,6 +150,108 @@ void test_small_reads() {
150150 assert (close (fd ) == 0 );
151151}
152152
153+ void test_small_chunks () {
154+ // Read the file in small amounts.
155+ printf ("Running %s...\n" , __FUNCTION__ );
156+
157+ char expected [] = "hello" ;
158+ size_t size = 5 ;
159+
160+ backend_t backend = wasmfs_create_fetch_backend ("small.dat" ,2 );
161+ int fd ;
162+ char buf [size + 1 ];
163+ fd = wasmfs_create_file ("/testfile4" , 0777 , backend );
164+ for (size_t i = 0 ; i < size ; i += 1 ) {
165+ int read_now = read (fd , buf + i , 1 );
166+ assert (read_now <= 1 );
167+ printf ("read some bytes smaller than chunk size\n" );
168+ }
169+ buf [size ] = 0 ;
170+ printf ("buf %s\n" ,buf );
171+ assert (strcmp (buf , "hello" ) == 0 );
172+
173+ assert (close (fd ) == 0 );
174+
175+ fd = wasmfs_create_file ("/testfile5" , 0777 , backend );
176+ for (size_t i = 0 ; i < size ; i += 2 ) {
177+ int read_now = read (fd , buf + i , 2 );
178+ assert (read_now <= 2 );
179+ printf ("read some bytes equal to chunk size\n" );
180+ }
181+ buf [size ] = 0 ;
182+ printf ("buf %s\n" ,buf );
183+ assert (strcmp (buf , "hello" ) == 0 );
184+
185+ assert (close (fd ) == 0 );
186+
187+ fd = wasmfs_create_file ("/testfile6" , 0777 , backend );
188+ for (size_t i = 0 ; i < size ; i += 5 ) {
189+ int read_now = read (fd , buf + i , 5 );
190+ assert (read_now <= 5 );
191+ printf ("read some bytes much larger than chunk size\n" );
192+ }
193+ buf [size ] = 0 ;
194+ printf ("buf %s\n" ,buf );
195+ assert (strcmp (buf , "hello" ) == 0 );
196+
197+ assert (close (fd ) == 0 );
198+ }
199+
200+ void test_small_chunks_divisor_of_size () {
201+ printf ("Running %s...\n" , __FUNCTION__ );
202+
203+ char expected [] = "hello, fetch" ;
204+ size_t size = 12 ;
205+
206+ backend_t backend = wasmfs_create_fetch_backend ("data.dat" ,4 );
207+ int fd ;
208+ char buf [size + 1 ];
209+ fd = wasmfs_create_file ("/testfile7" , 0777 , backend );
210+ for (size_t i = 0 ; i < size ; i += 3 ) {
211+ int read_now = read (fd , buf + i , 3 );
212+ assert (read_now <= 3 );
213+ printf ("read some bytes less than chunk size\n" );
214+ }
215+ buf [size ] = 0 ;
216+ printf ("buf %s\n" ,buf );
217+ assert (strcmp (buf , "hello, fetch" ) == 0 );
218+ assert (close (fd ) == 0 );
219+
220+ fd = wasmfs_create_file ("/testfile8" , 0777 , backend );
221+ for (size_t i = 0 ; i < size ; i += 4 ) {
222+ int read_now = read (fd , buf + i , 4 );
223+ assert (read_now <= 4 );
224+ printf ("read some bytes equal to chunk size\n" );
225+ }
226+ buf [size ] = 0 ;
227+ printf ("buf %s\n" ,buf );
228+ assert (strcmp (buf , "hello, fetch" ) == 0 );
229+
230+ assert (close (fd ) == 0 );
231+
232+ fd = wasmfs_create_file ("/testfile9" , 0777 , backend );
233+ for (size_t i = 0 ; i < size ; i += 5 ) {
234+ int read_now = read (fd , buf + i , 5 );
235+ assert (read_now <= 5 );
236+ printf ("read some bytes greater than chunk size\n" );
237+ }
238+ buf [size ] = 0 ;
239+ printf ("buf %s\n" ,buf );
240+ assert (strcmp (buf , "hello, fetch" ) == 0 );
241+
242+ assert (close (fd ) == 0 );
243+
244+ fd = wasmfs_create_file ("/testfile10" , 0777 , backend );
245+ int read_now = read (fd , buf , 12 );
246+ assert (read_now == 12 );
247+ printf ("read some bytes much greater than chunk size\n" );
248+ buf [size ] = 0 ;
249+ printf ("buf %s\n" ,buf );
250+ assert (strcmp (buf , "hello, fetch" ) == 0 );
251+
252+ assert (close (fd ) == 0 );
253+ }
254+
153255void test_nonexistent () {
154256 printf ("Running %s...\n" , __FUNCTION__ );
155257
@@ -185,6 +287,8 @@ int main() {
185287 test_url_absolute ();
186288 test_directory_abs ();
187289 test_small_reads ();
290+ test_small_chunks ();
291+ test_small_chunks_divisor_of_size ();
188292 test_nonexistent ();
189293
190294 return 0 ;
0 commit comments