Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add test for CHGHOST using services #223

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions irctest/basecontrollers.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ def registerUser(
case: irctest.cases.BaseServerTestCase, # type: ignore
username: str,
password: Optional[str] = None,
**kwargs: Any,
) -> None:
if self.services_controller is not None:
self.services_controller.registerUser(case, username, password)
self.services_controller.registerUser(case, username, password, **kwargs)
else:
raise NotImplementedByController("account registration")

Expand Down Expand Up @@ -293,7 +294,7 @@ def wait_for_services(self) -> None:
timeout = time.time() + 5
while True:
c.sendLine(f"PRIVMSG {self.server_controller.nickserv} :HELP")
msgs = self.getNickServResponse(c)
msgs = self.getServiceResponse(c)
for msg in msgs:
if msg.command == "401":
# NickServ not available yet
Expand All @@ -319,7 +320,7 @@ def wait_for_services(self) -> None:
c.disconnect()
self.services_up = True

def getNickServResponse(self, client: Any) -> List[Message]:
def getServiceResponse(self, client: Any) -> List[Message]:
"""Wrapper aroung getMessages() that waits longer, because NickServ
is queried asynchronously."""
msgs: List[Message] = []
Expand All @@ -333,11 +334,14 @@ def registerUser(
case: irctest.cases.BaseServerTestCase, # type: ignore
username: str,
password: Optional[str] = None,
**kwargs: Any,
) -> None:
if not case.run_services:
raise ValueError(
"Attempted to register a nick, but `run_services` it not True."
)
if kwargs:
raise NotImplementedByController(", ".join(kwargs))
assert password
client = case.addClient(show_io=True)
case.sendLine(client, "NICK " + username)
Expand All @@ -350,7 +354,7 @@ def registerUser(
f"PRIVMSG {self.server_controller.nickserv} "
f":REGISTER {password} [email protected]",
)
msgs = self.getNickServResponse(case.clients[client])
msgs = self.getServiceResponse(case.clients[client])
if self.server_controller.software_name == "inspircd":
assert "900" in {msg.command for msg in msgs}, msgs
assert "NOTICE" in {msg.command for msg in msgs}, msgs
Expand Down
67 changes: 65 additions & 2 deletions irctest/controllers/anope_services.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import shutil
import subprocess
from typing import Type
from typing import Any, Optional, Type

from irctest import cases, runner
from irctest.basecontrollers import BaseServicesController, DirectoryBasedController
from irctest.client_mock import ClientMock
from irctest.irc_utils.sasl import sasl_plain_blob

TEMPLATE_CONFIG = """
serverinfo {{
Expand All @@ -30,12 +33,17 @@
userlen = 10
hostlen = 64
chanlen = 32
vhost_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-"
}}

mail {{
usemail = no
}}

/************************
* NickServ:
*/

service {{
nick = "NickServ"
user = "services"
Expand All @@ -57,6 +65,29 @@
}}
command {{ service = "NickServ"; name = "REGISTER"; command = "nickserv/register"; }}


/************************
* HostServ:
*/

service {{
nick = "HostServ"
user = "services"
host = "services.host"
gecos = "vHost Service"
}}
module {{
name = "hostserv"
client = "HostServ"
}}
module {{ name = "hs_set" }}
command {{ service = "HostServ"; name = "SET"; command = "hostserv/set"; }}


/************************
* Misc:
*/

options {{
casemap = "ascii"
readtimeout = 5s
Expand All @@ -66,7 +97,6 @@
module {{ name = "m_sasl" }}
module {{ name = "enc_sha256" }}
module {{ name = "ns_cert" }}

"""


Expand Down Expand Up @@ -121,6 +151,39 @@ def run(self, protocol: str, server_hostname: str, server_port: int) -> None:
# stderr=subprocess.DEVNULL,
)

def registerUser(
self,
case: cases.BaseServerTestCase, # type: ignore
username: str,
password: Optional[str] = None,
vhost: Optional[str] = None,
**kwargs: Any,
) -> None:
super().registerUser(case, username, password)

if vhost:
if not password:
raise runner.NotImplementedByController(
"vHost for users with no password"
)
c = ClientMock(name="setVhost", show_io=True)
c.connect(self.server_controller.hostname, self.server_controller.port)
c.sendLine("CAP REQ :sasl")
c.sendLine("NICK " + username)
c.sendLine("USER r e g :user")
while c.getMessage(synchronize=False).command != "CAP":
pass
c.sendLine("AUTHENTICATE PLAIN")
while c.getMessage(synchronize=False).command != "AUTHENTICATE":
pass
c.sendLine(sasl_plain_blob(username, password))
c.sendLine("CAP END")
while c.getMessage(synchronize=False).command != "001":
pass
c.getMessages()
c.sendLine(f"PRIVMSG HostServ :SET {username} {vhost}")
self.getServiceResponse(c)


def get_irctest_controller_class() -> Type[AnopeController]:
return AnopeController
2 changes: 2 additions & 0 deletions irctest/controllers/inspircd.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@
# Protocol:
<module name="botmode">
<module name="cap">
<module name="chghost">
<module name="ircv3">
<module name="ircv3_accounttag">
<module name="ircv3_batch">
<module name="ircv3_capnotify">
<module name="ircv3_chghost">
<module name="ircv3_ctctags">
<module name="ircv3_echomessage">
<module name="ircv3_invitenotify">
Expand Down
46 changes: 46 additions & 0 deletions irctest/server_tests/chghost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
<http://ircv3.net/specs/extensions/chghost.html>
"""

from irctest import cases
from irctest.irc_utils.sasl import sasl_plain_blob
from irctest.patma import ANYSTR, StrRe


@cases.mark_services
class ChghostServicesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper):
def testChghostFromServices(self):
self.connectClient("observer", capabilities=["chghost"], skip_if_cap_nak=True)
self.connectClient("oldclient")

self.controller.registerUser(
self, "vhostuser", "sesame", vhost="vhost.example.com"
)
self.connectClient("vhost-user", capabilities=["sasl"], skip_if_cap_nak=True)

for i in (1, 2, 3):
self.sendLine(i, "JOIN #chan")
self.getMessages(i)

for i in (1, 2, 3):
self.getMessages(i)

self.sendLine(3, "AUTHENTICATE PLAIN")
self.assertMessageMatch(
self.getRegistrationMessage(3),
command="AUTHENTICATE",
params=["+"],
)
self.sendLine(3, sasl_plain_blob("vhostuser", "sesame"))
self.assertMessageMatch(
self.getRegistrationMessage(3),
command="900",
)

self.assertMessageMatch(
self.getMessage(1),
prefix=StrRe("vhost-user!.*@(?!vhost-user.example)"),
command="CHGHOST",
params=[ANYSTR, "vhost.example.com"],
)
self.assertEqual(self.getMessages(2), []) # cycle?