diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-15 09:52:53 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-15 09:52:53 -0700 |
commit | 0f76b5793a07987f17e95410af843eaa7f4eb823 (patch) | |
tree | ede0ab7aaab553b79b0348b53623a87d4127dafe | |
parent | 2e229a560955c07d1b66db27913af3284baa64fb (diff) | |
parent | 2d51d99e4f77aed13d05e40b24976b62f8e926d0 (diff) |
Merge pull request #1513 from modeswitch/webrtc-tests
Webrtc test
-rw-r--r-- | src/library.js | 8 | ||||
-rwxr-xr-x | tests/runner.py | 4 | ||||
-rw-r--r-- | tests/sockets/webrtc_host.c | 89 | ||||
-rw-r--r-- | tests/sockets/webrtc_peer.c | 81 | ||||
-rw-r--r-- | tests/test_sockets.py | 68 |
5 files changed, 244 insertions, 6 deletions
diff --git a/src/library.js b/src/library.js index ee49ae1f..9e78db13 100644 --- a/src/library.js +++ b/src/library.js @@ -7235,7 +7235,7 @@ LibraryManager.library = { socket__deps: ['$FS', '$Sockets'], socket: function(family, type, protocol) { var INCOMING_QUEUE_LENGTH = 64; - var stream = FS.createStream({ + var info = FS.createStream({ addr: null, port: null, inQueue: new CircularBuffer(INCOMING_QUEUE_LENGTH), @@ -7244,7 +7244,7 @@ LibraryManager.library = { socket: true, stream_ops: {} }); - assert(stream.fd < 64); // select() assumes socket fd values are in 0..63 + assert(info.fd < 64); // select() assumes socket fd values are in 0..63 var stream = type == {{{ cDefine('SOCK_STREAM') }}}; if (protocol) { assert(stream == (protocol == {{{ cDefine('IPPROTO_TCP') }}})); // if stream, must be tcp @@ -7357,8 +7357,7 @@ LibraryManager.library = { } }; }; - - return stream.fd; + return info.fd; }, mkport__deps: ['$Sockets'], @@ -7452,6 +7451,7 @@ LibraryManager.library = { buffer.set(data, info.header.byteLength); connection.send('unreliable', buffer.buffer); + return ret; }, recvmsg__deps: ['$FS', '$Sockets', 'bind', '__setErrNo', '$ERRNO_CODES', 'htons'], diff --git a/tests/runner.py b/tests/runner.py index 2f508dfc..2c6af351 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -480,7 +480,7 @@ def server_func(dir, q): if 'report_' in s.path: q.put(s.path) else: - filename = s.path[1:] + filename = s.path.split('?')[0][1:] if os.path.exists(filename): s.send_response(200) s.send_header("Content-type", "text/html") @@ -771,4 +771,4 @@ an individual test with else: testRunner = unittest.TextTestRunner(verbosity=2) for suite in suites: - testRunner.run(suite) + testRunner.run(suite)
\ No newline at end of file diff --git a/tests/sockets/webrtc_host.c b/tests/sockets/webrtc_host.c new file mode 100644 index 00000000..770e59e0 --- /dev/null +++ b/tests/sockets/webrtc_host.c @@ -0,0 +1,89 @@ +#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 <sys/ioctl.h> +#include <assert.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +#define EXPECTED_BYTES 5 +#define BUFLEN 16 + +int result = 0; +int sock; +char buf[BUFLEN]; +char expected[] = "emscripten"; +struct sockaddr_in si_host, + si_peer; +struct iovec iov[1]; +struct msghdr hdr; +int done = 0; + +void iter(void* arg) { + int n; + n = recvmsg(sock, &hdr, 0); + + if(0 < n) { + done = 1; + fprintf(stderr, "received %d bytes: %s", n, (char*)hdr.msg_iov[0].iov_base); + + shutdown(sock, SHUT_RDWR); + close(sock); + +#if EMSCRIPTEN + int result = 1; + REPORT_RESULT(); + exit(EXIT_SUCCESS); + emscripten_cancel_main_loop(); +#endif + } else if(EWOULDBLOCK != errno) { + perror("recvmsg failed"); + exit(EXIT_FAILURE); + emscripten_cancel_main_loop(); + } +} + +int main(void) +{ + memset(&si_host, 0, sizeof(struct sockaddr_in)); + memset(&si_peer, 0, sizeof(struct sockaddr_in)); + + si_host.sin_family = AF_INET; + si_host.sin_port = htons(8991); + si_host.sin_addr.s_addr = htonl(INADDR_ANY); + + if(-1 == (sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))) { + perror("cannot create host socket"); + exit(EXIT_FAILURE); + } + + if(-1 == bind(sock, (struct sockaddr*)&si_host, sizeof(struct sockaddr))) { + perror("cannot bind host socket"); + exit(EXIT_FAILURE); + } + + iov[0].iov_base = buf; + iov[0].iov_len = sizeof(buf); + + memset (&hdr, 0, sizeof (struct msghdr)); + + hdr.msg_name = &si_peer; + hdr.msg_namelen = sizeof(struct sockaddr_in); + hdr.msg_iov = iov; + hdr.msg_iovlen = 1; + +#if EMSCRIPTEN + emscripten_set_main_loop(iter, 0, 0); +#else + while (!done) iter(NULL); +#endif + + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/tests/sockets/webrtc_peer.c b/tests/sockets/webrtc_peer.c new file mode 100644 index 00000000..d24979e7 --- /dev/null +++ b/tests/sockets/webrtc_peer.c @@ -0,0 +1,81 @@ +#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 <sys/ioctl.h> +#include <assert.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +#define EXPECTED_BYTES 5 +#define BUFLEN 16 +#define HOST_ADDR "10.0.0.1" + +int result = 0; +int sock; +char buf[16] = "emscripten"; +struct sockaddr_in si_host; +struct iovec iov[1]; +struct msghdr hdr; +int done = 0; + +void iter(void* arg) { + int n; + n = sendmsg(sock, &hdr, 0); + + if(0 < n) { + done = 1; + fprintf(stderr, "sent %d bytes: %s", n, (char*)hdr.msg_iov[0].iov_base); + + shutdown(sock, SHUT_RDWR); + close(sock); + + exit(EXIT_SUCCESS); + emscripten_cancel_main_loop(); + } else if(EWOULDBLOCK != errno) { + perror("sendmsg failed"); + exit(EXIT_FAILURE); + emscripten_cancel_main_loop(); + } +} + +int main(void) +{ + memset(&si_host, 0, sizeof(struct sockaddr_in)); + + si_host.sin_family = AF_INET; + si_host.sin_port = htons(8991); + if(0 == inet_pton(AF_INET, HOST_ADDR, &si_host.sin_addr)) { + perror("inet_aton failed"); + exit(EXIT_FAILURE); + } + + if(-1 == (sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))) { + perror("cannot create socket"); + exit(EXIT_FAILURE); + } + + iov[0].iov_base = buf; + iov[0].iov_len = sizeof(buf); + + memset (&hdr, 0, sizeof (struct msghdr)); + + hdr.msg_name = &si_host; + hdr.msg_namelen = sizeof(struct sockaddr_in); + hdr.msg_iov = iov; + hdr.msg_iovlen = 1; + +#if EMSCRIPTEN + emscripten_set_main_loop(iter, 0, 0); +#else + while (!done) iter(NULL); +#endif + + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/tests/test_sockets.py b/tests/test_sockets.py index 03593674..4f6ee2a9 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -198,3 +198,71 @@ class sockets(BrowserCore): # ]: # with harness: # self.btest(os.path.join('sockets', 'test_enet_client.c'), expected='0', args=['-DSOCKK=9011'] + enet) + + def test_webrtc(self): + host_src = 'webrtc_host.c' + peer_src = 'webrtc_peer.c' + + host_outfile = 'host.html' + peer_outfile = 'peer.html' + + host_filepath = path_from_root('tests', 'sockets', host_src) + temp_host_filepath = os.path.join(self.get_dir(), os.path.basename(host_src)) + with open(host_filepath) as f: host_src = f.read() + with open(temp_host_filepath, 'w') as f: f.write(self.with_report_result(host_src)) + + peer_filepath = path_from_root('tests', 'sockets', peer_src) + temp_peer_filepath = os.path.join(self.get_dir(), os.path.basename(peer_src)) + with open(peer_filepath) as f: peer_src = f.read() + with open(temp_peer_filepath, 'w') as f: f.write(self.with_report_result(peer_src)) + + open(os.path.join(self.get_dir(), 'host_pre.js'), 'w').write(''' + var Module = { + webrtc: { + broker: 'https://mdsw.ch:8080', + session: undefined, + onpeer: function(peer, route) { + window.open('http://localhost:8888/peer.html?' + route); + // iframe = document.createElement("IFRAME"); + // iframe.setAttribute("src", "http://localhost:8888/peer.html?" + route); + // iframe.style.display = "none"; + // document.body.appendChild(iframe); + peer.listen(); + }, + onconnect: function(peer) { + }, + ondisconnect: function(peer) { + }, + onerror: function(error) { + console.error(error); + } + }, + }; + ''') + + open(os.path.join(self.get_dir(), 'peer_pre.js'), 'w').write(''' + var Module = { + webrtc: { + broker: 'https://mdsw.ch:8080', + session: window.location.toString().split('?')[1], + onpeer: function(peer, route) { + peer.connect(Module['webrtc']['session']); + }, + onconnect: function(peer) { + }, + ondisconnect: function(peer) { + // Calling window.close() from this handler hangs my browser, so run it in the next turn + setTimeout(window.close, 0); + }, + onerror: function(error) { + console.error(error); + } + } + }; + ''') + + Popen([PYTHON, EMCC, temp_host_filepath, '-o', host_outfile] + ['-s', 'GL_TESTING=1', '--pre-js', 'host_pre.js', '-s', 'SOCKET_WEBRTC=1', '-s', 'SOCKET_DEBUG=1']).communicate() + Popen([PYTHON, EMCC, temp_peer_filepath, '-o', peer_outfile] + ['-s', 'GL_TESTING=1', '--pre-js', 'peer_pre.js', '-s', 'SOCKET_WEBRTC=1', '-s', 'SOCKET_DEBUG=1']).communicate() + + expected = '1' + self.run_browser(host_outfile, '.', ['/report_result?' + e for e in expected])
\ No newline at end of file |