async def test_async_remote_control(): """Test async remote_control.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.login = mock_coro() mock_protocol.send = mock_coro() mock_protocol.sync_send = MagicMock() asyncio.ensure_future(mock_protocol.remote_control(128, 0)) # Mock login success. await asyncio.sleep(0) msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) await asyncio.sleep(1) assert mock_protocol.task is None assert len(mock_protocol.sync_send.mock_calls) == 2 # Test ps_hold. mock_protocol.sync_send = MagicMock() asyncio.ensure_future(mock_protocol.remote_control(128, 2000)) await asyncio.sleep(2) assert mock_protocol.task is None assert len(mock_protocol.sync_send.mock_calls) == 2 # Test Enter. mock_protocol.sync_send = MagicMock() asyncio.ensure_future(mock_protocol.remote_control(16, 0)) await asyncio.sleep(1) assert mock_protocol.task is None assert len(mock_protocol.sync_send.mock_calls) == 1
async def test_async_start_title(): """Test async start_title.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.login = mock_coro() mock_protocol.send = mock_coro() mock_protocol._send_remote_control_request_sync = MagicMock() # Mock login success. await asyncio.sleep(0) msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) asyncio.ensure_future( mock_protocol.start_title(MOCK_TITLE_ID, running_id="Some ID") ) await asyncio.sleep(0) mock_protocol.send.assert_called_with(MOCK_BOOT) msg = MOCK_BOOT_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) assert mock_protocol.task is None await asyncio.sleep(1) mock_protocol._send_remote_control_request_sync.assert_called_with( c._get_remote_control_request(16, 0), 16 )
async def test_create_ddp_protocol_sock(): """Test DDP Protocol init with socket.""" with patch("pyps4_2ndscreen.ddp.asyncio.get_event_loop", return_value=MagicMock()) as mock_loop: mock_loop = mock_loop() mock_create_task = mock_coro(return_value=(MagicMock(), MagicMock())) mock_call = mock_coro(return_value=(MagicMock(), MagicMock())) mock_loop.create_datagram_endpoint = mock_call mock_loop.create_task = mock_create_task mock_port = 1234 sock = ddp.get_socket(port=mock_port) assert sock.gettimeout() == 0 assert sock.getsockname() == (ddp.UDP_IP, mock_port) mock_kwargs = { "local_addr": None, "reuse_port": None, "allow_broadcast": None, "sock": sock, } _, mock_ddp = await ddp.async_create_ddp_endpoint(sock=sock) mock_ddp.close() args, kwargs = mock_call.call_args assert callable(args[0]) # Hack for lambda: DDPProtocol() for key, value in kwargs.items(): assert mock_kwargs[key] == value # Test if socket is changed to nonblocking assert kwargs["sock"].gettimeout() == 0
async def test_data_recv(): """Test data recv.""" mock_protocol, mock_ps4 = setup_mock_protocol() # Test status request msg = c.STATUS_REQUEST mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol._ack_status = mock_coro() mock_protocol.data_received(msg) assert len(mock_protocol._ack_status.mock_calls) == 1 # Test login response mock_protocol.task = "login" msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) assert mock_ps4.loggedin is True assert mock_protocol.task is None # Test login fail response mock_protocol.task = "login" msg = bytes(8) + b"\x15" + bytes(7) mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) assert mock_ps4.loggedin is False
async def test_async_login(): """Test async login.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.send = mock_coro() mock_protocol.sync_send = MagicMock() mock_protocol._send_remote_control_request_sync = MagicMock() mock_protocol.connection_made(MagicMock()) with patch( "pyps4_2ndscreen.connection.socket.gethostname", return_value=MOCK_MODEL ): await mock_protocol.login(pin=MOCK_PIN, delay=0.1, power_on=False) await asyncio.sleep(0) # Mock login success. msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) mock_protocol.send.assert_called_once_with(MOCK_LOGIN) # Test RC Open sent. await asyncio.sleep(1) mock_protocol.sync_send.assert_called_once_with(MOCK_RC_OPEN) # Test RC PS is sent if not powering on. await asyncio.sleep(1) assert len(mock_protocol._send_remote_control_request_sync.mock_calls) == 1 # Test login with no pin and powering on. mock_protocol._send_remote_control_request_sync = MagicMock() await mock_protocol.login(delay=0.1, power_on=True) await asyncio.sleep(0) # Mock login success. msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) len(mock_protocol.send.mock_calls) == 2 # Test RC Open sent. await asyncio.sleep(1) len(mock_protocol.sync_send.mock_calls) == 2 # Test RC PS not sent if not powering on. await asyncio.sleep(1) assert not mock_protocol._send_remote_control_request_sync.mock_calls # Test only one login task scheduled at a time. mock_protocol.task = "login" mock_protocol.add_task = mock_coro() await mock_protocol.login() assert not mock_protocol.add_task.mock_calls
async def test_async_standby(): """Test async standby.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.login = mock_coro() mock_protocol.send = mock_coro() asyncio.ensure_future(mock_protocol.standby()) await asyncio.sleep(0) # Mock login success. msg = MOCK_LOGIN_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) await asyncio.sleep(0) msg = MOCK_STANDBY_SUCCESS mock_ps4.connection._decipher.decrypt = MagicMock(return_value=msg) mock_protocol.data_received(msg) assert mock_protocol.task is None
async def test_connection_made_task_queue(): """Test task queue.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.start_title = mock_coro() mock_ps4.task_queue = ("start_title", MOCK_TITLE_ID) mock_ps4.loggedin = True mock_protocol.connection_made(MagicMock()) await asyncio.sleep(0) assert mock_protocol.task_available.is_set() assert len(mock_protocol.start_title.mock_calls) == 1
async def test_async_connect(): """Test Async connect.""" mock_transport = MagicMock() mock_protocol = MagicMock() loop = asyncio.get_event_loop() loop.sock_connect = mock_connect = mock_coro() loop.sock_sendall = mock_send = mock_coro() loop.sock_recv = mock_recv = mock_coro(return_value=bytes(20) + MOCK_SEED) loop.create_connection = mock_create = mock_coro( return_value=(mock_transport, mock_protocol) ) mock_ps4 = MagicMock() mock_ps4.host = MOCK_HOST mock_connection = c.AsyncConnection(mock_ps4, MOCK_CREDS) await mock_connection.async_connect(mock_ps4) assert len(mock_connect.mock_calls) == 1 assert len(mock_send.mock_calls) == 2 assert len(mock_recv.mock_calls) == 1 assert len(mock_create.mock_calls) == 1
async def test_task_timeout(): """Test task timeout.""" mock_protocol, mock_ps4 = setup_mock_protocol() mock_protocol.connection_made(MagicMock()) mock_protocol.send = mock_coro() mock_protocol.data_received = MagicMock() # Block task mock_protocol.task_available.clear() mock_protocol.task = "login" await mock_protocol.standby() await asyncio.sleep(6) assert mock_protocol.task is None assert mock_protocol.task_available.is_set()
async def test_create_ddp_protocol(): """Test DDP Protocol init.""" mock_sock = MagicMock() with patch("pyps4_2ndscreen.ddp.asyncio.get_event_loop", return_value=MagicMock()) as mock_loop, patch( "pyps4_2ndscreen.ddp.get_socket", return_value=mock_sock): mock_loop = mock_loop() mock_create_task = mock_coro(return_value=(MagicMock(), MagicMock())) mock_call = mock_coro(return_value=(MagicMock(), MagicMock())) mock_loop.create_datagram_endpoint = mock_call mock_loop.create_task = mock_create_task mock_kwargs = { "local_addr": None, "reuse_port": None, "allow_broadcast": None, "sock": mock_sock, } _, mock_ddp = await ddp.async_create_ddp_endpoint() mock_ddp.close() args, kwargs = mock_call.call_args assert callable(args[0]) # Hack for lambda: DDPProtocol() for key, value in kwargs.items(): assert mock_kwargs[key] == value