Newer
Older
% Regression tests for Scapy
# More informations at http://www.secdev.org/projects/UTscapy/
############
############
+ Informations on Scapy
= Get conf
~ conf command
* Dump the current configuration
conf
= List layers
~ conf command
ls()
= List commands
~ conf command
lsc()
= List contribs
import mock
result_list_contrib = ""
def test_list_contrib():
def write(s):
global result_list_contrib
result_list_contrib += s
mock_stdout = mock.Mock()
mock_stdout.write = write
bck_stdout = sys.stdout
sys.stdout = mock_stdout
list_contrib()
sys.stdout = bck_stdout
assert("http2 : HTTP/2 (RFC 7540, RFC 7541) status=loads" in result_list_contrib)
assert(result_list_contrib.split('\n') > 40)
test_list_contrib()
conf.debug_dissector = True
############
############
+ Scapy functions tests
= Interface related functions
get_if_raw_hwaddr(conf.iface)
get_if_raw_addr(conf.iface).encode("hex")
def get_dummy_interface():
"""Returns a dummy network interface"""
if WINDOWS:
data = {}
data["name"] = "dummy0"
data["description"] = "Does not exist"
data["win_index"] = -1
data["guid"] = "{0XX00000-X000-0X0X-X00X-00XXXX000XXX}"
data["invalid"] = True
return NetworkInterface(data)
else:
return "dummy0"
get_if_raw_addr(get_dummy_interface())
get_if_raw_addr6(conf.iface6)
= Test read_routes6() - default output
routes6 = read_routes6()
if WINDOWS:
route_add_loopback(routes6, True)
# Expected results:
# - one route if there is only the loopback interface
# - three routes if there is a network interface
if len(routes6):
iflist = get_if_list()
if WINDOWS:
route_add_loopback(ipv6=True, iflist=iflist)
# IPv6 seems disabled. Force a route to ::1
conf.route6.routes.append(("::1", 128, "::", LOOPBACK_NAME, ["::1"]))
True
= Test read_routes6() - check mandatory routes
if len(routes6):
assert(len(filter(lambda r: r[0] == "::1" and r[-1] == ["::1"], routes6)) >= 1)
if iflist >= 2:
assert(len(filter(lambda r: r[0] == "fe80::" and r[1] == 64, routes6)) >= 1)
len(filter(lambda r: in6_islladdr(r[0]) and r[1] == 128 and r[-1] == ["::1"], routes6)) >= 1
= Test ifchange()
conf.route6.ifchange(LOOPBACK_NAME, "::1/128")
True
= UTscapy route check
* Check that UTscapy has correctly replaced the routes. Many tests won't work otherwise
if WINDOWS:
route_add_loopback()
IP().src
assert _ == "127.0.0.1"
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
= Prepare emulator
import mock
from mock import Mock
if WINDOWS:
# This fix when the windows doesn't have a size (run with a windows server)
mock.patch("pyreadline.console.console.Console.size").return_value = (300, 300)
@mock.patch("scapy.config.conf.readfunc")
def emulate_main_input(data, mock_readfunc):
global index
index = 0 # reset var
def readlineScapy(*args, **kargs):
global index
if len(data) == index:
r_data = "exit(1 if hasattr(sys, 'last_value') and sys.last_value is not None else 0)"
else:
r_data = data[index]
index +=1
print r_data
return r_data
mock_readfunc.side_effect = readlineScapy
def reduce_mock(self):
return (Mock, ())
mock_readfunc.__reduce__ = reduce_mock
sys.argv = ['']
def console_exit(code):
raise SystemExit(code)
exit_code = -1
try:
interact(mydict={"exit": console_exit})
except SystemExit as e:
exit_code = str(e)
pass
assert exit_code == "0"
= Test basic save_session, load_session and update_session
data = ["init_session(\"scapySession1\")",\
"test_value = \"8.8.8.8\"",\
"save_session()",\
"del test_value",\
"load_session()",\
"assert test_value == \"8.8.8.8\""]
emulate_main_input(data)
= Test save_session, load_session and update_session with fname
data = ["init_session(\"scapySession2\")",\
"test_value = 7",\
"save_session(fname=\"scapySaveSession.dat\")",\
"del test_value",\
"load_session(fname=\"scapySaveSession.dat\")",\
"update_session(fname=\"scapySaveSession.dat\")",\
"assert test_value == 7"]
emulate_main_input(data)
= Test pickling with packets
data = ["init_session(\"scapySession1\")",\
"test_value = IP(src=\"127.0.0.1\", dst=\"8.8.8.8\")",\
"test_value2 = ICMPv6EchoReply(data=\"testData@%!\")",\
"save_session()",\
"del test_value",\
"load_session()",\
"assert test_value.src == \"127.0.0.1\"",\
"assert test_value.dst == \"8.8.8.8\"",\
"assert test_value2.data == \"testData@%!\""]
emulate_main_input(data)
Loading
Loading full blame...