Skip to content

Commit

Permalink
Merge pull request #1419 from p-l-/enh-sort-nets
Browse files Browse the repository at this point in the history
CLI/utils: support networks in ivre sort
  • Loading branch information
p-l- committed Aug 16, 2022
2 parents 11febf3 + 4adf71b commit eef0907
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ivre/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,12 @@ def key_sort_dom_addr(value: str) -> List[str]:
return [encode_hex(ip2bin(value)).decode()]
# IPv4 addresses before IPv6
return ["00000000000000000000000000000000%08x" % ip2int(value)]
if NETADDR.search(value):
addr, mask = value.split("/", 1)
if ":" in addr:
return [encode_hex(ip2bin(addr)).decode(), mask]
# IPv4 addresses before IPv6
return ["00000000000000000000000000000000%08x" % ip2int(addr), mask]
return value.strip().split(".")[::-1]


Expand Down
24 changes: 21 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4708,19 +4708,37 @@ def is_prime(n):
self.assertTrue(isinstance(json.loads(line), dict))

with tempfile.NamedTemporaryFile(delete=False) as fdesc:
fdesc.write(b"ivre.rocks\ngithub.com\n::1\n127.0.0.1\nivre.rocks\n")
fdesc.write(
b"ivre.rocks\ngithub.com\n::1\n127.0.0.1\nivre.rocks\n127.1.0.0/16\n127.1.0.0/24"
)
res, out, err = RUN(["ivre", "sort"], stdin=open(fdesc.name, "rb"))
self.assertEqual(res, 0)
self.assertFalse(err)
self.assertEqual(
out.splitlines(),
[b"127.0.0.1", b"::1", b"github.com", b"ivre.rocks", b"ivre.rocks"],
[
b"127.0.0.1",
b"127.1.0.0/16",
b"127.1.0.0/24",
b"::1",
b"github.com",
b"ivre.rocks",
b"ivre.rocks",
],
)
res, out, err = RUN(["ivre", "sort", "-u"], stdin=open(fdesc.name, "rb"))
self.assertEqual(res, 0)
self.assertFalse(err)
self.assertEqual(
out.splitlines(), [b"127.0.0.1", b"::1", b"github.com", b"ivre.rocks"]
out.splitlines(),
[
b"127.0.0.1",
b"127.1.0.0/16",
b"127.1.0.0/24",
b"::1",
b"github.com",
b"ivre.rocks",
],
)
os.unlink(fdesc.name)

Expand Down

0 comments on commit eef0907

Please sign in to comment.