예제 #1
0
class TestResolver(unittest.TestCase):
    '''
    Test for LibAsyncNSResolver and NSLookupResolver. Requires working
    network connection.
    '''
    def setUp(self):
        self.idlequeue_thread = IdleQueueThread()
        self.idlequeue_thread.start()
        self.main_context = GLib.MainContext()
        self.main_context.push_thread_default()
        self.main_loop = GLib.MainLoop(self.main_context)

        self.iq = self.idlequeue_thread.iq
        self._reset()
        self.resolver = None

    def tearDown(self):
        self.main_context.pop_thread_default()
        self.idlequeue_thread.stop_thread()
        self.idlequeue_thread.join()

    def _reset(self):
        self.expect_results = False
        self.resolver = None

    def testGioResolver(self):
        self._reset()
        self.resolver = resolver.GioResolver()

        for name, type_, expect_results in TEST_LIST:
            self.expect_results = expect_results
            self._runGR(name, type_)

    def _runGR(self, name, type_):
        self.resolver.resolve(host=name, type_=type_, on_ready=self._myonready)

        self.main_loop.run()

    def _myonready(self, name, result_set):
        if __name__ == '__main__':
            from pprint import pprint
            pprint('on_ready called ...')
            pprint('hostname: %s' % name)
            pprint('result set: %s' % result_set)
            pprint('res.resolved_hosts: %s' % self.resolver.resolved_hosts)
            pprint('')
        if self.expect_results:
            self.assertTrue(len(result_set) > 0)
        else:
            self.assertTrue(result_set == [])
        self.main_loop.quit()
class AbstractTransportTest(unittest.TestCase):
    ''' Encapsulates Idlequeue instantiation for transports and more...'''

    def setUp(self):
        ''' IdleQueue thread is run and dummy connection is created. '''
        self.idlequeue_thread = IdleQueueThread()
        self.idlequeue_thread.start()
        self._setup_hook()

    def tearDown(self):
        ''' IdleQueue thread is stopped. '''
        self._teardown_hook()
        self.idlequeue_thread.stop_thread()
        self.idlequeue_thread.join()

    def _setup_hook(self):
        pass

    def _teardown_hook(self):
        pass

    def expect_receive(self, expected, count=1, msg=None):
        '''
        Returns a callback function that will assert whether the data passed to
        it equals the one specified when calling this function.

        Can be used to make sure transport dispatch correct data.
        '''
        def receive(data, *args, **kwargs):
            self.assertEqual(data, expected, msg=msg)
            self._expected_count -= 1
        self._expected_count = count
        return receive

    def have_received_expected(self):
        '''
        Plays together with expect_receive(). Will return true if expected_rcv
        callback was called as often as specified
        '''
        return self._expected_count == 0
예제 #3
0
class TestNonBlockingClient(unittest.TestCase):
    '''
    Test Cases class for NonBlockingClient.
    '''
    def setUp(self):
        ''' IdleQueue thread is run and dummy connection is created. '''
        self.idlequeue_thread = IdleQueueThread()
        self.connection = MockConnection() # for dummy callbacks
        self.idlequeue_thread.start()

    def tearDown(self):
        ''' IdleQueue thread is stopped. '''
        self.idlequeue_thread.stop_thread()
        self.idlequeue_thread.join()
        del self.connection

        self.client = None

    def open_stream(self, server_port, wrong_pass=False):
        '''
        Method opening the XMPP connection. It returns when <stream:features>
        is received from server.

        :param server_port: tuple of (hostname, port) for where the client should
        connect.
        '''

        class TempConnection():
            def get_password(self, cb, mechanism):
                if wrong_pass:
                    cb('wrong pass')
                else:
                    cb(credentials[1])
            def on_connect_failure(self):
                pass

        self.client = client_nb.NonBlockingClient(
                domain=server_port[0],
                idlequeue=self.idlequeue_thread.iq,
                caller=Mock(realClass=TempConnection))

        self.client.connect(
                hostname=server_port[0],
                port=server_port[1],
                on_connect=lambda *args: self.connection.on_connect(True, *args),
                on_connect_failure=lambda *args: self.connection.on_connect(
                        False, *args))

        self.assert_(self.connection.wait(),
                msg='waiting for callback from client constructor')

        # if on_connect was called, client has to be connected and vice versa
        if self.connection.connect_succeeded:
            self.assert_(self.client.get_connect_type())
        else:
            self.assert_(not self.client.get_connect_type())

    def client_auth(self, username, password, resource, sasl):
        '''
        Method authenticating connected client with supplied credentials. Returns
        when authentication is over.

        :param sasl: whether to use sasl (sasl=1) or old (sasl=0) authentication
        :todo: to check and be more specific about when it returns
                (bind, session..)
        '''
        self.client.auth(username, password, resource, sasl,
            on_auth=self.connection.on_auth)

        self.assert_(self.connection.wait(), msg='waiting for authentication')

    def do_disconnect(self):
        '''
        Does disconnecting of connected client. Returns when TCP connection is
        closed.
        '''
        self.client.RegisterDisconnectHandler(self.connection.set_event)
        self.client.disconnect()

        self.assertTrue(self.connection.wait(), msg='waiting for disconnecting')

    def test_proper_connect_sasl(self):
        '''
        The ideal testcase - client is connected, authenticated with SASL and
        then disconnected.
        '''
        self.open_stream(xmpp_server_port)

        # if client is not connected, lets raise the AssertionError
        self.assert_(self.client.get_connect_type())
        # client.disconnect() is already called from NBClient via
        # _on_connected_failure, no need to call it here

        self.client_auth(credentials[0], credentials[1], credentials[2], sasl=1)
        self.assert_(self.connection.con)
        self.assert_(self.connection.auth=='sasl', msg='Unable to auth via SASL')

        self.do_disconnect()

    def test_proper_connect_oldauth(self):
        '''
        The ideal testcase - client is connected, authenticated with old auth and
        then disconnected.
        '''
        self.open_stream(xmpp_server_port)
        self.assert_(self.client.get_connect_type())
        self.client_auth(credentials[0], credentials[1], credentials[2], sasl=0)
        self.assert_(self.connection.con)
        features = self.client.Dispatcher.Stream.features
        if not features.getTag('auth'):
            print "Server doesn't support old authentication type, ignoring test"
        else:
            self.assert_(self.connection.auth=='old_auth',
                    msg='Unable to auth via old_auth')
        self.do_disconnect()

    def test_connect_to_nonexisting_host(self):
        '''
        Connect to nonexisting host. DNS request for A records should return
        nothing.
        '''
        self.open_stream(('fdsfsdf.fdsf.fss', 5222))
        self.assert_(not self.client.get_connect_type())

    def test_connect_to_wrong_port(self):
        '''
        Connect to nonexisting server. DNS request for A records should return an
        IP      but there shouldn't be XMPP server running on specified port.
        '''
        self.open_stream((xmpp_server_port[0], 31337))
        self.assert_(not self.client.get_connect_type())

    def test_connect_with_wrong_creds(self):
        '''
        Connecting with invalid password.
        '''
        self.open_stream(xmpp_server_port, wrong_pass=True)
        self.assert_(self.client.get_connect_type())
        self.client_auth(credentials[0], 'wrong pass', credentials[2], sasl=1)
        self.assert_(self.connection.auth is None)
        self.do_disconnect()
예제 #4
0
class TestResolver(unittest.TestCase):
    '''
    Test for LibAsyncNSResolver and NSLookupResolver. Requires working
    network connection.
    '''
    def setUp(self):
        self.idlequeue_thread = IdleQueueThread()
        self.idlequeue_thread.start()

        self.iq = self.idlequeue_thread.iq
        self._reset()
        self.resolver = None

    def tearDown(self):
        self.idlequeue_thread.stop_thread()
        self.idlequeue_thread.join()

    def _reset(self):
        self.flag = False
        self.expect_results = False
        self.nslookup = False
        self.resolver = None

    def testLibAsyncNSResolver(self):
        self._reset()
        if not resolver.USE_LIBASYNCNS:
            print 'testLibAsyncResolver: libasyncns-python not installed'
            return
        self.resolver = resolver.LibAsyncNSResolver()

        for name, type, expect_results in TEST_LIST:
            self.expect_results = expect_results
            self._runLANSR(name, type)
            self.flag = False

    def _runLANSR(self, name, type):
        self.resolver.resolve(
                host = name,
                type = type,
                on_ready = self._myonready)
        while not self.flag:
            time.sleep(1)
            self.resolver.process()

    def _myonready(self, name, result_set):
        if __name__ == '__main__':
            from pprint import pprint
            pprint('on_ready called ...')
            pprint('hostname: %s' % name)
            pprint('result set: %s' % result_set)
            pprint('res.resolved_hosts: %s' % self.resolver.resolved_hosts)
            pprint('')
        if self.expect_results:
            self.assert_(len(result_set) > 0)
        else:
            self.assert_(result_set == [])
        self.flag = True
        if self.nslookup:
            self._testNSLR()

    def testNSLookupResolver(self):
        self._reset()
        self.nslookup = True
        self.resolver = resolver.NSLookupResolver(self.iq)
        self.test_list = TEST_LIST
        self._testNSLR()

    def _testNSLR(self):
        if self.test_list == []:
            return
        name, type, self.expect_results = self.test_list.pop()
        self.resolver.resolve(
                host = name,
                type = type,
                on_ready = self._myonready)
예제 #5
0
class TestResolver(unittest.TestCase):
    '''
    Test for LibAsyncNSResolver and NSLookupResolver. Requires working
    network connection.
    '''
    def setUp(self):
        self.idlequeue_thread = IdleQueueThread()
        self.idlequeue_thread.start()

        self.iq = self.idlequeue_thread.iq
        self._reset()
        self.resolver = None

    def tearDown(self):
        self.idlequeue_thread.stop_thread()
        self.idlequeue_thread.join()

    def _reset(self):
        self.flag = False
        self.expect_results = False
        self.nslookup = False
        self.resolver = None

    def testLibAsyncNSResolver(self):
        self._reset()
        if not resolver.USE_LIBASYNCNS:
            print('testLibAsyncResolver: libasyncns-python not installed')
            return
        self.resolver = resolver.LibAsyncNSResolver()

        for name, type, expect_results in TEST_LIST:
            self.expect_results = expect_results
            self._runLANSR(name, type)
            self.flag = False

    def _runLANSR(self, name, type):
        self.resolver.resolve(host=name, type=type, on_ready=self._myonready)
        while not self.flag:
            time.sleep(1)
            self.resolver.process()

    def _myonready(self, name, result_set):
        if __name__ == '__main__':
            from pprint import pprint
            pprint('on_ready called ...')
            pprint('hostname: %s' % name)
            pprint('result set: %s' % result_set)
            pprint('res.resolved_hosts: %s' % self.resolver.resolved_hosts)
            pprint('')
        if self.expect_results:
            self.assert_(len(result_set) > 0)
        else:
            self.assert_(result_set == [])
        self.flag = True
        if self.nslookup:
            self._testNSLR()

    def testNSLookupResolver(self):
        self._reset()
        self.nslookup = True
        self.resolver = resolver.NSLookupResolver(self.iq)
        self.test_list = TEST_LIST
        self._testNSLR()

    def _testNSLR(self):
        if self.test_list == []:
            return
        name, type, self.expect_results = self.test_list.pop()
        self.resolver.resolve(host=name, type=type, on_ready=self._myonready)