async def test_copy_and_paste(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(["-s", "copy-and-paste"]) sig_server = create_signaling(args) sig_client = create_signaling(args) def make_pipes(): r, w = os.pipe() return os.fdopen(r, "r"), os.fdopen(w, "w") # mock out read / write pipes sig_server._read_pipe, sig_client._write_pipe = make_pipes() sig_client._read_pipe, sig_server._write_pipe = make_pipes() # connect await sig_server.connect() await sig_client.connect() res = await asyncio.gather(sig_server.send(offer), delay(sig_client.receive)) self.assertEqual(res[1], offer) res = await asyncio.gather(sig_client.send(answer), delay(sig_server.receive)) self.assertEqual(res[1], answer) await asyncio.gather(sig_server.close(), sig_client.close()) # cleanup mocks sig_client._write_pipe.close() sig_server._write_pipe.close()
async def test_tcp_socket_abrupt_disconnect(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(["-s", "tcp-socket"]) sig_server = create_signaling(args) sig_client = create_signaling(args) # connect await sig_server.connect() await sig_client.connect() res = await asyncio.gather(sig_server.send(offer), delay(sig_client.receive)) self.assertEqual(res[1], offer) # break connection sig_client._writer.close() sig_server._writer.close() res = await sig_server.receive() self.assertIsNone(res) res = await sig_client.receive() self.assertIsNone(res) await asyncio.gather(sig_server.close(), sig_client.close())
def test_apprtc_with_buffered_message(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(["-s", "apprtc"]) # connect first party and send offer sig_server = create_signaling(args) server_params = run(sig_server.connect()) self.assertTrue(server_params["is_initiator"]) res = run(sig_server.send(offer)) # connect second party and receive offer args.signaling_room = server_params["room_id"] sig_client = create_signaling(args) client_params = run(sig_client.connect()) self.assertTrue(client_params["is_initiator"]) received = run(sig_client.receive()) self.assertEqual(received, offer) # exchange answer res = run( asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) # shutdown run(asyncio.gather(sig_server.close(), sig_client.close()))
def test_apprtc(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(["-s", "apprtc"]) # connect sig_server = create_signaling(args) server_params = run(sig_server.connect()) self.assertTrue(server_params["is_initiator"]) args.signaling_room = server_params["room_id"] sig_client = create_signaling(args) client_params = run(sig_client.connect()) self.assertTrue(client_params["is_initiator"]) # exchange signaling res = run( asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) res = run( asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) # shutdown run(asyncio.gather(sig_server.close(), sig_client.close()))
def test_unix_socket_abrupt_disconnect(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(['-s', 'unix-socket']) sig_server = create_signaling(args) sig_client = create_signaling(args) # connect run(sig_server.connect()) run(sig_client.connect()) res = run(asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) # break connection sig_client._writer.close() sig_server._writer.close() res = run(sig_server.receive()) self.assertIsNone(res) res = run(sig_client.receive()) self.assertIsNone(res) run(asyncio.gather(sig_server.close(), sig_client.close()))
def test_copy_and_paste(self): def mock_print(v=''): pass # hijack builtins original_print = __builtins__['print'] __builtins__['print'] = mock_print parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(['-s', 'copy-and-paste']) sig_server = create_signaling(args) sig_client = create_signaling(args) class MockReader: def __init__(self, queue): self.queue = queue async def readline(self): return await self.queue.get() class MockWritePipe: def __init__(self, queue, encoding): self.encoding = encoding self.queue = queue def write(self, msg): asyncio.ensure_future(self.queue.put(msg.encode( self.encoding))) def dummy_stdio(encoding): queue = asyncio.Queue() return MockReader(queue), MockWritePipe(queue, encoding=encoding) # mock out reader / write pipe run(sig_server._connect()) run(sig_client._connect()) sig_server._reader, sig_client._write_pipe = dummy_stdio( sig_server._read_pipe.encoding) sig_client._reader, sig_server._write_pipe = dummy_stdio( sig_client._read_pipe.encoding) res = run( asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) res = run( asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) run(asyncio.gather(sig_server.close(), sig_client.close())) # restore builtins __builtins__['print'] = original_print
def test_unix_socket(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(['-s', 'unix-socket']) sig_server = create_signaling(args) sig_client = create_signaling(args) res = run( asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) res = run( asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) asyncio.gather(sig_server.close(), sig_client.close())
def test_tcp_socket(self): parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(["-s", "tcp-socket"]) sig_server = create_signaling(args) sig_client = create_signaling(args) # connect run(sig_server.connect()) run(sig_client.connect()) res = run(asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) res = run(asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) run(asyncio.gather(sig_server.close(), sig_client.close()))
def test_copy_and_paste(self): captured_sdp = None def mock_input(): return captured_sdp def mock_print(v=''): nonlocal captured_sdp if v.startswith('{'): captured_sdp = v # hijack builtins original_input = __builtins__['input'] original_print = __builtins__['print'] __builtins__['input'] = mock_input __builtins__['print'] = mock_print parser = argparse.ArgumentParser() add_signaling_arguments(parser) args = parser.parse_args(['-s', 'copy-and-paste']) sig_server = create_signaling(args) sig_client = create_signaling(args) res = run( asyncio.gather(sig_server.send(offer), delay(sig_client.receive))) self.assertEqual(res[1], offer) res = run( asyncio.gather(sig_client.send(answer), delay(sig_server.receive))) self.assertEqual(res[1], answer) run(asyncio.gather(sig_server.close(), sig_client.close())) # restore builtins __builtins__['input'] = original_input __builtins__['print'] = original_print
await done.wait() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Data channel file transfer') parser.add_argument('role', choices=['send', 'receive']) parser.add_argument('filename') parser.add_argument('--verbose', '-v', action='count') add_signaling_arguments(parser) args = parser.parse_args() if args.verbose: logging.basicConfig(level=logging.DEBUG) asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) signaling = create_signaling(args) pc = RTCPeerConnection() if args.role == 'send': fp = open(args.filename, 'rb') coro = run_offer(pc, signaling, fp) else: fp = open(args.filename, 'wb') coro = run_answer(pc, signaling, fp) # run event loop loop = asyncio.get_event_loop() try: loop.run_until_complete(coro) except KeyboardInterrupt: pass finally: