|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (C) 2007-2016 Giampaolo Rodola' <g.rodola@gmail.com>. |
| 4 | +# Use of this source code is governed by MIT license that can be |
| 5 | +# found in the LICENSE file. |
| 6 | + |
| 7 | +import contextlib |
| 8 | +import ftplib |
| 9 | +import os |
| 10 | +import socket |
| 11 | +import sys |
| 12 | +import ssl |
| 13 | + |
| 14 | +import OpenSSL # requires "pip install pyopenssl" |
| 15 | + |
| 16 | +from pyftpdlib.handlers import TLS_FTPHandler |
| 17 | +from pyftpdlib.test import configure_logging |
| 18 | +from pyftpdlib.test import PASSWD |
| 19 | +from pyftpdlib.test import remove_test_files |
| 20 | +from pyftpdlib.test import ThreadedTestFTPd |
| 21 | +from pyftpdlib.test import TIMEOUT |
| 22 | +from pyftpdlib.test import TRAVIS |
| 23 | +from pyftpdlib.test import unittest |
| 24 | +from pyftpdlib.test import USER |
| 25 | +from pyftpdlib.test import VERBOSITY |
| 26 | +from pyftpdlib.test.test_functional import TestCallbacks |
| 27 | +from pyftpdlib.test.test_functional import TestConfigurableOptions |
| 28 | +from pyftpdlib.test.test_functional import TestCornerCases |
| 29 | +from pyftpdlib.test.test_functional import TestFtpAbort |
| 30 | +from pyftpdlib.test.test_functional import TestFtpAuthentication |
| 31 | +from pyftpdlib.test.test_functional import TestFtpCmdsSemantic |
| 32 | +from pyftpdlib.test.test_functional import TestFtpDummyCmds |
| 33 | +from pyftpdlib.test.test_functional import TestFtpFsOperations |
| 34 | +from pyftpdlib.test.test_functional import TestFtpListingCmds |
| 35 | +from pyftpdlib.test.test_functional import TestFtpRetrieveData |
| 36 | +from pyftpdlib.test.test_functional import TestFtpStoreData |
| 37 | +from pyftpdlib.test.test_functional import TestIPv4Environment |
| 38 | +from pyftpdlib.test.test_functional import TestIPv6Environment |
| 39 | +from pyftpdlib.test.test_functional import TestSendfile |
| 40 | +from pyftpdlib.test.test_functional import TestTimeouts |
| 41 | +from _ssl import SSLError |
| 42 | + |
| 43 | + |
| 44 | +FTPS_SUPPORT = hasattr(ftplib, 'FTP_TLS') |
| 45 | +if sys.version_info < (2, 7): |
| 46 | + FTPS_UNSUPPORT_REASON = "requires python 2.7+" |
| 47 | +else: |
| 48 | + FTPS_UNSUPPORT_REASON = "FTPS test skipped" |
| 49 | + |
| 50 | +CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 51 | + 'keycert.pem')) |
| 52 | +CLIENT_CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 53 | + 'clientcert.pem')) |
| 54 | + |
| 55 | +del OpenSSL |
| 56 | + |
| 57 | + |
| 58 | +if FTPS_SUPPORT: |
| 59 | + class FTPSClient(ftplib.FTP_TLS): |
| 60 | + """A modified version of ftplib.FTP_TLS class which implicitly |
| 61 | + secure the data connection after login(). |
| 62 | + """ |
| 63 | + |
| 64 | + def login(self, *args, **kwargs): |
| 65 | + ftplib.FTP_TLS.login(self, *args, **kwargs) |
| 66 | + self.prot_p() |
| 67 | + |
| 68 | + class FTPSServerAuth(ThreadedTestFTPd): |
| 69 | + """A threaded FTPS server that forces client certificate |
| 70 | + authentication used for functional testing. |
| 71 | + """ |
| 72 | + handler = TLS_FTPHandler |
| 73 | + handler.certfile = CERTFILE |
| 74 | + handler.client_certfile = CLIENT_CERTFILE |
| 75 | + |
| 76 | + |
| 77 | +# ===================================================================== |
| 78 | +# dedicated FTPS tests with client authentication |
| 79 | +# ===================================================================== |
| 80 | + |
| 81 | + |
| 82 | +@unittest.skipUnless(FTPS_SUPPORT, FTPS_UNSUPPORT_REASON) |
| 83 | +class TestFTPS(unittest.TestCase): |
| 84 | + """Specific tests for TLS_FTPHandler class.""" |
| 85 | + |
| 86 | + def setUp(self): |
| 87 | + self.server = FTPSServerAuth() |
| 88 | + self.server.start() |
| 89 | + |
| 90 | + def tearDown(self): |
| 91 | + self.client.close() |
| 92 | + self.server.stop() |
| 93 | + |
| 94 | + def assertRaisesWithMsg(self, excClass, msg, callableObj, *args, **kwargs): |
| 95 | + try: |
| 96 | + callableObj(*args, **kwargs) |
| 97 | + except excClass as err: |
| 98 | + if str(err) == msg: |
| 99 | + return |
| 100 | + raise self.failureException("%s != %s" % (str(err), msg)) |
| 101 | + else: |
| 102 | + if hasattr(excClass, '__name__'): |
| 103 | + excName = excClass.__name__ |
| 104 | + else: |
| 105 | + excName = str(excClass) |
| 106 | + raise self.failureException("%s not raised" % excName) |
| 107 | + |
| 108 | + def test_auth_client_cert(self): |
| 109 | + self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CLIENT_CERTFILE) |
| 110 | + self.client.connect(self.server.host, self.server.port) |
| 111 | + # secured |
| 112 | + try: |
| 113 | + self.client.login() |
| 114 | + except Exception as e: |
| 115 | + self.fail("login with certificate should work") |
| 116 | + |
| 117 | + def test_auth_client_nocert(self): |
| 118 | + self.client = ftplib.FTP_TLS(timeout=TIMEOUT) |
| 119 | + self.client.connect(self.server.host, self.server.port) |
| 120 | + try: |
| 121 | + self.client.login() |
| 122 | + except SSLError as e: |
| 123 | + # client should not be able to log in |
| 124 | + if "SSLV3_ALERT_HANDSHAKE_FAILURE" in e.reason: |
| 125 | + pass |
| 126 | + else: |
| 127 | + self.fail("Incorrect SSL error with missing client certificate") |
| 128 | + else: |
| 129 | + self.fail("Client able to log in with no certificate") |
| 130 | + |
| 131 | + def test_auth_client_badcert(self): |
| 132 | + self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CERTFILE) |
| 133 | + self.client.connect(self.server.host, self.server.port) |
| 134 | + try: |
| 135 | + self.client.login() |
| 136 | + except Exception as e: |
| 137 | + # client should not be able to log in |
| 138 | + if "TLSV1_ALERT_UNKNOWN_CA" in e.reason: |
| 139 | + pass |
| 140 | + else: |
| 141 | + self.fail("Incorrect SSL error with bad client certificate") |
| 142 | + else: |
| 143 | + self.fail("Client able to log in with bad certificate") |
| 144 | + |
| 145 | +configure_logging() |
| 146 | +remove_test_files() |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + unittest.main(verbosity=VERBOSITY) |
0 commit comments