#!/usr/bin/env python3 """IMAPS/POP3S regression check. Usage: imap_pop3_regression.py """ import imaplib, poplib, ssl, sys, threading IMAPS, POP3S = int(sys.argv[1]), int(sys.argv[2]) USER, PASSWORD = "brad@mail.test", "secret" ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE results = [] def check(name, ok, detail=""): results.append(ok) print(("PASS " if ok else "FAIL ") + name + ("" if ok else " -> " + repr(detail)[:200])) def imap_session(): m = imaplib.IMAP4_SSL("127.0.0.1", IMAPS, ssl_context=ctx, timeout=10) m.login(USER, PASSWORD) typ, data = m.select("INBOX") n = int(data[0]) body = None if n: typ, msg = m.fetch(b"1", "(BODY.PEEK[])") body = msg[0][1] m.logout() return n, body try: n, body = imap_session() check("IMAPS login/select/fetch (INBOX has %d messages)" % n, n > 0 and body and body.startswith(b"Return-Path:") or n > 0, (n, (body or b"")[:80])) except Exception as e: check("IMAPS login/select/fetch", False, e) try: p = poplib.POP3_SSL("127.0.0.1", POP3S, context=ctx, timeout=10) p.user(USER); p.pass_(PASSWORD) count, size = p.stat() resp, lines, octets = p.retr(1) p.quit() check("POP3S login/stat/retr (%d messages)" % count, count > 0 and len(lines) > 0, (count, lines[:2])) except Exception as e: check("POP3S login/stat/retr", False, e) errors = [] def worker(): try: imap_session() except Exception as e: errors.append(repr(e)) ts = [threading.Thread(target=worker) for _ in range(30)] [t.start() for t in ts]; [t.join(30) for t in ts] check("30 concurrent IMAPS sessions", not errors, errors[:3]) print("%d/%d passed" % (sum(results), len(results))) sys.exit(0 if all(results) else 1)