Skip to content
Snippets Groups Projects
regression.uts 397 KiB
Newer Older
Phil's avatar
Phil committed
% 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

gpotter2's avatar
gpotter2 committed
IP().src

Phil's avatar
Phil committed
= List layers
~ conf command
ls()

= List commands
~ conf command
lsc()

= List contribs
def test_list_contrib():
    with ContextManagerCaptureOutput() as cmco:
        list_contrib()
        result_list_contrib = cmco.get_output()
    assert("http2               : HTTP/2 (RFC 7540, RFC 7541)              status=loads" in result_list_contrib)
    assert(len(result_list_contrib.split('\n')) > 40)
Phil's avatar
Phil committed
= Configuration
~ conf
conf.debug_dissector = True
Phil's avatar
Phil committed


############
############
+ Scapy functions tests

= Interface related functions

get_if_raw_hwaddr(conf.iface)

bytes_hex(get_if_raw_addr(conf.iface))
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"] = "{1XX00000-X000-0X0X-X00X-00XXXX000XXX}"
        data["invalid"] = True
        return NetworkInterface(data)
    else:
        return "dummy0"

get_if_raw_addr(get_dummy_interface())
get_if_list()

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)
    if iflist == [LOOPBACK_NAME]:
gpotter2's avatar
gpotter2 committed
        len(routes6) == 1
    elif len(iflist) >= 2:
gpotter2's avatar
gpotter2 committed
        len(routes6) >= 3
gpotter2's avatar
gpotter2 committed
        False
    # 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([r for r in routes6 if r[0] == "::1" and r[-1] == ["::1"]]) >= 1)
    if len(iflist) >= 2:
        assert(len([r for r in routes6 if r[0] == "fe80::" and r[1] == 64]) >= 1)
        len([r for r in routes6 if in6_islladdr(r[0]) and r[1] == 128 and r[-1] == ["::1"]]) >= 1
= Test ifchange()
conf.route6.ifchange(LOOPBACK_NAME, "::1/128")
True

gpotter2's avatar
gpotter2 committed
############
############
+ Main.py tests

= Pickle and unpickle a packet
import scapy.modules.six as six
a = IP(dst="192.168.0.1")/UDP()
b = six.moves.cPickle.dumps(a)
c = six.moves.cPickle.loads(b)
assert c[IP].dst == "192.168.0.1"
assert str(c) == str(a)
from scapy.main import _usage
try:
    _usage()
    assert False
except SystemExit:
    assert True

= Session test

# This is automatic when using the console
def get_var(var):
    return six.moves.builtins.__dict__["scapy_session"][var]

def set_var(var, value):
    six.moves.builtins.__dict__["scapy_session"][var] = value

def del_var(var):
    del(six.moves.builtins.__dict__["scapy_session"][var])

init_session(None, {"init_value": 123})
set_var("test_value", "8.8.8.8") # test_value = "8.8.8.8"
save_session()
del_var("test_value")
load_session()
update_session()
assert get_var("test_value") == "8.8.8.8" #test_value == "8.8.8.8"
assert get_var("init_value") == 123
 
= Session test with fname
init_session("scapySession2")
set_var("test_value", IP(dst="192.168.0.1")) # test_value = IP(dst="192.168.0.1")
save_session(fname="scapySession1.dat")
del_var("test_value")
gpotter2's avatar
gpotter2 committed

set_var("z", True) #z = True
load_session(fname="scapySession1.dat")
try:
    get_var("z")
    assert False
except:
    pass
gpotter2's avatar
gpotter2 committed

set_var("z", False) #z = False
update_session(fname="scapySession1.dat")
assert get_var("test_value").dst == "192.168.0.1" #test_value.dst == "192.168.0.1"
assert not get_var("z")
gpotter2's avatar
gpotter2 committed

= Clear session files
gpotter2's avatar
gpotter2 committed

os.remove("scapySession1.dat")
gpotter2's avatar
gpotter2 committed

= Test temporary file creation
tmpfile = get_temp_file(autoext=".ut")
    assert("scapy" in tmpfile and tmpfile.lower().startswith('c:\\users\\appveyor\\appdata\\local\\temp'))
    BYPASS_TMP = platform.python_implementation().lower() == "pypy" or DARWIN
    assert("scapy" in tmpfile and (BYPASS_TMP == True or "/tmp/" in tmpfile))
assert(conf.temp_files[0].endswith(".ut"))
scapy_delete_temp_files()
assert(len(conf.temp_files) == 0)
= Emulate interact()

import mock, sys
try:
    import IPython
except:
    code_interact_import = "scapy.main.code.interact"
Loading
Loading full blame...