2019-11-22 23:18:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
proxy.py
|
|
|
|
~~~~~~~~
|
|
|
|
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
|
|
|
|
Network monitoring, controls & Application development, testing, debugging.
|
|
|
|
|
|
|
|
:copyright: (c) 2013-present by Abhinav Singh and contributors.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
2021-11-08 16:52:23 +00:00
|
|
|
import os
|
2019-10-30 03:41:39 +00:00
|
|
|
import socket
|
2021-11-08 16:52:23 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2019-10-30 03:41:39 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2021-11-08 16:52:23 +00:00
|
|
|
from proxy.common.utils import bytes_
|
2021-11-07 17:32:34 +00:00
|
|
|
from proxy.common.flag import FlagParser
|
2019-10-30 03:41:39 +00:00
|
|
|
from proxy.core.acceptor import AcceptorPool
|
|
|
|
|
|
|
|
|
|
|
|
class TestAcceptorPool(unittest.TestCase):
|
|
|
|
|
2021-11-08 16:52:23 +00:00
|
|
|
@mock.patch('os.remove')
|
|
|
|
@mock.patch('os.path.exists')
|
|
|
|
@mock.patch('builtins.open')
|
2019-12-02 23:55:08 +00:00
|
|
|
@mock.patch('proxy.core.acceptor.pool.send_handle')
|
2019-10-30 03:41:39 +00:00
|
|
|
@mock.patch('multiprocessing.Pipe')
|
|
|
|
@mock.patch('socket.socket')
|
2019-12-02 23:55:08 +00:00
|
|
|
@mock.patch('proxy.core.acceptor.pool.Acceptor')
|
2019-10-30 03:41:39 +00:00
|
|
|
def test_setup_and_shutdown(
|
|
|
|
self,
|
2019-12-02 23:55:08 +00:00
|
|
|
mock_acceptor: mock.Mock,
|
2019-10-30 03:41:39 +00:00
|
|
|
mock_socket: mock.Mock,
|
|
|
|
mock_pipe: mock.Mock,
|
2021-11-04 11:28:36 +00:00
|
|
|
mock_send_handle: mock.Mock,
|
2021-11-08 16:52:23 +00:00
|
|
|
mock_open: mock.Mock,
|
|
|
|
mock_exists: mock.Mock,
|
|
|
|
mock_remove: mock.Mock,
|
2021-11-04 11:28:36 +00:00
|
|
|
) -> None:
|
2019-12-02 23:55:08 +00:00
|
|
|
acceptor1 = mock.MagicMock()
|
|
|
|
acceptor2 = mock.MagicMock()
|
|
|
|
mock_acceptor.side_effect = [acceptor1, acceptor2]
|
2019-10-30 03:41:39 +00:00
|
|
|
|
|
|
|
num_workers = 2
|
2021-11-08 16:52:23 +00:00
|
|
|
pid_file = os.path.join(tempfile.gettempdir(), 'pid')
|
2019-10-30 03:41:39 +00:00
|
|
|
sock = mock_socket.return_value
|
2021-11-08 20:04:17 +00:00
|
|
|
flags = FlagParser.initialize(
|
|
|
|
num_workers=2, pid_file=pid_file, threaded=True,
|
|
|
|
)
|
2019-10-30 03:41:39 +00:00
|
|
|
|
2021-11-09 22:57:40 +00:00
|
|
|
pool = AcceptorPool(flags=flags, executor_queues=[], executor_pids=[])
|
2019-12-02 23:55:08 +00:00
|
|
|
pool.setup()
|
|
|
|
mock_send_handle.assert_called()
|
2019-10-30 03:41:39 +00:00
|
|
|
|
|
|
|
mock_socket.assert_called_with(
|
2019-12-02 23:55:08 +00:00
|
|
|
socket.AF_INET6 if pool.flags.hostname.version == 6 else socket.AF_INET,
|
2021-11-04 11:28:36 +00:00
|
|
|
socket.SOCK_STREAM,
|
2019-10-30 03:41:39 +00:00
|
|
|
)
|
|
|
|
sock.setsockopt.assert_called_with(
|
2021-11-04 11:28:36 +00:00
|
|
|
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1,
|
|
|
|
)
|
2019-10-30 03:41:39 +00:00
|
|
|
sock.bind.assert_called_with(
|
2021-11-04 11:28:36 +00:00
|
|
|
(str(pool.flags.hostname), 8899),
|
|
|
|
)
|
2019-12-02 23:55:08 +00:00
|
|
|
sock.listen.assert_called_with(pool.flags.backlog)
|
2019-10-30 03:41:39 +00:00
|
|
|
sock.setblocking.assert_called_with(False)
|
|
|
|
|
|
|
|
self.assertTrue(mock_pipe.call_count, num_workers)
|
2019-12-02 23:55:08 +00:00
|
|
|
self.assertTrue(mock_acceptor.call_count, num_workers)
|
|
|
|
acceptor1.start.assert_called()
|
|
|
|
acceptor2.start.assert_called()
|
|
|
|
acceptor1.join.assert_not_called()
|
|
|
|
acceptor2.join.assert_not_called()
|
2019-10-30 03:41:39 +00:00
|
|
|
|
|
|
|
sock.close.assert_called()
|
|
|
|
|
2019-12-02 23:55:08 +00:00
|
|
|
pool.shutdown()
|
2021-11-08 16:52:23 +00:00
|
|
|
|
|
|
|
mock_open.assert_called_with(pid_file, 'wb')
|
|
|
|
mock_open.return_value.__enter__.return_value.write.assert_called_with(
|
|
|
|
bytes_(os.getpid()),
|
|
|
|
)
|
|
|
|
mock_exists.assert_called_with(pid_file)
|
|
|
|
mock_remove.assert_called_with(pid_file)
|
|
|
|
|
2019-12-02 23:55:08 +00:00
|
|
|
acceptor1.join.assert_called()
|
|
|
|
acceptor2.join.assert_called()
|