diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-10 18:44:42 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-10 18:44:42 -0700 |
commit | 3ecb9d7f45e8af5a980940af3ed8bc17efd1a300 (patch) | |
tree | c80166f322c334ceff4cc20332cb276b24d66be8 | |
parent | ed84a998aeeb7930bdfa9935f3fdeaf9cf7c0cf2 (diff) | |
parent | 1cac914671205affad037b6777378d876cf6e36a (diff) |
Merge pull request #1495 from inolen/websocket_tests
more websocket test updates
20 files changed, 823 insertions, 1063 deletions
diff --git a/tests/runner.py b/tests/runner.py index d6ec37a3..f7a0fa26 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -12528,11 +12528,10 @@ elif 'browser' in str(sys.argv): i = sys.argv.index('browser') sys.argv = sys.argv[:i] + sys.argv[i+1:] sys.argv += [ - 'browser.test_sockets_bi', - 'browser.test_sockets_gethostbyname', - 'browser.test_sockets_bi_bigdata', - 'browser.test_sockets_select_server_down', - 'browser.test_sockets_select_server_closes_connection', + 'browser.test_sockets_echo', + 'browser.test_sockets_echo_bigdata', + 'browser.test_sockets_partial', + 'browser.test_sockets_select_server_no_accept', 'browser.test_sockets_select_server_closes_connection_rw', 'browser.test_enet' ] @@ -13784,15 +13783,17 @@ Press any key to continue.''' self.btest(program, reference=book_path(basename.replace('.bc', '.png')), args=args) - def btest(self, filename, expected=None, reference=None, reference_slack=0, + def btest(self, filename, expected=None, reference=None, force_c=False, reference_slack=0, args=[], outfile='test.html', message='.'): # TODO: use in all other tests - filepath = path_from_root('tests', filename) - temp_filepath = os.path.join(self.get_dir(), os.path.basename(filename)) + # if we are provided the source and not a path, use that + filename_is_src = '\n' in filename + src = filename if filename_is_src else '' + filepath = path_from_root('tests', filename) if not filename_is_src else ('main.c' if force_c else 'main.cpp') + temp_filepath = os.path.join(self.get_dir(), os.path.basename(filepath)) + if filename_is_src: + with open(temp_filepath, 'w') as f: f.write(src) if not reference: - if '\n' in filename: # if we are provided the source and not a path, use that - src = filename - filename = 'main.cpp' - else: + if not src: with open(filepath) as f: src = f.read() with open(temp_filepath, 'w') as f: f.write(self.with_report_result(src)) else: @@ -14068,16 +14069,16 @@ Press any key to continue.''' def test_emscripten_async_wget2(self): self.btest('http.cpp', expected='0', args=['-I' + path_from_root('tests')]) - - pids_to_clean = [] - def clean_pids(self): + + @staticmethod + def clean_pids(pids): import signal, errno def pid_exists(pid): try: - # NOTE: may just kill the process in Windows - os.kill(pid, 0) + # NOTE: may just kill the process in Windows + os.kill(pid, 0) except OSError, e: - return e.errno == errno.EPERM + return e.errno == errno.EPERM else: return True def kill_pids(pids, sig): @@ -14091,198 +14092,143 @@ Press any key to continue.''' except: print '[kill fail]' # ask nicely (to try and catch the children) - kill_pids(browser.pids_to_clean, signal.SIGTERM) + kill_pids(pids, signal.SIGTERM) time.sleep(1) # extreme prejudice, may leave children - kill_pids(browser.pids_to_clean, signal.SIGKILL) - browser.pids_to_clean = [] + kill_pids(pids, signal.SIGKILL) - # Runs a websocket server at a specific port. port is the true tcp socket we forward to, port+1 is the websocket one - class WebsockHarness: - def __init__(self, port, server_func=None, no_server=False): - self.port = port - self.server_func = server_func - self.no_server = no_server + class WebsockifyServerHarness: + def __init__(self, filename, args, listen_port, target_port): + self.pids = [] + self.filename = filename + self.target_port = target_port + self.listen_port = listen_port + self.args = args or [] def __enter__(self): import socket, websockify - if not self.no_server: - def server_func(q): - q.put(None) # No sub-process to start - ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock.bind(("127.0.0.1", self.port)) - ssock.listen(2) - while True: - csock, addr = ssock.accept() - print "Connection from %s" % repr(addr) - csock.send("te\x01\xff\x79st\x02") - - server_func = self.server_func or server_func - - server_queue = multiprocessing.Queue() - self.server = multiprocessing.Process(target=server_func, args=(server_queue,)) - self.server.start() - browser.pids_to_clean.append(self.server.pid) - while True: - if not server_queue.empty(): - spid = server_queue.get() - if spid: - browser.pids_to_clean.append(spid) - break - time.sleep(0.1) - print '[Socket server on processes %s]' % str(browser.pids_to_clean[-2:]) - - def websockify_func(wsp): wsp.start_server() - print >> sys.stderr, 'running websockify on %d, forward to tcp %d' % (self.port+1, self.port) - wsp = websockify.WebSocketProxy(verbose=True, listen_port=self.port+1, target_host="127.0.0.1", target_port=self.port, run_once=True) - self.websockify = multiprocessing.Process(target=websockify_func, args=(wsp,)) + # compile the server + # NOTE empty filename support is a hack to support + # the current test_enet + if self.filename: + Popen([CLANG_CC, path_from_root('tests', self.filename), '-o', 'server'] + self.args).communicate() + process = Popen([os.path.abspath('server')]) + self.pids.append(process.pid) + + # start the websocket proxy + print >> sys.stderr, 'running websockify on %d, forward to tcp %d' % (self.listen_port, self.target_port) + wsp = websockify.WebSocketProxy(verbose=True, listen_port=self.listen_port, target_host="127.0.0.1", target_port=self.target_port, run_once=True) + self.websockify = multiprocessing.Process(target=wsp.start_server) self.websockify.start() - browser.pids_to_clean.append(self.websockify.pid) - print '[Websockify on processes %s]' % str(browser.pids_to_clean[-2:]) + self.pids.append(self.websockify.pid) + print '[Websockify on process %s]' % str(self.pids[-2:]) def __exit__(self, *args, **kwargs): + # try to kill the websockify proxy gracefully if self.websockify.is_alive(): self.websockify.terminate() self.websockify.join() + # clean up any processes we started + browser.clean_pids(self.pids) + + + class CompiledServerHarness: + def __init__(self, filename, args): + self.pids = browser.PidContainer() + self.filename = filename + self.args = args or [] + + def __enter__(self): + import socket, websockify + + # compile the server + Popen([PYTHON, EMCC, path_from_root('tests', self.filename), '-o', 'server.js'] + self.args).communicate() + process = Popen([NODE_JS, 'server.js']) + self.pids.append(process.pid) + + def __exit__(self, *args, **kwargs): + # clean up any processes we started + self.pids.kill_all() + + def make_relay_server(self, port1, port2): + print >> sys.stderr, 'creating relay server on ports %d,%d' % (port1, port2) + proc = Popen([PYTHON, path_from_root('tests', 'sockets', 'socket_relay.py'), str(port1), str(port2)]) + return proc + # always run these tests last # make sure to use different ports in each one because it takes a while for the processes to be cleaned up - def test_sockets(self): - try: - with self.WebsockHarness(8990): - self.btest('sockets/test_sockets.c', expected='571', args=['-DSOCKK=8991']) - finally: - self.clean_pids() - def test_sockets_partial(self): - def partial(q): - import socket - - q.put(None) # No sub-process to start - ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock.bind(("127.0.0.1", 8990)) - ssock.listen(2) - while True: - csock, addr = ssock.accept() - print "Connection from %s" % repr(addr) - csock.send("\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09") - csock.send("\x08\x01\x02\x03\x04\x05\x06\x07\x08") - csock.send("\x07\x01\x02\x03\x04\x05\x06\x07") - csock.send("\x06\x01\x02\x03\x04\x05\x06") - csock.send("\x05\x01\x02\x03\x04\x05") - csock.send("\x04\x01\x02\x03\x04") - csock.send("\x03\x01\x02\x03") - csock.send("\x02\x01\x02") - csock.send("\x01\x01") + # NOTE all datagram tests are temporarily disabled, as + # we can't truly test datagram sockets until we have + # proper listen server support. - try: - with self.WebsockHarness(8990, partial): - self.btest('sockets/test_sockets_partial.c', expected='165', args=['-DSOCKK=8991']) - finally: - self.clean_pids() + def test_sockets_echo(self): + sockets_include = '-I'+path_from_root('tests', 'sockets') - def make_relay_server(self, port1, port2): - def relay_server(q): - print >> sys.stderr, 'creating relay server on ports %d,%d' % (port1, port2) - proc = Popen([PYTHON, path_from_root('tests', 'socket_relay.py'), str(port1), str(port2)]) - q.put(proc.pid) - proc.communicate() - return relay_server - - def test_sockets_bi(self): - for datagram in [0,1]: - for fileops in [0,1]: - try: - print >> sys.stderr, 'test_websocket_bi datagram %d, fileops %d' % (datagram, fileops) - with self.WebsockHarness(6992, self.make_relay_server(6992, 6994)): - with self.WebsockHarness(6994, no_server=True): - Popen([PYTHON, EMCC, path_from_root('tests', 'sockets/test_sockets_bi_side.c'), '-o', 'side.html', '-DSOCKK=6995', '-DTEST_DGRAM=%d' % datagram]).communicate() - self.btest('sockets/test_sockets_bi.c', expected='2499', args=['-DSOCKK=6993', '-DTEST_DGRAM=%d' % datagram, '-DTEST_FILE_OPS=%s' % fileops]) - finally: - self.clean_pids() + for datagram in [0]: + dgram_define = '-DTEST_DGRAM=%d' % datagram - def test_sockets_gethostbyname(self): - try: - with self.WebsockHarness(7000): - self.btest('sockets/test_sockets_gethostbyname.c', expected='571', args=['-O2', '-DSOCKK=7001']) - finally: - self.clean_pids() + for harness in [ + self.WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), ['-DSOCKK=8990', dgram_define, sockets_include], 8991, 8990) + # self.CompiledServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), ['-DSOCKK=8990', dgram_define, sockets_include]) + ]: + with harness: + self.btest(os.path.join('sockets', 'test_sockets_echo_client.c'), expected='0', args=['-DSOCKK=8991', dgram_define, sockets_include]) - def test_sockets_bi_bigdata(self): - try: - with self.WebsockHarness(3992, self.make_relay_server(3992, 3994)): - with self.WebsockHarness(3994, no_server=True): - Popen([PYTHON, EMCC, path_from_root('tests', 'sockets/test_sockets_bi_side_bigdata.c'), '-o', 'side.html', '-DSOCKK=3995', '-s', 'SOCKET_DEBUG=0', '-I' + path_from_root('tests/sockets')]).communicate() - self.btest('sockets/test_sockets_bi_bigdata.c', expected='0', args=['-DSOCKK=3993', '-s', 'SOCKET_DEBUG=0', '-I' + path_from_root('tests/sockets')]) - finally: - self.clean_pids() + def test_sockets_echo_bigdata(self): + sockets_include = '-I'+path_from_root('tests', 'sockets') - def test_sockets_select_server_down(self): - def closedServer(q): - import socket + for datagram in [0]: + dgram_define = '-DTEST_DGRAM=%d' % datagram - q.put(None) # No sub-process to start - ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock.bind(("127.0.0.1", 8994)) - try: - with self.WebsockHarness(8994, closedServer): - self.btest('sockets/test_sockets_select.c', expected='266', args=['-DSOCKK=8995']) - finally: - self.clean_pids() - - def test_sockets_select_server_closes_connection(self): - def closingServer(q): - import socket - - q.put(None) # No sub-process to start - ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock.bind(("127.0.0.1", 8994)) - ssock.listen(2) - while True: - csock, addr = ssock.accept() - print "Connection from %s" % repr(addr) - csock.send("1234567") - csock.close() + # generate a large string literal to use as our message + message = '' + for i in range(256*256*2): + message += str(unichr(ord('a') + (i % 26))) - try: - with self.WebsockHarness(8994, closingServer): - self.btest('sockets/test_sockets_select_server_closes_connection.c', expected='266', args=['-DSOCKK=8995']) - finally: - self.clean_pids() + # re-write the client test with this literal (it's too big to pass via command line) + input_filename = path_from_root('tests', 'sockets', 'test_sockets_echo_client.c') + input = open(input_filename).read() + output = input.replace('#define MESSAGE "pingtothepong"', '#define MESSAGE "%s"' % message) + + for harness in [ + self.WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), ['-DSOCKK=8992', dgram_define, sockets_include], 8993, 8992) + ]: + with harness: + self.btest(output, expected='0', args=['-DSOCKK=8993', dgram_define, sockets_include], force_c=True) + + def test_sockets_partial(self): + for harness in [ + self.WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_partial_server.c'), ['-DSOCKK=8994'], 8995, 8994) + ]: + with harness: + self.btest(os.path.join('sockets', 'test_sockets_partial_client.c'), expected='165', args=['-DSOCKK=8995']) + + # TODO add support for gethostbyaddr to re-enable this test + # def test_sockets_gethostbyname(self): + # self.btest(os.path.join('sockets', 'test_sockets_gethostbyname.c'), expected='0', args=['-O2', '-DSOCKK=8997']) + + def test_sockets_select_server_no_accept(self): + for harness in [ + self.WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_select_server_no_accept_server.c'), ['-DSOCKK=8995'], 8996, 8995) + ]: + self.btest(os.path.join('sockets', 'test_sockets_select_server_no_accept_client.c'), expected='266', args=['-DSOCKK=8996']) def test_sockets_select_server_closes_connection_rw(self): - def closingServer_rw(q): - import socket - - q.put(None) # No sub-process to start - ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock.bind(("127.0.0.1", 8998)) - ssock.listen(2) - while True: - csock, addr = ssock.accept() - print "Connection from %s" % repr(addr) - readArray = bytearray(10) - #readBuffer = buffer(readArray) - bytesRead = 0 - # Let the client start to write data - while (bytesRead < 10): - (readBytes, address) = csock.recvfrom_into( readArray, 10 ) - bytesRead += readBytes - print "server: 10 bytes read" - # Now we write a message on our own ... - csock.send("0123456789") - print "server: 10 bytes written" - # And immediately close the connection - csock.close() - print "server: connection closed" + sockets_include = '-I'+path_from_root('tests', 'sockets') - try: - with self.WebsockHarness(8998, closingServer_rw): - self.btest('sockets/test_sockets_select_server_closes_connection_rw.c', expected='266', args=['-DSOCKK=8999']) - finally: - self.clean_pids() + for harness in [ + self.WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), ['-DSOCKK=9004', sockets_include], 9005, 9004) + ]: + with harness: + self.btest(os.path.join('sockets', 'test_sockets_select_server_closes_connection_client_rw.c'), expected='266', args=['-DSOCKK=9005', sockets_include]) + # TODO remove this once we have proper listen server support built into emscripten. + # being that enet uses datagram sockets, we can't proxy to a native server with + # websockify, so we're emulating the listen server in the browser and relaying + # between two TCP servers. def test_enet(self): try_delete(self.in_dir('enet')) shutil.copytree(path_from_root('tests', 'enet'), self.in_dir('enet')) @@ -14292,14 +14238,34 @@ Press any key to continue.''' Popen([PYTHON, path_from_root('emmake'), 'make']).communicate() enet = [self.in_dir('enet', '.libs', 'libenet.a'), '-I'+path_from_root('tests', 'enet', 'include')] os.chdir(pwd) - Popen([PYTHON, EMCC, path_from_root('tests', 'enet_server.c'), '-o', 'server.html'] + enet).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sockets', 'test_enet_server.c'), '-o', 'server.html', '-DSOCKK=2235'] + enet).communicate() - try: - with self.WebsockHarness(1234, self.make_relay_server(1234, 1236)): - with self.WebsockHarness(1236, no_server=True): - self.btest('enet_client.c', expected='0', args=enet) - finally: - self.clean_pids() + with self.WebsockifyServerHarness('', [], 2235, 2234): + with self.WebsockifyServerHarness('', [], 2237, 2236): + pids = [] + try: + proc = self.make_relay_server(2234, 2236) + pids.append(proc.pid) + self.btest(os.path.join('sockets', 'test_enet_client.c'), expected='0', args=['-DSOCKK=2237'] + enet) + finally: + browser.clean_pids(pids); + + # TODO use this once we have listen server support + # def test_enet(self): + # try_delete(self.in_dir('enet')) + # shutil.copytree(path_from_root('tests', 'enet'), self.in_dir('enet')) + # pwd = os.getcwd() + # os.chdir(self.in_dir('enet')) + # Popen([PYTHON, path_from_root('emconfigure'), './configure']).communicate() + # Popen([PYTHON, path_from_root('emmake'), 'make']).communicate() + # enet = [self.in_dir('enet', '.libs', 'libenet.a'), '-I'+path_from_root('tests', 'enet', 'include')] + # os.chdir(pwd) + + # for harness in [ + # self.CompiledServerHarness(os.path.join('sockets', 'test_enet_server.c'), ['-DSOCKK=9010'] + enet, 9011, 9010) + # ]: + # with harness: + # self.btest(os.path.join('sockets', 'test_enet_client.c'), expected='0', args=['-DSOCKK=9011'] + enet) elif 'benchmark' in str(sys.argv): # Benchmarks. Run them with argument |benchmark|. To run a specific test, do diff --git a/tests/socket_relay.py b/tests/sockets/socket_relay.py index 5b6403f9..5b6403f9 100644 --- a/tests/socket_relay.py +++ b/tests/sockets/socket_relay.py diff --git a/tests/enet_client.c b/tests/sockets/test_enet_client.c index 601b8769..bf14375c 100644 --- a/tests/enet_client.c +++ b/tests/sockets/test_enet_client.c @@ -1,20 +1,22 @@ #include <stdio.h> -#include <emscripten.h> - +#include <string.h> #include <enet/enet.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif ENetHost * host; void main_loop() { - static int counter = 0; #if EMSCRIPTEN + static int counter = 0; counter++; -#endif if (counter == 100) { printf("stop!\n"); emscripten_cancel_main_loop(); return; } +#endif ENetEvent event; if (enet_host_service (host, & event, 0) == 0) return; @@ -32,7 +34,11 @@ void main_loop() { event.channelID); int result = strcmp("packetfoo", event.packet->data); +#if EMSCRIPTEN REPORT_RESULT(); +#else + exit(EXIT_SUCCESS); +#endif /* Clean up the packet now that we're done using it. */ enet_packet_destroy (event.packet); @@ -73,11 +79,7 @@ int main (int argc, char ** argv) ENetAddress address; enet_address_set_host (& address, "localhost"); -#if EMSCRIPTEN - address.port = 1237; -#else - address.port = 1235; -#endif + address.port = SOCKK; printf("connecting to server...\n"); @@ -100,7 +102,11 @@ int main (int argc, char ** argv) "console.log('added.');"); #endif +#if EMSCRIPTEN emscripten_set_main_loop(main_loop, 3, 1); +#else + while (1) main_loop(); +#endif return 1; } diff --git a/tests/enet_server.c b/tests/sockets/test_enet_server.c index a8167e16..9a4518ac 100644 --- a/tests/enet_server.c +++ b/tests/sockets/test_enet_server.c @@ -1,10 +1,13 @@ // g++ -fpermissive ../enet_server.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_server ; g++ ../enet_client.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_client #include <stdio.h> -#include <emscripten.h> - +#include <string.h> #include <enet/enet.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + ENetHost *host; void send_msg(ENetPeer *peer) { @@ -31,7 +34,9 @@ void main_loop() { #endif if (counter == 100) { printf("stop!\n"); +#if EMSCRIPTEN emscripten_cancel_main_loop(); +#endif return; } @@ -82,7 +87,7 @@ int main (int argc, char ** argv) ENetAddress address; address.host = ENET_HOST_ANY; - address.port = 1235; + address.port = SOCKK; printf("create!\n"); host = enet_host_create (& address /* the address to bind the server host to */, 32 /* allow up to 32 clients and/or outgoing connections */, @@ -96,7 +101,11 @@ int main (int argc, char ** argv) exit (EXIT_FAILURE); } +#if EMSCRIPTEN emscripten_set_main_loop(main_loop, 3, 1); +#else + while (1) main_loop(); +#endif return 1; } diff --git a/tests/sockets/test_sockets.c b/tests/sockets/test_sockets.c deleted file mode 100644 index 8845ef43..00000000 --- a/tests/sockets/test_sockets.c +++ /dev/null @@ -1,143 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <assert.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#define EXPECTED_BYTES 5 - -int sockfd; -int not_always_data = 0; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - // select check for IO - fd_set sett; - FD_ZERO(&sett); - assert(select(64, &sett, NULL, NULL, NULL) == 0); // empty set - FD_SET(sock, &sett); - assert(select(0, &sett, NULL, NULL, NULL) == 0); // max FD to check is 0 - assert(FD_ISSET(sock, &sett) == 0); - FD_SET(sock, &sett); - int select_says_yes = select(64, &sett, NULL, NULL, NULL); - - // ioctl check for IO - int bytes; - if (ioctl(sock, FIONREAD, &bytes) || bytes == 0) { - not_always_data = 1; - printf("ioctl says 0, FD_ISSET says %ld\n", FD_ISSET(sock, &sett)); - assert(FD_ISSET(sock, &sett) == 0); - return 0; - } - - assert(FD_ISSET(sock, &sett)); - assert(select_says_yes); // ioctl must agree with select - - char buffer[1024]; - int n; - unsigned int offset = 0; - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || - errno == EINTR) { - if(n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - fprintf(stderr, "error in get_all_buf! %d", errno); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter(void *arg) { - static char out[1024*2]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - int n = get_all_buf(sockfd, out+pos, 1024-pos); - if (n) printf("read! %d\n", n); - pos += n; - if (pos >= EXPECTED_BYTES) { - int i, sum = 0; - for (i=0; i < pos; i++) { - printf("%x\n", out[i]); - sum += out[i]; - } - - shutdown(sockfd, SHUT_RDWR); - - close(sockfd); - - printf("sum: %d\n", sum); - finish(sum); - } -} - -void main() { - struct sockaddr_in addr; - int res; - - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_set_main_loop(iter, 0, 0); -#else - while (1) iter(NULL); -#endif -} - diff --git a/tests/sockets/test_sockets_bi.c b/tests/sockets/test_sockets_bi.c deleted file mode 100644 index 4266d20c..00000000 --- a/tests/sockets/test_sockets_bi.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endif - -#define EXPECTED_BYTES 28 - -int sockfd; - -void finish(int result) { - close(sockfd); -#if EMSCRIPTEN - REPORT_RESULT(); -#endif - exit(result); -} - -unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) { - int bytes; - if (ioctl(sock, FIONREAD, &bytes)) return 0; - if (bytes == 0) return 0; - - char buffer[1024]; - int n; - unsigned int offset = 0; -#if TEST_FILE_OPS - while((errno = 0, (n = read(sock, buffer, sizeof(buffer)))>0) || -#else - while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) || -#endif - errno == EINTR) { - if(n > 0) { - if (((unsigned int) n)+offset > maxsize) { - fprintf(stderr, "too much data!"); - finish(EXIT_FAILURE); - } - memcpy(output+offset, buffer, n); - offset += n; - } - } - - if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { - fprintf(stderr, "error in get_all_buf!"); - finish(EXIT_FAILURE); - } - return offset; -} - -void iter(void *arg) { - static char out[1024*2]; - static int pos = 0; - fd_set fdr; - int res; - - // make sure that sockfd is ready to read - FD_ZERO(&fdr); - FD_SET(sockfd, &fdr); - res = select(64, &fdr, NULL, NULL, NULL); - if (res == -1) { - perror("select failed"); - finish(EXIT_FAILURE); - } else if (!FD_ISSET(sockfd, &fdr)) { - return; - } - - // perform read write operations ... - int n = get_all_buf(sockfd, out+pos, 1024-pos); - if (n) printf("read! %d\n", n); - pos += n; - if (pos >= EXPECTED_BYTES) { - int i, sum = 0; - for (i=0; i < pos; i++) { - printf("%x\n", out[i]); - sum += out[i]; - } - - shutdown(sockfd, SHUT_RDWR); - close(sockfd); - - printf("sum: %d\n", sum); - finish(sum); - } -} - -int main() { - struct sockaddr_in addr; - int res; - - printf("hello from main page\n"); - -#if !TEST_DGRAM - sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); -#else - sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); -#endif - if (sockfd == -1) { - perror("cannot create socket"); - finish(EXIT_FAILURE); - } - fcntl(sockfd, F_SETFL, O_NONBLOCK); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(SOCKK); - if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { - perror("inet_pton failed"); - finish(EXIT_FAILURE); - } - - res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); - if (res == -1 && errno != EINPROGRESS) { - perror("connect failed"); - finish(EXIT_FAILURE); - } - -#if EMSCRIPTEN - emscripten_run_script("console.log('adding iframe');" - "var iframe = document.createElement('iframe');" - "iframe.src = 'side.html';" - "document.body.appendChild(iframe);" - "console.log('added.');"); - emscripten_set_main_loop(iter, 0, 0); -#else - while (1) iter(NULL); -#endif - - return EXIT_SUCCESS; -} diff --git a/tests/sockets/test_sockets_bi_bigdata.c b/tests/sockets/test_sockets_bi_bigdata.c deleted file mode 100644 index c1d8100e..00000000 --- a/tests/sockets/test_sockets_bi_bigdata.c +++ /dev/null @@ -1,138 +0,0 @@ -#include <errno.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#if EMSCRIPTEN -#include <emscripten.h> -#endi |