2008-09-01 01:40:06 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
|
|
|
|
|
|
|
from common import resolver
|
|
|
|
|
|
|
|
from mock import Mock, expectParams
|
2009-01-11 14:49:03 +01:00
|
|
|
from gajim_mocks import *
|
|
|
|
from xmpp_mocks import IdleQueueThread
|
2008-09-01 01:40:06 +02:00
|
|
|
|
|
|
|
GMAIL_SRV_NAME = '_xmpp-client._tcp.gmail.com'
|
|
|
|
NONSENSE_NAME = 'sfsdfsdfsdf.sdfs.fsd'
|
|
|
|
JABBERCZ_TXT_NAME = '_xmppconnect.jabber.cz'
|
|
|
|
JABBERCZ_SRV_NAME = '_xmpp-client._tcp.jabber.cz'
|
|
|
|
|
|
|
|
TEST_LIST = [(GMAIL_SRV_NAME, 'srv', True),
|
|
|
|
(NONSENSE_NAME, 'srv', False),
|
|
|
|
(JABBERCZ_SRV_NAME, 'srv', True)]
|
|
|
|
|
|
|
|
class TestResolver(unittest.TestCase):
|
2009-01-11 14:49:03 +01:00
|
|
|
'''
|
|
|
|
Test for LibAsyncNSResolver and NSLookupResolver. Requires working
|
|
|
|
network connection.
|
|
|
|
'''
|
2008-09-01 01:40:06 +02:00
|
|
|
def setUp(self):
|
2009-01-11 14:49:03 +01:00
|
|
|
self.idlequeue_thread = IdleQueueThread()
|
|
|
|
self.idlequeue_thread.start()
|
|
|
|
|
|
|
|
self.iq = self.idlequeue_thread.iq
|
|
|
|
self._reset()
|
2008-09-01 01:40:06 +02:00
|
|
|
self.resolver = None
|
2009-01-11 14:49:03 +01:00
|
|
|
|
|
|
|
def tear_down(self):
|
|
|
|
self.idlequeue_thread.stop_thread()
|
|
|
|
self.idlequeue_thread.join()
|
2008-09-01 01:40:06 +02:00
|
|
|
|
2009-01-11 14:49:03 +01:00
|
|
|
def _reset(self):
|
2008-09-01 01:40:06 +02:00
|
|
|
self.flag = False
|
|
|
|
self.expect_results = False
|
|
|
|
self.nslookup = False
|
|
|
|
self.resolver = None
|
|
|
|
|
|
|
|
def testLibAsyncNSResolver(self):
|
2009-01-11 14:49:03 +01:00
|
|
|
self._reset()
|
2008-09-01 01:40:06 +02:00
|
|
|
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
|
2009-01-11 14:49:03 +01:00
|
|
|
self._runLANSR(name, type)
|
2008-09-01 01:40:06 +02:00
|
|
|
self.flag = False
|
|
|
|
|
2009-01-11 14:49:03 +01:00
|
|
|
def _runLANSR(self, name, type):
|
2008-09-01 01:40:06 +02:00
|
|
|
self.resolver.resolve(
|
|
|
|
host = name,
|
|
|
|
type = type,
|
2009-01-11 14:49:03 +01:00
|
|
|
on_ready = self._myonready)
|
2008-09-01 01:40:06 +02:00
|
|
|
while not self.flag:
|
|
|
|
time.sleep(1)
|
|
|
|
self.resolver.process()
|
|
|
|
|
2009-01-11 14:49:03 +01:00
|
|
|
def _myonready(self, name, result_set):
|
2008-12-24 15:44:07 +01:00
|
|
|
if __name__ == '__main__':
|
2009-01-11 14:49:03 +01:00
|
|
|
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('')
|
2008-09-01 01:40:06 +02:00
|
|
|
if self.expect_results:
|
|
|
|
self.assert_(len(result_set) > 0)
|
|
|
|
else:
|
|
|
|
self.assert_(result_set == [])
|
|
|
|
self.flag = True
|
2009-01-11 14:49:03 +01:00
|
|
|
if self.nslookup:
|
|
|
|
self._testNSLR()
|
2008-09-01 01:40:06 +02:00
|
|
|
|
|
|
|
def testNSLookupResolver(self):
|
2009-01-11 14:49:03 +01:00
|
|
|
self._reset()
|
2008-09-01 01:40:06 +02:00
|
|
|
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,
|
2009-01-11 14:49:03 +01:00
|
|
|
on_ready = self._myonready)
|
2008-09-01 01:40:06 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2008-12-24 15:44:07 +01:00
|
|
|
|
|
|
|
# vim: se ts=3:
|