Пример #1
0
class AuthorizationTestCase(unittest.TestCase):
    def setUp(self):
        self.proxy = sip.RegisterProxy(host="intarweb.us")
        self.proxy.authorizers = self.proxy.authorizers.copy()
        self.proxy.authorizers['digest'] = FakeDigestAuthorizer()

        self.registry = FakeRegistry("intarweb.us")
        self.proxy.registry = self.proxy.locator = self.registry
        self.transport = proto_helpers.FakeDatagramTransport()
        self.proxy.transport = self.transport

        r = TestRealm()
        p = cred.portal.Portal(r)
        c = cred.checkers.InMemoryUsernamePasswordDatabaseDontUse()
        c.addUser('*****@*****.**', 'password')
        p.registerChecker(c)
        self.proxy.portal = p

    setUp = utils.suppressWarnings(
        setUp,
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestAuthorizer was deprecated'))

    def tearDown(self):
        for d, uri in self.registry.users.values():
            d.cancel()
        del self.proxy

    def testChallenge(self):
        self.proxy.datagramReceived(registerRequest, ("127.0.0.1", 5632))

        self.assertEqual(self.transport.written[-1],
                         ((challengeResponse, ("127.0.0.1", 5632))))
        self.transport.written = []

        self.proxy.datagramReceived(authRequest, ("127.0.0.1", 5632))

        self.assertEqual(self.transport.written[-1],
                         ((okResponse, ("127.0.0.1", 5632))))

    testChallenge.suppress = [
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestAuthorizer was deprecated'),
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestedCredentials was deprecated'
        ),
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestCalcHA1 was deprecated'),
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestCalcResponse was deprecated')
    ]
Пример #2
0
class SuppressionMixin(EmitMixin):
    suppress = [util.suppress(message=CLASS_WARNING_MSG)]

    def testSuppressMethod(self):
        self._emit()
    testSuppressMethod.suppress = [util.suppress(message=METHOD_WARNING_MSG)]

    def testSuppressClass(self):
        self._emit()

    def testOverrideSuppressClass(self):
        self._emit()
    testOverrideSuppressClass.suppress = []
Пример #3
0
    def _run(self, methodName, result):
        from twisted.internet import reactor
        timeout = self.getTimeout()

        def onTimeout(d):
            e = defer.TimeoutError("%r (%s) still running at %s secs" %
                                   (self, methodName, timeout))
            f = failure.Failure(e)
            # try to errback the deferred that the test returns (for no gorram
            # reason) (see issue1005 and test_errorPropagation in
            # test_deferred)
            try:
                d.errback(f)
            except defer.AlreadyCalledError:
                # if the deferred has been called already but the *back chain
                # is still unfinished, crash the reactor and report timeout
                # error ourself.
                reactor.crash()
                self._timedOut = True  # see self._wait
                todo = self.getTodo()
                if todo is not None and todo.expected(f):
                    result.addExpectedFailure(self, f, todo)
                else:
                    result.addError(self, f)

        onTimeout = utils.suppressWarnings(
            onTimeout, util.suppress(category=DeprecationWarning))
        method = getattr(self, methodName)
        d = defer.maybeDeferred(utils.runWithWarningsSuppressed,
                                self._getSuppress(), method)
        call = reactor.callLater(timeout, onTimeout, d)
        d.addBoth(lambda x: call.active() and call.cancel() or x)
        return d
Пример #4
0
 def _run(self, methodName, result):
     from twisted.internet import reactor
     timeout = self.getTimeout()
     def onTimeout(d):
         e = defer.TimeoutError("%r (%s) still running at %s secs"
             % (self, methodName, timeout))
         f = failure.Failure(e)
         # try to errback the deferred that the test returns (for no gorram
         # reason) (see issue1005 and test_errorPropagation in
         # test_deferred)
         try:
             d.errback(f)
         except defer.AlreadyCalledError:
             # if the deferred has been called already but the *back chain
             # is still unfinished, crash the reactor and report timeout
             # error ourself.
             reactor.crash()
             self._timedOut = True # see self._wait
             todo = self.getTodo()
             if todo is not None and todo.expected(f):
                 result.addExpectedFailure(self, f, todo)
             else:
                 result.addError(self, f)
     onTimeout = utils.suppressWarnings(
         onTimeout, util.suppress(category=DeprecationWarning))
     method = getattr(self, methodName)
     d = defer.maybeDeferred(
         utils.runWithWarningsSuppressed, self._getSuppress(), method)
     call = reactor.callLater(timeout, onTimeout, d)
     d.addBoth(lambda x : call.active() and call.cancel() or x)
     return d
Пример #5
0
class TestPyUnitTestCase(TestCase):
    class PyUnitTest(pyunit.TestCase):
        def test_pass(self):
            pass

    def setUp(self):
        self.original = self.PyUnitTest('test_pass')
        self.test = ITestCase(self.original)

    def test_visit(self):
        """
        Trial assumes that test cases implement visit().
        """
        log = []

        def visitor(test):
            log.append(test)

        self.test.visit(visitor)
        self.assertEqual(log, [self.test])

    test_visit.suppress = [
        util.suppress(category=DeprecationWarning,
                      message="Test visitors deprecated in Twisted 8.0")
    ]

    def test_callable(self):
        """
        Tests must be callable in order to be used with Python's unittest.py.
        """
        self.assertTrue(callable(self.test),
                        "%r is not callable." % (self.test, ))
Пример #6
0
class TestAppSupport(unittest.TestCase):
    def testPassphrase(self):
        self.assertEqual(app.getPassphrase(0), None)

    def testLoadApplication(self):
        """
        Test loading an application file in different dump format.
        """
        a = service.Application("hello")
        baseconfig = {'file': None, 'source': None, 'python': None}
        for style in 'source pickle'.split():
            config = baseconfig.copy()
            config[{'pickle': 'file'}.get(style, style)] = 'helloapplication'
            sob.IPersistable(a).setStyle(style)
            sob.IPersistable(a).save(filename='helloapplication')
            a1 = app.getApplication(config, None)
            self.assertEqual(service.IService(a1).name, "hello")
        config = baseconfig.copy()
        config['python'] = 'helloapplication'
        f = open("helloapplication", 'w')
        f.writelines([
            "from twisted.application import service\n",
            "application = service.Application('hello')\n",
        ])
        f.close()
        a1 = app.getApplication(config, None)
        self.assertEqual(service.IService(a1).name, "hello")

    def test_convertStyle(self):
        appl = service.Application("lala")
        for instyle in 'source pickle'.split():
            for outstyle in 'source pickle'.split():
                sob.IPersistable(appl).setStyle(instyle)
                sob.IPersistable(appl).save(filename="converttest")
                app.convertStyle("converttest", instyle, None,
                                 "converttest.out", outstyle, 0)
                appl2 = service.loadApplication("converttest.out", outstyle)
                self.assertEqual(service.IService(appl2).name, "lala")

    def test_getLogFile(self):
        """
        Test L{app.getLogFile}, veryfying the LogFile instance it returns.
        """
        os.mkdir("logfiledir")
        l = app.getLogFile(os.path.join("logfiledir", "lala"))
        self.assertEqual(l.path,
                         os.path.abspath(os.path.join("logfiledir", "lala")))
        self.assertEqual(l.name, "lala")
        self.assertEqual(l.directory, os.path.abspath("logfiledir"))

    test_getLogFile.suppress = [
        util.suppress(message="app.getLogFile is deprecated. Use "
                      "twisted.python.logfile.LogFile.fromFullPath instead",
                      category=DeprecationWarning)
    ]

    def test_startApplication(self):
        appl = service.Application("lala")
        app.startApplication(appl, 0)
        self.assert_(service.IService(appl).running)
Пример #7
0
class TestTreeReporter(unittest.TestCase):
    def setUp(self):
        from twisted.trial.test import sample
        self.test = sample.FooTest('test_foo')
        self.stream = StringIO.StringIO()
        self.result = reporter.TreeReporter(self.stream)
        self.result._colorizer = MockColorizer(self.stream)
        self.log = self.result._colorizer.log

    def makeError(self):
        try:
            1 / 0
        except ZeroDivisionError:
            f = failure.Failure()
        return f

    def test_cleanupError(self):
        """
        Run cleanupErrors and check that the output is correct, and colored
        correctly.
        """
        f = self.makeError()
        self.result.cleanupErrors(f)
        color, text = self.log[0]
        self.assertEqual(color.strip(), self.result.ERROR)
        self.assertEqual(text.strip(), 'cleanup errors')
        color, text = self.log[1]
        self.assertEqual(color.strip(), self.result.ERROR)
        self.assertEqual(text.strip(), '[ERROR]')

    test_cleanupError = suppressWarnings(
        test_cleanupError,
        util.suppress(category=reporter.BrokenTestCaseWarning))
Пример #8
0
 def _run(self, methodName, result):
     # Difference from unittest.TestCase: we use maybe_deferred_with_noncleaning_failure in order to avoid having
     #                                    t.i.defer mangle our locals and globals
     timeout = self.getTimeout()
     def onTimeout(d):
         e = defer.TimeoutError("%r (%s) still running at %s secs"
             % (self, methodName, timeout))
         f = failure.Failure(e)
         # try to errback the deferred that the test returns (for no gorram
         # reason) (see issue1005 and test_errorPropagation in
         # test_deferred)
         try:
             d.errback(f)
         except defer.AlreadyCalledError:
             # if the deferred has been called already but the *back chain
             # is still unfinished, crash the reactor and report timeout
             # error ourself.
             # reactor.crash() # TODO: decide what to do wrt timeouts -- Njal
             self._timedOut = True # see self._wait
             todo = self.getTodo()
             if todo is not None and todo.expected(f):
                 result.addExpectedFailure(self, f, todo)
             else:
                 result.addError(self, f)
     onTimeout = utils.suppressWarnings(
         onTimeout, util.suppress(category=DeprecationWarning))
     method = getattr(self, methodName)
     d = status_util.maybe_deferred_with_noncleaning_failure(utils.runWithWarningsSuppressed,
                             self.getSuppress(), method)
     call = reactor.callLater(timeout, onTimeout, d)
     d.addBoth(lambda x : call.active() and call.cancel() or x)
     return d
Пример #9
0
class SuppressionMixin(EmitMixin):
    suppress = [util.suppress(message=CLASS_WARNING_MSG)]

    def testSuppressMethod(self):
        self._emit()

    testSuppressMethod.suppress = [util.suppress(message=METHOD_WARNING_MSG)
                                   ]  # type: ignore[attr-defined] # noqa

    def testSuppressClass(self):
        self._emit()

    def testOverrideSuppressClass(self):
        self._emit()

    testOverrideSuppressClass.suppress = []  # type: ignore[attr-defined]
Пример #10
0
class DelayedCall(unittest.TestCase):
    hiddenExceptionMsg = "something blew up"

    def go(self):
        raise RuntimeError(self.hiddenExceptionMsg)

    def testHiddenException(self):
        """
        What happens if an error is raised in a DelayedCall and an error is
        also raised in the test?

        L{test_reporter.TestErrorReporting.testHiddenException} checks that
        both errors get reported.

        Note that this behaviour is deprecated. A B{real} test would return a
        Deferred that got triggered by the callLater. This would guarantee the
        delayed call error gets reported.
        """
        reactor.callLater(0, self.go)
        reactor.iterate(0.01)
        self.fail("Deliberate failure to mask the hidden exception")

    testHiddenException.suppress = [
        util.suppress(message=r'reactor\.iterate cannot be used.*',
                      category=DeprecationWarning)
    ]
Пример #11
0
class TestContext(unittest.TestCase):
    """Check that each of the standard loaders supports load with and without a
    context.
    """
    def test_stan(self):
        doc = t.p['hello']
        self._withAndWithout(loaders.stan(doc))

    def test_xmlstr(self):
        doc = '<p>hello</p>'
        self._withAndWithout(loaders.xmlstr(doc))

    def test_xmlfile(self):
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write('<p>hello</p>')
        f.close()
        self._withAndWithout(loaders.xmlfile(temp))

    def test_htmlstr(self):
        doc = '<p>hello</p>'
        self._withAndWithout(loaders.htmlstr(doc))

    test_htmlstr.suppress = [
        util.suppress(
            message=r"\[v0.8\] htmlstr is deprecated because it's buggy. "
            "Please start using xmlfile and/or xmlstr.")
    ]

    def test_htmlfile(self):
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write('<p>hello</p>')
        f.close()
        self._withAndWithout(loaders.htmlfile(temp))

    test_htmlfile.suppress = [
        util.suppress(
            message=r"\[v0.8\] htmlfile is deprecated because it's buggy. "
            "Please start using xmlfile and/or xmlstr.")
    ]

    def _withAndWithout(self, loader):
        ctx = context.WovenContext()
        self.assertEqual(loader.load(), ['<p>hello</p>'])
        self.assertEqual(loader.load(ctx), ['<p>hello</p>'])
Пример #12
0
class POP3TestCase(unittest.TestCase):

    message = '''\
Subject: urgent

Someone set up us the bomb!
'''

    expectedOutput = '''\
+OK <moshez>\015
+OK Authentication succeeded\015
+OK \015
1 0\015
.\015
+OK %d\015
Subject: urgent\015
\015
Someone set up us the bomb!\015
.\015
+OK \015
''' % len(message)

    def setUp(self):
        self.factory = internet.protocol.Factory()
        self.factory.domains = {}
        self.factory.domains['baz.com'] = DummyDomain()
        self.factory.domains['baz.com'].addUser('hello')
        self.factory.domains['baz.com'].addMessage('hello', self.message)

    def testMessages(self):
        client = LineSendingProtocol([
            'APOP [email protected] world',
            'UIDL',
            'RETR 1',
            'QUIT',
        ])
        server = MyVirtualPOP3()
        server.service = self.factory

        def check(ignored):
            output = '\r\n'.join(client.response) + '\r\n'
            self.assertEquals(output, self.expectedOutput)

        return loopback.loopbackTCP(server, client).addCallback(check)

    def testLoopback(self):
        protocol = MyVirtualPOP3()
        protocol.service = self.factory
        clientProtocol = MyPOP3Downloader()
        loopback.loopback(protocol, clientProtocol)
        self.failUnlessEqual(clientProtocol.message, self.message)
        protocol.connectionLost(
            failure.Failure(Exception("Test harness disconnect")))

    testLoopback.suppress = [
        util.suppress(message="twisted.mail.pop3.POP3Client is deprecated")
    ]
Пример #13
0
class ServiceTestCase(unittest.TestCase):
    def testRegisterService(self):
        a = app.Application("foo")
        svc = app.ApplicationService("service", a)
        self.assertEquals(a.getServiceNamed("service"), svc)
        self.assertEquals(a, svc.serviceParent)

    testRegisterService.suppress = [
        util.suppress(message='twisted.internet.app is deprecated',
                      category=DeprecationWarning)
    ]
Пример #14
0
class LoopbackTestCase(LoopbackMixin):
    def testMessages(self):
        factory = smtp.SMTPFactory()
        factory.domains = {}
        factory.domains['foo.bar'] = DummyDomain(['moshez'])
        from twisted.mail.protocols import DomainSMTP
        protocol =  DomainSMTP()
        protocol.service = factory
        protocol.factory = factory
        clientProtocol = self.clientClass()
        return self.loopback(protocol, clientProtocol)
    testMessages.suppress = [util.suppress(message='DomainSMTP', category=DeprecationWarning)]
Пример #15
0
class TestSuppression(unittest.TestCase, EmitMixin):
    def testSuppressMethod(self):
        self._emit()

    testSuppressMethod.suppress = [util.suppress(message=METHOD_WARNING_MSG)]

    def testSuppressClass(self):
        self._emit()

    def testOverrideSuppressClass(self):
        self._emit()

    testOverrideSuppressClass.suppress = []
Пример #16
0
class AppTestCase(unittest.TestCase):
    suppress = [
        util.suppress(message='twisted.internet.app is deprecated',
                      category=DeprecationWarning)
    ]

    def testListenUnlistenTCP(self):
        a = app.Application("foo")
        f = protocol.ServerFactory()
        a.listenTCP(9999, f)
        a.listenTCP(9998, f)
        self.assertEquals(len(a.tcpPorts), 2)
        a.unlistenTCP(9999)
        self.assertEquals(len(a.tcpPorts), 1)
        a.listenTCP(9999, f, interface='127.0.0.1')
        self.assertEquals(len(a.tcpPorts), 2)
        a.unlistenTCP(9999, '127.0.0.1')
        self.assertEquals(len(a.tcpPorts), 1)
        a.unlistenTCP(9998)
        self.assertEquals(len(a.tcpPorts), 0)

    def testListenUnlistenUDP(self):
        a = app.Application("foo")
        f = protocol.DatagramProtocol()
        a.listenUDP(9999, f)
        a.listenUDP(9998, f)
        self.assertEquals(len(a.udpPorts), 2)
        a.unlistenUDP(9999)
        self.assertEquals(len(a.udpPorts), 1)
        a.listenUDP(9999, f, interface='127.0.0.1')
        self.assertEquals(len(a.udpPorts), 2)
        a.unlistenUDP(9999, '127.0.0.1')
        self.assertEquals(len(a.udpPorts), 1)
        a.unlistenUDP(9998)
        self.assertEquals(len(a.udpPorts), 0)

    def testListenUnlistenUNIX(self):
        a = app.Application("foo")
        f = protocol.ServerFactory()
        a.listenUNIX("xxx", f)
        self.assertEquals(len(a.unixPorts), 1)
        a.unlistenUNIX("xxx")
        self.assertEquals(len(a.unixPorts), 0)

    def testIllegalUnlistens(self):
        a = app.Application("foo")

        self.assertRaises(error.NotListeningError, a.unlistenTCP, 1010)
        self.assertRaises(error.NotListeningError, a.unlistenUNIX, '1010')
        self.assertRaises(error.NotListeningError, a.unlistenSSL, 1010)
        self.assertRaises(error.NotListeningError, a.unlistenUDP, 1010)
Пример #17
0
class DryRunMixin(object):

    suppress = [util.suppress(
        category=DeprecationWarning,
        message="Test visitors deprecated in Twisted 8.0")]


    def setUp(self):
        self.log = []
        self.stream = StringIO.StringIO()
        self.runner = runner.TrialRunner(CapturingReporter,
                                         runner.TrialRunner.DRY_RUN,
                                         stream=self.stream)
        self.makeTestFixtures()


    def makeTestFixtures(self):
        """
        Set C{self.test} and C{self.suite}, where C{self.suite} is an empty
        TestSuite.
        """


    def test_empty(self):
        """
        If there are no tests, the reporter should not receive any events to
        report.
        """
        result = self.runner.run(runner.TestSuite())
        self.assertEqual(result._calls, [])


    def test_singleCaseReporting(self):
        """
        If we are running a single test, check the reporter starts, passes and
        then stops the test during a dry run.
        """
        result = self.runner.run(self.test)
        self.assertEqual(result._calls, ['startTest', 'addSuccess', 'stopTest'])


    def test_testsNotRun(self):
        """
        When we are doing a dry run, the tests should not actually be run.
        """
        self.runner.run(self.test)
        self.assertEqual(self.log, [])
Пример #18
0
class SMTPTestCase(unittest.TestCase):

    messages = [('*****@*****.**', ['*****@*****.**', '*****@*****.**'], '''\
Subject: urgent\015
\015
Someone set up us the bomb!\015
''')]

    mbox = {'foo': ['Subject: urgent\n\nSomeone set up us the bomb!\n']}

    def setUp(self):
        """
        Create an in-memory mail domain to which messages may be delivered by
        tests and create a factory and transport to do the delivering.
        """
        self.factory = smtp.SMTPFactory()
        self.factory.domains = {}
        self.factory.domains['baz.com'] = DummyDomain(['foo'])
        self.transport = StringTransport()

    def testMessages(self):
        from twisted.mail import protocols
        protocol = protocols.DomainSMTP()
        protocol.service = self.factory
        protocol.factory = self.factory
        protocol.receivedHeader = spameater
        protocol.makeConnection(self.transport)
        protocol.lineReceived('HELO yyy.com')
        for message in self.messages:
            protocol.lineReceived('MAIL FROM:<%s>' % message[0])
            for target in message[1]:
                protocol.lineReceived('RCPT TO:<%s>' % target)
            protocol.lineReceived('DATA')
            protocol.dataReceived(message[2])
            protocol.lineReceived('.')
        protocol.lineReceived('QUIT')
        if self.mbox != self.factory.domains['baz.com'].messages:
            raise AssertionError(self.factory.domains['baz.com'].messages)
        protocol.setTimeout(None)

    testMessages.suppress = [
        util.suppress(message='DomainSMTP', category=DeprecationWarning)
    ]
Пример #19
0
class UNIXDatagramTestsBuilder(UNIXFamilyMixin, ReactorBuilder):
    """
    Builder defining tests relating to L{IReactorUNIXDatagram}.
    """

    # There's no corresponding test_connectMode because the mode parameter to
    # connectUNIXDatagram has been completely ignored since that API was first
    # introduced.
    def test_listenMode(self):
        """
        The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
        is created with the mode specified.
        """
        self._modeTest('listenUNIXDatagram', self.mktemp(), DatagramProtocol())

    test_listenMode.suppress = [
        util.suppress(category=DeprecationWarning,
                      message=_deprecatedModeMessage %
                      dict(interface='IReactorUNIXDatagram',
                           method='listenUNIXDatagram'))
    ]

    def test_deprecatedListenMode(self):
        """
        Passing any value for the C{mode} parameter of L{listenUNIXDatagram}
        causes a deprecation warning to be emitted.
        """
        self._deprecatedModeTest('IReactorUNIXDatagram', 'listenUNIXDatagram',
                                 self.mktemp(), DatagramProtocol())

    def test_deprecatedConnectMode(self):
        """
        Passing any value for the C{mode} parameter of L{connectUNIXDatagram}
        causes a deprecation warning to be emitted.
        """
        path = self.mktemp()
        server = socket(AF_UNIX, SOCK_DGRAM)
        server.bind(path)
        self.addCleanup(server.close)

        self._deprecatedModeTest('IReactorUNIXDatagram', 'connectUNIXDatagram',
                                 path, ConnectedDatagramProtocol())
Пример #20
0
class UNIXTestsBuilder(UNIXFamilyMixin, ReactorBuilder):
    """
    Builder defining tests relating to L{IReactorUNIX}.
    """
    def test_mode(self):
        """
        The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with
        the mode specified.
        """
        self._modeTest('listenUNIX', self.mktemp(), ServerFactory())

    test_mode.suppress = [
        util.suppress(category=DeprecationWarning,
                      message=_deprecatedModeMessage %
                      dict(interface='IReactorUNIX', method='listenUNIX'))
    ]

    def test_deprecatedMode(self):
        """
        Passing any value for the C{mode} parameter of L{listenUNIX} causes a
        deprecation warning to be emitted.
        """
        self._deprecatedModeTest('IReactorUNIX', 'listenUNIX', self.mktemp(),
                                 ServerFactory())
Пример #21
0
movedModules = [('twisted.protocols.smtp', 'twisted.mail.smtp'),
                ('twisted.protocols.imap4', 'twisted.mail.imap4'),
                ('twisted.protocols.pop3', 'twisted.mail.pop3'),
                ('twisted.protocols.dns', 'twisted.names.dns'),
                ('twisted.protocols.ethernet', 'twisted.pair.ethernet'),
                ('twisted.protocols.raw', 'twisted.pair.raw'),
                ('twisted.protocols.rawudp', 'twisted.pair.rawudp'),
                ('twisted.protocols.ip', 'twisted.pair.ip'),
                ('twisted.protocols.irc', 'twisted.words.protocols.irc'),
                ('twisted.protocols.msn', 'twisted.words.protocols.msn'),
                ('twisted.protocols.toc', 'twisted.words.protocols.toc'),
                ('twisted.protocols.oscar', 'twisted.words.protocols.oscar'),
                ]


class TestCompatibility(unittest.TestCase):
    def testCompatibility(self):
        for oldName, newName in movedModules:
            try:
                old = reflect.namedModule(oldName)
                new = reflect.namedModule(newName)
            except ImportError, e:
                continue
            for someName in vars(new):
                if someName == '__doc__':
                    continue
                self.assertIdentical(getattr(old, someName),
                                     getattr(new, someName))
    testCompatibility.suppress = [util.suppress(category=DeprecationWarning)]
Пример #22
0
"""

import gc

from twisted.trial import unittest, util
from twisted.internet import reactor, defer
from twisted.python import failure, log

from twisted.internet.task import Clock

class GenericError(Exception):
    pass


_setTimeoutSuppression = util.suppress(
    message="Deferred.setTimeout is deprecated.  Look for timeout "
            "support specific to the API you are using instead.",
    category=DeprecationWarning)

_firstErrorSuppression = util.suppress(
    message="FirstError.__getitem__ is deprecated.  Use attributes instead.",
    category=DeprecationWarning)


class DeferredTestCase(unittest.TestCase):

    def setUp(self):
        self.callback_results = None
        self.errback_results = None
        self.callback2_results = None

    def _callback(self, *args, **kw):
Пример #23
0


class TearDownSuppressionMixin(object):
    def tearDown(self):
        self._emit()



class TestSuppression2Mixin(EmitMixin):
    def testSuppressModule(self):
        self._emit()



suppress = [util.suppress(message=MODULE_WARNING_MSG)]


class SynchronousTestSuppression(SuppressionMixin, unittest.SynchronousTestCase):
    pass



class SynchronousTestSetUpSuppression(SetUpSuppressionMixin, SynchronousTestSuppression):
    pass



class SynchronousTestTearDownSuppression(TearDownSuppressionMixin, SynchronousTestSuppression):
    pass
Пример #24
0
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.


"""Test cases for Twisted component architecture."""

from zope import interface as zinterface

from twisted.trial import unittest, util
from twisted.python import components
import warnings

compWarn = {'category':components.ComponentsDeprecationWarning}
suppress = [util.suppress(**compWarn)]

# Also filter warnings generated by top-level code
warnings.filterwarnings('ignore', **compWarn)

class IAdder(components.Interface):
    """A sample interface that adds stuff."""

    def add(self, a, b):
        """Returns the sub of a and b."""
        raise NotImplementedError

class ISub(IAdder):
    """Sub-interface."""

class IMultiply(components.Interface):
    """Interface that multiplies stuff."""
Пример #25
0
from xmantissa.publicweb import LoginPage
from xmantissa.offering import installOffering
from xmantissa.plugins.baseoff import baseOffering
from xmantissa.cachejs import theHashModuleProvider
from xmantissa.website import WebSite
from xmantissa.webapp import PrivateApplication
from xmantissa.websharing import SharingIndex

from xmantissa.website import MantissaLivePage, APIKey, PrefixURLMixin
from xmantissa.web import SecuringWrapper, _SecureWrapper, StaticContent, UnguardedWrapper, SiteConfiguration


maybeEncryptedRootWarningMessage = (
    "Use ISiteURLGenerator.rootURL instead of WebSite.maybeEncryptedRoot")
maybeEncryptedRootSuppression = util.suppress(
    message=maybeEncryptedRootWarningMessage,
    category=DeprecationWarning)



class SiteConfigurationTests(TestCase):
    """
    L{xmantissa.web.Site} defines how to create an HTTP server.
    """
    def setUp(self):
        self.domain = u"example.com"
        self.store = Store()
        self.site = SiteConfiguration(store=self.store, hostname=self.domain)


    def test_interfaces(self):
Пример #26
0
class ZipstreamTest(unittest.TestCase):
    """
    Tests for twisted.python.zipstream
    """
    def setUp(self):
        """
        Creates junk data that can be compressed and a test directory for any
        files that will be created
        """
        self.testdir = filepath.FilePath(self.mktemp())
        self.testdir.makedirs()
        self.unzipdir = self.testdir.child('unzipped')
        self.unzipdir.makedirs()

    def makeZipFile(self, contents, directory=''):
        """
        Makes a zip file archive containing len(contents) files.  Contents
        should be a list of strings, each string being the content of one file.
        """
        zpfilename = self.testdir.child('zipfile.zip').path
        zpfile = zipfile.ZipFile(zpfilename, 'w')
        for i, content in enumerate(contents):
            filename = str(i)
            if directory:
                filename = directory + "/" + filename
            zpfile.writestr(filename, content)
        zpfile.close()
        return zpfilename

    def test_countEntries(self):
        """
        Make sure the deprecated L{countZipFileEntries} returns the correct
        number of entries for a zip file.
        """
        name = self.makeZipFile(["one", "two", "three", "four", "five"])
        result = self.assertWarns(DeprecationWarning,
                                  "countZipFileEntries is deprecated.",
                                  __file__,
                                  lambda: zipstream.countZipFileEntries(name))
        self.assertEqual(result, 5)

    def test_invalidMode(self):
        """
        A ChunkingZipFile opened in write-mode should not allow .readfile(),
        and raise a RuntimeError instead.
        """
        czf = zipstream.ChunkingZipFile(self.mktemp(), "w")
        self.assertRaises(RuntimeError, czf.readfile, "something")

    def test_closedArchive(self):
        """
        A closed ChunkingZipFile should raise a L{RuntimeError} when
        .readfile() is invoked.
        """
        czf = zipstream.ChunkingZipFile(self.makeZipFile(["something"]), "r")
        czf.close()
        self.assertRaises(RuntimeError, czf.readfile, "something")

    def test_invalidHeader(self):
        """
        A zipfile entry with the wrong magic number should raise BadZipfile for
        readfile(), but that should not affect other files in the archive.
        """
        fn = self.makeZipFile(["test contents", "more contents"])
        zf = zipfile.ZipFile(fn, "r")
        zeroOffset = zf.getinfo("0").header_offset
        zf.close()
        # Zero out just the one header.
        scribble = file(fn, "r+b")
        scribble.seek(zeroOffset, 0)
        scribble.write(chr(0) * 4)
        scribble.close()
        czf = zipstream.ChunkingZipFile(fn)
        self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
        self.assertEqual(czf.readfile("1").read(), "more contents")

    def test_filenameMismatch(self):
        """
        A zipfile entry with a different filename than is found in the central
        directory should raise BadZipfile.
        """
        fn = self.makeZipFile(["test contents", "more contents"])
        zf = zipfile.ZipFile(fn, "r")
        info = zf.getinfo("0")
        info.filename = "not zero"
        zf.close()
        scribble = file(fn, "r+b")
        scribble.seek(info.header_offset, 0)
        scribble.write(info.FileHeader())
        scribble.close()

        czf = zipstream.ChunkingZipFile(fn)
        self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
        self.assertEqual(czf.readfile("1").read(), "more contents")

    if sys.version_info < (2, 5):
        # In python 2.4 and earlier, consistency between the directory and the
        # file header are verified at archive-opening time.  In python 2.5
        # (and, presumably, later) it is readzipfile's responsibility.
        message = "Consistency-checking only necessary in 2.5."
        test_invalidHeader.skip = message
        test_filenameMismatch.skip = message

    def test_unsupportedCompression(self):
        """
        A zipfile which describes an unsupported compression mechanism should
        raise BadZipfile.
        """
        fn = self.mktemp()
        zf = zipfile.ZipFile(fn, "w")
        zi = zipfile.ZipInfo("0")
        zf.writestr(zi, "some data")
        # Mangle its compression type in the central directory; can't do this
        # before the writestr call or zipfile will (correctly) tell us not to
        # pass bad compression types :)
        zi.compress_type = 1234
        zf.close()

        czf = zipstream.ChunkingZipFile(fn)
        self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")

    def test_extraData(self):
        """
        readfile() should skip over 'extra' data present in the zip metadata.
        """
        fn = self.mktemp()
        zf = zipfile.ZipFile(fn, 'w')
        zi = zipfile.ZipInfo("0")
        zi.extra = "hello, extra"
        zf.writestr(zi, "the real data")
        zf.close()
        czf = zipstream.ChunkingZipFile(fn)
        self.assertEqual(czf.readfile("0").read(), "the real data")

    def test_unzipIter(self):
        """
        L{twisted.python.zipstream.unzipIter} should unzip a file for each
        iteration and yield the number of files left to unzip after that
        iteration
        """
        numfiles = 10
        contents = ['This is test file %d!' % i for i in range(numfiles)]
        zpfilename = self.makeZipFile(contents)
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path)
        for i in range(numfiles):
            self.assertEqual(len(list(self.unzipdir.children())), i)
            self.assertEqual(uziter.next(), numfiles - i - 1)
        self.assertEqual(len(list(self.unzipdir.children())), numfiles)

        for child in self.unzipdir.children():
            num = int(child.basename())
            self.assertEqual(child.open().read(), contents[num])

    test_unzipIter.suppress = [
        util.suppress(message="zipstream.unzipIter is deprecated")
    ]

    def test_unzipIterDeprecated(self):
        """
        Use of C{twisted.python.zipstream.unzipIter} will emit a
        deprecated warning.
        """
        zpfilename = self.makeZipFile('foo')

        self.assertEqual(len(self.flushWarnings()), 0)

        for f in zipstream.unzipIter(zpfilename, self.unzipdir.path):
            pass

        warnings = self.flushWarnings()
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "zipstream.unzipIter is deprecated since Twisted 11.0.0 for "
            "security reasons.  Use Python's zipfile instead.")

    def test_unzipIterChunky(self):
        """
        L{twisted.python.zipstream.unzipIterChunky} returns an iterator which
        must be exhausted to completely unzip the input archive.
        """
        numfiles = 10
        contents = ['This is test file %d!' % i for i in range(numfiles)]
        zpfilename = self.makeZipFile(contents)
        list(zipstream.unzipIterChunky(zpfilename, self.unzipdir.path))
        self.assertEqual(set(self.unzipdir.listdir()),
                         set(map(str, range(numfiles))))

        for child in self.unzipdir.children():
            num = int(child.basename())
            self.assertEqual(child.getContent(), contents[num])

    def test_unzipIterChunkyDirectory(self):
        """
        The path to which a file is extracted by L{zipstream.unzipIterChunky}
        is determined by joining the C{directory} argument to C{unzip} with the
        path within the archive of the file being extracted.
        """
        numfiles = 10
        contents = ['This is test file %d!' % i for i in range(numfiles)]
        zpfilename = self.makeZipFile(contents, 'foo')
        list(zipstream.unzipIterChunky(zpfilename, self.unzipdir.path))
        self.assertEqual(set(self.unzipdir.child('foo').listdir()),
                         set(map(str, range(numfiles))))

        for child in self.unzipdir.child('foo').children():
            num = int(child.basename())
            self.assertEqual(child.getContent(), contents[num])

    def test_unzip(self):
        """
        L{twisted.python.zipstream.unzip} should extract all files from a zip
        archive
        """
        numfiles = 3
        zpfilename = self.makeZipFile([str(i) for i in range(numfiles)])
        zipstream.unzip(zpfilename, self.unzipdir.path)
        self.assertEqual(set(self.unzipdir.listdir()),
                         set(map(str, range(numfiles))))
        for i in range(numfiles):
            self.assertEqual(self.unzipdir.child(str(i)).getContent(), str(i))

    test_unzip.suppress = [
        util.suppress(message="zipstream.unzip is deprecated")
    ]

    def test_unzipDeprecated(self):
        """
        Use of C{twisted.python.zipstream.unzip} will emit a deprecated warning.
        """
        zpfilename = self.makeZipFile('foo')

        self.assertEqual(len(self.flushWarnings()), 0)

        zipstream.unzip(zpfilename, self.unzipdir.path)

        warnings = self.flushWarnings()
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "zipstream.unzip is deprecated since Twisted 11.0.0 for "
            "security reasons.  Use Python's zipfile instead.")

    def test_unzipDirectory(self):
        """
        The path to which a file is extracted by L{zipstream.unzip} is
        determined by joining the C{directory} argument to C{unzip} with the
        path within the archive of the file being extracted.
        """
        numfiles = 3
        zpfilename = self.makeZipFile([str(i) for i in range(numfiles)], 'foo')
        zipstream.unzip(zpfilename, self.unzipdir.path)
        self.assertEqual(set(self.unzipdir.child('foo').listdir()),
                         set(map(str, range(numfiles))))
        for i in range(numfiles):
            self.assertEqual(
                self.unzipdir.child('foo').child(str(i)).getContent(), str(i))

    test_unzipDirectory.suppress = [
        util.suppress(message="zipstream.unzip is deprecated")
    ]

    def test_overwrite(self):
        """
        L{twisted.python.zipstream.unzip} and
        L{twisted.python.zipstream.unzipIter} shouldn't overwrite files unless
        the 'overwrite' flag is passed
        """
        testfile = self.unzipdir.child('0')
        zpfilename = self.makeZipFile(['OVERWRITTEN'])

        testfile.setContent('NOT OVERWRITTEN')
        zipstream.unzip(zpfilename, self.unzipdir.path)
        self.assertEqual(testfile.open().read(), 'NOT OVERWRITTEN')
        zipstream.unzip(zpfilename, self.unzipdir.path, overwrite=True)
        self.assertEqual(testfile.open().read(), 'OVERWRITTEN')

        testfile.setContent('NOT OVERWRITTEN')
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path)
        uziter.next()
        self.assertEqual(testfile.open().read(), 'NOT OVERWRITTEN')
        uziter = zipstream.unzipIter(zpfilename,
                                     self.unzipdir.path,
                                     overwrite=True)
        uziter.next()
        self.assertEqual(testfile.open().read(), 'OVERWRITTEN')

    test_overwrite.suppress = [
        util.suppress(message="zipstream.unzip is deprecated"),
        util.suppress(message="zipstream.unzipIter is deprecated")
    ]

    # XXX these tests are kind of gross and old, but I think unzipIterChunky is
    # kind of a gross function anyway.  We should really write an abstract
    # copyTo/moveTo that operates on FilePath and make sure ZipPath can support
    # it, then just deprecate / remove this stuff.
    def _unzipIterChunkyTest(self, compression, chunksize, lower, upper):
        """
        unzipIterChunky should unzip the given number of bytes per iteration.
        """
        junk = ' '.join([str(random.random()) for n in xrange(1000)])
        junkmd5 = md5(junk).hexdigest()

        tempdir = filepath.FilePath(self.mktemp())
        tempdir.makedirs()
        zfpath = tempdir.child('bigfile.zip').path
        self._makebigfile(zfpath, compression, junk)
        uziter = zipstream.unzipIterChunky(zfpath,
                                           tempdir.path,
                                           chunksize=chunksize)
        r = uziter.next()
        # test that the number of chunks is in the right ballpark;
        # this could theoretically be any number but statistically it
        # should always be in this range
        approx = lower < r < upper
        self.failUnless(approx)
        for r in uziter:
            pass
        self.assertEqual(r, 0)
        newmd5 = md5(tempdir.child("zipstreamjunk").open().read()).hexdigest()
        self.assertEqual(newmd5, junkmd5)

    def test_unzipIterChunkyStored(self):
        """
        unzipIterChunky should unzip the given number of bytes per iteration on
        a stored archive.
        """
        self._unzipIterChunkyTest(zipfile.ZIP_STORED, 500, 35, 45)

    def test_chunkyDeflated(self):
        """
        unzipIterChunky should unzip the given number of bytes per iteration on
        a deflated archive.
        """
        self._unzipIterChunkyTest(zipfile.ZIP_DEFLATED, 972, 23, 27)

    def _makebigfile(self, filename, compression, junk):
        """
        Create a zip file with the given file name and compression scheme.
        """
        zf = zipfile.ZipFile(filename, 'w', compression)
        for i in range(10):
            fn = 'zipstream%d' % i
            zf.writestr(fn, "")
        zf.writestr('zipstreamjunk', junk)
        zf.close()
Пример #27
0
class LiveTest(unittest.TestCase):
    def setUp(self):
        self.proxy = sip.RegisterProxy(host="127.0.0.1")
        self.registry = sip.InMemoryRegistry("bell.example.com")
        self.proxy.registry = self.proxy.locator = self.registry
        self.serverPort = reactor.listenUDP(0,
                                            self.proxy,
                                            interface="127.0.0.1")
        self.client = Client()
        self.clientPort = reactor.listenUDP(0,
                                            self.client,
                                            interface="127.0.0.1")
        self.serverAddress = (self.serverPort.getHost().host,
                              self.serverPort.getHost().port)

    setUp = utils.suppressWarnings(
        setUp,
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestAuthorizer was deprecated'))

    def tearDown(self):
        for d, uri in self.registry.users.values():
            d.cancel()
        d1 = defer.maybeDeferred(self.clientPort.stopListening)
        d2 = defer.maybeDeferred(self.serverPort.stopListening)
        return defer.gatherResults([d1, d2])

    def testRegister(self):
        p = self.clientPort.getHost().port
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:%d" % p)
        r.addHeader("via", sip.Via("127.0.0.1", port=p).toString())
        self.client.sendMessage(
            sip.URL(host="127.0.0.1", port=self.serverAddress[1]), r)
        d = self.client.deferred

        def check(received):
            self.assertEqual(len(received), 1)
            r = received[0]
            self.assertEqual(r.code, 200)

        d.addCallback(check)
        return d

    def test_amoralRPort(self):
        """
        rport is allowed without a value, apparently because server
        implementors might be too stupid to check the received port
        against 5060 and see if they're equal, and because client
        implementors might be too stupid to bind to port 5060, or set a
        value on the rport parameter they send if they bind to another
        port.
        """
        p = self.clientPort.getHost().port
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:%d" % p)
        r.addHeader("via", sip.Via("127.0.0.1", port=p, rport=True).toString())
        warnings = self.flushWarnings(
            offendingFunctions=[self.test_amoralRPort])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['message'],
                         'rport=True is deprecated since Twisted 9.0.')
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.client.sendMessage(
            sip.URL(host="127.0.0.1", port=self.serverAddress[1]), r)
        d = self.client.deferred

        def check(received):
            self.assertEqual(len(received), 1)
            r = received[0]
            self.assertEqual(r.code, 200)

        d.addCallback(check)
        return d
Пример #28
0
class POP3Tests(unittest.TestCase):
    """
    Tests for L{pop3.POP3}.
    """

    message = b"""\
Subject: urgent

Someone set up us the bomb!
"""

    expectedOutput = b"""\
+OK <moshez>\015
+OK Authentication succeeded\015
+OK \015
1 0\015
.\015
+OK %d\015
Subject: urgent\015
\015
Someone set up us the bomb!\015
.\015
+OK \015
""" % (len(message), )

    def setUp(self):
        """
        Set up a POP3 server with virtual domain support.
        """
        self.factory = internet.protocol.Factory()
        self.factory.domains = {}
        self.factory.domains[b"baz.com"] = DummyDomain()
        self.factory.domains[b"baz.com"].addUser(b"hello")
        self.factory.domains[b"baz.com"].addMessage(b"hello", self.message)

    def test_messages(self):
        """
        Messages can be downloaded over a loopback TCP connection.
        """
        client = LineSendingProtocol([
            b"APOP [email protected] world",
            b"UIDL",
            b"RETR 1",
            b"QUIT",
        ])
        server = MyVirtualPOP3()
        server.service = self.factory

        def check(ignored):
            output = b"\r\n".join(client.response) + b"\r\n"
            self.assertEqual(output, self.expectedOutput)

        return loopback.loopbackTCP(server, client).addCallback(check)

    def test_loopback(self):
        """
        Messages can be downloaded over a loopback connection.
        """
        protocol = MyVirtualPOP3()
        protocol.service = self.factory
        clientProtocol = MyPOP3Downloader()

        def check(ignored):
            self.assertEqual(clientProtocol.message, self.message)
            protocol.connectionLost(
                failure.Failure(Exception("Test harness disconnect")))

        d = loopback.loopbackAsync(protocol, clientProtocol)
        return d.addCallback(check)

    test_loopback.suppress = [  # type: ignore[attr-defined]
        util.suppress(message="twisted.mail.pop3.POP3Client is deprecated")
    ]

    def test_incorrectDomain(self):
        """
        Look up a user in a domain which this server does not support.
        """
        factory = internet.protocol.Factory()
        factory.domains = {}
        factory.domains[b"twistedmatrix.com"] = DummyDomain()

        server = MyVirtualPOP3()
        server.service = factory
        exc = self.assertRaises(pop3.POP3Error, server.authenticateUserAPOP,
                                b"*****@*****.**", b"password")
        self.assertEqual(exc.args[0], "no such domain baz.com")
Пример #29
0


class StubDNSDatagramProtocol:
    """
    A do-nothing stand-in for L{DNSDatagramProtocol} which can be used to avoid
    network traffic in tests where that kind of thing doesn't matter.
    """
    def query(self, *a, **kw):
        return Deferred()



_retrySuppression = util.suppress(
    category=DeprecationWarning,
    message=(
        'twisted.names.root.retry is deprecated since Twisted 10.0.  Use a '
        'Resolver object for retry logic.'))


class DiscoveryToolsTests(TestCase):
    """
    Tests for the free functions in L{twisted.names.root} which help out with
    authority discovery.  Since these are mostly deprecated, these are mostly
    deprecation tests.
    """
    def test_lookupNameserversDeprecated(self):
        """
        Calling L{root.lookupNameservers} produces a deprecation warning.
        """
        # Don't care about the return value, since it will never have a result,
from twisted.internet import interfaces, defer
from twisted.protocols import wire, basic
from twisted.internet import protocol, reactor
from twisted.internet.utils import getProcessOutputAndValue
from twisted.application import reactors

try:
    from twisted.web import microdom
    gotMicrodom = True
except ImportError:
    warnings.warn("Not testing xml persistence as twisted.web.microdom "
                  "not available")
    gotMicrodom = False


oldAppSuppressions = [util.suppress(message='twisted.internet.app is deprecated',
                                    category=DeprecationWarning)]

class Dummy:
    processName=None

class TestService(unittest.TestCase):

    def testName(self):
        s = service.Service()
        s.setName("hello")
        self.failUnlessEqual(s.name, "hello")

    def testParent(self):
        s = service.Service()
        p = service.MultiService()
        s.setServiceParent(p)
Пример #31
0
class HashObjectTests(TestCase):
    """
    Tests for the hash object APIs presented by L{hashlib}, C{md5} and C{sha1}.
    """
    def test_deprecation(self):
        """
        Ensure the deprecation of L{twisted.python.hashlib} is working.
        """
        __import__('twisted.python.hashlib')
        warnings = self.flushWarnings(
                offendingFunctions=[self.test_deprecation])
        self.assertIdentical(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['message'],
                "twisted.python.hashlib was deprecated in "
                "Twisted 13.1.0: Please use hashlib from stdlib.")


    def test_md5(self):
        """
        L{hashlib.md5} returns an object which can be used to compute an MD5
        hash as defined by U{RFC 1321<http://www.ietf.org/rfc/rfc1321.txt>}.
        """
        from twisted.python.hashlib import md5

        # Test the result using values from section A.5 of the RFC.
        self.assertEqual(
            md5().hexdigest(), "d41d8cd98f00b204e9800998ecf8427e")
        self.assertEqual(
            md5("a").hexdigest(), "0cc175b9c0f1b6a831c399e269772661")
        self.assertEqual(
            md5("abc").hexdigest(), "900150983cd24fb0d6963f7d28e17f72")
        self.assertEqual(
            md5("message digest").hexdigest(),
            "f96b697d7cb7938d525a2f31aaf161d0")
        self.assertEqual(
            md5("abcdefghijklmnopqrstuvwxyz").hexdigest(),
            "c3fcd3d76192e4007dfb496cca67e13b")
        self.assertEqual(
            md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                "0123456789").hexdigest(),
            "d174ab98d277d9f5a5611c2c9f419d9f")
        self.assertEqual(
            md5("1234567890123456789012345678901234567890123456789012345678901"
                "2345678901234567890").hexdigest(),
            "57edf4a22be3c955ac49da2e2107b67a")

        # It should have digest and update methods, too.
        self.assertEqual(
            md5().digest().encode('hex'),
            "d41d8cd98f00b204e9800998ecf8427e")
        hash = md5()
        hash.update("a")
        self.assertEqual(
            hash.digest().encode('hex'),
            "0cc175b9c0f1b6a831c399e269772661")

        # Instances of it should have a digest_size attribute
        self.assertEqual(md5().digest_size, 16)
    test_md5.suppress = [util.suppress(message="twisted.python.hashlib"
          "was deprecated in Twisted 13.1.0: Please use hashlib from stdlib.")]


    def test_sha1(self):
        """
        L{hashlib.sha1} returns an object which can be used to compute a SHA1
        hash as defined by U{RFC 3174<http://tools.ietf.org/rfc/rfc3174.txt>}.
        """

        from twisted.python.hashlib import sha1

        def format(s):
            return ''.join(s.split()).lower()
        # Test the result using values from section 7.3 of the RFC.
        self.assertEqual(
            sha1("abc").hexdigest(),
            format(
                "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D"))
        self.assertEqual(
            sha1("abcdbcdecdefdefgefghfghighijhi"
                 "jkijkljklmklmnlmnomnopnopq").hexdigest(),
            format(
                "84 98 3E 44 1C 3B D2 6E BA AE 4A A1 F9 51 29 E5 E5 46 70 F1"))

        # It should have digest and update methods, too.
        self.assertEqual(
            sha1("abc").digest().encode('hex'),
            format(
                "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D"))
        hash = sha1()
        hash.update("abc")
        self.assertEqual(
            hash.digest().encode('hex'),
            format(
                "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D"))

        # Instances of it should have a digest_size attribute.
        self.assertEqual(
            sha1().digest_size, 20)
    test_sha1.suppress = [util.suppress(message="twisted.python.hashlib"
          "was deprecated in Twisted 13.1.0: Please use hashlib from stdlib.")]
        L{root.bootstrap} accepts a C{resolverFactory} argument which is passed
        as an argument to L{root.Resolver} when it has successfully looked up
        root hints.
        """
        stubResolver = StubResolver()
        deferredResolver = root.bootstrap(
            stubResolver, resolverFactory=raisingResolverFactory)

        for d in stubResolver.pendingResults:
            d.callback("192.0.2.101")

        self.assertIs(deferredResolver._resolverFactory,
                      raisingResolverFactory)


class StubDNSDatagramProtocol:
    """
    A do-nothing stand-in for L{DNSDatagramProtocol} which can be used to avoid
    network traffic in tests where that kind of thing doesn't matter.
    """
    def query(self, *a, **kw):
        return Deferred()


_retrySuppression = util.suppress(
    category=DeprecationWarning,
    message=(
        "twisted.names.root.retry is deprecated since Twisted 10.0.  Use a "
        "Resolver object for retry logic."),
)
Пример #33
0
"""

import gc

from twisted.trial import unittest, util
from twisted.internet import reactor, defer
from twisted.python import failure, log

from twisted.internet.task import Clock

class GenericError(Exception):
    pass


_setTimeoutSuppression = util.suppress(
    message="Deferred.setTimeout is deprecated.  Look for timeout "
            "support specific to the API you are using instead.",
    category=DeprecationWarning)


class DeferredTestCase(unittest.TestCase):

    def setUp(self):
        self.callbackResults = None
        self.errbackResults = None
        self.callback2Results = None

    def _callback(self, *args, **kw):
        self.callbackResults = args, kw
        return args[0]

    def _callback2(self, *args, **kw):
Пример #34
0
            UnintelligentProtocol.pretext
        )
        self.failUnless(self.serverFactory.rawdata, "No encrypted bytes received")


    def testBackwardsTLS(self):
        self._runTest(LineCollector(1, self.fillBuffer), UnintelligentProtocol(), True)
        self.assertEquals(
            self.clientFactory.lines,
            UnintelligentProtocol.pretext + UnintelligentProtocol.posttext
        )



_bufferedSuppression = trial_util.suppress(
    message="startTLS with unwritten buffered data currently doesn't work "
            "right. See issue #686. Closing connection.",
    category=RuntimeWarning)


class SpammyTLSTestCase(TLSTestCase):
    """
    Test TLS features with bytes sitting in the out buffer.
    """
    fillBuffer = 1

    def testTLS(self):
        return TLSTestCase.testTLS(self)
    testTLS.suppress = [_bufferedSuppression]
    testTLS.todo = "startTLS doesn't empty buffer before starting TLS. :("

    def _emit(self):
        warnings.warn(METHOD_WARNING_MSG, MethodWarning)
        warnings.warn(CLASS_WARNING_MSG, ClassWarning)
        warnings.warn(MODULE_WARNING_MSG, ModuleWarning)


class TestSuppression(unittest.TestCase, EmitMixin):
    def testSuppressMethod(self):
        self._emit()

    testSuppressMethod.suppress = [util.suppress(message=METHOD_WARNING_MSG)]

    def testSuppressClass(self):
        self._emit()

    def testOverrideSuppressClass(self):
        self._emit()

    testOverrideSuppressClass.suppress = []


TestSuppression.suppress = [util.suppress(message=CLASS_WARNING_MSG)]


class TestSuppression2(unittest.TestCase, EmitMixin):
    def testSuppressModule(self):
        self._emit()


suppress = [util.suppress(message=MODULE_WARNING_MSG)]
Пример #36
0
class RegistrationTestCase(unittest.TestCase):
    def setUp(self):
        self.proxy = sip.RegisterProxy(host="127.0.0.1")
        self.registry = sip.InMemoryRegistry("bell.example.com")
        self.proxy.registry = self.proxy.locator = self.registry
        self.sent = []
        self.proxy.sendMessage = lambda dest, msg: self.sent.append(
            (dest, msg))

    setUp = utils.suppressWarnings(
        setUp,
        util.suppress(
            category=DeprecationWarning,
            message=r'twisted.protocols.sip.DigestAuthorizer was deprecated'))

    def tearDown(self):
        for d, uri in self.registry.users.values():
            d.cancel()
        del self.proxy

    def register(self):
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:1234")
        r.addHeader("via", sip.Via("client.com").toString())
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))

    def unregister(self):
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "*")
        r.addHeader("via", sip.Via("client.com").toString())
        r.addHeader("expires", "0")
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))

    def testRegister(self):
        self.register()
        dest, m = self.sent[0]
        self.assertEqual((dest.host, dest.port), ("client.com", 5060))
        self.assertEqual(m.code, 200)
        self.assertEqual(m.headers["via"], ["SIP/2.0/UDP client.com:5060"])
        self.assertEqual(m.headers["to"], ["sip:[email protected]"])
        self.assertEqual(m.headers["contact"], ["sip:[email protected]:5060"])
        self.failUnless(
            int(m.headers["expires"][0]) in (3600, 3601, 3599, 3598))
        self.assertEqual(len(self.registry.users), 1)
        dc, uri = self.registry.users["joe"]
        self.assertEqual(uri.toString(), "sip:[email protected]:5060")
        d = self.proxy.locator.getAddress(
            sip.URL(username="******", host="bell.example.com"))
        d.addCallback(lambda desturl: (desturl.host, desturl.port))
        d.addCallback(self.assertEqual, ('client.com', 5060))
        return d

    def testUnregister(self):
        self.register()
        self.unregister()
        dest, m = self.sent[1]
        self.assertEqual((dest.host, dest.port), ("client.com", 5060))
        self.assertEqual(m.code, 200)
        self.assertEqual(m.headers["via"], ["SIP/2.0/UDP client.com:5060"])
        self.assertEqual(m.headers["to"], ["sip:[email protected]"])
        self.assertEqual(m.headers["contact"], ["sip:[email protected]:5060"])
        self.assertEqual(m.headers["expires"], ["0"])
        self.assertEqual(self.registry.users, {})

    def addPortal(self):
        r = TestRealm()
        p = cred.portal.Portal(r)
        c = cred.checkers.InMemoryUsernamePasswordDatabaseDontUse()
        c.addUser('[email protected]', 'passXword')
        p.registerChecker(c)
        self.proxy.portal = p

    def testFailedAuthentication(self):
        self.addPortal()
        self.register()

        self.assertEqual(len(self.registry.users), 0)
        self.assertEqual(len(self.sent), 1)
        dest, m = self.sent[0]
        self.assertEqual(m.code, 401)

    def test_basicAuthentication(self):
        """
        Test that registration with basic authentication succeeds.
        """
        self.addPortal()
        self.proxy.authorizers = self.proxy.authorizers.copy()
        self.proxy.authorizers['basic'] = sip.BasicAuthorizer()
        warnings = self.flushWarnings(
            offendingFunctions=[self.test_basicAuthentication])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.protocols.sip.BasicAuthorizer was deprecated in "
            "Twisted 9.0.0")
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:1234")
        r.addHeader("via", sip.Via("client.com").toString())
        r.addHeader("authorization",
                    "Basic " + "userXname:passXword".encode('base64'))
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))

        self.assertEqual(len(self.registry.users), 1)
        self.assertEqual(len(self.sent), 1)
        dest, m = self.sent[0]
        self.assertEqual(m.code, 200)

    def test_failedBasicAuthentication(self):
        """
        Failed registration with basic authentication results in an
        unauthorized error response.
        """
        self.addPortal()
        self.proxy.authorizers = self.proxy.authorizers.copy()
        self.proxy.authorizers['basic'] = sip.BasicAuthorizer()
        warnings = self.flushWarnings(
            offendingFunctions=[self.test_failedBasicAuthentication])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.protocols.sip.BasicAuthorizer was deprecated in "
            "Twisted 9.0.0")
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:1234")
        r.addHeader("via", sip.Via("client.com").toString())
        r.addHeader("authorization",
                    "Basic " + "userXname:password".encode('base64'))
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))

        self.assertEqual(len(self.registry.users), 0)
        self.assertEqual(len(self.sent), 1)
        dest, m = self.sent[0]
        self.assertEqual(m.code, 401)

    def testWrongDomainRegister(self):
        r = sip.Request("REGISTER", "sip:wrong.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:1234")
        r.addHeader("via", sip.Via("client.com").toString())
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))
        self.assertEqual(len(self.sent), 0)

    def testWrongToDomainRegister(self):
        r = sip.Request("REGISTER", "sip:bell.example.com")
        r.addHeader("to", "sip:[email protected]")
        r.addHeader("contact", "sip:[email protected]:1234")
        r.addHeader("via", sip.Via("client.com").toString())
        self.proxy.datagramReceived(r.toString(), ("client.com", 5060))
        self.assertEqual(len(self.sent), 0)

    def testWrongDomainLookup(self):
        self.register()
        url = sip.URL(username="******", host="foo.com")
        d = self.proxy.locator.getAddress(url)
        self.assertFailure(d, LookupError)
        return d

    def testNoContactLookup(self):
        self.register()
        url = sip.URL(username="******", host="bell.example.com")
        d = self.proxy.locator.getAddress(url)
        self.assertFailure(d, LookupError)
        return d
Пример #37
0
        p.mbox.messages.append(self.extraMessage)

        p.lineReceived(b"RETR 2")
        self._flush()
        p.lineReceived(b"RSET")
        s.seek(0)
        s.truncate(0)
        p.lineReceived(b"LAST")
        self.assertEqual(s.getvalue(), b"+OK 0\r\n")


_listMessageDeprecation = (
    "twisted.mail.pop3.IMailbox.listMessages may not "
    "raise IndexError for out-of-bounds message numbers: "
    "raise ValueError instead.")
_listMessageSuppression = util.suppress(message=_listMessageDeprecation,
                                        category=PendingDeprecationWarning)

_getUidlDeprecation = ("twisted.mail.pop3.IMailbox.getUidl may not "
                       "raise IndexError for out-of-bounds message numbers: "
                       "raise ValueError instead.")
_getUidlSuppression = util.suppress(message=_getUidlDeprecation,
                                    category=PendingDeprecationWarning)


class IndexErrorCommandTests(CommandMixin, unittest.TestCase):
    """
    Run all of the command tests against a mailbox which raises IndexError
    when an out of bounds request is made.  This behavior will be deprecated
    shortly and then removed.
    """
Пример #38
0
class TestDocFactories(unittest.TestCase):

    def _preprocessorTest(self, docFactory):
        def preprocessor(uncompiled):
            self.assertEquals(len(uncompiled), 1)
            uncompiled = uncompiled[0]
            self.assertEquals(uncompiled.tagName, 'div')
            self.assertEquals(len(uncompiled.children), 2)
            self.assertEquals(uncompiled.children[0].tagName, 'span')
            self.assertEquals(uncompiled.children[0].children, ['Hello'])
            self.assertEquals(uncompiled.children[1].tagName, 'span')
            self.assertEquals(uncompiled.children[1].children, ['world'])
            return t.div['goodbye.']
        doc = docFactory.load(preprocessors=[preprocessor])
        self.assertEquals(doc, ['<div>goodbye.</div>'])


    def test_stanPreprocessors(self):
        """
        Test that the stan loader properly passes uncompiled documents to
        preprocessors it is given.
        """
        factory = loaders.stan(
            t.div[t.span['Hello'], t.span['world']])
        return self._preprocessorTest(factory)


    def test_stan(self):
        doc = t.ul(id='nav')[t.li['one'], t.li['two'], t.li['three']]
        df = loaders.stan(doc)
        self.assertEquals(df.load()[0], '<ul id="nav"><li>one</li><li>two</li><li>three</li></ul>')


    def test_stanPrecompiled(self):
        """
        Test that a stan loader works with precompiled documents.

        (This behavior will probably be deprecated soon, but we need to test
        that it works right until we remove it.)
        """
        doc = flat.precompile(t.ul(id='nav')[t.li['one'], t.li['two'], t.slot('three')])
        df = loaders.stan(doc)
        loaded = df.load()
        self.assertEqual(loaded[0], '<ul id="nav"><li>one</li><li>two</li>')
        self.failUnless(isinstance(loaded[1], _PrecompiledSlot))
        self.assertEqual(loaded[1].name, 'three')
        self.assertEqual(loaded[2], '</ul>')


    def test_htmlstr(self):
        doc = '<ul id="nav"><li>a</li><li>b</li><li>c</li></ul>'
        df = loaders.htmlstr(doc)
        self.assertEquals(df.load()[0], doc)
    test_htmlstr.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlstr is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]


    def test_htmlfile(self):
        doc = '<ul id="nav"><li>a</li><li>b</li><li>c</li></ul>'
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(doc)
        f.close()
        df = loaders.htmlfile(temp)
        self.assertEquals(df.load()[0], doc)
    test_htmlfile.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlfile is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]


    def test_htmlfile_slots(self):
        doc = '<nevow:slot name="foo">Hi there</nevow:slot>'
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(doc)
        f.close()
        df = loaders.htmlfile(temp)
        self.assertEquals(df.load()[0].children, ['Hi there'])
    test_htmlfile_slots.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlfile is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]


    def test_xmlstr(self):
        doc = '<ul id="nav"><li>a</li><li>b</li><li>c</li></ul>'
        df = loaders.xmlstr(doc)
        self.assertEquals(df.load()[0], doc)


    def test_xmlstrPreprocessors(self):
        """
        Test that the xmlstr loader properly passes uncompiled documents to
        preprocessors it is given.
        """
        factory = loaders.xmlstr(
            '<div><span>Hello</span><span>world</span></div>')
        return self._preprocessorTest(factory)


    def test_xmlfile(self):
        doc = '<ul id="nav"><li>a</li><li>b</li><li>c</li></ul>'
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(doc)
        f.close()
        df = loaders.xmlfile(temp)
        self.assertEquals(df.load()[0], doc)


    def test_xmlfilePreprocessors(self):
        """
        Test that the xmlstr loader properly passes uncompiled documents to
        preprocessors it is given.
        """
        xmlFile = self.mktemp()
        f = file(xmlFile, 'w')
        f.write('<div><span>Hello</span><span>world</span></div>')
        f.close()
        factory = loaders.xmlfile(xmlFile)
        return self._preprocessorTest(factory)


    def test_patterned(self):
        """Test fetching a specific part (a pattern) of the document.
        """
        doc = t.div[t.p[t.span(pattern='inner')['something']]]
        df = loaders.stan(doc, pattern='inner')
        self.assertEquals(df.load()[0].tagName, 'span')
        self.assertEquals(df.load()[0].children[0], 'something')


    def test_ignoreDocType(self):
        doc = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html><body><p>Hello.</p></body></html>'''
        df = loaders.xmlstr(doc, ignoreDocType=True)
        self.assertEquals(flat.flatten(df), '<html><body><p>Hello.</p></body></html>')

    def test_ignoreComment(self):
        doc = '<!-- skip this --><p>Hello.</p>'
        df = loaders.xmlstr(doc, ignoreComment=True)
        self.assertEquals(flat.flatten(df), '<p>Hello.</p>')
Пример #39
0
    def _wait(self, d, running=_wait_is_running):
        """Take a Deferred that only ever callbacks. Block until it happens."""
        if running:
            raise RuntimeError("_wait is not reentrant")

        from twisted.internet import reactor

        results = []

        def append(any):
            if results is not None:
                results.append(any)

        def crash(ign):
            if results is not None:
                reactor.crash()

        crash = utils.suppressWarnings(
            crash,
            util.suppress(
                message=r"reactor\.crash cannot be used.*", category=DeprecationWarning
            ),
        )

        def stop():
            reactor.crash()

        stop = utils.suppressWarnings(
            stop,
            util.suppress(
                message=r"reactor\.crash cannot be used.*", category=DeprecationWarning
            ),
        )

        running.append(None)
        try:
            d.addBoth(append)
            if results:
                # d might have already been fired, in which case append is
                # called synchronously. Avoid any reactor stuff.
                return
            d.addBoth(crash)
            reactor.stop = stop
            try:
                reactor.run()
            finally:
                del reactor.stop

            # If the reactor was crashed elsewhere due to a timeout, hopefully
            # that crasher also reported an error. Just return.
            # _timedOut is most likely to be set when d has fired but hasn't
            # completed its callback chain (see self._run)
            if results or self._timedOut:  # defined in run() and _run()
                return

            # If the timeout didn't happen, and we didn't get a result or
            # a failure, then the user probably aborted the test, so let's
            # just raise KeyboardInterrupt.

            # FIXME: imagine this:
            # web/test/test_webclient.py:
            # exc = self.assertRaises(error.Error, wait, method(url))
            #
            # wait() will raise KeyboardInterrupt, and assertRaises will
            # swallow it. Therefore, wait() raising KeyboardInterrupt is
            # insufficient to stop trial. A suggested solution is to have
            # this code set a "stop trial" flag, or otherwise notify trial
            # that it should really try to stop as soon as possible.
            raise KeyboardInterrupt()
        finally:
            results = None
            running.pop()
Пример #40
0
        self._flush()
        p.lineReceived('RSET')
        s.truncate(0)
        p.lineReceived('LAST')
        self.assertEqual(
            s.getvalue(),
            '+OK 0\r\n')



_listMessageDeprecation = (
    "twisted.mail.pop3.IMailbox.listMessages may not "
    "raise IndexError for out-of-bounds message numbers: "
    "raise ValueError instead.")
_listMessageSuppression = util.suppress(
    message=_listMessageDeprecation,
    category=PendingDeprecationWarning)

_getUidlDeprecation = (
    "twisted.mail.pop3.IMailbox.getUidl may not "
    "raise IndexError for out-of-bounds message numbers: "
    "raise ValueError instead.")
_getUidlSuppression = util.suppress(
    message=_getUidlDeprecation,
    category=PendingDeprecationWarning)

class IndexErrorCommandTestCase(CommandMixin, unittest.TestCase):
    """
    Run all of the command tests against a mailbox which raises IndexError
    when an out of bounds request is made.  This behavior will be deprecated
    shortly and then removed.
Пример #41
0
class TestDocFactoriesCache(unittest.TestCase):

    doc = '''
    <div>
    <p nevow:pattern="1">one</p>
    <p nevow:pattern="2">two</p>
    </div>
    '''

    nsdoc = '''
    <div xmlns:nevow="http://nevow.com/ns/nevow/0.1">
    <p nevow:pattern="1">one</p>
    <p nevow:pattern="2">two</p>
    </div>
    '''

    stan = t.div[t.p(pattern='1')['one'],t.p(pattern='2')['two']]

    def test_stan(self):

        loader = loaders.stan(self.stan)
        self.assertEquals( id(loader.load()), id(loader.load()) )

        loader = loaders.stan(self.stan, pattern='1')
        self.assertEquals( id(loader.load()), id(loader.load()) )

        l1 = loaders.stan(self.stan, pattern='1')
        l2 = loaders.stan(self.stan, pattern='1')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

        l1 = loaders.stan(self.stan, pattern='1')
        l2 = loaders.stan(self.stan, pattern='2')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )


    def test_htmlstr(self):
        loader = loaders.htmlstr(self.doc)
        self.assertEquals( id(loader.load()), id(loader.load()) )

        loader = loaders.htmlstr(self.doc, pattern='1')
        self.assertEquals( id(loader.load()), id(loader.load()) )

        l1 = loaders.htmlstr(self.doc, pattern='1')
        l2 = loaders.htmlstr(self.doc, pattern='1')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

        l1 = loaders.htmlstr(self.doc, pattern='1')
        l2 = loaders.htmlstr(self.doc, pattern='2')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )
    test_htmlstr.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlstr is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]


    def test_htmlfile(self):
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(self.doc)
        f.close()

        loader = loaders.htmlfile(temp)
        self.assertEquals( id(loader.load()), id(loader.load()) )

        l1 = loaders.htmlfile(temp, pattern='1')
        l2 = loaders.htmlfile(temp, pattern='1')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

        l1 = loaders.htmlfile(temp, pattern='1')
        l2 = loaders.htmlfile(temp, pattern='2')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )
    test_htmlfile.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlfile is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]


    def test_htmlfileReload(self):
        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(self.doc)
        f.close()

        loader = loaders.htmlfile(temp)
        r = loader.load()
        self.assertEquals(id(r), id(loader.load()))
        os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5))
        self.assertNotEqual(id(r), id(loader.load()))
    test_htmlfileReload.suppress = [
        util.suppress(message=
                      r"\[v0.8\] htmlfile is deprecated because it's buggy. "
                      "Please start using xmlfile and/or xmlstr.")]



    def test_xmlstr(self):

        loader = loaders.xmlstr(self.nsdoc)
        self.assertEquals( id(loader.load()), id(loader.load()) )

        loader = loaders.xmlstr(self.nsdoc, pattern='1')
        self.assertEquals( id(loader.load()), id(loader.load()) )

        l1 = loaders.xmlstr(self.nsdoc, pattern='1')
        l2 = loaders.xmlstr(self.nsdoc, pattern='1')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

        l1 = loaders.xmlstr(self.nsdoc, pattern='1')
        l2 = loaders.xmlstr(self.nsdoc, pattern='2')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )


    def test_xmlSlotDefault(self):
        """
        An I{nevow:slot} tag in an XML template may have a I{default}
        attribute specifying a value for the slot if it is not otherwise
        given one.
        """
        slotsdoc = '''
        <div xmlns:nevow="http://nevow.com/ns/nevow/0.1">
        <nevow:slot name="1" />
        <nevow:slot name="2" default="3" />
        </div>
        '''
        loader = loaders.xmlstr(slotsdoc)
        loaded = loader.load()
        self.assertEquals(loaded[1].default, None)
        self.assertEquals(loaded[3].default, "3")


    def test_xmlfile(self):

        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(self.nsdoc)
        f.close()

        loader = loaders.xmlfile(temp)
        self.assertEquals( id(loader.load()), id(loader.load()) )

        loader = loaders.xmlfile(temp, pattern='1')
        self.assertEquals( id(loader.load()), id(loader.load()) )

        l1 = loaders.xmlfile(temp, pattern='1')
        l2 = loaders.xmlfile(temp, pattern='1')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

        l1 = loaders.xmlfile(temp, pattern='1')
        l2 = loaders.xmlfile(temp, pattern='2')
        self.assertNotEqual( id(l1.load()), id(l2.load()) )

    def test_xmlfileReload(self):

        temp = self.mktemp()
        f = file(temp, 'w')
        f.write(self.nsdoc)
        f.close()

        loader = loaders.xmlfile(temp)
        r = loader.load()
        self.assertEquals(id(r), id(loader.load()))
        os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5))
        self.assertNotEqual(id(r), id(loader.load()))

    def test_reloadAfterPrecompile(self):
        """
        """
        # Get a filename
        temp = self.mktemp()

        # Write some content
        f = file(temp, 'w')
        f.write('<p>foo</p>')
        f.close()

        # Precompile the doc
        ctx = context.WovenContext()
        doc = loaders.htmlfile(temp)
        pc = flat.precompile(flat.flatten(doc), ctx)

        before = ''.join(flat.serialize(pc, ctx))


        # Write the file with different content and make sure the
        # timestamp changes
        f = file(temp, 'w')
        f.write('<p>bar</p>')
        f.close()
        os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5))

        after = ''.join(flat.serialize(pc, ctx))

        self.assertIn('foo', before)
        self.assertIn('bar', after)
        self.failIfEqual(before, after)
    test_reloadAfterPrecompile.todo = \
        'Fix so that disk templates are reloaded even after a precompile. ' \
        'Probably just a matter of making the DocSerializer really lazy'
Пример #42
0
    def _wait(self, d, running=_wait_is_running):
        """Take a Deferred that only ever callbacks. Block until it happens.
        """
        if running:
            raise RuntimeError("_wait is not reentrant")

        from twisted.internet import reactor
        results = []
        def append(any):
            if results is not None:
                results.append(any)
        def crash(ign):
            if results is not None:
                reactor.crash()
        crash = utils.suppressWarnings(
            crash, util.suppress(message=r'reactor\.crash cannot be used.*',
                                 category=DeprecationWarning))
        def stop():
            reactor.crash()
        stop = utils.suppressWarnings(
            stop, util.suppress(message=r'reactor\.crash cannot be used.*',
                                category=DeprecationWarning))

        running.append(None)
        try:
            d.addBoth(append)
            if results:
                # d might have already been fired, in which case append is
                # called synchronously. Avoid any reactor stuff.
                return
            d.addBoth(crash)
            reactor.stop = stop
            try:
                reactor.run()
            finally:
                del reactor.stop

            # If the reactor was crashed elsewhere due to a timeout, hopefully
            # that crasher also reported an error. Just return.
            # _timedOut is most likely to be set when d has fired but hasn't
            # completed its callback chain (see self._run)
            if results or self._timedOut: #defined in run() and _run()
                return

            # If the timeout didn't happen, and we didn't get a result or
            # a failure, then the user probably aborted the test, so let's
            # just raise KeyboardInterrupt.

            # FIXME: imagine this:
            # web/test/test_webclient.py:
            # exc = self.assertRaises(error.Error, wait, method(url))
            #
            # wait() will raise KeyboardInterrupt, and assertRaises will
            # swallow it. Therefore, wait() raising KeyboardInterrupt is
            # insufficient to stop trial. A suggested solution is to have
            # this code set a "stop trial" flag, or otherwise notify trial
            # that it should really try to stop as soon as possible.
            raise KeyboardInterrupt()
        finally:
            results = None
            running.pop()
Пример #43
0
class TestInternet2(unittest.TestCase):
    def testTCP(self):
        s = service.MultiService()
        s.startService()
        factory = protocol.ServerFactory()
        factory.protocol = TestEcho
        TestEcho.d = defer.Deferred()
        t = internet.TCPServer(0, factory)
        t.setServiceParent(s)
        num = t._port.getHost().port
        factory = protocol.ClientFactory()
        factory.d = defer.Deferred()
        factory.protocol = Foo
        factory.line = None
        internet.TCPClient('127.0.0.1', num, factory).setServiceParent(s)
        factory.d.addCallback(self.assertEqual, 'lalala')
        factory.d.addCallback(lambda x: s.stopService())
        factory.d.addCallback(lambda x: TestEcho.d)
        return factory.d

    def test_UDP(self):
        """
        Test L{internet.UDPServer} with a random port: starting the service
        should give it valid port, and stopService should free it so that we
        can start a server on the same port again.
        """
        if not interfaces.IReactorUDP(reactor, None):
            raise unittest.SkipTest(
                "This reactor does not support UDP sockets")
        p = protocol.DatagramProtocol()
        t = internet.UDPServer(0, p)
        t.startService()
        num = t._port.getHost().port
        self.assertNotEquals(num, 0)

        def onStop(ignored):
            t = internet.UDPServer(num, p)
            t.startService()
            return t.stopService()

        return defer.maybeDeferred(t.stopService).addCallback(onStop)

    def testPrivileged(self):
        factory = protocol.ServerFactory()
        factory.protocol = TestEcho
        TestEcho.d = defer.Deferred()
        t = internet.TCPServer(0, factory)
        t.privileged = 1
        t.privilegedStartService()
        num = t._port.getHost().port
        factory = protocol.ClientFactory()
        factory.d = defer.Deferred()
        factory.protocol = Foo
        factory.line = None
        c = internet.TCPClient('127.0.0.1', num, factory)
        c.startService()
        factory.d.addCallback(self.assertEqual, 'lalala')
        factory.d.addCallback(lambda x: c.stopService())
        factory.d.addCallback(lambda x: t.stopService())
        factory.d.addCallback(lambda x: TestEcho.d)
        return factory.d

    def testConnectionGettingRefused(self):
        factory = protocol.ServerFactory()
        factory.protocol = wire.Echo
        t = internet.TCPServer(0, factory)
        t.startService()
        num = t._port.getHost().port
        t.stopService()
        d = defer.Deferred()
        factory = protocol.ClientFactory()
        factory.clientConnectionFailed = lambda *args: d.callback(None)
        c = internet.TCPClient('127.0.0.1', num, factory)
        c.startService()
        return d

    def testUNIX(self):
        # FIXME: This test is far too dense.  It needs comments.
        #  -- spiv, 2004-11-07
        if not interfaces.IReactorUNIX(reactor, None):
            raise unittest.SkipTest, "This reactor does not support UNIX domain sockets"
        s = service.MultiService()
        s.startService()
        factory = protocol.ServerFactory()
        factory.protocol = TestEcho
        TestEcho.d = defer.Deferred()
        t = internet.UNIXServer('echo.skt', factory)
        t.setServiceParent(s)
        factory = protocol.ClientFactory()
        factory.protocol = Foo
        factory.d = defer.Deferred()
        factory.line = None
        internet.UNIXClient('echo.skt', factory).setServiceParent(s)
        factory.d.addCallback(self.assertEqual, 'lalala')
        factory.d.addCallback(lambda x: s.stopService())
        factory.d.addCallback(lambda x: TestEcho.d)
        factory.d.addCallback(self._cbTestUnix, factory, s)
        return factory.d

    def _cbTestUnix(self, ignored, factory, s):
        TestEcho.d = defer.Deferred()
        factory.line = None
        factory.d = defer.Deferred()
        s.startService()
        factory.d.addCallback(self.assertEqual, 'lalala')
        factory.d.addCallback(lambda x: s.stopService())
        factory.d.addCallback(lambda x: TestEcho.d)
        return factory.d

    def testVolatile(self):
        if not interfaces.IReactorUNIX(reactor, None):
            raise unittest.SkipTest, "This reactor does not support UNIX domain sockets"
        factory = protocol.ServerFactory()
        factory.protocol = wire.Echo
        t = internet.UNIXServer('echo.skt', factory)
        t.startService()
        self.failIfIdentical(t._port, None)
        t1 = copy.copy(t)
        self.assertIdentical(t1._port, None)
        t.stopService()
        self.assertIdentical(t._port, None)
        self.failIf(t.running)

        factory = protocol.ClientFactory()
        factory.protocol = wire.Echo
        t = internet.UNIXClient('echo.skt', factory)
        t.startService()
        self.failIfIdentical(t._connection, None)
        t1 = copy.copy(t)
        self.assertIdentical(t1._connection, None)
        t.stopService()
        self.assertIdentical(t._connection, None)
        self.failIf(t.running)

    def testStoppingServer(self):
        if not interfaces.IReactorUNIX(reactor, None):
            raise unittest.SkipTest, "This reactor does not support UNIX domain sockets"
        factory = protocol.ServerFactory()
        factory.protocol = wire.Echo
        t = internet.UNIXServer('echo.skt', factory)
        t.startService()
        t.stopService()
        self.failIf(t.running)
        factory = protocol.ClientFactory()
        d = defer.Deferred()
        factory.clientConnectionFailed = lambda *args: d.callback(None)
        reactor.connectUNIX('echo.skt', factory)
        return d

    def testPickledTimer(self):
        target = TimerTarget()
        t0 = internet.TimerService(1, target.append, "hello")
        t0.startService()
        s = pickle.dumps(t0)
        t0.stopService()

        t = pickle.loads(s)
        self.failIf(t.running)

    def testBrokenTimer(self):
        d = defer.Deferred()
        t = internet.TimerService(1, lambda: 1 / 0)
        oldFailed = t._failed

        def _failed(why):
            oldFailed(why)
            d.callback(None)

        t._failed = _failed
        t.startService()
        d.addCallback(lambda x: t.stopService)
        d.addCallback(lambda x: self.assertEqual([ZeroDivisionError], [
            o.value.__class__
            for o in self.flushLoggedErrors(ZeroDivisionError)
        ]))
        return d

    def test_genericServerDeprecated(self):
        """
        Instantiating L{GenericServer} emits a deprecation warning.
        """
        internet.GenericServer()
        warnings = self.flushWarnings(
            offendingFunctions=[self.test_genericServerDeprecated])
        self.assertEquals(warnings[0]['message'],
                          'GenericServer was deprecated in Twisted 10.1.')
        self.assertEquals(warnings[0]['category'], DeprecationWarning)
        self.assertEquals(len(warnings), 1)

    def test_genericClientDeprecated(self):
        """
        Instantiating L{GenericClient} emits a deprecation warning.
        """
        internet.GenericClient()
        warnings = self.flushWarnings(
            offendingFunctions=[self.test_genericClientDeprecated])
        self.assertEquals(warnings[0]['message'],
                          'GenericClient was deprecated in Twisted 10.1.')
        self.assertEquals(warnings[0]['category'], DeprecationWarning)
        self.assertEquals(len(warnings), 1)

    def test_everythingThere(self):
        """
        L{twisted.application.internet} dynamically defines a set of
        L{service.Service} subclasses that in general have corresponding
        reactor.listenXXX or reactor.connectXXX calls.
        """
        trans = 'TCP UNIX SSL UDP UNIXDatagram Multicast'.split()
        for tran in trans[:]:
            if not getattr(interfaces, "IReactor" + tran)(reactor, None):
                trans.remove(tran)
        if interfaces.IReactorArbitrary(reactor, None) is not None:
            trans.insert(0, "Generic")
        for tran in trans:
            for side in 'Server Client'.split():
                if tran == "Multicast" and side == "Client":
                    continue
                self.assertTrue(hasattr(internet, tran + side))
                method = getattr(internet, tran + side).method
                prefix = {'Server': 'listen', 'Client': 'connect'}[side]
                self.assertTrue(
                    hasattr(reactor, prefix + method)
                    or (prefix == "connect" and method == "UDP"))
                o = getattr(internet, tran + side)()
                self.assertEquals(service.IService(o), o)

    test_everythingThere.suppress = [
        util.suppress(message='GenericServer was deprecated in Twisted 10.1.',
                      category=DeprecationWarning),
        util.suppress(message='GenericClient was deprecated in Twisted 10.1.',
                      category=DeprecationWarning),
        util.suppress(
            message='twisted.internet.interfaces.IReactorArbitrary was '
            'deprecated in Twisted 10.1.0: See IReactorFDSet.')
    ]

    def test_importAll(self):
        """
        L{twisted.application.internet} dynamically defines L{service.Service}
        subclasses. This test ensures that the subclasses exposed by C{__all__}
        are valid attributes of the module.
        """
        for cls in internet.__all__:
            self.assertTrue(
                hasattr(internet, cls),
                '%s not importable from twisted.application.internet' %
                (cls, ))

    def test_reactorParametrizationInServer(self):
        """
        L{internet._AbstractServer} supports a C{reactor} keyword argument
        that can be used to parametrize the reactor used to listen for
        connections.
        """
        reactor = MemoryReactor()

        factory = object()
        t = internet.TCPServer(1234, factory, reactor=reactor)
        t.startService()
        self.assertEquals(reactor.tcpServers.pop()[:2], (1234, factory))

    def test_reactorParametrizationInClient(self):
        """
        L{internet._AbstractClient} supports a C{reactor} keyword arguments
        that can be used to parametrize the reactor used to create new client
        connections.
        """
        reactor = MemoryReactor()

        factory = object()
        t = internet.TCPClient('127.0.0.1', 1234, factory, reactor=reactor)
        t.startService()
        self.assertEquals(reactor.tcpClients.pop()[:3],
                          ('127.0.0.1', 1234, factory))

    def test_reactorParametrizationInServerMultipleStart(self):
        """
        Like L{test_reactorParametrizationInServer}, but stop and restart the
        service and check that the given reactor is still used.
        """
        reactor = MemoryReactor()

        factory = object()
        t = internet.TCPServer(1234, factory, reactor=reactor)
        t.startService()
        self.assertEquals(reactor.tcpServers.pop()[:2], (1234, factory))
        t.stopService()
        t.startService()
        self.assertEquals(reactor.tcpServers.pop()[:2], (1234, factory))

    def test_reactorParametrizationInClientMultipleStart(self):
        """
        Like L{test_reactorParametrizationInClient}, but stop and restart the
        service and check that the given reactor is still used.
        """
        reactor = MemoryReactor()

        factory = object()
        t = internet.TCPClient('127.0.0.1', 1234, factory, reactor=reactor)
        t.startService()
        self.assertEquals(reactor.tcpClients.pop()[:3],
                          ('127.0.0.1', 1234, factory))
        t.stopService()
        t.startService()
        self.assertEquals(reactor.tcpClients.pop()[:3],
                          ('127.0.0.1', 1234, factory))
Пример #44
0
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Test cases for timeoutqueue module.
"""

import time

from twisted.python import timeoutqueue
from twisted.trial import unittest, util
from twisted.internet import reactor, interfaces

timeoutqueueSuppression = util.suppress(
    message="timeoutqueue is deprecated since Twisted 8.0",
    category=DeprecationWarning)


class TimeoutQueueTest(unittest.TestCase):
    """
    Test L{timeoutqueue.TimeoutQueue} class.
    """

    def tearDown(self):
        del self.q

    def put(self):
        time.sleep(1)
        self.q.put(1)