1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
# Copyright (C) 2007 Giampaolo Rodola' <g.rodola@gmail.com>.
# Use of this source code is governed by MIT license that can be
# found in the LICENSE file.
import contextlib
import ftplib
import socket
import pytest
from pyftpdlib import handlers
from pyftpdlib import servers
from . import GLOBAL_TIMEOUT
from . import HOST
from . import PASSWD
from . import SUPPORTS_MULTIPROCESSING
from . import USER
from . import WINDOWS
from . import FtpdThreadWrapper
from . import PyftpdlibTestCase
from . import close_client
from .test_functional import TestCornerCases
from .test_functional import TestFtpAbort
from .test_functional import TestFtpAuthentication
from .test_functional import TestFtpCmdsSemantic
from .test_functional import TestFtpDummyCmds
from .test_functional import TestFtpFsOperations
from .test_functional import TestFtpListingCmds
from .test_functional import TestFtpRetrieveData
from .test_functional import TestFtpStoreData
from .test_functional import TestIPv4Environment
from .test_functional import TestIPv6Environment
class TestFTPServer(PyftpdlibTestCase):
"""Tests for *FTPServer classes."""
server_class = FtpdThreadWrapper
client_class = ftplib.FTP
def setUp(self):
super().setUp()
self.server = None
self.client = None
def tearDown(self):
if self.client is not None:
close_client(self.client)
if self.server is not None:
self.server.stop()
super().tearDown()
@pytest.mark.skipif(WINDOWS, reason="POSIX only")
def test_sock_instead_of_addr(self):
# pass a socket object instead of an address tuple to FTPServer
# constructor
with contextlib.closing(socket.socket()) as sock:
sock.bind((HOST, 0))
sock.listen(5)
ip, port = sock.getsockname()[:2]
self.server = self.server_class(sock)
self.server.start()
self.client = self.client_class(timeout=GLOBAL_TIMEOUT)
self.client.connect(ip, port)
self.client.login(USER, PASSWD)
self.client.quit()
def test_ctx_mgr(self):
with servers.FTPServer((HOST, 0), handlers.FTPHandler) as server:
assert server is not None
# =====================================================================
# --- threaded FTP server mixin tests
# =====================================================================
# What we're going to do here is repeat the original functional tests
# defined in test_functinal.py but by using different concurrency
# modules (multi thread and multi process instead of async.
# This is useful as we reuse the existent functional tests which are
# supposed to work no matter what the concurrency model is.
class _TFTPd(FtpdThreadWrapper):
server_class = servers.ThreadedFTPServer
class ThreadFTPTestMixin:
server_class = _TFTPd
class TestFtpAuthenticationThreadMixin(
ThreadFTPTestMixin, TestFtpAuthentication
):
pass
class TestTFtpDummyCmdsThreadMixin(ThreadFTPTestMixin, TestFtpDummyCmds):
pass
class TestFtpCmdsSemanticThreadMixin(ThreadFTPTestMixin, TestFtpCmdsSemantic):
pass
class TestFtpFsOperationsThreadMixin(ThreadFTPTestMixin, TestFtpFsOperations):
pass
class TestFtpStoreDataThreadMixin(ThreadFTPTestMixin, TestFtpStoreData):
pass
class TestFtpRetrieveDataThreadMixin(ThreadFTPTestMixin, TestFtpRetrieveData):
pass
class TestFtpListingCmdsThreadMixin(ThreadFTPTestMixin, TestFtpListingCmds):
pass
class TestFtpAbortThreadMixin(ThreadFTPTestMixin, TestFtpAbort):
pass
# class TestTimeoutsThreadMixin(ThreadFTPTestMixin, TestTimeouts):
# def test_data_timeout_not_reached(self): pass
# class TestConfOptsThreadMixin(ThreadFTPTestMixin, TestConfigurableOptions):
# pass
# class TestCallbacksThreadMixin(ThreadFTPTestMixin, TestCallbacks):
# pass
class TestIPv4EnvironmentThreadMixin(ThreadFTPTestMixin, TestIPv4Environment):
pass
class TestIPv6EnvironmentThreadMixin(ThreadFTPTestMixin, TestIPv6Environment):
pass
class TestCornerCasesThreadMixin(ThreadFTPTestMixin, TestCornerCases):
pass
# class TestFTPServerThreadMixin(ThreadFTPTestMixin, TestFTPServer):
# pass
# =====================================================================
# --- multiprocess FTP server mixin tests
# =====================================================================
if SUPPORTS_MULTIPROCESSING:
class MultiProcFTPd(FtpdThreadWrapper):
server_class = servers.MultiprocessFTPServer
class MProcFTPTestMixin:
server_class = MultiProcFTPd
else:
@pytest.mark.skip(reason="multiprocessing module not installed")
class MProcFTPTestMixin:
pass
class TestFtpAuthenticationMProcMixin(
MProcFTPTestMixin, TestFtpAuthentication
):
pass
class TestTFtpDummyCmdsMProcMixin(MProcFTPTestMixin, TestFtpDummyCmds):
pass
class TestFtpCmdsSemanticMProcMixin(MProcFTPTestMixin, TestFtpCmdsSemantic):
pass
class TestFtpFsOperationsMProcMixin(MProcFTPTestMixin, TestFtpFsOperations):
def test_unforeseen_mdtm_event(self):
pass
class TestFtpStoreDataMProcMixin(MProcFTPTestMixin, TestFtpStoreData):
pass
class TestFtpRetrieveDataMProcMixin(MProcFTPTestMixin, TestFtpRetrieveData):
pass
class TestFtpListingCmdsMProcMixin(MProcFTPTestMixin, TestFtpListingCmds):
pass
class TestFtpAbortMProcMixin(MProcFTPTestMixin, TestFtpAbort):
pass
# class TestTimeoutsMProcMixin(MProcFTPTestMixin, TestTimeouts):
# def test_data_timeout_not_reached(self): pass
# class TestConfiOptsMProcMixin(MProcFTPTestMixin, TestConfigurableOptions):
# pass
# class TestCallbacksMProcMixin(MProcFTPTestMixin, TestCallbacks): pass
class TestIPv4EnvironmentMProcMixin(MProcFTPTestMixin, TestIPv4Environment):
pass
class TestIPv6EnvironmentMProcMixin(MProcFTPTestMixin, TestIPv6Environment):
pass
class TestCornerCasesMProcMixin(MProcFTPTestMixin, TestCornerCases):
pass
# class TestFTPServerMProcMixin(MProcFTPTestMixin, TestFTPServer):
# pass
|