aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-08-10 18:44:42 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-08-10 18:44:42 -0700
commit3ecb9d7f45e8af5a980940af3ed8bc17efd1a300 (patch)
treec80166f322c334ceff4cc20332cb276b24d66be8 /tests
parented84a998aeeb7930bdfa9935f3fdeaf9cf7c0cf2 (diff)
parent1cac914671205affad037b6777378d876cf6e36a (diff)
Merge pull request #1495 from inolen/websocket_tests
more websocket test updates
Diffstat (limited to 'tests')
-rwxr-xr-xtests/runner.py344
-rw-r--r--tests/sockets/socket_relay.py (renamed from tests/socket_relay.py)0
-rw-r--r--tests/sockets/test_enet_client.c (renamed from tests/enet_client.c)24
-rw-r--r--tests/sockets/test_enet_server.c (renamed from tests/enet_server.c)15
-rw-r--r--tests/sockets/test_sockets.c143
-rw-r--r--tests/sockets/test_sockets_bi.c138
-rw-r--r--tests/sockets/test_sockets_bi_bigdata.c138
-rw-r--r--tests/sockets/test_sockets_bi_side.c93
-rw-r--r--tests/sockets/test_sockets_bi_side_bigdata.c90
-rw-r--r--tests/sockets/test_sockets_bigdata.h20
-rw-r--r--tests/sockets/test_sockets_echo_client.c148
-rw-r--r--tests/sockets/test_sockets_echo_server.c152
-rw-r--r--tests/sockets/test_sockets_gethostbyname.c131
-rw-r--r--tests/sockets/test_sockets_msg.h78
-rw-r--r--tests/sockets/test_sockets_partial_client.c (renamed from tests/sockets/test_sockets_partial.c)2
-rw-r--r--tests/sockets/test_sockets_partial_server.c130
-rw-r--r--tests/sockets/test_sockets_select_server_closes_connection.c112
-rw-r--r--tests/sockets/test_sockets_select_server_closes_connection_client_rw.c (renamed from tests/sockets/test_sockets_select_server_closes_connection_rw.c)42
-rw-r--r--tests/sockets/test_sockets_select_server_no_accept_client.c (renamed from tests/sockets/test_sockets_select.c)0
-rw-r--r--tests/sockets/test_sockets_select_server_no_accept_server.c86
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>
-#endif
-
-#include "test_sockets_bigdata.h"
-
-#define EXPECTED_BYTES DATA_SIZE
-
-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[EXPECTED_BYTES];
- 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!");
- finish(EXIT_FAILURE);
- }
- return offset;
-}
-
-void iter(void *arg) {
- static char out[EXPECTED_BYTES];
- static int pos = 0;
- fd_set fdr;
- int res;
-
- // make sure that sockfd has finished connecting and 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);
- return;
- } else if (!FD_ISSET(sockfd, &fdr)) {
- return;
- }
-
- // perform read write operations ...
- printf("so far %d, expecting up to %d\n", pos, EXPECTED_BYTES-pos);
- res = get_all_buf(sockfd, out+pos, EXPECTED_BYTES-pos);
- if (res) printf("read! %d\n", res);
- pos += res;
- if (pos >= EXPECTED_BYTES) {
- shutdown(sockfd, SHUT_RDWR);
-
- close(sockfd);
-
- char *comp = generateData();
- int result = strcmp(comp, out);
- if (result != 0) {
- for (int i = 0; i < DATA_SIZE; i++) {
- printf("%d:%d\n", comp[i], out[i]);
- }
- }
- finish(result);
- }
-}
-
-int main() {
- struct sockaddr_in addr;
- int res;
-
- printf("hello from main page\n");
-
- 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_run_script("console.log('adding iframe');"
- "var iframe = document.createElement('iframe');"
- "iframe.src = 'side.html';"
- "iframe.width = '100%';"
- "iframe.width = '40%';"
- "document.body.appendChild(iframe);"
- "console.log('added.');");
- emscripten_set_main_loop(iter, 3, 0);
-#else
- while (1) iter(NULL);
-#endif
-
- return EXIT_SUCCESS;
-}
-
diff --git a/tests/sockets/test_sockets_bi_side.c b/tests/sockets/test_sockets_bi_side.c
deleted file mode 100644
index b8910632..00000000
--- a/tests/sockets/test_sockets_bi_side.c
+++ /dev/null
@@ -1,93 +0,0 @@
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <fcntl.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 5
-
-int sockfd = -1;
-
-void finish(int result) {
- close(sockfd);
- exit(result);
-}
-
-void loop() {
- fd_set fdw;
- int res;
-
- // Make sure that sockfd has actually finished connecting
- // and is ready to read.
- FD_ZERO(&fdw);
- FD_SET(sockfd, &fdw);
- res = select(64, NULL, &fdw, NULL, NULL);
- if (res == -1) {
- perror("select failed");
- finish(EXIT_FAILURE);
- } else if (!FD_ISSET(sockfd, &fdw)) {
- return;
- }
-
- char data[] = "hello from the other siide\n";
-
- printf("send..\n");
-#if TEST_FILE_OPS
- res = write(sockfd, data, sizeof(data));
-#else
- res = send(sockfd, data, sizeof(data), 0);
-#endif
- if (res == -1) {
- if (errno != EAGAIN) {
- perror("send error");
- finish(EXIT_FAILURE);
- }
- return;
- }
-
- finish(EXIT_SUCCESS);
-}
-
-int main() {
- struct sockaddr_in addr;
- int res;
-#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);
- }
-
- emscripten_set_main_loop(loop, 0, 0);
-
- return EXIT_SUCCESS;
-}
-
diff --git a/tests/sockets/test_sockets_bi_side_bigdata.c b/tests/sockets/test_sockets_bi_side_bigdata.c
deleted file mode 100644
index e31029b6..00000000
--- a/tests/sockets/test_sockets_bi_side_bigdata.c
+++ /dev/null
@@ -1,90 +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
-
-#include "test_sockets_bigdata.h"
-
-#define EXPECTED_BYTES 5
-
-int sockfd = -1;
-char *data = NULL;
-
-void finish(int result) {
- close(sockfd);
- exit(result);
-}
-
-void loop() {
- fd_set fdw;
- int res;
-
- // make sure that sockfd has finished connecting and is ready to write
- FD_ZERO(&fdw);
- FD_SET(sockfd, &fdw);
- res = select(64, NULL, &fdw, NULL, NULL);
- if (res == -1) {
- perror("select failed");
- finish(EXIT_FAILURE);
- return;
- } else if (!FD_ISSET(sockfd, &fdw)) {
- return;
- }
-
- printf("send..\n");
-
- res = send(sockfd, data, DATA_SIZE, 0);
- if (res == -1) {
- if (errno != EAGAIN) {
- perror("send error");
- finish(EXIT_FAILURE);
- }
- return;
- }
-
- finish(EXIT_SUCCESS);
-}
-
-int main() {
- struct sockaddr_in addr;
- int res;
-
- sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sockfd == -1) {
- perror("cannot create socket");
- exit(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);
- }
-
- printf("connect..\n");
-
- res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
- if (res == -1 && errno != EINPROGRESS) {
- perror("connect failed");
- finish(EXIT_FAILURE);
- }
-
- data = generateData();
-
- emscripten_set_main_loop(loop, 1, 0);
-
- return EXIT_SUCCESS;
-} \ No newline at end of file
diff --git a/tests/sockets/test_sockets_bigdata.h b/tests/sockets/test_sockets_bigdata.h
deleted file mode 100644
index 17149ad6..00000000
--- a/tests/sockets/test_sockets_bigdata.h
+++ /dev/null
@@ -1,20 +0,0 @@
-
-#include <stdlib.h>
-
-#define DATA_SIZE (256*256*2)
-// 1500 fails
-
-char *generateData() {
- char *ret = malloc(256*256*2);
- char *curr = ret;
- for (int i = 0; i < 256; i++) {
- for (int j = 0; j < 256; j++) {
- *curr = i;
- curr++;
- *curr = j;
- curr++;
- }
- }
- return ret;
-}
-
diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c
new file mode 100644
index 00000000..6b3ccef3
--- /dev/null
+++ b/tests/sockets/test_sockets_echo_client.c
@@ -0,0 +1,148 @@
+#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
+
+#include "test_sockets_msg.h"
+
+// message to send to the server
+#ifndef MESSAGE
+#define MESSAGE "pingtothepong"
+#endif
+
+typedef enum {
+ MSG_READ,
+ MSG_WRITE
+} msg_state_t;
+
+typedef struct {
+ int fd;
+ msg_t msg;
+ msg_state_t state;
+} server_t;
+
+server_t server;
+msg_t echo_msg;
+int echo_read;
+int echo_wrote;
+
+void finish(int result) {
+ close(server.fd);
+#if EMSCRIPTEN
+ REPORT_RESULT();
+#endif
+ exit(result);
+}
+
+void main_loop(void *arg) {
+ static char out[1024*2];
+ static int pos = 0;
+ fd_set fdr;
+ fd_set fdw;
+ int res;
+
+ // make sure that server.fd is ready to read / write
+ FD_ZERO(&fdr);
+ FD_ZERO(&fdw);
+ FD_SET(server.fd, &fdr);
+ FD_SET(server.fd, &fdw);
+ res = select(64, &fdr, &fdw, NULL, NULL);
+ if (res == -1) {
+ perror("select failed");
+ finish(EXIT_FAILURE);
+ }
+
+ if (server.state == MSG_READ) {
+ if (!FD_ISSET(server.fd, &fdr)) {
+ return;
+ }
+
+ // as a test, confirm with ioctl that we have data available
+ // after selecting
+ int available;
+ res = ioctl(server.fd, FIONREAD, &available);
+ assert(res != -1);
+ assert(available);
+
+ res = do_msg_read(server.fd, &server.msg, echo_read, 0, NULL, NULL);
+ if (res != -1) echo_read += res;
+
+ // once we've read the entire message, validate it
+ if (echo_read >= server.msg.length) {
+ assert(!strcmp(server.msg.buffer, MESSAGE));
+ finish(EXIT_SUCCESS);
+ }
+ } else {
+ if (!FD_ISSET(server.fd, &fdw)) {
+ return;
+ }
+
+ res = do_msg_write(server.fd, &echo_msg, echo_wrote, 0, NULL, 0);
+ if (res != -1) echo_wrote += res;
+
+ // once we're done writing the message, read it back
+ if (echo_wrote >= echo_msg.length) {
+ server.state = MSG_READ;
+ }
+ }
+}
+
+int main() {
+ struct sockaddr_in addr;
+ int res;
+
+ memset(&server, 0, sizeof(server_t));
+ server.state = MSG_WRITE;
+
+ // setup the message we're going to echo
+ memset(&echo_msg, 0, sizeof(msg_t));
+ echo_msg.length = strlen(MESSAGE) + 1;
+ echo_msg.buffer = malloc(echo_msg.length);
+ strncpy(echo_msg.buffer, MESSAGE, echo_msg.length);
+
+ // create the socket and set to non-blocking
+#if !USE_UDP
+ server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+#else
+ server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+#endif
+ if (server.fd == -1) {
+ perror("cannot create socket");
+ finish(EXIT_FAILURE);
+ }
+ fcntl(server.fd, F_SETFL, O_NONBLOCK);
+
+ // connect the socket
+ 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(server.fd, (struct sockaddr *)&addr, sizeof(addr));
+ if (res == -1 && errno != EINPROGRESS) {
+ perror("connect failed");
+ finish(EXIT_FAILURE);
+ }
+
+#if EMSCRIPTEN
+ emscripten_set_main_loop(main_loop, 0, 0);
+#else
+ while (1) main_loop(NULL);
+#endif
+
+ return EXIT_SUCCESS;
+} \ No newline at end of file
diff --git a/tests/sockets/test_sockets_echo_server.c b/tests/sockets/test_sockets_echo_server.c
new file mode 100644
index 00000000..8a48b878
--- /dev/null
+++ b/tests/sockets/test_sockets_echo_server.c
@@ -0,0 +1,152 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+#include "test_sockets_msg.h"
+
+typedef enum {
+ MSG_READ,
+ MSG_WRITE
+} msg_state_t;
+
+typedef struct {
+ int fd;
+} server_t;
+
+typedef struct {
+ int fd;
+ struct sockaddr_in addr;
+ msg_t msg;
+ msg_state_t state;
+ int read;
+ int wrote;
+} client_t;
+
+server_t server;
+client_t client;
+
+void main_loop(void *arg) {
+ int res;
+ fd_set fdr;
+ fd_set fdw;
+
+ // see if there are any connections to accept or read / write from
+ FD_ZERO(&fdr);
+ FD_ZERO(&fdw);
+ FD_SET(server.fd, &fdr);
+ FD_SET(server.fd, &fdw);
+#if !USE_UDP
+ if (client.fd) FD_SET(client.fd, &fdr);
+ if (client.fd) FD_SET(client.fd, &fdw);
+#endif
+ res = select(64, &fdr, &fdw, NULL, NULL);
+ if (res == -1) {
+ perror("select failed");
+ exit(EXIT_SUCCESS);
+ }
+
+#if !USE_UDP
+ // for TCP sockets, we may need to accept a connection
+ if (FD_ISSET(server.fd, &fdr)) {
+ client.fd = accept(server.fd, NULL, NULL);
+ assert(client.fd != -1);
+ }
+#endif
+
+#if !USE_UDP
+ int fd = client.fd;
+#else
+ int fd = server.fd;
+#endif
+ if (client.state == MSG_READ) {
+ socklen_t addrlen;
+
+ if (!FD_ISSET(fd, &fdr)) {
+ return;
+ }
+
+ res = do_msg_read(fd, &client.msg, client.read, 0, (struct sockaddr *)&client.addr, &addrlen);
+ if (res != -1) client.read += res;
+
+ // once we've read the entire message, echo it back
+ if (client.read >= client.msg.length) {
+ client.read = 0;
+ client.state = MSG_WRITE;
+ }
+ } else {
+ if (!FD_ISSET(fd, &fdw)) {
+ return;
+ }
+
+ res = do_msg_write(fd, &client.msg, client.wrote, 0, (struct sockaddr *)&client.addr, sizeof(client.addr));
+ if (res != -1) client.wrote += res;
+
+ // close the client once we've echo'd back the entire message
+ if (client.wrote >= client.msg.length) {
+ close(client.fd);
+ memset(&client, 0, sizeof(client_t));
+ }
+ }
+}
+
+int main() {
+ struct sockaddr_in addr;
+ int res;
+
+ memset(&server, 0, sizeof(server_t));
+ memset(&client, 0, sizeof(client_t));
+
+ // create the socket and set to non-blocking
+#if !USE_UDP
+ server.fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+#else
+ server.fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+#endif
+ if (server.fd == -1) {
+ perror("cannot create socket");
+ exit(EXIT_FAILURE);
+ }
+ fcntl(server.fd, 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");
+ exit(EXIT_FAILURE);
+ }
+
+ res = bind(server.fd, (struct sockaddr *)&addr, sizeof(addr));
+ if (res == -1) {
+ perror("bind failed");
+ exit(EXIT_FAILURE);
+ }
+
+#if !USE_UDP
+ res = listen(server.fd, 50);
+ if (res == -1) {
+ perror("listen failed");
+ exit(EXIT_FAILURE);
+ }
+#endif
+
+#if EMSCRIPTEN
+ emscripten_set_main_loop(main_loop, 60, 0);
+#else
+ while (1) main_loop(NULL);
+#endif
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/sockets/test_sockets_gethostbyname.c b/tests/sockets/test_sockets_gethostbyname.c
index 59c55ba1..12fc6d9d 100644
--- a/tests/sockets/test_sockets_gethostbyname.c
+++ b/tests/sockets/test_sockets_gethostbyname.c
@@ -1,20 +1,16 @@
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <assert.h>
+#include <errno.h>
+#include <netdb.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 5
-
int sockfd;
void finish(int result) {
@@ -25,113 +21,28 @@ void finish(int result) {
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;
- 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) {
- perror("error in get_all_buf!");
- finish(EXIT_FAILURE);
- }
- return offset;
-}
-
-void iter() {
- static char out[1024*2];
- static int pos = 0;
- fd_set fdr;
- int res;
-
- // make sure that sockfd has finished connecting and 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;
-
- 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);
- struct hostent *host0 = gethostbyname("test.com"); // increment hostname counter to check for possible but at 0,0 not differentiating low/high
- struct hostent *host = gethostbyname("localhost");
+ char str[INET_ADDRSTRLEN];
+ struct in_addr addr;
+ const char *res;
+ int err;
+
+ // resolve the hostname ot an actual address
+ struct hostent *host = gethostbyname("slashdot.org");
+
+ // convert the raw address to a string
char **raw_addr_list = host->h_addr_list;
int *raw_addr = (int*)*raw_addr_list;
- printf("raw addr: %d\n", *raw_addr);
- char name[INET_ADDRSTRLEN];
- if (!inet_ntop(AF_INET, raw_addr, name, sizeof(name))) {
- printf("could not figure out name\n");
- finish(EXIT_FAILURE);
- }
- printf("localhost has 'ip' of %s\n", name);
-
- if (inet_pton(AF_INET, name, &addr.sin_addr) != 1) {
- perror("inet_pton failed");
- finish(EXIT_FAILURE);
- }
+ res = inet_ntop(host->h_addrtype, raw_addr, str, INET_ADDRSTRLEN);
+ assert(res);
- res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
- if (res == -1 && errno != EINPROGRESS) {
- perror("connect failed");
- finish(EXIT_FAILURE);
- }
+ // convert the string to an in_addr structure
+ err = inet_pton(AF_INET, str, &addr);
+ assert(err == 1);
-#if EMSCRIPTEN
- emscripten_set_main_loop(iter, 0, 0);
-#else
- while (1) iter();
-#endif
+ // do a reverse lookup on the ip address
+ struct hostent *host1 = gethostbyaddr(&addr, sizeof(addr), host->h_addrtype);
+ assert(strstr(host1->h_name, "slashdot.org"));
return EXIT_SUCCESS;
}
diff --git a/tests/sockets/test_sockets_msg.h b/tests/sockets/test_sockets_msg.h
new file mode 100644
index 00000000..30094d65
--- /dev/null
+++ b/tests/sockets/test_sockets_msg.h
@@ -0,0 +1,78 @@
+#ifndef __TEST_SOCKETS_MSG_H__
+#define __TEST_SOCKETS_MSG_H__
+
+typedef struct {
+ char *buffer;
+ int length;
+} msg_t;
+
+int do_msg_read(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t *addrlen) {
+ int res;
+
+ if (!msg->length) {
+ // read the message length
+ res = recvfrom(sockfd, &msg->length, sizeof(int), 0, (struct sockaddr *)addr, addrlen);
+ if (res == -1) {
+ assert(errno == EAGAIN);
+ return res;
+ }
+ assert(res != 0);
+ msg->buffer = malloc(msg->length);
+
+ printf("do_msg_read: allocating %d bytes for message\n", msg->length);
+ }
+
+ // read the actual message
+ int max = msg->length - offset;
+ if (length && max > length) {
+ max = length;
+ }
+ res = recvfrom(sockfd, msg->buffer + offset, max, 0, (struct sockaddr *)addr, addrlen);
+ if (res == -1) {
+ assert(errno == EAGAIN);
+ return res;
+ }
+
+ printf("do_msg_read: read %d bytes\n", res);
+
+ return res;
+}
+
+int do_msg_write(int sockfd, msg_t *msg, int offset, int length, struct sockaddr *addr, socklen_t addrlen) {
+ int res;
+
+ // send the message length first
+ if (!offset) {
+ if (addr) {
+ res = sendto(sockfd, &msg->length, sizeof(int), 0, addr, addrlen);
+ } else {
+ res = send(sockfd, &msg->length, sizeof(int), 0);
+ }
+ if (res == -1) {
+ assert(errno == EAGAIN);
+ return res;
+ }
+ assert(res == sizeof(int));
+ }
+
+ // then the actual message
+ int max = msg->length - offset;
+ if (length && max > length) {
+ max = length;
+ }
+ if (addr) {
+ res = sendto(sockfd, msg->buffer + offset, max, 0, addr, addrlen);
+ } else {
+ res = send(sockfd, msg->buffer + offset, max, 0);
+ }
+ if (res == -1) {
+ assert(errno == EAGAIN);
+ return res;
+ }
+
+ printf("do_msg_write: wrote %d bytes %d\n", res, msg->length);
+
+ return res;
+}
+
+#endif \ No newline at end of file
diff --git a/tests/sockets/test_sockets_partial.c b/tests/sockets/test_sockets_partial_client.c
index 5fe34721..dcf90f19 100644
--- a/tests/sockets/test_sockets_partial.c
+++ b/tests/sockets/test_sockets_partial_client.c
@@ -111,7 +111,7 @@ int main() {
#if EMSCRIPTEN
emscripten_set_main_loop(iter, 0, 0);
#else
- while (!done) iter(NULL);
+ while (1) iter(NULL);
#endif
return EXIT_SUCCESS;
diff --git a/tests/sockets/test_sockets_partial_server.c b/tests/sockets/test_sockets_partial_server.c
new file mode 100644
index 00000000..57fae84b
--- /dev/null
+++ b/tests/sockets/test_sockets_partial_server.c
@@ -0,0 +1,130 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+int serverfd = -1;
+int clientfd = -1;
+
+// 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")
+
+void do_send(int sockfd) {
+ static char* buffers[] = {
+ "\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\0",
+ "\x08\x01\x02\x03\x04\x05\x06\x07\x08\0",
+ "\x07\x01\x02\x03\x04\x05\x06\x07\0",
+ "\x06\x01\x02\x03\x04\x05\x06\0",
+ "\x05\x01\x02\x03\x04\x05\0",
+ "\x04\x01\x02\x03\x04\0",
+ "\x03\x01\x02\x03\0",
+ "\x02\x01\x02\0",
+ "\x01\x01\0"
+ };
+
+ int i;
+ int res;
+ char *buffer;
+ struct sockaddr_in addr;
+ socklen_t addrlen;
+
+ for (i = 0; i < sizeof(buffers) / sizeof(char*); i++) {
+ buffer = buffers[i];
+
+ res = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&addr, sizeof(addr));
+ if (res == -1) {
+ perror("send failed");
+ exit(EXIT_FAILURE);
+ }
+ printf("sent \"%s\" (%d bytes)\n", buffer, res);
+ }
+
+ exit(EXIT_SUCCESS);
+}
+
+void iter(void *arg) {
+ int res;
+ fd_set fdr;
+ fd_set fdw;
+
+ // see if there are any connections to accept / write to
+ FD_ZERO(&fdr);
+ FD_ZERO(&fdw);
+ FD_SET(serverfd, &fdr);
+ if (clientfd != -1) FD_SET(clientfd, &fdw);
+ res = select(64, &fdr, &fdw, NULL, NULL);
+ if (res == -1) {
+ perror("select failed");
+ exit(EXIT_SUCCESS);
+ }
+
+ if (FD_ISSET(serverfd, &fdr)) {
+ printf("accepted someone\n");
+ clientfd = accept(serverfd, NULL, NULL);
+ assert(clientfd != -1);
+ }
+
+ if (FD_ISSET(clientfd, &fdw)) {
+ do_send(clientfd);
+ }
+}
+
+int main() {
+ struct sockaddr_in addr;
+ int res;
+
+ // create the socket
+ serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (serverfd == -1) {
+ perror("cannot create socket");
+ exit(EXIT_FAILURE);
+ }
+ fcntl(serverfd, F_SETFL, O_NONBLOCK);
+
+ // bind and listen to the supplied port
+ 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");
+ exit(EXIT_FAILURE);
+ }
+
+ res = bind(serverfd, (struct sockaddr *)&addr, sizeof(addr));
+ if (res == -1) {
+ perror("bind failed");
+ exit(EXIT_FAILURE);
+ }
+
+ res = listen(serverfd, 50);
+ if (res == -1) {
+ perror("listen failed");
+ exit(EXIT_FAILURE);
+ }
+
+#if EMSCRIPTEN
+ emscripten_set_main_loop(iter, 60, 0);
+#else
+ while (1) iter(NULL);
+#endif
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/sockets/test_sockets_select_server_closes_connection.c b/tests/sockets/test_sockets_select_server_closes_connection.c
deleted file mode 100644
index 4181b12b..00000000
--- a/tests/sockets/test_sockets_select_server_closes_connection.c
+++ /dev/null
@@ -1,112 +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 = -1;
-
-void finish(int result) {
- close(sockfd);
-#if EMSCRIPTEN
- REPORT_RESULT();
-#endif
- exit(result);
-}
-
-void iter(void *arg) {
- static char readbuf[1024];
- static int readPos = 0;
-
- fd_set sett;
- FD_ZERO(&sett);
- FD_SET(sockfd, &sett);
-
- int res = select(64, &sett, NULL, NULL, NULL);
-
- if (res == -1) {
- perror("select failed");
- finish(EXIT_FAILURE);
- } else if (res == 0) {
- return;
- } else if (res > 0) {
- assert(FD_ISSET(sockfd, &sett));
-
- int bytesRead = recv(sockfd, readbuf+readPos, 7-readPos, 0);
- if (bytesRead == -1) {
- if (errno != EAGAIN) {
- perror("recv error");
- finish(EXIT_FAILURE);
- }
- // try again
- return;
- }
-
- if (readPos < 7) {
- readPos += bytesRead;
- } else {
- if (!bytesRead) {
- perror("Connection to websocket server was closed as expected");
- finish(266);
- } else {
- perror("Connection to websocket server was not closed");
- finish(EXIT_FAILURE);
- }
- }
- }
-
- return;
-}
-
-// Scenario: the server sends data and closes the connection after 7 bytes.
-// This test should provoke the situation in which the underlying
-// tcp connection has been torn down already but there is still data
-// in the queue. The select call has to succeed as long the queue
-// still contains data and only then start to throw errors.
-int 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);
- }
-
- // This call should succeed (even if the server port is closed)
- 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
-
- return EXIT_FAILURE;
-}
-
diff --git a/tests/sockets/test_sockets_select_server_closes_connection_rw.c b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c
index f7e19aca..198ad232 100644
--- a/tests/sockets/test_sockets_select_server_closes_connection_rw.c
+++ b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c
@@ -14,9 +14,13 @@
#include <emscripten.h>
#endif
-#define EXPECTED_BYTES 5
+#include "test_sockets_msg.h"
-int sockfd = -1;
+#define MESSAGE "0123456789"
+
+int sockfd;
+msg_t readmsg;
+msg_t writemsg;
void finish(int result) {
close(sockfd);
@@ -26,12 +30,10 @@ void finish(int result) {
exit(result);
}
-void iter(void *arg) {
+void main_loop(void *arg) {
static int state = 0;
- static char writebuf[] = "01234567890123456789";
- static int writePos = 0;
- static char readbuf[1024];
static int readPos = 0;
+ static int writePos = 0;
int selectRes;
ssize_t transferAmount;
fd_set sett;
@@ -63,11 +65,11 @@ void iter(void *arg) {
}
// send a single byte
- transferAmount = send(sockfd, writebuf+writePos, 1, 0);
- writePos += transferAmount;
+ transferAmount = do_msg_write(sockfd, &writemsg, writePos, 1, NULL, 0);
+ if (transferAmount != -1) writePos += transferAmount;
// after 10 bytes switch to next state
- if (writePos >= 10) {
+ if (writePos >= writemsg.length) {
state = 1;
}
break;
@@ -86,8 +88,8 @@ void iter(void *arg) {
}
// read a single byte
- transferAmount = recv(sockfd, readbuf+readPos, 1, 0);
- readPos += transferAmount;
+ transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
+ if (transferAmount != -1) readPos += transferAmount;
// if successfully reading 1 byte, switch to next state
if (readPos >= 1) {
@@ -119,11 +121,11 @@ void iter(void *arg) {
}
// read a single byte
- transferAmount = recv(sockfd, readbuf+readPos, 1, 0);
- readPos += transferAmount;
+ transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
+ if (transferAmount != -1) readPos += transferAmount;
// with 10 bytes read the inQueue is empty => switch state
- if (readPos >= 10) {
+ if (readPos >= readmsg.length) {
state = 3;
}
break;
@@ -141,7 +143,7 @@ void iter(void *arg) {
// but recv should return 0 signaling the remote
// end has closed the connection.
- transferAmount = recv(sockfd, readbuf, 1, 0);
+ transferAmount = do_msg_read(sockfd, &readmsg, readPos, 0, NULL, NULL);
if (transferAmount) {
printf("case 3: read != 0\n");
finish(EXIT_FAILURE);
@@ -176,6 +178,12 @@ int main() {
struct sockaddr_in addr;
int res;
+ memset(&readmsg, 0, sizeof(msg_t));
+ memset(&writemsg, 0, sizeof(msg_t));
+ writemsg.length = strlen(MESSAGE) + 1;
+ writemsg.buffer = malloc(writemsg.length);
+ strncpy(writemsg.buffer, MESSAGE, writemsg.length);
+
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) {
perror("cannot create socket");
@@ -199,9 +207,9 @@ int main() {
}
#if EMSCRIPTEN
- emscripten_set_main_loop(iter, 0, 0);
+ emscripten_set_main_loop(main_loop, 0, 0);
#else
- while (1) iter(NULL);
+ while (1) main_loop(NULL);
#endif
return EXIT_SUCCESS;
diff --git a/tests/sockets/test_sockets_select.c b/tests/sockets/test_sockets_select_server_no_accept_client.c
index e05bd4c8..e05bd4c8 100644
--- a/tests/sockets/test_sockets_select.c
+++ b/tests/sockets/test_sockets_select_server_no_accept_client.c
diff --git a/tests/sockets/test_sockets_select_server_no_accept_server.c b/tests/sockets/test_sockets_select_server_no_accept_server.c
new file mode 100644
index 00000000..4a399ed1
--- /dev/null
+++ b/tests/sockets/test_sockets_select_server_no_accept_server.c
@@ -0,0 +1,86 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+int serverfd = -1;
+
+void iter(void *arg) {
+ int res;
+ fd_set fdr;
+ fd_set fdw;
+
+ // see if there are any connections to accept / write to
+ FD_ZERO(&fdr);
+ FD_ZERO(&fdw);
+ FD_SET(serverfd, &fdr);
+ if (clientfd != -1) FD_SET(clientfd, &fdw);
+ res = select(64, &fdr, &fdw, NULL, NULL);
+ if (res == -1) {
+ perror("select failed");
+ exit(EXIT_SUCCESS);
+ }
+
+ if (FD_ISSET(serverfd, &fdr)) {
+ printf("accepted someone\n");
+ clientfd = accept(serverfd, NULL, NULL);
+ assert(clientfd != -1);
+ }
+
+ if (FD_ISSET(clientfd, &fdw)) {
+ do_send(clientfd);
+ }
+}
+
+int main() {
+ struct sockaddr_in addr;
+ int res;
+
+ // create the socket
+ serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (serverfd == -1) {
+ perror("cannot create socket");
+ exit(EXIT_FAILURE);
+ }
+ fcntl(serverfd, F_SETFL, O_NONBLOCK);
+
+ // bind and listen to the supplied port
+ 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");
+ exit(EXIT_FAILURE);
+ }
+
+ res = bind(serverfd, (struct sockaddr *)&addr, sizeof(addr));
+ if (res == -1) {
+ perror("bind failed");
+ exit(EXIT_FAILURE);
+ }
+
+ res = listen(serverfd, 50);
+ if (res == -1) {
+ perror("listen failed");
+ exit(EXIT_FAILURE);
+ }
+
+#if EMSCRIPTEN
+ emscripten_set_main_loop(iter, 60, 0);
+#else
+ while (1) iter(NULL);
+#endif
+
+ return EXIT_SUCCESS;
+}