Пример #1
0
 def test_validate_authentication(self):
     # we can only test for invalid credentials
     auth = authorizers.UnixAuthorizer(require_valid_shell=False)
     ret = auth.validate_authentication('?!foo', '?!foo')
     self.assertFalse(ret)
     auth = authorizers.UnixAuthorizer(require_valid_shell=True)
     ret = auth.validate_authentication('?!foo', '?!foo')
     self.assertFalse(ret)
Пример #2
0
    def test_require_valid_shell(self):

        def get_fake_shell_user():
            for user in self.get_users():
                shell = pwd.getpwnam(user).pw_shell
                # On linux fake shell is usually /bin/false, on
                # freebsd /usr/sbin/nologin;  in case of other
                # UNIX variants test needs to be adjusted.
                if '/false' in shell or '/nologin' in shell:
                    return user
            self.fail("no user found")

        user = get_fake_shell_user()
        self.assertRaisesWithMsg(ValueError,
                             "user %s has not a valid shell" % user,
                             authorizers.UnixAuthorizer, allowed_users=[user])
        # commented as it first fails for invalid home
        #self.assertRaisesWithMsg(ValueError,
        #                     "user %s has not a valid shell" % user,
        #                     authorizers.UnixAuthorizer, anonymous_user=user)
        auth = authorizers.UnixAuthorizer()
        self.assertTrue(auth._has_valid_shell(self.get_current_user()))
        self.assertFalse(auth._has_valid_shell(user))
        self.assertRaisesWithMsg(ValueError,
                                 "user %s has not a valid shell" % user,
                                 auth.override_user, user, perm='r')
Пример #3
0
 def test_validate_authentication_anonymous(self):
     current_user = self.get_current_user()
     auth = authorizers.UnixAuthorizer(anonymous_user=current_user,
                                       require_valid_shell=False)
     self.assertFalse(auth.validate_authentication('foo', 'passwd'))
     self.assertFalse(auth.validate_authentication(current_user, 'passwd'))
     self.assertTrue(auth.validate_authentication('anonymous', 'passwd'))
Пример #4
0
 def test_get_perms_anonymous(self):
     auth = authorizers.UnixAuthorizer(global_perm='elr',
                                       anonymous_user=self.get_current_user())
     self.assertTrue('e' in auth.get_perms('anonymous'))
     self.assertFalse('w' in auth.get_perms('anonymous'))
     warnings.filterwarnings("ignore")
     auth.override_user('anonymous', perm='w')
     warnings.resetwarnings()
     self.assertTrue('w' in auth.get_perms('anonymous'))
Пример #5
0
def test_main():
    test_suite = unittest.TestSuite()
    tests = []
    warns = []

    # FTPS tests
    if FTPS_SUPPORT:
        ftps_tests = [TestFTPS,
                      TestFtpAuthenticationTLSMixin,
                      TestTFtpDummyCmdsTLSMixin,
                      TestFtpCmdsSemanticTLSMixin,
                      TestFtpFsOperationsTLSMixin,
                      TestFtpStoreDataTLSMixin,
                      TestFtpRetrieveDataTLSMixin,
                      TestFtpListingCmdsTLSMixin,
                      TestFtpAbortTLSMixin,
                      TestTimeoutsTLSMixin,
                      TestConfigurableOptionsTLSMixin,
                      TestCallbacksTLSMixin,
                      TestCornerCasesTLSMixin,
                     ]
        if SUPPORTS_IPV4:
            ftps_tests.append(TestIPv4EnvironmentTLSMixin)
        if SUPPORTS_IPV6:
            ftps_tests.append(TestIPv6EnvironmentTLSMixin)
        tests += ftps_tests
    else:
        if sys.version_info < (2, 7):
            warns.append("FTPS tests skipped (requires python 2.7)")
        elif ssl is None:
            warns.append("FTPS tests skipped (requires ssl module)")
        elif not hasattr(handlers, 'TLS_FTPHandler'):
            warns.append("FTPS tests skipped (requires PyOpenSSL module)")
        else:
            warns.append("FTPS tests skipped")

    # threaded FTP server tests
    ftp_thread_tests = [
        TestFtpAuthenticationThreadMixin,
        TestTFtpDummyCmdsThreadMixin,
        TestFtpCmdsSemanticThreadMixin,
        TestFtpFsOperationsThreadMixin,
        TestFtpStoreDataThreadMixin,
        TestFtpRetrieveDataThreadMixin,
        TestFtpListingCmdsThreadMixin,
        TestFtpAbortThreadMixin,
        #TestTimeoutsThreadMixin,
        #TestConfigurableOptionsThreadMixin,
        TestCallbacksThreadMixin,
        TestCornerCasesThreadMixin,
    ]
    tests += ftp_thread_tests

    # multi process FTP server tests
    ftp_mproc_tests = [
        TestFtpAuthenticationMProcMixin,
        TestTFtpDummyCmdsMProcMixin,
        TestFtpCmdsSemanticMProcMixin,
        TestFtpFsOperationsMProcMixin,
        TestFtpStoreDataMProcMixin,
        TestFtpRetrieveDataMProcMixin,
        TestFtpListingCmdsMProcMixin,
        TestFtpAbortMProcMixin,
        #TestTimeoutsMProcMixin,
        #TestConfigurableOptionsMProcMixin,
        #TestCallbacksMProcMixin,
        TestCornerCasesMProcMixin,
    ]
    tests += ftp_mproc_tests

    # POSIX tests
    if os.name == 'posix':
        tests.append(TestUnixFilesystem)

        if hasattr(authorizers, "UnixAuthorizer"):
            try:
                authorizers.UnixAuthorizer()
            except ftpserver.AuthorizerError:  # not root
                warn("UnixAuthorizer tests skipped (root privileges are " \
                     "required)")
            else:
                tests.append(TestUnixAuthorizer)
        else:
            try:
                import spwd
            except ImportError:
                warn("UnixAuthorizer tests skipped (spwd module is missing")
            else:
                warn("UnixAuthorizer tests skipped")

    # Windows tests
    elif os.name in ('nt', 'ce'):
        if hasattr(authorizers, "WindowsAuthorizer"):
            tests.append(TestWindowsAuthorizer)
        else:
            try:
                import win32api
            except ImportError:
                warn("WindowsAuthorizer tests skipped (pywin32 extension " \
                     "is required)")
            else:
                warn("WindowsAuthorizer tests skipped")

    for test in tests:
        test_suite.addTest(unittest.makeSuite(test))
    unittest.TextTestRunner(verbosity=2).run(test_suite)
Пример #6
0
def test_main():
    test_suite = unittest.TestSuite()
    tests = []
    warns = []

    # FTPS tests
    if FTPS_SUPPORT:
        ftps_tests = [
            TestFTPS,
            TestFtpAuthenticationTLSMixin,
            TestTFtpDummyCmdsTLSMixin,
            TestFtpCmdsSemanticTLSMixin,
            TestFtpFsOperationsTLSMixin,
            TestFtpStoreDataTLSMixin,
            TestFtpRetrieveDataTLSMixin,
            TestFtpListingCmdsTLSMixin,
            TestFtpAbortTLSMixin,
            TestTimeoutsTLSMixin,
            TestConfigurableOptionsTLSMixin,
            TestCallbacksTLSMixin,
            TestCornerCasesTLSMixin,
        ]
        if SUPPORTS_IPV4:
            ftps_tests.append(TestIPv4EnvironmentTLSMixin)
        if SUPPORTS_IPV6:
            ftps_tests.append(TestIPv6EnvironmentTLSMixin)
        tests += ftps_tests
    else:
        if sys.version_info < (2, 7):
            warns.append("FTPS tests skipped (requires python 2.7)")
        elif ssl is None:
            warns.append("FTPS tests skipped (requires ssl module)")
        elif not hasattr(handlers, 'TLS_FTPHandler'):
            warns.append("FTPS tests skipped (requires PyOpenSSL module)")
        else:
            warns.append("FTPS tests skipped")

    # authorizers tests
    if os.name == 'posix':
        if hasattr(authorizers, "UnixAuthorizer"):
            try:
                authorizers.UnixAuthorizer()
            except ftpserver.AuthorizerError:  # not root
                warns.append(
                    "UnixAuthorizer tests skipped (root privileges are "
                    "required)")
            else:
                tests.append(TestUnixAuthorizer)
        else:
            try:
                import spwd
            except ImportError:
                warns.append("UnixAuthorizer tests skipped (spwd module is "
                             "missing")
            else:
                warns.append("UnixAuthorizer tests skipped")
    elif os.name in ('nt', 'ce'):
        if hasattr(authorizers, "WindowsAuthorizer"):
            tests.append(TestWindowsAuthorizer)
        else:
            try:
                import win32api
            except ImportError:
                warns.append(
                    "WindowsAuthorizer tests skipped (pywin32 extension "
                    "is required)")
            else:
                warns.append("WindowsAuthorizer tests skipped")

    if os.name == 'posix':
        tests.append(TestUnixFilesystem)

    for test in tests:
        test_suite.addTest(unittest.makeSuite(test))
    try:
        unittest.TextTestRunner(verbosity=2).run(test_suite)
    except:
        # in case of KeyboardInterrupt grant that the threaded FTP
        # server running in background gets stopped
        asyncore.socket_map.clear()
        raise
    for warn in warns:
        warnings.warn(warn, RuntimeWarning)