Add cmd_paste to flaskpaste plugin (create paste, return URL). Add test suites for encode, hash, defang, cidr, and dns plugins (83 new test cases, 1093 total). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
137 lines
4.2 KiB
Python
137 lines
4.2 KiB
Python
"""Tests for the CIDR calculator plugin."""
|
|
|
|
import asyncio
|
|
import importlib.util
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from derp.irc import Message
|
|
|
|
# plugins/ is not a Python package -- load the module from file path
|
|
_spec = importlib.util.spec_from_file_location(
|
|
"plugins.cidr", Path(__file__).resolve().parent.parent / "plugins" / "cidr.py",
|
|
)
|
|
_mod = importlib.util.module_from_spec(_spec)
|
|
sys.modules[_spec.name] = _mod
|
|
_spec.loader.exec_module(_mod)
|
|
|
|
from plugins.cidr import cmd_cidr # noqa: E402
|
|
|
|
# -- Helpers -----------------------------------------------------------------
|
|
|
|
class _FakeBot:
|
|
def __init__(self):
|
|
self.replied: list[str] = []
|
|
|
|
async def reply(self, message, text: str) -> None:
|
|
self.replied.append(text)
|
|
|
|
|
|
def _msg(text: str) -> Message:
|
|
return Message(
|
|
raw="", prefix="alice!~alice@host", nick="alice",
|
|
command="PRIVMSG", params=["#test", text], tags={},
|
|
)
|
|
|
|
|
|
# -- Info: IPv4 --------------------------------------------------------------
|
|
|
|
class TestCidrInfoIPv4:
|
|
def test_slash24(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 192.168.1.0/24")))
|
|
reply = bot.replied[0]
|
|
assert "net:192.168.1.0/24" in reply
|
|
assert "hosts:254" in reply
|
|
assert "mask:255.255.255.0" in reply
|
|
assert "broadcast:192.168.1.255" in reply
|
|
|
|
def test_slash31_no_broadcast(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 10.0.0.0/31")))
|
|
reply = bot.replied[0]
|
|
assert "hosts:2" in reply
|
|
assert "broadcast" not in reply
|
|
|
|
def test_slash32(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 10.0.0.1/32")))
|
|
reply = bot.replied[0]
|
|
assert "hosts:1" in reply
|
|
assert "broadcast" not in reply
|
|
|
|
def test_range(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 10.0.0.0/30")))
|
|
reply = bot.replied[0]
|
|
assert "range:10.0.0.0-10.0.0.3" in reply
|
|
assert "hosts:2" in reply
|
|
|
|
def test_wildcard(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 172.16.0.0/16")))
|
|
reply = bot.replied[0]
|
|
assert "wildcard:0.0.255.255" in reply
|
|
|
|
|
|
# -- Info: IPv6 --------------------------------------------------------------
|
|
|
|
class TestCidrInfoIPv6:
|
|
def test_ipv6_slash64(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr 2001:db8::/64")))
|
|
reply = bot.replied[0]
|
|
assert "net:2001:db8::/64" in reply
|
|
assert "hosts:" in reply
|
|
# No mask/wildcard/broadcast for IPv6
|
|
assert "mask:" not in reply
|
|
|
|
def test_ipv6_slash128(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr ::1/128")))
|
|
reply = bot.replied[0]
|
|
assert "hosts:1" in reply
|
|
|
|
|
|
# -- Contains ----------------------------------------------------------------
|
|
|
|
class TestCidrContains:
|
|
def test_ip_in_network(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr contains 10.0.0.0/8 10.1.2.3")))
|
|
assert "is in" in bot.replied[0]
|
|
|
|
def test_ip_not_in_network(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr contains 10.0.0.0/8 192.168.1.1")))
|
|
assert "NOT in" in bot.replied[0]
|
|
|
|
def test_invalid_network(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr contains badnet 10.0.0.1")))
|
|
assert "Invalid network" in bot.replied[0]
|
|
|
|
def test_invalid_ip(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr contains 10.0.0.0/8 notanip")))
|
|
assert "Invalid IP" in bot.replied[0]
|
|
|
|
def test_missing_args(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr contains 10.0.0.0/8")))
|
|
assert "Usage" in bot.replied[0]
|
|
|
|
|
|
# -- Errors ------------------------------------------------------------------
|
|
|
|
class TestCidrErrors:
|
|
def test_invalid_network(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr notanetwork")))
|
|
assert "Invalid network" in bot.replied[0]
|
|
|
|
def test_missing_args(self):
|
|
bot = _FakeBot()
|
|
asyncio.run(cmd_cidr(bot, _msg("!cidr")))
|
|
assert "Usage" in bot.replied[0]
|