diff --git a/test/fetch/response_headers.cpp b/test/fetch/response_headers.cpp deleted file mode 100644 index 2b0759bbbb21f..0000000000000 --- a/test/fetch/response_headers.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2018 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 -#include -#include -#include -#include - -int result = 1; - -int main() -{ - static const char* const headers[] = { - "X-Emscripten-Test", - "1", - 0, - }; - const size_t n_values = sizeof( headers ) / sizeof( headers[ 0 ] ) - 1; - emscripten_fetch_attr_t attr; - emscripten_fetch_attr_init( &attr ); - strcpy( attr.requestMethod, "GET" ); - attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS; - attr.requestHeaders = headers; - - attr.onsuccess = [] ( emscripten_fetch_t *fetch ) - { - assert( fetch->__attributes.requestHeaders != 0 ); - assert( fetch->__attributes.requestHeaders != headers ); - for ( size_t i = 0; i < n_values; ++i ) - { - const char* origHeader = headers[ i ]; - const char* header = fetch->__attributes.requestHeaders[ i ]; - assert( origHeader != header ); - assert( strcmp( origHeader, header ) == 0 ); - } - assert( fetch->__attributes.requestHeaders[ n_values ] == 0 ); - - printf( "Finished downloading %llu bytes\n", fetch->numBytes ); - // Compute rudimentary checksum of data - uint8_t checksum = 0; - for ( int i = 0; i < fetch->numBytes; ++i ) - checksum ^= fetch->data[ i ]; - printf( "Data checksum: %02X\n", checksum ); - assert( checksum == 0x08 ); - emscripten_fetch_close( fetch ); - - if ( result == 1 ) result = 0; - }; - - attr.onprogress = [] ( emscripten_fetch_t *fetch ) - { - if ( fetch->totalBytes > 0 ) - { - printf( "Downloading.. %.2f%% complete.\n", ( fetch->dataOffset + fetch->numBytes ) * 100.0 / fetch->totalBytes ); - } - else - { - printf( "Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes ); - } - }; - - attr.onerror = [] ( emscripten_fetch_t *fetch ) - { - printf( "Download failed!\n" ); - emscripten_fetch_close(fetch); - assert( false && "Shouldn't fail!" ); - }; - - emscripten_fetch_t *fetch = emscripten_fetch( &attr, "gears.png" ); - if ( result != 0 ) - { - result = 2; - printf( "emscripten_fetch() failed to run synchronously!\n" ); - } - return result; -} diff --git a/test/fetch/cached_xhr.cpp b/test/fetch/test_fetch_cached_xhr.cpp similarity index 88% rename from test/fetch/cached_xhr.cpp rename to test/fetch/test_fetch_cached_xhr.cpp index 396916fd368db..9efcfc1041fe5 100644 --- a/test/fetch/cached_xhr.cpp +++ b/test/fetch/test_fetch_cached_xhr.cpp @@ -12,8 +12,7 @@ int result = 0; // Fetch file without XHRing. -void fetchFromIndexedDB() -{ +void fetchFromIndexedDB() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); @@ -36,8 +35,7 @@ void fetchFromIndexedDB() } // XHR and store to cache. -int main() -{ +int main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); @@ -50,6 +48,10 @@ int main() // Test that the file now exists: fetchFromIndexedDB(); }; + attr.onerror = [](emscripten_fetch_t *fetch) { + printf("Error downloading\n"); + abort(); + }; attr.onprogress = [](emscripten_fetch_t *fetch) { if (fetch->totalBytes > 0) { printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes); @@ -60,6 +62,8 @@ int main() attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE; emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch != 0); - memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call. + // emscripten_fetch() must be able to operate without referencing to this + // structure after the call. + memset(&attr, 0, sizeof(attr)); return 99; } diff --git a/test/fetch/from_thread.cpp b/test/fetch/test_fetch_from_thread.cpp similarity index 100% rename from test/fetch/from_thread.cpp rename to test/fetch/test_fetch_from_thread.cpp diff --git a/test/fetch/headers_received.cpp b/test/fetch/test_fetch_headers_received.c similarity index 78% rename from test/fetch/headers_received.cpp rename to test/fetch/test_fetch_headers_received.c index 321be6b655111..32534627869f6 100644 --- a/test/fetch/headers_received.cpp +++ b/test/fetch/test_fetch_headers_received.c @@ -9,12 +9,11 @@ #include #include -void readyStateChange(emscripten_fetch_t *fetch) -{ - if(fetch->readyState != 2) return; +void readyStateChange(emscripten_fetch_t *fetch) { + if (fetch->readyState != 2) return; size_t headersLengthBytes = emscripten_fetch_get_response_headers_length(fetch) + 1; - char *headerString = new char[headersLengthBytes]; + char *headerString = malloc(headersLengthBytes); assert(headerString); emscripten_fetch_get_response_headers(fetch, headerString, headersLengthBytes); @@ -23,11 +22,10 @@ void readyStateChange(emscripten_fetch_t *fetch) char **responseHeaders = emscripten_fetch_unpack_response_headers(headerString); assert(responseHeaders); - delete[] headerString; + free(headerString); int numHeaders = 0; - for(; responseHeaders[numHeaders * 2]; ++numHeaders) - { + for (; responseHeaders[numHeaders * 2]; ++numHeaders) { // Check both the header and its value are present. assert(responseHeaders[(numHeaders * 2) + 1]); printf("Got response header: %s:%s\n", responseHeaders[numHeaders * 2], responseHeaders[(numHeaders * 2) + 1]); @@ -38,22 +36,27 @@ void readyStateChange(emscripten_fetch_t *fetch) emscripten_fetch_free_unpacked_response_headers(responseHeaders); } -void success(emscripten_fetch_t *fetch) -{ +void success(emscripten_fetch_t *fetch) { printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1]; emscripten_fetch_close(fetch); // Free data associated with the fetch. } -int main() -{ +void onerror(emscripten_fetch_t *fetch) { + printf("onerror: %d '%s'\n", fetch->status, fetch->statusText); + abort(); +} + +int main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE; attr.onsuccess = success; + attr.onerror = onerror; attr.onreadystatechange = readyStateChange; attr.timeoutMSecs = 2*60; + printf("Calling emscripten_fetch\n"); emscripten_fetch(&attr, "myfile.dat"); return 0; } diff --git a/test/fetch/idb_delete.cpp b/test/fetch/test_fetch_idb_delete.cpp similarity index 98% rename from test/fetch/idb_delete.cpp rename to test/fetch/test_fetch_idb_delete.cpp index 8c37392c33bb5..ee7655f4e3c8f 100644 --- a/test/fetch/idb_delete.cpp +++ b/test/fetch/test_fetch_idb_delete.cpp @@ -17,6 +17,7 @@ int main() strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE; emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); + printf("fetch->status: %d\n", fetch->status); assert(fetch->status == 200 && "Initial XHR GET of gears.png should have succeeded"); emscripten_fetch_close(fetch); diff --git a/test/fetch/idb_store.cpp b/test/fetch/test_fetch_idb_store.cpp similarity index 100% rename from test/fetch/idb_store.cpp rename to test/fetch/test_fetch_idb_store.cpp diff --git a/test/fetch/test_fetch_response_headers.cpp b/test/fetch/test_fetch_response_headers.cpp new file mode 100644 index 0000000000000..ec5178622dbfe --- /dev/null +++ b/test/fetch/test_fetch_response_headers.cpp @@ -0,0 +1,71 @@ +// Copyright 2018 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 +#include +#include +#include +#include + +int result = 1; + +int main() { + static const char* const headers[] = { + "X-Emscripten-Test", + "1", + 0, + }; + const size_t n_values = sizeof(headers) / sizeof(headers[0]) - 1; + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + strcpy(attr.requestMethod, "GET"); + attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS; + attr.requestHeaders = headers; + + attr.onsuccess = [] (emscripten_fetch_t *fetch) { + assert(fetch->__attributes.requestHeaders != 0); + assert(fetch->__attributes.requestHeaders != headers); + for (size_t i = 0; i < n_values; ++i) { + const char* origHeader = headers[i]; + const char* header = fetch->__attributes.requestHeaders[i]; + assert(origHeader != header); + assert(strcmp(origHeader, header) == 0); + } + assert(fetch->__attributes.requestHeaders[n_values] == 0); + + printf("Finished downloading %llu bytes\n", fetch->numBytes); + // Compute rudimentary checksum of data + uint8_t checksum = 0; + for (int i = 0; i < fetch->numBytes; ++i) { + checksum ^= fetch->data[i]; + } + printf("Data checksum: %02X\n", checksum); + assert(checksum == 0x08); + emscripten_fetch_close(fetch); + + if (result == 1) result = 0; + }; + + attr.onprogress = [] (emscripten_fetch_t *fetch) { + if (fetch->totalBytes > 0) { + printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes); + } else { + printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes); + } + }; + + attr.onerror = [] (emscripten_fetch_t *fetch) { + printf("Download failed!\n"); + emscripten_fetch_close(fetch); + assert(false && "Shouldn't fail!"); + }; + + emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); + if (result != 0) { + result = 2; + printf("emscripten_fetch() failed to run synchronously!\n"); + } + return result; +} diff --git a/test/fetch/stream_file.cpp b/test/fetch/test_fetch_stream_file.cpp similarity index 100% rename from test/fetch/stream_file.cpp rename to test/fetch/test_fetch_stream_file.cpp diff --git a/test/fetch/sync_fetch_in_main_thread.cpp b/test/fetch/test_fetch_sync_in_main_thread.c similarity index 100% rename from test/fetch/sync_fetch_in_main_thread.cpp rename to test/fetch/test_fetch_sync_in_main_thread.c diff --git a/test/fetch/sync_xhr.cpp b/test/fetch/test_fetch_sync_xhr.cpp similarity index 95% rename from test/fetch/sync_xhr.cpp rename to test/fetch/test_fetch_sync_xhr.cpp index a77c9aa2f37cd..b77a7748243a4 100644 --- a/test/fetch/sync_xhr.cpp +++ b/test/fetch/test_fetch_sync_xhr.cpp @@ -12,10 +12,9 @@ int result = -1; -int main() -{ - // If an exception is thrown from the user callback, it bubbles up to self.onerror but is otherwise completely - // swallowed by xhr.send. +int main() { + // If an exception is thrown from the user callback, it bubbles up to + // self.onerror but is otherwise completely swallowed by xhr.send. EM_ASM({self.onerror = function() { out('Got error'); HEAP32[$0 >> 2] = 2; diff --git a/test/fetch/to_indexeddb.cpp b/test/fetch/test_fetch_to_indexeddb.cpp similarity index 90% rename from test/fetch/to_indexeddb.cpp rename to test/fetch/test_fetch_to_indexeddb.cpp index 5634c1e268b1e..5d67c79f11736 100644 --- a/test/fetch/to_indexeddb.cpp +++ b/test/fetch/test_fetch_to_indexeddb.cpp @@ -9,8 +9,7 @@ #include #include -int main() -{ +int main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); @@ -45,6 +44,8 @@ int main() emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch != 0); - memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call. + // emscripten_fetch() must be able to operate without referencing to this + // structure after the call. + memset(&attr, 0, sizeof(attr)); return 99; } diff --git a/test/fetch/to_memory.cpp b/test/fetch/test_fetch_to_memory.cpp similarity index 100% rename from test/fetch/to_memory.cpp rename to test/fetch/test_fetch_to_memory.cpp diff --git a/test/fetch/xhr_abort.cpp b/test/fetch/test_fetch_xhr_abort.cpp similarity index 100% rename from test/fetch/xhr_abort.cpp rename to test/fetch/test_fetch_xhr_abort.cpp diff --git a/test/test_browser.py b/test/test_browser.py index 95057897a9344..ccf1ebd51c6d7 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -4823,13 +4823,13 @@ def test_preallocated_heap(self): @also_with_wasm2js def test_fetch_to_memory(self): # Test error reporting in the negative case when the file URL doesn't exist. (http 404) - self.btest_exit('fetch/to_memory.cpp', + self.btest_exit('fetch/test_fetch_to_memory.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST']) # Test the positive case when the file URL exists. (http 200) shutil.copyfile(test_file('gears.png'), 'gears.png') for arg in [[], ['-sFETCH_SUPPORT_INDEXEDDB=0']]: - self.btest_exit('fetch/to_memory.cpp', + self.btest_exit('fetch/test_fetch_to_memory.cpp', args=['-sFETCH_DEBUG', '-sFETCH'] + arg) @parameterized({ @@ -4840,22 +4840,20 @@ def test_fetch_to_memory(self): @requires_threads def test_fetch_from_thread(self, args): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/from_thread.cpp', + self.btest_exit('fetch/test_fetch_from_thread.cpp', args=args + ['-pthread', '-sPROXY_TO_PTHREAD', '-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST'], also_wasm2js=True) @also_with_wasm2js def test_fetch_to_indexdb(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/to_indexeddb.cpp', - args=['-sFETCH_DEBUG', '-sFETCH']) + self.btest_exit('fetch/test_fetch_to_indexeddb.cpp', args=['-sFETCH_DEBUG', '-sFETCH']) # Tests emscripten_fetch() usage to persist an XHR into IndexedDB and subsequently load up from there. @also_with_wasm2js def test_fetch_cached_xhr(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/cached_xhr.cpp', - args=['-sFETCH_DEBUG', '-sFETCH']) + self.btest_exit('fetch/test_fetch_cached_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH']) # Tests that response headers get set on emscripten_fetch_t values. @no_firefox('https://github.com/emscripten-core/emscripten/issues/16868') @@ -4863,7 +4861,7 @@ def test_fetch_cached_xhr(self): @requires_threads def test_fetch_response_headers(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD']) + self.btest_exit('fetch/test_fetch_response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD']) # Test emscripten_fetch() usage to stream a XHR in to memory without storing the full file in memory @also_with_wasm2js @@ -4877,15 +4875,16 @@ def test_fetch_stream_file(self): with open('largefile.txt', 'w') as f: for _ in range(1024): f.write(s) - self.btest_exit('fetch/stream_file.cpp', + self.btest_exit('fetch/test_fetch_stream_file.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sINITIAL_MEMORY=536870912']) def test_fetch_headers_received(self): - self.btest_exit('fetch/headers_received.cpp', args=['-sFETCH_DEBUG', '-sFETCH']) + create_file('myfile.dat', 'hello world\n') + self.btest_exit('fetch/test_fetch_headers_received.c', args=['-sFETCH_DEBUG', '-sFETCH']) def test_fetch_xhr_abort(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH']) + self.btest_exit('fetch/test_fetch_xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH']) # Tests emscripten_fetch() usage in synchronous mode when used from the main # thread proxied to a Worker with -sPROXY_TO_PTHREAD option. @@ -4894,7 +4893,7 @@ def test_fetch_xhr_abort(self): @requires_threads def test_fetch_sync_xhr(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD']) + self.btest_exit('fetch/test_fetch_sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD']) # Tests emscripten_fetch() usage when user passes none of the main 3 flags (append/replace/no_download). # In that case, in append is implicitly understood. @@ -4917,7 +4916,7 @@ def test_fetch_sync_xhr_in_wasm(self): @requires_threads def test_fetch_sync_xhr_in_proxy_to_worker(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/sync_xhr.cpp', + self.btest_exit('fetch/test_fetch_sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '--proxy-to-worker']) # Tests waiting on EMSCRIPTEN_FETCH_WAITABLE request from a worker thread @@ -4925,18 +4924,18 @@ def test_fetch_sync_xhr_in_proxy_to_worker(self): @requires_threads def test_fetch_sync_fetch_in_main_thread(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/sync_fetch_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD']) + self.btest_exit('fetch/test_fetch_sync_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD']) @requires_threads @disabled('https://github.com/emscripten-core/emscripten/issues/16746') def test_fetch_idb_store(self): - self.btest_exit('fetch/idb_store.cpp', args=['-pthread', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD']) + self.btest_exit('fetch/test_fetch_idb_store.cpp', args=['-pthread', '-sFETCH', '-sPROXY_TO_PTHREAD']) @requires_threads @disabled('https://github.com/emscripten-core/emscripten/issues/16746') def test_fetch_idb_delete(self): shutil.copyfile(test_file('gears.png'), 'gears.png') - self.btest_exit('fetch/idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD']) + self.btest_exit('fetch/test_fetch_idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD']) def test_fetch_post(self): self.btest_exit('fetch/test_fetch_post.c', args=['-sFETCH'])