def test_create_client_side_connection_hint_already_in_use(self): """Tests Sl4aSession._create_client_side_connection(). Tests that if the hinted port is already in use, the function will call itself with a hinted port of 0 (random). """ session = mock.Mock() session._create_client_side_connection = mock.Mock() with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. error = OSError() error.errno = errno.EADDRINUSE socket_instance = mock.Mock() socket_instance.bind = mock.Mock() socket_instance.bind.side_effect = error socket.return_value = socket_instance Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(1, 2, 3)) fn = session._create_client_side_connection self.assertEqual(fn.call_count, 1) # Asserts that the 1st argument (Sl4aPorts) sent to the function # has a client port of 0. self.assertEqual(fn.call_args_list[0][0][0].client_port, 0)
def test_create_client_side_connection_hint_taken_during_fn(self): """Tests Sl4aSession._create_client_side_connection(). Tests that the function will call catch an EADDRNOTAVAIL OSError and call itself again, this time with a hinted port of 0 (random). """ session = mock.Mock() session._create_client_side_connection = mock.Mock() error = socket_error() error.errno = errno.EADDRNOTAVAIL with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. socket_instance = mock.Mock() socket_instance.connect = mock.Mock() socket_instance.connect.side_effect = error socket.return_value = socket_instance Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(0, 2, 3)) fn = session._create_client_side_connection self.assertEqual(fn.call_count, 1) # Asserts that the 1st argument (Sl4aPorts) sent to the function # has a client port of 0. self.assertEqual(fn.call_args_list[0][0][0].client_port, 0)
def _create_rpc_connection(self, ports=None, uid=UNKNOWN_UID): """Creates an RPC Connection with the specified ports. Args: ports: A Sl4aPorts object or a tuple of (host/client_port, forwarded_port, device/server_port). If any of these are zero, the OS will determine their values during connection. Note that these ports are only suggestions. If they are not available, the a different port will be selected. uid: The UID of the SL4A Session. To create a new session, use UNKNOWN_UID. Returns: An Sl4aClient. """ if ports is None: ports = sl4a_ports.Sl4aPorts(0, 0, 0) # Open a new server if a server cannot be inferred. ports.server_port = self.obtain_server_port(ports.server_port) self.server_port = ports.server_port # Forward the device port to the host. ports.forwarded_port = int(self.adb.tcp_forward(0, ports.server_port)) client_socket, fd = self._create_client_side_connection(ports) client = rpc_connection.RpcConnection(self.adb, ports, client_socket, fd, uid=uid) client.open() if uid == UNKNOWN_UID: self.uid = client.uid return client
def test_create_client_side_connection_catches_timeout(self): """Tests Sl4aSession._create_client_side_connection(). Tests that the function will raise an Sl4aConnectionError upon timeout. """ session = mock.Mock() session._create_client_side_connection = mock.Mock() error = timeout() with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. socket_instance = mock.Mock() socket_instance.connect = mock.Mock() socket_instance.connect.side_effect = error socket.return_value = socket_instance with self.assertRaises(rpc_client.Sl4aConnectionError): Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(0, 2, 3))
def test_create_client_side_connection_re_raises_uncaught_errors(self): """Tests Sl4aSession._create_client_side_connection(). Tests that the function will re-raise any socket error that does not have errno.EADDRNOTAVAIL. """ session = mock.Mock() session._create_client_side_connection = mock.Mock() error = socket_error() # Some error that isn't EADDRNOTAVAIL error.errno = errno.ESOCKTNOSUPPORT with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. socket_instance = mock.Mock() socket_instance.connect = mock.Mock() socket_instance.connect.side_effect = error socket.return_value = socket_instance with self.assertRaises(socket_error): Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(0, 2, 3))
def create_client(uid): return self._create_rpc_connection(ports=sl4a_ports.Sl4aPorts( host_port, 0, self.server_port), uid=uid)