Merge branch 'master' into master-2
# Conflicts: # LICENSE
This commit is contained in:
10
test/service/webservice/cgi-bin/file_content_response.py
Executable file
10
test/service/webservice/cgi-bin/file_content_response.py
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
|
||||
response_file_path = "cgi-bin/test.html"
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
7
test/service/webservice/cgi-bin/test.html
Normal file
7
test/service/webservice/cgi-bin/test.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTML</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
9
test/service/webservice/cgi-bin/test.py
Executable file
9
test/service/webservice/cgi-bin/test.py
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
print("""
|
||||
<html>
|
||||
<Title>Hello in HTML</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html> """)
|
||||
7
test/service/webservice/cgi-bin/testA.html
Normal file
7
test/service/webservice/cgi-bin/testA.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLA</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
7
test/service/webservice/cgi-bin/testB.html
Normal file
7
test/service/webservice/cgi-bin/testB.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLB</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
7
test/service/webservice/cgi-bin/testC.html
Normal file
7
test/service/webservice/cgi-bin/testC.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<Title>Hello in HTMLC</Title>
|
||||
<body>
|
||||
<p>Hello There!</p>
|
||||
<p><b>Hi There!</b></p>
|
||||
</body>
|
||||
</html>
|
||||
15
test/service/webservice/file_content_response.py
Executable file
15
test/service/webservice/file_content_response.py
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
import cgi
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import RESPONSE_PATH_PARAMETER_KEY_NAME
|
||||
|
||||
response_file_path = cgi.FieldStorage()[RESPONSE_PATH_PARAMETER_KEY_NAME].value
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
|
||||
|
||||
15
test/service/webservice/pybin/file_content_response.py
Executable file
15
test/service/webservice/pybin/file_content_response.py
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
__author__ = 'dev'
|
||||
import cgi
|
||||
|
||||
from fake_services.service.webservice.fake_http_request_handler import RESPONSE_PATH_PARAMETER_KEY_NAME
|
||||
|
||||
response_file_path = cgi.FieldStorage()[RESPONSE_PATH_PARAMETER_KEY_NAME].value
|
||||
response_file = open(response_file_path, "r")
|
||||
response_file_content = response_file.read()
|
||||
response_file.close()
|
||||
|
||||
print("\n")
|
||||
print(response_file_content)
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"":
|
||||
{
|
||||
"host_pattern": "10.0.0.1",
|
||||
"path_pattern": "index.php",
|
||||
"protocol_pattern": "http",
|
||||
"response_content_path": "cgi-bin/test.html"
|
||||
}
|
||||
}
|
||||
156
test/service/webservice/test_fake_web_server.py
Normal file
156
test/service/webservice/test_fake_web_server.py
Normal file
@@ -0,0 +1,156 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
import os
|
||||
import stat
|
||||
import unittest
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
|
||||
from fake_services.service.webservice.fake_web_server_manager import FakeWebServerManager
|
||||
|
||||
|
||||
CGI_TEST_SCRIPT_PATH = "cgi-bin/test.py"
|
||||
CGI_WRONG_SCRIPT_PATH = "cgi"
|
||||
CGI_FILE_SCRIPT_PATH = "cgi-bin/file_content_response.py"
|
||||
CGI_VAR_BIN_SCRIPT_PATH = "pybin/file_content_response.py"
|
||||
SERVER_PORT = 8080
|
||||
TEST_URL = "http://0.0.0.0:" + str(SERVER_PORT)
|
||||
TEST_URL_EXPAND = TEST_URL + "/1/a/3/foo/bar.php"
|
||||
DEFAULT_CONFIG = {
|
||||
"/": [{"response_content_path": "cgi-bin/testA.html"}],
|
||||
"/1/a/3/foo/bar.php":
|
||||
[
|
||||
{
|
||||
"host_pattern": "^0.0.0.0$",
|
||||
"response_content_path": "cgi-bin/testA.html"
|
||||
},
|
||||
{
|
||||
"host_pattern": "0.0.0.0:8080",
|
||||
"response_content_path": "cgi-bin/testB.html"
|
||||
},
|
||||
{
|
||||
"response_content_path": "cgi-bin/testC.html"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
class TestFakeSever(unittest.TestCase):
|
||||
def setUp(self):
|
||||
try:
|
||||
os.chdir("service/webservice")
|
||||
except:
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
os.chdir("../..")
|
||||
except:
|
||||
pass
|
||||
|
||||
def test_moved_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_TEST_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL).read().decode('utf-8')
|
||||
content2 = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertEqual(content, content2)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_not_found_script(self):
|
||||
self.assertRaises(FileNotFoundError, FakeWebServerManager, SERVER_PORT, None, CGI_WRONG_SCRIPT_PATH)
|
||||
FakeWebServerManager(server_port=SERVER_PORT)
|
||||
|
||||
def test_set_executable(self):
|
||||
st = os.stat(CGI_FILE_SCRIPT_PATH)
|
||||
if os.access(CGI_FILE_SCRIPT_PATH, os.X_OK):
|
||||
os.chmod(CGI_FILE_SCRIPT_PATH, st.st_mode ^ stat.S_IEXEC)
|
||||
self.assertFalse(os.access(CGI_FILE_SCRIPT_PATH, os.X_OK))
|
||||
FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
self.assertTrue(os.access(CGI_FILE_SCRIPT_PATH, os.X_OK))
|
||||
|
||||
def test_file_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL).read().decode('utf-8')
|
||||
content2 = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertEqual(content, content2)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_file_var_bin_request(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_file_var_bin_request_post(self):
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, request_handle_script_path=CGI_FILE_SCRIPT_PATH, requests_config=DEFAULT_CONFIG)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
data = urllib.parse.urlencode({'q': 'Status'})
|
||||
content2 = urllib.request.urlopen(TEST_URL_EXPAND, data.encode('utf-8')).read().decode('utf-8')
|
||||
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
self.assertEqual(content, content2)
|
||||
|
||||
def test_config_request(self):
|
||||
config = {
|
||||
"/test_request":
|
||||
[
|
||||
{
|
||||
"host_pattern": "10.0.0.1",
|
||||
"response_content_path": "cgi-bin/test.html"
|
||||
}
|
||||
],
|
||||
"/1/a/3/foo/bar.php":
|
||||
[
|
||||
{
|
||||
"host_pattern": "^0.0.0.0$",
|
||||
"response_content_path": "cgi-bin/testA.html"
|
||||
},
|
||||
{
|
||||
"host_pattern": "0.0.0.0:8080",
|
||||
"response_content_path": "cgi-bin/testB.html"
|
||||
},
|
||||
{
|
||||
"response_content_path": "cgi-bin/testC.html"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=config,
|
||||
request_handle_script_path=CGI_VAR_BIN_SCRIPT_PATH)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
def test_config_request_default_script(self):
|
||||
path = os.path.abspath("cgi-bin/testA.html")
|
||||
config = {
|
||||
"/1/a/3/foo/bar.php":
|
||||
[
|
||||
{
|
||||
"response_content_path": path
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
server = FakeWebServerManager(server_port=SERVER_PORT, requests_config=config)
|
||||
server.start_server()
|
||||
content = urllib.request.urlopen(TEST_URL_EXPAND).read().decode('utf-8')
|
||||
server.stop_server()
|
||||
self.assertNotEqual("", content)
|
||||
self.assertFalse(content.startswith("#"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
73
test/utility/network/test_ip_address_manager.py
Normal file
73
test/utility/network/test_ip_address_manager.py
Normal file
@@ -0,0 +1,73 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
import os
|
||||
import re
|
||||
import unittest
|
||||
|
||||
import fake_services.utility.network.ip_address_manager as ip
|
||||
|
||||
|
||||
FIRST_IP_ADDRESS = "1.1.1.1"
|
||||
SECOND_IP_ADDRESS = "1.1.1.2"
|
||||
NETWORK_DEVICE_NAME = "enp20s0"
|
||||
|
||||
|
||||
class TestIpManager(unittest.TestCase):
|
||||
|
||||
def tearDown(self):
|
||||
ip.remove_ip_address(FIRST_IP_ADDRESS, NETWORK_DEVICE_NAME)
|
||||
ip.remove_ip_address(SECOND_IP_ADDRESS, NETWORK_DEVICE_NAME)
|
||||
|
||||
def test_add_address(self):
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
ip.add_ip_address(FIRST_IP_ADDRESS, NETWORK_DEVICE_NAME)
|
||||
self.assertTrue(self.ping_able(FIRST_IP_ADDRESS))
|
||||
|
||||
def test_add_addresses(self):
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
self.assertFalse(self.ping_able(SECOND_IP_ADDRESS))
|
||||
ip.add_ip_addresses([FIRST_IP_ADDRESS,SECOND_IP_ADDRESS], NETWORK_DEVICE_NAME)
|
||||
self.assertTrue(self.ping_able(FIRST_IP_ADDRESS))
|
||||
self.assertTrue(self.ping_able(SECOND_IP_ADDRESS))
|
||||
|
||||
def test_rm_addresses(self):
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
self.assertFalse(self.ping_able(SECOND_IP_ADDRESS))
|
||||
ip.add_ip_addresses([FIRST_IP_ADDRESS,SECOND_IP_ADDRESS], NETWORK_DEVICE_NAME)
|
||||
self.assertTrue(self.ping_able(FIRST_IP_ADDRESS))
|
||||
self.assertTrue(self.ping_able(SECOND_IP_ADDRESS))
|
||||
ip.remove_ip_addresses([FIRST_IP_ADDRESS,SECOND_IP_ADDRESS], NETWORK_DEVICE_NAME)
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
self.assertFalse(self.ping_able(SECOND_IP_ADDRESS))
|
||||
|
||||
def test_rm_address(self):
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
ip.add_ip_address(FIRST_IP_ADDRESS, NETWORK_DEVICE_NAME)
|
||||
self.assertTrue(self.ping_able(FIRST_IP_ADDRESS))
|
||||
ip.remove_ip_address(FIRST_IP_ADDRESS, NETWORK_DEVICE_NAME)
|
||||
self.assertFalse(self.ping_able(FIRST_IP_ADDRESS))
|
||||
|
||||
def test_massive_adds(self):
|
||||
ip_prefix = "10.0.0."
|
||||
ip_list = []
|
||||
|
||||
for i in range(0, 255):
|
||||
ip_list.append(ip_prefix + (str(i)))
|
||||
|
||||
try:
|
||||
ip.add_ip_addresses(ip_list, NETWORK_DEVICE_NAME)
|
||||
for ip_address in ip_list:
|
||||
self.assertTrue(self.ping_able(ip_address))
|
||||
finally:
|
||||
ip.remove_ip_addresses(ip_list, NETWORK_DEVICE_NAME)
|
||||
|
||||
def ping_able(self, ip_address):
|
||||
cmd = "ping -c1 " + ip_address
|
||||
r = "".join(str(os.popen(cmd).readlines()))
|
||||
if re.search("64 bytes from", r):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user