# Path: conpot/protocols/ftp/ftp_server.py
# class FTPServer(object):
#     def __init__(self, template, template_directory, args):
#         self.template = template
#         self.server = None  # Initialize later
#         # Initialize vfs here..
#         self.handler = FTPCommandChannel
#         self.handler.config = FTPConfig(self.template)
# 
#     def start(self, host, port):
#         self.handler.host, self.handler.port = host, port
#         connection = (self.handler.host, self.handler.port)
#         self.server = StreamServer(connection, self.handler.stream_server_handle)
#         logger.info("FTP server started on: {}".format(connection))
#         self.server.serve_forever()
# 
#     def stop(self):
#         logger.debug("Stopping FTP server")
#         self.server.stop()
#         del self.handler
# 
# Path: conpot/protocols/ftp/ftp_utils.py
# class FTPException(Exception):
# class FTPMaxLoginAttemptsExceeded(FTPException):
# class FTPPrivilegeException(FTPException):
# def get_data_from_iter(iterator):
# 
# Path: conpot/utils/networking.py
# def sanitize_file_name(name, host, port):
#     """
#     Ensure that file_name is legal. Slug the filename and store it onto the server.
#     This would ensure that there are no duplicates as far as writing a file is concerned. Also client addresses are
#     noted so that one can verify which client uploaded the file.
#     :param name: Name of the file
#     :param host: host/client address
#     :param port port/client port
#     :type name: str
#     """
#     return (
#         "("
#         + host
#         + ", "
#         + str(port)
#         + ")-"
#         + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#         + "-"
#         + slugify(name)
#     )


from gevent import monkey
from datetime import datetime
from tempfile import NamedTemporaryFile
from freezegun import freeze_time
from conpot.protocols.ftp.ftp_server import FTPServer
from conpot.protocols.ftp.ftp_utils import ftp_commands
from conpot.utils.greenlet import spawn_test_server, teardown_test_server
from conpot.utils.networking import sanitize_file_name
import unittest
import os
import conpot
import conpot.core as conpot_core
import ftplib  # Use ftplib's client for more authentic testing
        buffer = ""
        with open(_path + "/ftp_testing_retr.txt", mode="r") as _file:
            buffer += _file.readline()
        self.assertEqual(buffer, "This is just a test file for Conpot's FTP server\n")
        os.remove(_path + "/ftp_testing_retr.txt")

    def test_rein(self):
        self.client_init()
        self.assertEqual(self.client.sendcmd("rein"), "230 Ready for new user.")
        self.assertRaisesRegex(
            ftplib.error_perm,
            "503 Login with USER first.",
            self.client.sendcmd,
            "pass testing",
        )
        # TODO: Add test with existing transfer in progress.

    @freeze_time("2018-07-15 17:51:17")
    def test_stor(self):
        # let us test by uploading a file called ftp_testing.txt
        self.client_init()
        _path = os.path.join(
            "".join(conpot.__path__), "tests", "data", "test_data_fs", "ftp"
        )
        with open(_path + "/ftp_testing.txt", mode="rb") as _file:
            self.client.storbinary("stor ftp_testing_stor.txt", _file)
        self.assertIn(
            "ftp_testing_stor.txt", self.ftp_server.handler.config.vfs.listdir("/")
        )
        self.vfs.remove("ftp_testing_stor.txt")

# Path: conpot/protocols/ftp/ftp_server.py
# class FTPServer(object):
#     def __init__(self, template, template_directory, args):
#         self.template = template
#         self.server = None  # Initialize later
#         # Initialize vfs here..
#         self.handler = FTPCommandChannel
#         self.handler.config = FTPConfig(self.template)
# 
#     def start(self, host, port):
#         self.handler.host, self.handler.port = host, port
#         connection = (self.handler.host, self.handler.port)
#         self.server = StreamServer(connection, self.handler.stream_server_handle)
#         logger.info("FTP server started on: {}".format(connection))
#         self.server.serve_forever()
# 
#     def stop(self):
#         logger.debug("Stopping FTP server")
#         self.server.stop()
#         del self.handler
# 
# Path: conpot/protocols/ftp/ftp_utils.py
# class FTPException(Exception):
# class FTPMaxLoginAttemptsExceeded(FTPException):
# class FTPPrivilegeException(FTPException):
# def get_data_from_iter(iterator):
# 
# Path: conpot/utils/networking.py
# def sanitize_file_name(name, host, port):
#     """
#     Ensure that file_name is legal. Slug the filename and store it onto the server.
#     This would ensure that there are no duplicates as far as writing a file is concerned. Also client addresses are
#     noted so that one can verify which client uploaded the file.
#     :param name: Name of the file
#     :param host: host/client address
#     :param port port/client port
#     :type name: str
#     """
#     return (
#         "("
#         + host
#         + ", "
#         + str(port)
#         + ")-"
#         + datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#         + "-"
#         + slugify(name)
#     )


# Path: conpot/tests/test_ftp.py
from gevent import monkey
from datetime import datetime
from tempfile import NamedTemporaryFile
from freezegun import freeze_time
from conpot.protocols.ftp.ftp_server import FTPServer
from conpot.protocols.ftp.ftp_utils import ftp_commands
from conpot.utils.greenlet import spawn_test_server, teardown_test_server
from conpot.utils.networking import sanitize_file_name
import unittest
import os
import conpot
import conpot.core as conpot_core
import ftplib  # Use ftplib's client for more authentic testing

        buffer = ""
        with open(_path + "/ftp_testing_retr.txt", mode="r") as _file:
            buffer += _file.readline()
        self.assertEqual(buffer, "This is just a test file for Conpot's FTP server\n")
        os.remove(_path + "/ftp_testing_retr.txt")

    def test_rein(self):
        self.client_init()
        self.assertEqual(self.client.sendcmd("rein"), "230 Ready for new user.")
        self.assertRaisesRegex(
            ftplib.error_perm,
            "503 Login with USER first.",
            self.client.sendcmd,
            "pass testing",
        )
        # TODO: Add test with existing transfer in progress.

    @freeze_time("2018-07-15 17:51:17")
    def test_stor(self):
        # let us test by uploading a file called ftp_testing.txt
        self.client_init()
        _path = os.path.join(
            "".join(conpot.__path__), "tests", "data", "test_data_fs", "ftp"
        )
        with open(_path + "/ftp_testing.txt", mode="rb") as _file:
            self.client.storbinary("stor ftp_testing_stor.txt", _file)
        self.assertIn(
            "ftp_testing_stor.txt", self.ftp_server.handler.config.vfs.listdir("/")
        )
        self.vfs.remove("ftp_testing_stor.txt")

        _data_fs_file = sanitize_file_name(