dymokomi/luce-std
Source of release 0.7.3
0.7.3 / tests / ip_address / reference.py
#!/usr/bin/env python3
"""Write corpus.bin into the directory given: IP parsing and formatting references from
Python's ipaddress, seed 5952, which main.luc hands to check.lucb."""
from pathlib import Path
import ipaddress
import random
import sys
OUTPUT = Path(sys.argv[1])
randomizer = random.Random(5952)
corpus = bytearray()
def add(text):
raw = text.encode("utf-8")
try:
# Interface zones belong to socket endpoints, not bare IP values.
if "%" in text:
raise ValueError("zone")
value = ipaddress.ip_address(text)
canonical = str(value)
if value.version == 6 and value.ipv4_mapped is not None:
canonical = "::ffff:" + str(value.ipv4_mapped)
encoded = canonical.encode("ascii")
corpus.extend((len(raw), value.version, len(encoded)))
corpus.extend(raw)
corpus.extend(encoded)
corpus.extend(value.packed)
except ValueError:
corpus.extend((len(raw), 0, 0))
corpus.extend(raw)
for text in ("", "0.0.0.0", "255.255.255.255", "::", "::1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"2001:db8:0:0:1:0:0:1", "2001:db8:0:1:1:1:1:1", "::ffff:192.0.2.1", "::192.0.2.1",
"1", "127.1", "127.0.1", "01.2.3.4", "1.2.3.256", "1.2.3.4.5", "0x7f.0.0.1",
"1.2.3.4:80", "1.2.3.4\0suffix", "::1\0", " ::1", "::1 ", ":::1", "1::2::3",
"1:2:3:4:5:6:7", "1:2:3:4:5:6:7:8:9", "10000::", "::ffff:001.2.3.4",
"[::1]", "[::1]:80", "fe80::1%lo0", "::1/128", "é::1", "x" * 46):
add(text)
for _ in range(1000):
add(str(ipaddress.IPv4Address(randomizer.getrandbits(32))))
# Every zero-field pattern probes longest runs, ties, single zeroes and boundaries.
for mask in range(256):
words = [0 if mask & (1 << index) else randomizer.randrange(1, 65536) for index in range(8)]
add(":".join(f"{word:04X}" for word in words))
for _ in range(2000):
value = ipaddress.IPv6Address(randomizer.getrandbits(128))
add(value.exploded.upper())
add(value.compressed)
text = value.compressed
position = randomizer.randrange(len(text) + 1)
add(text[:position] + randomizer.choice((":", "g", "0", ".", "\0", " ")) + text[position:])
for _ in range(256):
add("::FFFF:" + str(ipaddress.IPv4Address(randomizer.getrandbits(32))))
(OUTPUT / "corpus.bin").write_bytes(corpus)