Esempio n. 1
0
    def test_open_timeout(self, mock_create_connection):
        """Test socket timeout

        Test that a timeout exception will be raised if the socket gives a
        timeout.
        """
        mock_create_connection.side_effect = socket.timeout

        with self.assertRaises(socket.timeout):
            client = sl4a_client.Sl4aClient()
            client.open(connection_timeout=0.1)
Esempio n. 2
0
    def test_open_timeout_io_error(self, mock_create_connection):
        """Test socket timeout with io error

        Test that if the net socket gives an io error, then the sl4a client
        will eventually exit with an IOError.
        """
        mock_create_connection.side_effect = IOError()

        with self.assertRaises(IOError):
            client = sl4a_client.Sl4aClient()
            client.open(connection_timeout=0.1)
Esempio n. 3
0
    def test_handshake_error(self, mock_create_connection):
        """Test error in sl4a handshake

        Test that if there is an error in the sl4a handshake then a protocol
        error will be raised.
        """
        fake_conn = mock.MagicMock()
        fake_conn.makefile.return_value = MockSocketFile(None)
        mock_create_connection.return_value = fake_conn

        with self.assertRaises(sl4a_client.Sl4aProtocolError):
            client = sl4a_client.Sl4aClient()
            client.open()
Esempio n. 4
0
    def test_open_handshake(self, mock_create_connection):
        """Test sl4a client handshake

        Test that at the end of a handshake with no errors the client object
        has the correct parameters.
        """
        fake_conn = mock.MagicMock()
        fake_conn.makefile.return_value = MockSocketFile(MOCK_RESP)
        mock_create_connection.return_value = fake_conn

        client = sl4a_client.Sl4aClient()
        client.open()

        self.assertEqual(client.uid, 1)
Esempio n. 5
0
    def test_rpc_call_increment_counter(self, mock_create_connection):
        """Test rpc counter

        Test that with each rpc call the counter is incremented by 1.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        for i in range(0, 10):
            fake_file.resp = (MOCK_RESP_TEMPLATE % i).encode('utf-8')
            client.some_rpc()

        self.assertEqual(next(client._counter), 10)
Esempio n. 6
0
    def test_rpc_error_response(self, mock_create_connection):
        """Test rpc that is given an error response

        Test that when an rpc recieves a reponse with an error will raised
        an api error.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        fake_file.resp = MOCK_RESP_WITH_ERROR

        with self.assertRaises(sl4a_client.Sl4aApiError, msg=1):
            client.some_rpc(1, 2, 3)
Esempio n. 7
0
    def test_open_handshake_unknown_status(self, mock_create_connection):
        """Test handshake with unknown status response

        Test that when the handshake is given an unknown status then the client
        will not be given a uid.
        """
        fake_conn = mock.MagicMock()
        fake_conn.makefile.return_value = MockSocketFile(
            MOCK_RESP_UNKWN_STATUS)
        mock_create_connection.return_value = fake_conn

        client = sl4a_client.Sl4aClient()
        client.open()

        self.assertEqual(client.uid, sl4a_client.UNKNOWN_UID)
Esempio n. 8
0
    def test_rpc_no_response(self, mock_create_connection):
        """Test rpc that does not get a reponse

        Test that when an rpc does not get a response it throws a protocol
        error.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        fake_file.resp = None

        with self.assertRaises(
                sl4a_client.Sl4aProtocolError,
                msg=sl4a_client.Sl4aProtocolError.NO_RESPONSE_FROM_SERVER):
            client.some_rpc(1, 2, 3)
Esempio n. 9
0
    def test_rpc_id_mismatch(self, mock_create_connection):
        """Test rpc that returns a different id than expected

        Test that if an rpc returns with an id that is different than what
        is expected will give a protocl error.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        fake_file.resp = (MOCK_RESP_TEMPLATE % 52).encode('utf8')

        with self.assertRaises(
                sl4a_client.Sl4aProtocolError,
                msg=sl4a_client.Sl4aProtocolError.MISMATCHED_API_ID):
            client.some_rpc(1, 2, 3)
Esempio n. 10
0
    def test_open_no_response(self, mock_create_connection):
        """Test handshake no response

        Test that if a handshake recieves no response then it will give a
        protocol error.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        fake_file.resp = None

        with self.assertRaises(
                sl4a_client.Sl4aProtocolError,
                msg=sl4a_client.Sl4aProtocolError.NO_RESPONSE_FROM_HANDSHAKE):
            client.some_rpc(1, 2, 3)
Esempio n. 11
0
    def test_rpc_send_to_socket(self, mock_create_connection):
        """Test rpc sending and recieving

        Tests that when an rpc is sent and received the corrent data
        is used.
        """
        fake_file = self.setup_mock_socket_file(mock_create_connection)

        client = sl4a_client.Sl4aClient()
        client.open()

        result = client.some_rpc(1, 2, 3)
        self.assertEqual(result, 123)

        expected = {'id': 0, 'method': 'some_rpc', 'params': [1, 2, 3]}
        actual = json.loads(fake_file.last_write.decode('utf-8'))

        self.assertEqual(expected, actual)
    def add_new_connection_to_session(self, session_id):
        """Create a new connection to an existing sl4a session.

        Args:
            session_id: UID of the sl4a session to add connection to.

        Returns:
            An Android object used to communicate with sl4a on the android
                device.

        Raises:
            DoesNotExistError: Raised if the session it's trying to connect to
            does not exist.
        """
        if session_id not in self._droid_sessions:
            raise DoesNotExistError("Session %d doesn't exist." % session_id)
        droid = sl4a_client.Sl4aClient(port=self.h_port, uid=session_id)
        droid.open(cmd=sl4a_client.Sl4aCommand.CONTINUE)
        return droid
    def start_new_session(self):
        """Start a new session in sl4a.

        Also caches the droid in a dict with its uid being the key.

        Returns:
            An Android object used to communicate with sl4a on the android
                device.

        Raises:
            Sl4aException: Something is wrong with sl4a and it returned an
            existing uid to a new session.
        """
        droid = sl4a_client.Sl4aClient(port=self.h_port)
        droid.open()
        if droid.uid in self._droid_sessions:
            raise sl4a_client.Sl4aException(
                "SL4A returned an existing uid for a new session. Abort.")
        self._droid_sessions[droid.uid] = [droid]
        return droid