From 5b9debfbdf0f648690f4a242a5151d96a35cfefa Mon Sep 17 00:00:00 2001 From: link2xt Date: Tue, 30 Jan 2024 18:58:17 +0000 Subject: [PATCH] Test dict protocol handler as a separate function --- chatmaild/src/chatmaild/doveauth.py | 24 +++++++++++-------- .../src/chatmaild/tests/test_doveauth.py | 19 +++++++++++++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/chatmaild/src/chatmaild/doveauth.py b/chatmaild/src/chatmaild/doveauth.py index b95f2240..f25ca1b1 100644 --- a/chatmaild/src/chatmaild/doveauth.py +++ b/chatmaild/src/chatmaild/doveauth.py @@ -160,6 +160,19 @@ def handle_dovecot_request(msg, db, config: Config): return None +def handle_dovecot_protocol(rfile, wfile, db: Database, config: Config): + while True: + msg = rfile.readline().strip().decode() + if not msg: + break + res = handle_dovecot_request(msg, db, config) + if res: + wfile.write(res.encode("ascii")) + wfile.flush() + else: + logging.warning("request had no answer: %r", msg) + + class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer): request_queue_size = 100 @@ -173,16 +186,7 @@ def main(): class Handler(StreamRequestHandler): def handle(self): try: - while True: - msg = self.rfile.readline().strip().decode() - if not msg: - break - res = handle_dovecot_request(msg, db, config) - if res: - self.wfile.write(res.encode("ascii")) - self.wfile.flush() - else: - logging.warn("request had no answer: %r", msg) + handle_dovecot_protocol(self.rfile, self.wfile, db, config) except Exception: logging.exception("Exception in the handler") raise diff --git a/chatmaild/src/chatmaild/tests/test_doveauth.py b/chatmaild/src/chatmaild/tests/test_doveauth.py index 74b647cb..909ee47c 100644 --- a/chatmaild/src/chatmaild/tests/test_doveauth.py +++ b/chatmaild/src/chatmaild/tests/test_doveauth.py @@ -1,11 +1,17 @@ +import io import json import pytest -import threading import queue +import threading import traceback import chatmaild.doveauth -from chatmaild.doveauth import get_user_data, lookup_passdb, handle_dovecot_request +from chatmaild.doveauth import ( + get_user_data, + lookup_passdb, + handle_dovecot_request, + handle_dovecot_protocol, +) from chatmaild.database import DBError @@ -69,6 +75,15 @@ def test_handle_dovecot_request(db, example_config): assert userdata["password"].startswith("{SHA512-CRYPT}") +def test_handle_dovecot_protocol(db, example_config): + rfile = io.BytesIO( + b"H3\t2\t0\t\tauth\nLshared/userdb/foobar@chat.example.org\tfoobar@chat.example.org\n" + ) + wfile = io.BytesIO() + handle_dovecot_protocol(rfile, wfile, db, example_config) + assert wfile.getvalue() == b"N\n" + + def test_50_concurrent_lookups_different_accounts(db, gencreds, example_config): num_threads = 50 req_per_thread = 5