Skip to content
Snippets Groups Projects
regression.uts 329 KiB
Newer Older
Florian Maury's avatar
Florian Maury committed
assert('+' in x)
assert('*' in x)
assert('A' not in x)
assert('B' not in x)
try:
    x = f.any2i(mp, {'A'})
    ret = False
except AssertionError:
    ret = True

assert(ret)
#Following test demonstrate a non-sensical yet acceptable usage :(
x = f.any2i(None, {'Toto'})
assert('Toto' in x)

= Test calls on MultiFlagsField.i2m
~ multiflagsfield

import collections
MockPacket = collections.namedtuple('MockPacket', ['type'])

f = MultiFlagsField('flags', set(), 3, {
        0: {
            0: MultiFlagsEntry('A', 'OptionA'),
            1: MultiFlagsEntry('B', 'OptionB'),
        },
        1: {
            0: MultiFlagsEntry('+', 'Plus'),
            1: MultiFlagsEntry('*', 'Star'),
        },
    },
    depends_on=lambda x: x.type
)

mp = MockPacket(0)
x = f.i2m(mp, set())
assert(isinstance(x, (int, long)))
assert(x == 0)
x = f.i2m(mp, {'A'})
assert(isinstance(x, (int, long)))
assert(x == 1)
x = f.i2m(mp, {'A', 'B'})
assert(isinstance(x, (int, long)))
assert(x == 3)
x = f.i2m(mp, {'A', 'B', 'bit 2'})
assert(isinstance(x, (int, long)))
assert(x == 7)
try:
    x = f.i2m(mp, {'+'})
    ret = False
except:
    ret = True

assert(ret)

= Test calls on MultiFlagsField.m2i
~ multiflagsfield

import collections
MockPacket = collections.namedtuple('MockPacket', ['type'])

f = MultiFlagsField('flags', set(), 3, {
        0: {
            0: MultiFlagsEntry('A', 'OptionA'),
            1: MultiFlagsEntry('B', 'OptionB'),
        },
        1: {
            0: MultiFlagsEntry('+', 'Plus'),
            1: MultiFlagsEntry('*', 'Star'),
        },
    },
    depends_on=lambda x: x.type
)

mp = MockPacket(0)
x = f.m2i(mp, 2)
assert(isinstance(x, set))
assert(len(x) == 1)
assert('B' in x)
assert('A' not in x)
assert('*' not in x)

x = f.m2i(mp, 7)
assert(isinstance(x, set))
assert('B' in x)
assert('A' in x)
assert('bit 2' in x)
assert('*' not in x)
assert('+' not in x)
x = f.m2i(mp, 0)
assert(len(x) == 0)
mp = MockPacket(1)
x = f.m2i(mp, 2)
assert(isinstance(x, set))
assert(len(x) == 1)
assert('*' in x)
assert('+' not in x)
assert('B' not in x)

= Test calls on MultiFlagsField.i2repr
~ multiflagsfield

import collections, re
MockPacket = collections.namedtuple('MockPacket', ['type'])

f = MultiFlagsField('flags', set(), 3, {
        0: {
            0: MultiFlagsEntry('A', 'OptionA'),
            1: MultiFlagsEntry('B', 'OptionB'),
        },
        1: {
            0: MultiFlagsEntry('+', 'Plus'),
            1: MultiFlagsEntry('*', 'Star'),
        },
    },
    depends_on=lambda x: x.type
)

mp = MockPacket(0)
x = f.i2repr(mp, {'A', 'B'})
assert(re.match(r'^.*OptionA \(A\).*$', x) is not None)
assert(re.match(r'^.*OptionB \(B\).*$', x) is not None)
mp = MockPacket(1)
x = f.i2repr(mp, {'*', '+', 'bit 2'})
assert(re.match(r'^.*Star \(\*\).*$', x) is not None)
assert(re.match(r'^.*Plus \(\+\).*$', x) is not None)
assert(re.match(r'^.*bit 2.*$', x) is not None)

###########################################################################################################
+ Test correct conversion from binary to string of IPv6 addresses

= IPv6 bin to string conversion - all zero bytes
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' # All zero
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '::')

= IPv6 bin to string conversion - non-compressable
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77\x88\x88' # Not compressable
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '1111:2222:3333:4444:5555:6666:7777:8888')

= IPv6 bin to string conversion - Zero-block right
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x00\x00\x00\x00\x00\x00' # Zeroblock right
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '1111:2222:3333:4444:5555::')

= IPv6 bin to string conversion - Zero-block left
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x00\x00\x00\x00\x00\x00\x44\x44\x55\x55\x66\x66\x77\x77\x88\x88' # Zeroblock left
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '::4444:5555:6666:7777:8888')

= IPv6 bin to string conversion - Two zero-block with different length
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x00\x00\x00\x00\x33\x33\x44\x44\x00\x00\x00\x00\x00\x00\x88\x88' # Short and long zero block
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '0:0:3333:4444::8888')

= IPv6 bin to string conversion - Address 1::
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' # only 1 on the left
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '1::')

= IPv6 bin to string conversion - Address ::1
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' # loopback
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '::1')

= IPv6 bin to string conversion - Zero-block of length 1
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x00\x00\x88\x88' # only one zero block
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == '1111:2222:3333:4444:5555:6666:0:8888')
# On Mac OS socket.inet_ntop is not fully compliant with RFC 5952 and shortens the single zero block to '::'. Still
# this is a valid IPv6 address representation.
assert(compressed2 in ('1111:2222:3333:4444:5555:6666:0:8888', '1111:2222:3333:4444:5555:6666::8888'))

= IPv6 bin to string conversion - Two zero-blocks with equal length
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x11\x11\x00\x00\x00\x00\x44\x44\x00\x00\x00\x00\x77\x77\x88\x88' # two zero blocks of equal length
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '1111::4444:0:0:7777:8888')

= IPv6 bin to string conversion - Leading zero suppression
from scapy.pton_ntop import _ipv6_bin_to_str, inet_ntop
import socket
address=b'\x10\x00\x02\x00\x00\x30\x00\x04\x00\x05\x00\x60\x07\x00\x80\x00' # Leading zero suppression
compressed1, compressed2 = _ipv6_bin_to_str(address), inet_ntop(socket.AF_INET6, address)
assert(compressed1 == compressed2 == '1000:200:30:4:5:60:700:8000')


############
############

+ VRRP tests

= VRRP - build
s = str(IP()/VRRP())
s == 'E\x00\x00$\x00\x01\x00\x00@p|g\x7f\x00\x00\x01\x7f\x00\x00\x01!\x01d\x00\x00\x01z\xfd\x00\x00\x00\x00\x00\x00\x00\x00'

= VRRP - dissection
p = IP(s)
VRRP in p and p[VRRP].chksum == 0x7afd


############
############

+ L2TP tests

= L2TP - build
s = str(IP()/UDP()/L2TP())
s == 'E\x00\x00*\x00\x01\x00\x00@\x11|\xc0\x7f\x00\x00\x01\x7f\x00\x00\x01\x06\xa5\x06\xa5\x00\x16\xf4e\x00\x02\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

= L2TP - dissection
p = IP(s)
L2TP in p and p[L2TP].len == 14 and p.tunnel_id == 0 and p[UDP].chksum == 0xf465


############
############

+ HSRP tests

= HSRP - build & dissection
defaddr = conf.route.route('0.0.0.0')[1]
pkt = IP(str(IP()/UDP(dport=1985, sport=1985)/HSRP()/HSRPmd5()))
assert pkt[IP].dst == "224.0.0.2" and pkt[UDP].sport == pkt[UDP].dport == 1985
assert pkt[HSRP].opcode == 0 and pkt[HSRP].state == 16
assert pkt[HSRPmd5].type == 4 and pkt[HSRPmd5].sourceip == defaddr


############
############

+ RIP tests

= RIP - build
s = str(IP()/UDP(sport=520)/RIP()/RIPEntry()/RIPAuth(authtype=2, password="scapy"))
s == 'E\x00\x00H\x00\x01\x00\x00@\x11|\xa2\x7f\x00\x00\x01\x7f\x00\x00\x01\x02\x08\x02\x08\x004\xae\x99\x01\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xff\x00\x02scapy\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

= RIP - dissection
p = IP(s)
RIPEntry in p and RIPAuth in p and p[RIPAuth].password.startswith("scapy")


############
############

+ Radius tests

= Radius - build
s = str(IP()/UDP(sport=1812)/Radius(authenticator="scapy")/RadiusAttribute(value="scapy"))
s == 'E\x00\x007\x00\x01\x00\x00@\x11|\xb3\x7f\x00\x00\x01\x7f\x00\x00\x01\x07\x14\x07\x15\x00#U\xb2\x01\x00\x00\x1bscapy\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x07scapy'

= Radius - dissection
p = IP(s)
Radius in p and len(p[Radius].attributes) == 1 and p[Radius].attributes[0].value == "scapy"


############
############
+ Addresses generators

= Net

n1 = Net("192.168.0.0/31")
[ip for ip in n1] == ["192.168.0.0", "192.168.0.1"]

n2 = Net("192.168.0.*")
len([ip for ip in n2]) == 256

n3 = Net("192.168.0.1-5")
len([ip for ip in n3]) == 5

(n1 == n3) == False

(n3 in n2) == True

= OID

oid = OID("1.2.3.4.5.6-8")
len([ o for o in oid ]) == 3

= Net6

n1 = Net6("2001:db8::/127")
len([ip for ip in n1]) == 2


############
############
+ IPv6 helpers

= in6_getLocalUniquePrefix()

p = in6_getLocalUniquePrefix()
len(p) == 16 and p.startswith("fd")

= Misc addresses manipulation functions

teredoAddrExtractInfo("2001:0:0a0b:0c0d:0028:f508:f508:08f5") == ("10.11.12.13", 40, "10.247.247.10", 2807)

in6_iseui64("fe80::bae8:58ff:fed4:e5f6") == True

in6_isanycast("2001:db8::fdff:ffff:ffff:ff80") == True

a = inet_pton(socket.AF_INET6, "2001:db8::2807")
in6_xor(a, a) == "\x00" * 16

a = inet_pton(socket.AF_INET6, "fe80::bae8:58ff:fed4:e5f6")
r = inet_ntop(socket.AF_INET6, in6_getnsma(a)) 
r == "ff02::1:ffd4:e5f6"

in6_isllsnmaddr(r) == True

in6_isdocaddr("2001:db8::2807") == True

in6_isaddrllallnodes("ff02::1") == True

in6_isaddrllallservers("ff02::2") == True

= in6_getscope()

in6_getscope("2001:db8::2807") == IPV6_ADDR_GLOBAL
in6_getscope("fec0::2807") == IPV6_ADDR_SITELOCAL
in6_getscope("fe80::2807") == IPV6_ADDR_LINKLOCAL
in6_getscope("ff02::2807") == IPV6_ADDR_LINKLOCAL
in6_getscope("ff0e::2807") == IPV6_ADDR_GLOBAL
in6_getscope("ff05::2807") == IPV6_ADDR_SITELOCAL
in6_getscope("ff01::2807") == IPV6_ADDR_LOOPBACK
in6_getscope("::1") == IPV6_ADDR_LOOPBACK


############
############
+ Test Route class

= make_route()

r4 = Route()
len_r4 = len(r4.routes)
r4.make_route(host="10.12.13.14") == (168561934, 4294967295L, '0.0.0.0', LOOPBACK_NAME, '0.0.0.0')
r4.make_route(net="10.12.13.0/24") == (168561920, 4294967040L, '0.0.0.0', LOOPBACK_NAME, '0.0.0.0')
r4.make_route(net="10.12.0.0/16", dev="dummy0") == (168558592, 4294901760L, '0.0.0.0', 'dummy0', '0.0.0.0')

= add() & delt()

r4.add(net="192.168.1.0/24", gw="1.2.3.4", dev="dummy0")
len(r4.routes) == len_r4 + 1
r4.delt(net="192.168.1.0/24", gw="1.2.3.4", dev="dummy0")
len(r4.routes) == len_r4

= ifchange()

r4.add(net="192.168.1.0/24", gw="1.2.3.4", dev="dummy0")
r4.ifchange("dummy0", "5.6.7.8")
r4.routes[-1][-1] == "5.6.7.8"

= ifdel()

r4.ifdel("dummy0")
len(r4.routes) == len_r4

= ifadd() & get_if_bcast()

r4.ifadd("dummy0", "1.2.3.4/24")
r4.get_if_bcast("dummy0") == "1.2.3.255"

r4.ifdel("dummy0")
len(r4.routes) == len_r4


############
############
+ Random objects

= RandomEnumeration

re = RandomEnumeration(0, 7, seed=0x2807, forever=False)
[x for x in re] == [3, 4, 2, 5, 1, 6, 0, 7]

= RandIP6

random.seed(0x2807)
r6 = RandIP6()
str(r6) == "d279:1205:e445:5a9f:db28:efc9:afd7:f594"

random.seed(0x2807)
r6 = RandIP6("2001:db8::-") 
print r6 == "2001:0db8::9e9c"

r6 = RandIP6("2001:db8::*")
print r6 == "2001:0db8::9ccb"

= RandMAC

random.seed(0x2807)
rm = RandMAC() 
rm == "12:5a:ef:f5:91:44"

rm = RandMAC("00:01:02:03:04:0-7")
rm == "00:01:02:03:04:01"

= RandOID

random.seed(0x2807)
ro = RandOID()
ro == "7.222.44.194.276.116.320.6.84.97.31.5.25.20.13.84.104.18"

ro = RandOID("1.2.3.*")
ro == "1.2.3.41"

ro = RandOID("1.2.3.0-28")
ro == "1.2.3.11"

= RandRegExp

random.seed(0x2807)
re = RandRegExp("[g-v]* @? [0-9]{3} . (g|v)")
print re == "vmuvr @ 906  g"

= Corrupted(Bytes|Bits)

random.seed(0x2807)
cb = CorruptedBytes("ABCDE", p=0.5)
sane(cb) == "pB..E"

cb = CorruptedBits("ABCDE", p=0.2)
sane(cb) == "E.Kl`"

= Rand*

random.seed(0x2807)
rs = RandSingNum(-28, 07)
print rs == -7

rss = RandSingString()
rss == "CON:"

rek = RandEnumKeys({'a': 1, 'b': 2})
str(rek) == 'b'

rts = RandTermString(4, "scapy")
sane(str(rts)) == "[...scapy"