% Regression tests for Scapy random objects

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

= RandomEnumeration

ren = RandomEnumeration(0, 7, seed=0x2807, forever=False)
[x for x in ren] == ([3, 4, 2, 5, 1, 6, 0, 7] if six.PY2 else [5, 0, 2, 7, 6, 3, 1, 4])

= RandIP6

random.seed(0x2807)
r6 = RandIP6()
assert(r6 == ("d279:1205:e445:5a9f:db28:efc9:afd7:f594" if six.PY2 else
              "240b:238f:b53f:b727:d0f9:bfc4:2007:e265"))
assert(r6.command() == "RandIP6()")

random.seed(0x2807)
r6 = RandIP6("2001:db8::-")
assert(r6 == ("2001:0db8::e445" if six.PY2 else "2001:0db8::b53f"))
assert(r6.command() == "RandIP6(ip6template='2001:db8::-')")

r6 = RandIP6("2001:db8::*")
assert(r6 == ("2001:0db8::efc9" if six.PY2 else "2001:0db8::bfc4"))
assert(r6.command() == "RandIP6(ip6template='2001:db8::*')")

= RandMAC

random.seed(0x2807)
rm = RandMAC()
assert(rm == ("d2:12:e4:5a:db:ef" if six.PY2 else "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:05" if six.PY2 else "00:01:02:03:04:01"))
assert(rm.command() == "RandMAC(template='00:01:02:03:04:0-7')")


= RandOID

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

ro = RandOID("1.2.3.*")
assert(ro == "1.2.3.41")
assert(ro.command() == "RandOID(fmt='1.2.3.*')")

ro = RandOID("1.2.3.0-28")
assert(ro == ("1.2.3.11" if six.PY2 else "1.2.3.12"))
assert(ro.command() == "RandOID(fmt='1.2.3.0-28')")

ro = RandOID("1.2.3.0-28", depth=RandNumExpo(0.2), idnum=RandNumExpo(0.02))
assert(ro.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) == ('vmuvr @ 906 \x9e g' if six.PY2 else 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 == ('c' if six.PY2 else '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 == ("CON:" if six.PY2 else "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=%s'scapy')" % '' if six.PY2 else 'b')

= 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 == (13 if six.PY2 else 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)"