# 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
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

monkey.patch_all()


class TestFTPServer(unittest.TestCase):

    """
    All tests are executed in a similar way. We run a valid/invalid FTP request/command and check for valid
    response. Testing is done by sending/receiving files in data channel related commands.
    Implementation Note: There are no explicit tests for active/passive mode. These are covered in list and nlst
    tests
    """

    def setUp(self):
        conpot_core.initialize_vfs()


# 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

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

monkey.patch_all()


class TestFTPServer(unittest.TestCase):

    """
    All tests are executed in a similar way. We run a valid/invalid FTP request/command and check for valid
    response. Testing is done by sending/receiving files in data channel related commands.
    Implementation Note: There are no explicit tests for active/passive mode. These are covered in list and nlst
    tests
    """

    def setUp(self):
        conpot_core.initialize_vfs()


        self.ftp_server, self.greenlet = spawn_test_server(FTPServer, "default", "ftp")