tests/dummysmtpd.py
author Matt Harbison <matt_harbison@yahoo.com>
Mon, 29 Jul 2024 12:10:08 -0400
changeset 51740 7226f2626fb1
parent 51710 8fe7c0e1df1e
child 52093 dbd2d56224d1
permissions -rwxr-xr-x
httppeer: avoid another bad reference before assignment warning This wasn't a problem, because `b''` from the `AttributeError` handler is in `bundle2.bundletypes`, so the following loop and conditional always run at least once. But PyCharm can't figure that out on its own, and it took a little exploring to figure out it wasn't a problem. The usage in `bundle2.writebundle` is to look it up in the map of bundle types, so it will break in a more obvious way in the unlikely event that the empty string is removed from the map in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47500
23f5ed6dbcb1 run-tests: stop writing a `python3` symlink pointing to python2
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 45830
diff changeset
     1
#!/usr/bin/env python
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     3
"""dummy SMTP server for use in tests"""
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     4
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     5
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     6
import optparse
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
     7
import os
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
     8
import socket
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
import ssl
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    10
import sys
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    11
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    12
from mercurial import (
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    13
    pycompat,
30506
d9d8d78e6bc9 server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29556
diff changeset
    14
    server,
29556
1b8b6adb2365 tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29332
diff changeset
    15
    sslutil,
1b8b6adb2365 tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29332
diff changeset
    16
    ui as uimod,
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    18
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
    19
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    20
if os.environ.get('HGIPV6', '0') == '1':
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    21
    family = socket.AF_INET6
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    22
else:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    23
    family = socket.AF_INET
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    24
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    25
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
def log(msg):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
    sys.stdout.write(msg)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    28
    sys.stdout.flush()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    29
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
    30
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    31
def mocksmtpserversession(conn, addr):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    32
    conn.send(b'220 smtp.example.com ESMTP\r\n')
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    33
51710
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    34
    try:
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    35
        # Newer versions of OpenSSL raise on EOF
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    36
        line = conn.recv(1024)
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    37
    except ssl.SSLError:
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    38
        log('no hello: EOF\n')
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    39
        return
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    40
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    41
    if not line.lower().startswith(b'ehlo '):
51710
8fe7c0e1df1e dummysmtpd: fix EOF handling on newer versions of OpenSSL
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
    42
        # Older versions of OpenSSl don't raise
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    43
        log('no hello: %s\n' % line)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    44
        return
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    45
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    46
    conn.send(b'250 Hello\r\n')
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    47
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    48
    line = conn.recv(1024)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    49
    if not line.lower().startswith(b'mail from:'):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    50
        log('no mail from: %s\n' % line)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    51
        return
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    52
    mailfrom = line[10:].decode().rstrip()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    53
    if mailfrom.startswith('<') and mailfrom.endswith('>'):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    54
        mailfrom = mailfrom[1:-1]
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    55
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    56
    conn.send(b'250 Ok\r\n')
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    57
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    58
    rcpttos = []
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    59
    while True:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    60
        line = conn.recv(1024)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    61
        if not line.lower().startswith(b'rcpt to:'):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    62
            break
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    63
        rcptto = line[8:].decode().rstrip()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    64
        if rcptto.startswith('<') and rcptto.endswith('>'):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    65
            rcptto = rcptto[1:-1]
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    66
        rcpttos.append(rcptto)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    67
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    68
        conn.send(b'250 Ok\r\n')
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    69
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    70
    if not line.lower().strip() == b'data':
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    71
        log('no rcpt to or data: %s' % line)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    72
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    73
    conn.send(b'354 Go ahead\r\n')
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    74
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    75
    data = b''
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    76
    while True:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    77
        line = conn.recv(1024)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    78
        if not line:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    79
            log('connection closed before end of data')
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    80
            break
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    81
        data += line
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    82
        if data.endswith(b'\r\n.\r\n'):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    83
            data = data[:-5]
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    84
            break
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    85
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    86
    conn.send(b'250 Ok\r\n')
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    87
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    88
    log(
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    89
        '%s from=%s to=%s\n%s\n'
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    90
        % (addr[0], mailfrom, ', '.join(rcpttos), data.decode())
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    91
    )
35776
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    92
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
    93
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    94
def run(host, port, certificate):
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    95
    ui = uimod.ui.load()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    96
    with socket.socket(family, socket.SOCK_STREAM) as s:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    97
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    98
        s.bind((host, port))
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
    99
        # log('listening at %s:%d\n' % (host, port))
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   100
        s.listen(1)
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   101
        try:
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   102
            while True:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   103
                conn, addr = s.accept()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   104
                if certificate:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   105
                    try:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   106
                        conn = sslutil.wrapserversocket(
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   107
                            conn, ui, certfile=certificate
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   108
                        )
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   109
                    except ssl.SSLError as e:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   110
                        log('%s ssl error: %s\n' % (addr[0], e))
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   111
                        conn.close()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   112
                        continue
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   113
                log("connection from %s:%s\n" % addr)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   114
                mocksmtpserversession(conn, addr)
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   115
                conn.close()
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   116
        except KeyboardInterrupt:
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   117
            pass
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   118
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   119
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   120
def _encodestrsonly(v):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   121
    if isinstance(v, type(u'')):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   122
        return v.encode('ascii')
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   123
    return v
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   124
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   125
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   126
def bytesvars(obj):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   127
    unidict = vars(obj)
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   128
    bd = {k.encode('ascii'): _encodestrsonly(v) for k, v in unidict.items()}
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   129
    if bd[b'daemon_postexec'] is not None:
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   130
        bd[b'daemon_postexec'] = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   131
            _encodestrsonly(v) for v in bd[b'daemon_postexec']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   132
        ]
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   133
    return bd
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   134
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   135
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   136
def main():
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   137
    op = optparse.OptionParser()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   138
    op.add_option('-d', '--daemon', action='store_true')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   139
    op.add_option('--daemon-postexec', action='append')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   140
    op.add_option('-p', '--port', type=int, default=8025)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   141
    op.add_option('-a', '--address', default='localhost')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   142
    op.add_option('--pid-file', metavar='FILE')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   143
    op.add_option('--tls', choices=['none', 'smtps'], default='none')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   144
    op.add_option('--certificate', metavar='FILE')
50729
5644f6286618 tests: show test-patchbomb-tls.t smtp server log
Mads Kiilerich <mads@kiilerich.com>
parents: 48875
diff changeset
   145
    op.add_option('--logfile', metavar='FILE')
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   146
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   147
    opts, args = op.parse_args()
50731
8823e4d411ba tests: fix dummysmtpd argument check
Mads Kiilerich <mads@kiilerich.com>
parents: 50730
diff changeset
   148
    if (opts.tls == 'smtps') != bool(opts.certificate):
8823e4d411ba tests: fix dummysmtpd argument check
Mads Kiilerich <mads@kiilerich.com>
parents: 50730
diff changeset
   149
        op.error('--certificate must be specified with --tls=smtps')
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   150
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   151
    server.runservice(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   152
        bytesvars(opts),
50732
b3a5af04da35 tests: use simple mock smtp server instead of deprecated asyncore smtpd
Mads Kiilerich <mads@kiilerich.com>
parents: 50731
diff changeset
   153
        runfn=lambda: run(opts.address, opts.port, opts.certificate),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   154
        runargs=[pycompat.sysexecutable, pycompat.fsencode(__file__)]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   155
        + pycompat.sysargv[1:],
50729
5644f6286618 tests: show test-patchbomb-tls.t smtp server log
Mads Kiilerich <mads@kiilerich.com>
parents: 48875
diff changeset
   156
        logfile=opts.logfile,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   157
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39029
diff changeset
   158
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   159
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   160
if __name__ == '__main__':
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   161
    main()