Example #1
0
    def test_client_attach_timeout(self, mode):
        """
        Tests the case when a client attaches but no tracer does.
        """
        with QdbServer(tracer_server=QdbNopServer(),
                       client_host='localhost',
                       client_port=0,
                       attach_timeout=0.01,
                       timeout_disable_mode=mode) as server:

            client = create_connection(
                'ws://localhost:%d%s' % (server.client_server.server_port,
                                         DEFAULT_ROUTE_FMT.format(uuid='test'))
            )
            send_client_event(client, 'start', '')
            disable_event = None
            with Timeout(0.1, False):
                error_event = recv_client_event(client)
                disable_event = recv_client_event(client)

            error_dict = fmt_err_msg('tracer', 'No tracer')

            self.assertEqual(error_dict, error_event)
            self.assertEqual(fmt_msg('disable'), disable_event)
            self.assertNotIn('test', server.session_store)
Example #2
0
    def test_bad_auth_client(self):
        """
        Tests a non-valid auth message for a client.
        """
        with QdbServer(
                client_host='localhost',
                client_port=0,
                client_auth_fn=lambda _: False,  # Fail all new clients.
                tracer_server=QdbNopServer()) as server:

            ws = create_connection('ws://localhost:%d%s' %
                                   (server.client_server.server_port,
                                    DEFAULT_ROUTE_FMT.format(uuid='test')))
            send_client_event(ws, 'start', 'friendzoned-again')

            auth_failed_event = disable_event = None

            with gevent.Timeout(2, False):
                # The server should time us out in 1 second and send back these
                # two messages.
                auth_failed_event = recv_client_event(ws)
                disable_event = recv_client_event(ws)

            auth_failed_dict = fmt_err_msg('auth', 'Authentication failed')

            self.assertEqual(auth_failed_event, auth_failed_dict)
            self.assertEqual(disable_event['e'], 'disable')
            self.assertFalse('test' in server.session_store)
Example #3
0
    def test_client_attach_timeout(self, mode):
        """
        Tests the case when a client attaches but no tracer does.
        """
        with QdbServer(tracer_server=QdbNopServer(),
                       client_host='localhost',
                       client_port=0,
                       attach_timeout=0.01,
                       timeout_disable_mode=mode) as server:

            client = create_connection(
                'ws://localhost:%d%s' % (server.client_server.server_port,
                                         DEFAULT_ROUTE_FMT.format(uuid='test'))
            )
            send_client_event(client, 'start', '')
            disable_event = None
            with gevent.Timeout(0.1, False):
                error_event = recv_client_event(client)
                disable_event = recv_client_event(client)

            error_dict = fmt_err_msg('tracer', 'No tracer')

            self.assertEqual(error_dict, error_event)
            self.assertEqual(fmt_msg('disable'), disable_event)
            self.assertNotIn('test', server.session_store)
Example #4
0
    def test_client_auth_timeout(self):
        with QdbServer(
                client_host='localhost',
                client_port=0,
                auth_timeout=1,  # Timeout after 1 second.
                tracer_server=QdbNopServer()) as server:
            ws = create_connection('ws://localhost:%d%s' %
                                   (server.client_server.server_port,
                                    DEFAULT_ROUTE_FMT.format(uuid='test')))

            auth_failed_dict = fmt_err_msg('auth', 'No start event received')
            disable_dict = fmt_msg('disable')

            auth_failed_msg = ''
            disable_msg = ''

            with gevent.Timeout(2, False):
                # The server should time us out in 1 second and send back these
                # two messages.
                auth_failed_msg = ws.recv()
                disable_msg = ws.recv()

            self.assertEqual(auth_failed_msg, json.dumps(auth_failed_dict))
            self.assertEqual(disable_msg, json.dumps(disable_dict))
            self.assertFalse('test' in server.session_store)
Example #5
0
    def test_tracer_attach_timeout(self, mode):
        """
        Tests the case where a tracer attaches but no client does.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       client_server=QdbNopServer(),
                       attach_timeout=0.01,
                       timeout_disable_mode=mode) as server:

            tracer = gevent.socket.create_connection(
                ('localhost', server.tracer_server.server_port))
            send_tracer_event(tracer, 'start', {
                'uuid': 'test',
                'auth': '',
                'local': (0, 0),
            })
            disable_event = None
            with gevent.Timeout(0.1, False):
                error_event = recv_tracer_event(tracer)
                disable_event = recv_tracer_event(tracer)

            error_dict = fmt_err_msg('client', 'No client')

            self.assertEqual(error_dict, error_event)
            self.assertEqual(fmt_msg('disable', mode), disable_event)
            self.assertNotIn('test', server.session_store)
Example #6
0
    def test_bad_auth_client(self):
        """
        Tests a non-valid auth message for a client.
        """
        with QdbServer(client_host='localhost',
                       client_port=0,
                       client_auth_fn=lambda _: False,  # Fail all new clients.
                       tracer_server=QdbNopServer()) as server:

            ws = create_connection(
                'ws://localhost:%d%s' % (server.client_server.server_port,
                                         DEFAULT_ROUTE_FMT.format(uuid='test'))
            )
            send_client_event(ws, 'start', 'friendzoned-again')

            auth_failed_event = disable_event = None

            with Timeout(2, False):
                # The server should time us out in 1 second and send back these
                # two messages.
                auth_failed_event = recv_client_event(ws)
                disable_event = recv_client_event(ws)

            auth_failed_dict = fmt_err_msg('auth', 'Authentication failed')

            self.assertEquals(auth_failed_event, auth_failed_dict)
            self.assertEquals(disable_event['e'], 'disable')
            self.assertFalse('test' in server.session_store)
Example #7
0
    def test_client_auth_timeout(self):
        with QdbServer(client_host='localhost',
                       client_port=0,
                       auth_timeout=1,  # Timeout after 1 second.
                       tracer_server=QdbNopServer()) as server:
            ws = create_connection(
                'ws://localhost:%d%s' % (server.client_server.server_port,
                                         DEFAULT_ROUTE_FMT.format(uuid='test'))
            )

            auth_failed_dict = fmt_err_msg('auth', 'No start event received')
            disable_dict = fmt_msg('disable')

            auth_failed_msg = ''
            disable_msg = ''

            with Timeout(2, False):
                # The server should time us out in 1 second and send back these
                # two messages.
                auth_failed_msg = ws.recv()
                disable_msg = ws.recv()

            self.assertEquals(auth_failed_msg, json.dumps(auth_failed_dict))
            self.assertEquals(disable_msg, json.dumps(disable_dict))
            self.assertFalse('test' in server.session_store)
Example #8
0
    def test_tracer_attach_timeout(self, mode):
        """
        Tests the case where a tracer attaches but no client does.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       client_server=QdbNopServer(),
                       attach_timeout=0.01,
                       timeout_disable_mode=mode) as server:

            tracer = socket.create_connection(
                ('localhost', server.tracer_server.server_port)
            )
            send_tracer_event(tracer, 'start', {
                'uuid': 'test',
                'auth': '',
                'local': (0, 0),
            })
            disable_event = None
            with Timeout(0.1, False):
                error_event = recv_tracer_event(tracer)
                disable_event = recv_tracer_event(tracer)

            error_dict = fmt_err_msg('client', 'No client')

            self.assertEqual(error_dict, error_event)
            self.assertEqual(fmt_msg('disable', mode), disable_event)
            self.assertNotIn('test', server.session_store)
Example #9
0
    def attach_client(self, uuid, ws):
        """
        Attaches a client for uuid at the ws.
        This call waits for the client to come.
        Returns True iff the client came and the session is ready to begin,
        otherwise returns False and does not add the session to the store.
        """
        log.info('Attaching a client for session %s' % uuid)
        if uuid in self._sessions:
            session = self._sessions[uuid].update_timestamp()
        else:
            session = DebuggingSession()

        self._sessions[uuid] = session.attach_client(ws)
        # Wait for the tracer if needed.
        if self.attach_timeout == ALLOW_ORPHANS:
            log.info(
                'Attached %sclient for session %s' %
                ('' if self._sessions[uuid].tracer else 'orphaned ', uuid))
            return True
        if not self._sessions[uuid].both_sides_event.wait(self.attach_timeout):
            # Signal to the client that no tracer attached.
            ws.send(fmt_err_msg('tracer', 'No tracer', serial=json.dumps))
            self.slaughter(uuid)
            log.warn('No tracer attached for %s' % uuid)
            return False
        return True
Example #10
0
    def attach_tracer(self, uuid, socket, local_pid, pause_signal):
        """
        Attaches the tracer for uuid at the socket.
        This call waits for at least one client to come.
        Returns True iff a client came and the session is ready to begin,
        otherwise, returns false and does not add the session to the store.
        """
        log.info('Attaching a tracer for session %s' % uuid)
        if uuid in self._sessions:
            session = self._sessions[uuid].update_timestamp()
        else:
            session = DebuggingSession()

        self._sessions[uuid] = session.attach_tracer(socket, local_pid,
                                                     pause_signal)
        # Wait for the client if needed.
        if self.attach_timeout == 0:
            log.info(
                'Attached %s%stracer for session %s' %
                ('local ' if local_pid else '',
                 '' if self._sessions[uuid].clients else 'orphaned ', uuid))
            return True
        if not self._sessions[uuid].both_sides_event.wait(self.attach_timeout):
            # Signal to the tracer that no client attached.
            self._send_to_socket(
                socket, fmt_err_msg('client', 'No client',
                                    serial=pickle.dumps))
            self.slaughter(uuid, self.timeout_disable_mode)
            log.warn('No client came to debug %s' % uuid)
            return False
        log.info('Session %s has started' % uuid)
        return True
Example #11
0
    def attach_client(self, uuid, ws):
        """
        Attaches a client for uuid at the ws.
        This call waits for the client to come.
        Returns True iff the client came and the session is ready to begin,
        otherwise returns False and does not add the session to the store.
        """
        log.info('Attaching a client for session %s' % uuid)
        if uuid in self._sessions:
            session = self._sessions[uuid].update_timestamp()
        else:
            session = DebuggingSession()

        self._sessions[uuid] = session.attach_client(ws)
        # Wait for the tracer if needed.
        if self.attach_timeout == ALLOW_ORPHANS:
            log.info('Attached %sclient for session %s'
                     % ('' if self._sessions[uuid].tracer else 'orphaned ',
                        uuid))
            return True
        if not self._sessions[uuid].both_sides_event.wait(self.attach_timeout):
            # Signal to the client that no tracer attached.
            ws.send(fmt_err_msg('tracer', 'No tracer', serial=json.dumps))
            self.slaughter(uuid)
            log.warn('No tracer attached for %s' % uuid)
            return False
        return True
Example #12
0
 def send_error(self, ws, error_type, error_data):
     """
     Sends an error event back to the client.
     """
     try:
         ws.send(fmt_err_msg(error_type, error_data, serial=json.dumps))
     except WebSocketError:
         return
Example #13
0
 def send_error(self, ws, error_type, error_data):
     """
     Sends an error event back to the client.
     """
     try:
         ws.send(fmt_err_msg(error_type, error_data, serial=json.dumps))
     except WebSocketError:
         return
Example #14
0
    def test_tracer_auth_timeout(self):
        """
        Tests the auth timeout for new connections from the client.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       client_server=QdbNopServer(),
                       auth_timeout=1) as server:

            auth_failed_dict = fmt_err_msg('auth', 'No start event received')
            sck = gevent.socket.create_connection(
                ('localhost', server.tracer_server.server_port))

            self.assertEqual(auth_failed_dict, recv_tracer_event(sck))
            self.assertFalse('test' in server.session_store)
Example #15
0
    def test_tracer_auth_timeout(self):
        """
        Tests the auth timeout for new connections from the client.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       client_server=QdbNopServer(),
                       auth_timeout=1) as server:

            auth_failed_dict = fmt_err_msg('auth', 'No start event received')
            sck = socket.create_connection(
                ('localhost', server.tracer_server.server_port)
            )

            self.assertEqual(auth_failed_dict, recv_tracer_event(sck))
            self.assertFalse('test' in server.session_store)
Example #16
0
    def test_bad_auth_tracer(self):
        """
        Tests a non-valid auth message for a tracer.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       tracer_auth_fn=lambda _: False,
                       client_server=QdbNopServer()) as server:

            auth_failed_dict = fmt_err_msg('auth', 'Authentication failed')

            sck = gevent.socket.create_connection(
                ('localhost', server.tracer_server.server_port))

            send_tracer_event(sck, 'start', {
                'uuid': 'test',
                'auth': 'friendzoned-again',
                'local': (0, 0),
            })
            # We failed auth so the socket should be closed.
            self.assertEqual(auth_failed_dict, recv_tracer_event(sck))
            self.assertFalse('test' in server.session_store)
Example #17
0
    def test_bad_auth_tracer(self):
        """
        Tests a non-valid auth message for a tracer.
        """
        with QdbServer(tracer_host='localhost',
                       tracer_port=0,
                       tracer_auth_fn=lambda _: False,
                       client_server=QdbNopServer()) as server:

            auth_failed_dict = fmt_err_msg('auth', 'Authentication failed')

            sck = socket.create_connection(
                ('localhost', server.tracer_server.server_port)
            )

            send_tracer_event(sck, 'start', {
                'uuid': 'test',
                'auth': 'friendzoned-again',
                'local': (0, 0),
            })
            # We failed auth so the socket should be closed.
            self.assertEqual(auth_failed_dict,
                             recv_tracer_event(sck))
            self.assertFalse('test' in server.session_store)
Example #18
0
    def attach_tracer(self, uuid, socket, local_pid, pause_signal):
        """
        Attaches the tracer for uuid at the socket.
        This call waits for at least one client to come.
        Returns True iff a client came and the session is ready to begin,
        otherwise, returns false and does not add the session to the store.
        """
        log.info('Attaching a tracer for session %s' % uuid)
        if uuid in self._sessions:
            session = self._sessions[uuid].update_timestamp()
        else:
            session = DebuggingSession()

        self._sessions[uuid] = session.attach_tracer(
            socket,
            local_pid,
            pause_signal
        )
        # Wait for the client if needed.
        if self.attach_timeout == 0:
            log.info('Attached %s%stracer for session %s'
                     % ('local ' if local_pid else '',
                        '' if self._sessions[uuid].clients else 'orphaned ',
                        uuid))
            return True
        if not self._sessions[uuid].both_sides_event.wait(self.attach_timeout):
            # Signal to the tracer that no client attached.
            self._send_to_socket(socket, fmt_err_msg(
                'client', 'No client',
                serial=pickle.dumps
            ))
            self.slaughter(uuid, self.timeout_disable_mode)
            log.warn('No client came to debug %s' % uuid)
            return False
        log.info('Session %s has started' % uuid)
        return True