# Path: replayenhancer/packetcapture.py
# def main(*, runonce=False):
#     """
#     Captures telemetry packets. CTRL+C to exit.
#     runonce exists only for unit testing. Don't ever actually use.
#     """
#     # Create a new UDP socket.
#     udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 
#     # Bind the socket to the port
#     server_address = ("", 5606)
#     print("Starting listener on port {}".format(server_address[1]))
#     udp_socket.bind(server_address)
# 
#     i = 0
#     directory_name = "packetdata-"+datetime.datetime.now().strftime(
#         "%Y%m%d-%H%M%S")
#     try:
#         if not os.path.exists(directory_name):
#             os.makedirs(directory_name)
#         while True:
#             data, _ = udp_socket.recvfrom(65565)
#             print("Writing packet {}".format(i))
#             file = open('./'+directory_name+'/pdata'+str(i), 'wb')
#             file.write(data)
#             file.close()
#             i += 1
# 
#             if runonce:
#                 i = 0
#                 raise KeyboardInterrupt
# 
#     except KeyboardInterrupt:
#         print("Closing listener on port {}".format(server_address[1]))
# 
#     finally:
#         if i == 0:
#             os.rmdir(directory_name)


import sys
import unittest
from unittest.mock import patch, MagicMock, mock_open
from replayenhancer.packetcapture import main as packetcapture

            make_args, _ = mock_os.makedirs.call_args
            self.assertEqual('packetdata', exist_args[0].split('-')[0])
            self.assertEqual('packetdata', make_args[0].split('-')[0])

            mock_udp_socket.recvfrom.called_with(65565)

            args, kwargs = mock_file_open.call_args
            self.assertTrue('wb' in args)
            mock_file = m()
            mock_file.write.assert_called_once_with(mock_data)
            mock_file.close.assert_called_once_with()

            rm_args, _ = mock_os.rmdir.call_args
            self.assertEqual('packetdata', rm_args[0].split('-')[0])

    @unittest.skipIf(sys.version_info >= (3, 5), "Newer version available.")
    @patch('replayenhancer.packetcapture.os', autospec=True)
    @patch('replayenhancer.packetcapture.socket', autospec=True)
    def test_pre35(self, mock_socket, mock_os):
        mock_os.path.exists.return_value = False

        mock_data = MagicMock()

        mock_udp_socket = MagicMock()
        mock_udp_socket.recvfrom.return_value = (mock_data, None)

        mock_socket.socket.return_value = mock_udp_socket

        m = mock_open()
        with patch('builtins.open', m) as mock_file_open:

# Path: replayenhancer/packetcapture.py
# def main(*, runonce=False):
#     """
#     Captures telemetry packets. CTRL+C to exit.
#     runonce exists only for unit testing. Don't ever actually use.
#     """
#     # Create a new UDP socket.
#     udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 
#     # Bind the socket to the port
#     server_address = ("", 5606)
#     print("Starting listener on port {}".format(server_address[1]))
#     udp_socket.bind(server_address)
# 
#     i = 0
#     directory_name = "packetdata-"+datetime.datetime.now().strftime(
#         "%Y%m%d-%H%M%S")
#     try:
#         if not os.path.exists(directory_name):
#             os.makedirs(directory_name)
#         while True:
#             data, _ = udp_socket.recvfrom(65565)
#             print("Writing packet {}".format(i))
#             file = open('./'+directory_name+'/pdata'+str(i), 'wb')
#             file.write(data)
#             file.close()
#             i += 1
# 
#             if runonce:
#                 i = 0
#                 raise KeyboardInterrupt
# 
#     except KeyboardInterrupt:
#         print("Closing listener on port {}".format(server_address[1]))
# 
#     finally:
#         if i == 0:
#             os.rmdir(directory_name)


# Path: test/test_packetcapture.py
import sys
import unittest
from unittest.mock import patch, MagicMock, mock_open
from replayenhancer.packetcapture import main as packetcapture

            make_args, _ = mock_os.makedirs.call_args
            self.assertEqual('packetdata', exist_args[0].split('-')[0])
            self.assertEqual('packetdata', make_args[0].split('-')[0])

            mock_udp_socket.recvfrom.called_with(65565)

            args, kwargs = mock_file_open.call_args
            self.assertTrue('wb' in args)
            mock_file = m()
            mock_file.write.assert_called_once_with(mock_data)
            mock_file.close.assert_called_once_with()

            rm_args, _ = mock_os.rmdir.call_args
            self.assertEqual('packetdata', rm_args[0].split('-')[0])

    @unittest.skipIf(sys.version_info >= (3, 5), "Newer version available.")
    @patch('replayenhancer.packetcapture.os', autospec=True)
    @patch('replayenhancer.packetcapture.socket', autospec=True)
    def test_pre35(self, mock_socket, mock_os):
        mock_os.path.exists.return_value = False

        mock_data = MagicMock()

        mock_udp_socket = MagicMock()
        mock_udp_socket.recvfrom.return_value = (mock_data, None)

        mock_socket.socket.return_value = mock_udp_socket

        m = mock_open()
        with patch('builtins.open', m) as mock_file_open:

            packetcapture(runonce=True)
