~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/conch/test/test_tap.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2009 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
"""
 
5
Tests for L{twisted.conch.tap}.
 
6
"""
 
7
 
 
8
try:
 
9
    import Crypto.Cipher.DES3
 
10
except:
 
11
    Crypto = None
 
12
 
 
13
try:
 
14
    import pyasn1
 
15
except ImportError:
 
16
    pyasn1 = None
 
17
 
 
18
try:
 
19
    from twisted.conch import unix
 
20
except ImportError:
 
21
    unix = None
 
22
 
 
23
if Crypto and pyasn1 and unix:
 
24
    from twisted.conch import tap
 
25
    from twisted.conch.openssh_compat.factory import OpenSSHFactory
 
26
 
 
27
from twisted.python.compat import set
 
28
from twisted.application.internet import TCPServer
 
29
from twisted.cred.credentials import IPluggableAuthenticationModules
 
30
from twisted.cred.credentials import ISSHPrivateKey
 
31
from twisted.cred.credentials import IUsernamePassword
 
32
 
 
33
from twisted.trial.unittest import TestCase
 
34
 
 
35
 
 
36
 
 
37
class MakeServiceTest(TestCase):
 
38
    """
 
39
    Tests for L{tap.makeService}.
 
40
    """
 
41
 
 
42
    if not Crypto:
 
43
        skip = "can't run w/o PyCrypto"
 
44
 
 
45
    if not pyasn1:
 
46
        skip = "can't run w/o PyASN1"
 
47
 
 
48
    if not unix:
 
49
        skip = "can't run on non-posix computers"
 
50
 
 
51
    def test_basic(self):
 
52
        """
 
53
        L{tap.makeService} returns a L{TCPServer} instance running on port 22,
 
54
        and the linked protocol factory is an instance of L{OpenSSHFactory}.
 
55
        """
 
56
        config = tap.Options()
 
57
        service = tap.makeService(config)
 
58
        self.assertIsInstance(service, TCPServer)
 
59
        self.assertEquals(service.args[0], 22)
 
60
        factory = service.args[1]
 
61
        self.assertIsInstance(factory, OpenSSHFactory)
 
62
 
 
63
 
 
64
    def test_checkersPamAuth(self):
 
65
        """
 
66
        The L{OpenSSHFactory} built by L{tap.makeService} has a portal with
 
67
        L{IPluggableAuthenticationModules}, L{ISSHPrivateKey} and
 
68
        L{IUsernamePassword} interfaces registered as checkers if C{pamauth} is
 
69
        available.
 
70
        """
 
71
        # Fake the presence of pamauth, even if PyPAM is not installed
 
72
        self.patch(tap, "pamauth", object())
 
73
        config = tap.Options()
 
74
        service = tap.makeService(config)
 
75
        portal = service.args[1].portal
 
76
        self.assertEquals(
 
77
            set(portal.checkers.keys()),
 
78
            set([IPluggableAuthenticationModules, ISSHPrivateKey,
 
79
                 IUsernamePassword]))
 
80
 
 
81
 
 
82
    def test_checkersWithoutPamAuth(self):
 
83
        """
 
84
        The L{OpenSSHFactory} built by L{tap.makeService} has a portal with
 
85
        L{ISSHPrivateKey} and L{IUsernamePassword} interfaces registered as
 
86
        checkers if C{pamauth} is not available.
 
87
        """
 
88
        # Fake the absence of pamauth, even if PyPAM is installed
 
89
        self.patch(tap, "pamauth", None)
 
90
        config = tap.Options()
 
91
        service = tap.makeService(config)
 
92
        portal = service.args[1].portal
 
93
        self.assertEquals(
 
94
            set(portal.checkers.keys()),
 
95
            set([ISSHPrivateKey, IUsernamePassword]))