1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
% Regression tests for Scapy random objects
############
############
+ Random objects
= RandomEnumeration
ren = RandomEnumeration(0, 7, seed=0x2807, forever=False)
[x for x in ren] == [5, 0, 2, 7, 6, 3, 1, 4]
= RandIP6
random.seed(0x2807)
r6 = RandIP6()
assert r6 == "240b:238f:b53f:b727:d0f9:bfc4:2007:e265"
assert r6.command() == "RandIP6()"
random.seed(0x2807)
r6 = RandIP6("2001:db8::-")
assert r6 == "2001:0db8::b53f"
assert r6.command() == "RandIP6(ip6template='2001:db8::-')"
r6 = RandIP6("2001:db8::*")
assert r6 == "2001:0db8::bfc4"
assert r6.command() == "RandIP6(ip6template='2001:db8::*')"
= RandMAC
random.seed(0x2807)
rm = RandMAC()
assert rm == "24:23:b5:b7:d0:bf"
assert rm.command() == "RandMAC()"
rm = RandMAC("00:01:02:03:04:0-7")
assert rm == "00:01:02:03:04:01"
assert rm.command() == "RandMAC(template='00:01:02:03:04:0-7')"
= RandOID
random.seed(0x2807)
rand_obj = RandOID()
assert rand_obj == "7.222.44.194.276.116.320.6.84.97.31.5.25.20.13.84.104.18"
assert rand_obj.command() == "RandOID()"
rand_obj = RandOID("1.2.3.*")
assert rand_obj == "1.2.3.41"
assert rand_obj.command() == "RandOID(fmt='1.2.3.*')"
rand_obj = RandOID("1.2.3.0-28")
assert rand_obj == "1.2.3.12"
assert rand_obj.command() == "RandOID(fmt='1.2.3.0-28')"
rand_obj = RandOID("1.2.3.0-28", depth=RandNumExpo(0.2), idnum=RandNumExpo(0.02))
assert rand_obj.command() == "RandOID(fmt='1.2.3.0-28', depth=RandNumExpo(lambd=0.2), idnum=RandNumExpo(lambd=0.02))"
= RandRegExp
~ not_pyannotate
random.seed(0x2807)
rex = RandRegExp("[g-v]* @? [0-9]{3} . (g|v)")
bytes(rex) == b'irrtv @ 517 \xc2\xb8 v'
assert rex.command() == "RandRegExp(regexp='[g-v]* @? [0-9]{3} . (g|v)')"
rex = RandRegExp("[:digit:][:space:][:word:]")
assert re.match(b"\\d\\s\\w", bytes(rex))
= Corrupted(Bytes|Bits)
random.seed(0x2807)
cb = CorruptedBytes("ABCDE", p=0.5)
assert cb.command() == "CorruptedBytes(s='ABCDE', p=0.5)"
assert sane(raw(cb)) in [".BCD)", "&BCDW"]
cb = CorruptedBits("ABCDE", p=0.2)
assert cb.command() == "CorruptedBits(s='ABCDE', p=0.2)"
assert sane(raw(cb)) in ["ECk@Y", "QB.P."]
= RandEnumKeys
random.seed(0x2807)
rek = RandEnumKeys({'a': 1, 'b': 2, 'c': 3}, seed=0x2807)
rek.enum.sort()
assert rek.command() == "RandEnumKeys(enum=['a', 'b', 'c'], seed=10247)"
r = str(rek)
assert r == 'a'
= RandSingNum
random.seed(0x2807)
rs = RandSingNum(-28, 7)
assert rs._fix() in [2, 3]
assert rs.command() == "RandSingNum(mn=-28, mx=7)"
= Rand*
random.seed(0x2807)
rss = RandSingString()
assert rss == "foo.exe:"
assert rss.command() == "RandSingString()"
random.seed(0x2807)
rts = RandTermString(4, "scapy")
assert sane(raw(rts)) in ["...Zscapy", "$#..scapy"]
assert rts.command() == "RandTermString(size=4, term=b'scapy')"
= RandInt (test __bool__)
a = "True" if RandNum(False, True) else "False"
assert a in ["True", "False"]
= Various volatiles
random.seed(0x2807)
rng = RandNumGamma(1, 42)
assert rng._fix() in (8, 73)
assert rng.command() == "RandNumGamma(alpha=1, beta=42)"
random.seed(0x2807)
rng = RandNumGauss(1, 42)
assert rng._fix() == 8
assert rng.command() == "RandNumGauss(mu=1, sigma=42)"
renum = RandEnum(1, 42, seed=0x2807)
assert renum == 37
assert renum.command() == "RandEnum(min=1, max=42, seed=10247)"
rp = RandPool((IncrementalValue(), 42), (IncrementalValue(), 0))
assert rp == 0
assert rp.command() == "RandPool((IncrementalValue(), 42), (IncrementalValue(), 0))"
de = DelayedEval("3 + 1")
assert de == 4
assert de.command() == "DelayedEval(expr='3 + 1')"
v = IncrementalValue(restart=2)
assert v == 0 and v == 1 and v == 2 and v == 0
assert v.command() == "IncrementalValue(restart=2)"
|