tests/dummysmtpd.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 14 Apr 2018 11:50:19 -0700
changeset 37719 a656cba08a04
parent 36566 ed96d1116302
child 39029 78f1899e4202
permissions -rwxr-xr-x
wireprotov2: move response handling out of httppeer And fix some bugs while we're here. The code for processing response data from the unified framing protocol is mostly peer agnostic. The peer-specific bits are the configuration of the client reactor and how I/O is performed. I initially implemented things in httppeer for expediency. This commit establishes a module for holding the peer API level code for the framing based protocol. Inside this module we have a class to help coordinate higher-level activities, such as managing response object. The client handler bits could be rolled into clientreactor. However, I want clientreactor to be sans I/O and I want it to only be concerned with protocol-level details, not higher-level concepts like how protocol events are converted into peer API concepts. I want clientreactor to receive a frame and then tell the caller what should probably be done about it. If we start putting things like future resolution into clientreactor, we'll constrain how the protocol can be used (e.g. by requiring futures). The new code is loosely based on what was in httppeer before. I changed things a bit around response handling. We now buffer the entire response "body" and then handle it as one atomic unit. This fixed a bug around decoding CBOR data that spanned multiple frames. I also fixed an off-by-one bug where we failed to read a single byte CBOR value at the end of the stream. That's why tests have changed. The new state of httppeer is much cleaner. It is largely agnostic about framing protocol implementation details. That's how it should be: the framing protocol is designed to be largely transport agnostic. We want peers merely putting bytes on the wire and telling the framing protocol where to read response data from. There's still a bit of work to be done here, especially for representing responses. But at least we're a step closer to having a higher-level peer interface that can be plugged into the SSH peer someday. I initially added this class to wireprotoframing. However, we'll eventually need version 2 specific functions to convert CBOR responses into data structures expected by the code calling commands. This needs to live somewhere. Since that code would be shared across peers, we need a common module. We have wireprotov1peer for the equivalent version 1 code. So I decided to establish wireprotov2peer. Differential Revision: https://phab.mercurial-scm.org/D3379
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
#!/usr/bin/env python
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
from __future__ import absolute_import
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     6
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     7
import asyncore
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     8
import optparse
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
import smtpd
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    10
import ssl
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    11
import sys
35776
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    12
import traceback
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    13
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    14
from mercurial import (
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    15
    pycompat,
30506
d9d8d78e6bc9 server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29556
diff changeset
    16
    server,
29556
1b8b6adb2365 tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29332
diff changeset
    17
    sslutil,
1b8b6adb2365 tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29332
diff changeset
    18
    ui as uimod,
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    19
)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
def log(msg):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
    sys.stdout.write(msg)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    23
    sys.stdout.flush()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    24
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    25
class dummysmtpserver(smtpd.SMTPServer):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
    def __init__(self, localaddr):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
        smtpd.SMTPServer.__init__(self, localaddr, remoteaddr=None)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    28
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    29
    def process_message(self, peer, mailfrom, rcpttos, data):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    30
        log('%s from=%s to=%s\n' % (peer[0], mailfrom, ', '.join(rcpttos)))
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    31
35776
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    32
    def handle_error(self):
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    33
        # On Windows, a bad SSL connection sometimes generates a WSAECONNRESET.
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    34
        # The default handler will shutdown this server, and then both the
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    35
        # current connection and subsequent ones fail on the client side with
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    36
        # "No connection could be made because the target machine actively
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    37
        # refused it".  If we eat the error, then the client properly aborts in
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    38
        # the expected way, and the server is available for subsequent requests.
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    39
        traceback.print_exc()
75bae69747f0 dummysmtpd: don't die on client connection errors
Matt Harbison <matt_harbison@yahoo.com>
parents: 30559
diff changeset
    40
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    41
class dummysmtpsecureserver(dummysmtpserver):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    42
    def __init__(self, localaddr, certfile):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    43
        dummysmtpserver.__init__(self, localaddr)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    44
        self._certfile = certfile
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    45
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    46
    def handle_accept(self):
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    47
        pair = self.accept()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    48
        if not pair:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    49
            return
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    50
        conn, addr = pair
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30506
diff changeset
    51
        ui = uimod.ui.load()
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    52
        try:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    53
            # wrap_socket() would block, but we don't care
29556
1b8b6adb2365 tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29332
diff changeset
    54
            conn = sslutil.wrapserversocket(conn, ui, certfile=self._certfile)
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    55
        except ssl.SSLError:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    56
            log('%s ssl error\n' % addr[0])
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    57
            conn.close()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    58
            return
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    59
        smtpd.SMTPChannel(self, conn, addr)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    60
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    61
def run():
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    62
    try:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    63
        asyncore.loop()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    64
    except KeyboardInterrupt:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    65
        pass
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    66
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    67
def _encodestrsonly(v):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    68
    if isinstance(v, type(u'')):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    69
        return v.encode('ascii')
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    70
    return v
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    71
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    72
def bytesvars(obj):
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    73
    unidict = vars(obj)
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    74
    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
    75
    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
    76
        bd[b'daemon_postexec'] = [
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    77
            _encodestrsonly(v) for v in bd[b'daemon_postexec']]
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    78
    return bd
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
    79
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    80
def main():
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    81
    op = optparse.OptionParser()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    82
    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
    83
    op.add_option('--daemon-postexec', action='append')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    84
    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
    85
    op.add_option('-a', '--address', default='localhost')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    86
    op.add_option('--pid-file', metavar='FILE')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    87
    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
    88
    op.add_option('--certificate', metavar='FILE')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    89
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    90
    opts, args = op.parse_args()
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    91
    if opts.tls == 'smtps' and not opts.certificate:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    92
        op.error('--certificate must be specified')
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    93
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    94
    addr = (opts.address, opts.port)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    95
    def init():
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    96
        if opts.tls == 'none':
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    97
            dummysmtpserver(addr)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    98
        else:
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    99
            dummysmtpsecureserver(addr, opts.certificate)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   100
        log('listening at %s:%d\n' % addr)
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   101
36566
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   102
    server.runservice(
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   103
        bytesvars(opts), initfn=init, runfn=run,
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   104
        runargs=[pycompat.sysexecutable,
ed96d1116302 tests: help dummysmtpd work on python 3
Augie Fackler <augie@google.com>
parents: 35776
diff changeset
   105
                 pycompat.fsencode(__file__)] + pycompat.sysargv[1:])
29332
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   106
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   107
if __name__ == '__main__':
2bb0ddd8267b tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   108
    main()