~certify-web-dev/twisted/certify-staging

« back to all changes in this revision

Viewing changes to twisted/test/test_ssl.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-01-02 19:38:17 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100102193817-jphp464ppwh7dulg
Tags: 9.0.0-1
* python-twisted: Depend on the python-twisted-* 9.0 packages.
* python-twisted: Depend on python-zope.interface only. Closes: #557781.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
 
1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
2
2
# See LICENSE for details.
3
3
 
4
4
"""
9
9
from twisted.internet import protocol, reactor, interfaces, defer
10
10
from twisted.protocols import basic
11
11
from twisted.python import util
 
12
from twisted.python.reflect import getClass, fullyQualifiedName
12
13
from twisted.python.runtime import platform
13
14
from twisted.test.test_tcp import WriteDataTestCase, ProperlyCloseFilesMixin
14
15
 
268
269
 
269
270
 
270
271
 
271
 
class StolenTCPTestCase(ProperlyCloseFilesMixin, WriteDataTestCase):
 
272
class StolenTCPTestCase(ProperlyCloseFilesMixin, unittest.TestCase):
272
273
    """
273
274
    For SSL transports, test many of the same things which are tested for
274
275
    TCP transports.
301
302
        return SSL.Error
302
303
 
303
304
 
 
305
    _iocp = 'twisted.internet.iocpreactor.reactor.IOCPReactor'
 
306
 
304
307
    def getHandleErrorCode(self):
305
308
        """
306
309
        Return the argument L{SSL.Error} will be constructed with for this
309
312
        this.
310
313
        """
311
314
        # Windows 2000 SP 4 and Windows XP SP 2 give back WSAENOTSOCK for
312
 
        # SSL.Connection.write for some reason.
313
 
        if platform.getType() == 'win32':
 
315
        # SSL.Connection.write for some reason.  The twisted.protocols.tls
 
316
        # implementation of IReactorSSL doesn't suffer from this imprecation,
 
317
        # though, since it is isolated from the Windows I/O layer (I suppose?).
 
318
 
 
319
        # If test_properlyCloseFiles waited for the SSL handshake to complete
 
320
        # and performed an orderly shutdown, then this would probably be a
 
321
        # little less weird: writing to a shutdown SSL connection has a more
 
322
        # well-defined failure mode (or at least it should).
 
323
        name = fullyQualifiedName(getClass(reactor))
 
324
        if platform.getType() == 'win32' and name != self._iocp:
314
325
            return errno.WSAENOTSOCK
315
326
        # This is terribly implementation-specific.
316
327
        return [('SSL routines', 'SSL_write', 'protocol is shutdown')]
547
558
 
548
559
 
549
560
 
 
561
class FakeContext:
 
562
    """
 
563
    L{OpenSSL.SSL.Context} double which can more easily be inspected.
 
564
    """
 
565
    def __init__(self, method):
 
566
        self._method = method
 
567
        self._options = 0
 
568
 
 
569
 
 
570
    def set_options(self, options):
 
571
        self._options |= options
 
572
 
 
573
 
 
574
    def use_certificate_file(self, fileName):
 
575
        pass
 
576
 
 
577
 
 
578
    def use_privatekey_file(self, fileName):
 
579
        pass
 
580
 
 
581
 
 
582
 
 
583
class DefaultOpenSSLContextFactoryTests(unittest.TestCase):
 
584
    """
 
585
    Tests for L{ssl.DefaultOpenSSLContextFactory}.
 
586
    """
 
587
    def setUp(self):
 
588
        # pyOpenSSL Context objects aren't introspectable enough.  Pass in
 
589
        # an alternate context factory so we can inspect what is done to it.
 
590
        self.contextFactory = ssl.DefaultOpenSSLContextFactory(
 
591
            certPath, certPath, _contextFactory=FakeContext)
 
592
        self.context = self.contextFactory.getContext()
 
593
 
 
594
 
 
595
    def test_method(self):
 
596
        """
 
597
        L{ssl.DefaultOpenSSLContextFactory.getContext} returns an SSL context
 
598
        which can use SSLv3 or TLSv1 but not SSLv2.
 
599
        """
 
600
        # SSLv23_METHOD allows SSLv2, SSLv3, or TLSv1
 
601
        self.assertEqual(self.context._method, SSL.SSLv23_METHOD)
 
602
 
 
603
        # And OP_NO_SSLv2 disables the SSLv2 support.
 
604
        self.assertTrue(self.context._options & SSL.OP_NO_SSLv2)
 
605
 
 
606
        # Make sure SSLv3 and TLSv1 aren't disabled though.
 
607
        self.assertFalse(self.context._options & SSL.OP_NO_SSLv3)
 
608
        self.assertFalse(self.context._options & SSL.OP_NO_TLSv1)
 
609
 
 
610
 
 
611
    def test_missingCertificateFile(self):
 
612
        """
 
613
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a certificate
 
614
        filename which does not identify an existing file results in the
 
615
        initializer raising L{OpenSSL.SSL.Error}.
 
616
        """
 
617
        self.assertRaises(
 
618
            SSL.Error,
 
619
            ssl.DefaultOpenSSLContextFactory, certPath, self.mktemp())
 
620
 
 
621
 
 
622
    def test_missingPrivateKeyFile(self):
 
623
        """
 
624
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a private key
 
625
        filename which does not identify an existing file results in the
 
626
        initializer raising L{OpenSSL.SSL.Error}.
 
627
        """
 
628
        self.assertRaises(
 
629
            SSL.Error,
 
630
            ssl.DefaultOpenSSLContextFactory, self.mktemp(), certPath)
 
631
 
 
632
 
 
633
 
 
634
class ClientContextFactoryTests(unittest.TestCase):
 
635
    """
 
636
    Tests for L{ssl.ClientContextFactory}.
 
637
    """
 
638
    def setUp(self):
 
639
        self.contextFactory = ssl.ClientContextFactory()
 
640
        self.contextFactory._contextFactory = FakeContext
 
641
        self.context = self.contextFactory.getContext()
 
642
 
 
643
 
 
644
    def test_method(self):
 
645
        """
 
646
        L{ssl.ClientContextFactory.getContext} returns a context which can use
 
647
        SSLv3 or TLSv1 but not SSLv2.
 
648
        """
 
649
        self.assertEqual(self.context._method, SSL.SSLv23_METHOD)
 
650
        self.assertTrue(self.context._options & SSL.OP_NO_SSLv2)
 
651
        self.assertFalse(self.context._options & SSL.OP_NO_SSLv3)
 
652
        self.assertFalse(self.context._options & SSL.OP_NO_TLSv1)
 
653
 
 
654
 
 
655
 
550
656
if interfaces.IReactorSSL(reactor, None) is None:
551
657
    for tCase in [StolenTCPTestCase, TLSTestCase, SpammyTLSTestCase,
552
 
                  BufferingTestCase, ConnectionLostTestCase]:
 
658
                  BufferingTestCase, ConnectionLostTestCase,
 
659
                  DefaultOpenSSLContextFactoryTests,
 
660
                  ClientContextFactoryTests]:
553
661
        tCase.skip = "Reactor does not support SSL, cannot run SSL tests"
554
662
 
555
663
# Otherwise trial will run this test here