Ejemplo n.º 1
0
 def test_loop_keyboard_interrupt(self):
     # We mock out so much of this, is it even a worthwhile test?  Well, it
     # does give us coverage.
     self.run_forever.side_effect = KeyboardInterrupt
     main(('-n',))
     # loop.run_until_complete() was still executed.
     self.assertTrue(self.run_until_complete.called)
Ejemplo n.º 2
0
 def test_loop_keyboard_interrupt(self):
     # We mock out so much of this, is it even a worthwhile test?  Well, it
     # does give us coverage.
     self.run_forever.side_effect = KeyboardInterrupt
     main(('-n', ))
     # loop.run_until_complete() was still executed.
     self.assertTrue(self.run_until_complete.called)
Ejemplo n.º 3
0
 def test_setuid(self):
     with patch('os.setuid', side_effect=RuntimeError) as mock:
         try:
             main(args=())
         except RuntimeError:
             pass
         mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)
Ejemplo n.º 4
0
 def test_smtputf8(self):
     # We mock out so much of this, is it even a worthwhile test?  Well, it
     # does give us coverage.
     main(('-n', '--smtputf8'))
     positional, keywords = self.create_server.call_args
     self.assertEqual(positional[0].keywords,
                      dict(data_size_limit=None, enable_SMTPUTF8=True))
Ejemplo n.º 5
0
 def test_setuid(self):
     with patch('os.setuid', side_effect=RuntimeError) as mock:
         try:
             main(args=())
         except RuntimeError:
             pass
         mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)
Ejemplo n.º 6
0
 def test_setuid_permission_error(self, nobody_uid, mocker, capsys):
     mock = mocker.patch("os.setuid", side_effect=PermissionError)
     with pytest.raises(SystemExit) as excinfo:
         main(args=())
     assert excinfo.value.code == 1
     mock.assert_called_with(nobody_uid)
     assert (capsys.readouterr().err ==
             'Cannot setuid "nobody"; try running with -n option.\n')
Ejemplo n.º 7
0
 def test_debug_0(self):
     # For this test, the runner will have already set the log level so it
     # may not be logging.ERROR.
     log = logging.getLogger('mail.log')
     default_level = log.getEffectiveLevel()
     with patch.object(log, 'info'):
         main(('-n', ))
         self.assertEqual(log.getEffectiveLevel(), default_level)
Ejemplo n.º 8
0
 def test_debug_0(self):
     # For this test, the runner will have already set the log level so it
     # may not be logging.ERROR.
     log = logging.getLogger('mail.log')
     default_level = log.getEffectiveLevel()
     with patch.object(log, 'info'):
         main(('-n',))
         self.assertEqual(log.getEffectiveLevel(), default_level)
Ejemplo n.º 9
0
 def test_smtputf8(self):
     # We mock out so much of this, is it even a worthwhile test?  Well, it
     # does give us coverage.
     main(('-n', '--smtputf8'))
     positional, keywords = self.create_server.call_args
     self.assertEqual(positional[0].keywords, dict(
         data_size_limit=None,
         enable_SMTPUTF8=True))
Ejemplo n.º 10
0
 def test_debug_2(self):
     # The main loop will produce an error, but that's fine.  Also, mock
     # the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         try:
             main(('-n', '-dd'))
         except RuntimeError:
             pass
         self.assertEqual(log.getEffectiveLevel(), logging.DEBUG)
Ejemplo n.º 11
0
 def test_debug_2(self):
     # The main loop will produce an error, but that's fine.  Also, mock
     # the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         try:
             main(('-n', '-dd'))
         except RuntimeError:
             pass
         self.assertEqual(log.getEffectiveLevel(), logging.DEBUG)
Ejemplo n.º 12
0
 def test_setuid_no_pwd_module(self):
     self.resources.enter_context(patch('aiosmtpd.main.pwd', None))
     stderr = StringIO()
     self.resources.enter_context(patch('sys.stderr', stderr))
     with self.assertRaises(SystemExit) as cm:
         main(args=())
     self.assertEqual(cm.exception.code, 1)
     self.assertEqual(
         stderr.getvalue(),
         'Cannot import module "pwd"; try running with -n option.\n')
Ejemplo n.º 13
0
 def test_setuid_no_pwd_module(self):
     self.resources.enter_context(patch('aiosmtpd.main.pwd', None))
     stderr = StringIO()
     self.resources.enter_context(patch('sys.stderr', stderr))
     with self.assertRaises(SystemExit) as cm:
         main(args=())
     self.assertEqual(cm.exception.code, 1)
     self.assertEqual(
         stderr.getvalue(),
         'Cannot import module "pwd"; try running with -n option.\n')
Ejemplo n.º 14
0
 def test_setuid_permission_error(self):
     mock = self.resources.enter_context(
         patch('os.setuid', side_effect=PermissionError))
     stderr = StringIO()
     self.resources.enter_context(patch('sys.stderr', stderr))
     with self.assertRaises(SystemExit) as cm:
         main(args=())
     self.assertEqual(cm.exception.code, 1)
     mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)
     self.assertEqual(
         stderr.getvalue(),
         'Cannot setuid "nobody"; try running with -n option.\n')
Ejemplo n.º 15
0
 def test_setuid_no_pwd_module(self, nobody_uid, mocker, capsys):
     mocker.patch("aiosmtpd.main.pwd", None)
     with pytest.raises(SystemExit) as excinfo:
         main(args=())
     assert excinfo.value.code == 1
     # On Python 3.8 on Linux, a bunch of "RuntimeWarning: coroutine
     # 'AsyncMockMixin._execute_mock_call' was never awaited" messages
     # gets mixed up into stderr causing test fail.
     # Therefore, we use assertIn instead of assertEqual here, because
     # the string DOES appear in stderr, just buried.
     assert ('Cannot import module "pwd"; try running with -n option.\n'
             in capsys.readouterr().err)
Ejemplo n.º 16
0
 def test_setuid_permission_error(self):
     mock = self.resources.enter_context(
         patch('os.setuid', side_effect=PermissionError))
     stderr = StringIO()
     self.resources.enter_context(patch('sys.stderr', stderr))
     with self.assertRaises(SystemExit) as cm:
         main(args=())
     self.assertEqual(cm.exception.code, 1)
     mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)
     self.assertEqual(
         stderr.getvalue(),
         'Cannot setuid "nobody"; try running with -n option.\n')
Ejemplo n.º 17
0
 def test_debug_0(self):
     # The main loop will produce an error, but that's fine.  Also, mock
     # the logger to eliminate console noise.  For this test, the runner
     # will have already set the log level so it may not be logging.ERROR.
     log = logging.getLogger('mail.log')
     default_level = log.getEffectiveLevel()
     with patch.object(log, 'info'):
         try:
             main(('-n', ))
         except RuntimeError:
             pass
         self.assertEqual(log.getEffectiveLevel(), default_level)
Ejemplo n.º 18
0
 def test_debug_0(self):
     # The main loop will produce an error, but that's fine.  Also, mock
     # the logger to eliminate console noise.  For this test, the runner
     # will have already set the log level so it may not be logging.ERROR.
     log = logging.getLogger('mail.log')
     default_level = log.getEffectiveLevel()
     with patch.object(log, 'info'):
         try:
             main(('-n',))
         except RuntimeError:
             pass
         self.assertEqual(log.getEffectiveLevel(), default_level)
Ejemplo n.º 19
0
    def test_keyboard_interrupt(self):
        """
        main() must close loop gracefully on Ctrl-C.
        """
        def interrupt():
            raise KeyboardInterrupt

        self.loop.call_later(1.5, interrupt)

        try:
            main(("-n", ))
        except Exception:
            self.fail("main() should've closed cleanly without exceptions!")
        else:
            self.assertFalse(self.loop.is_running())
Ejemplo n.º 20
0
 def test_setuid_no_pwd_module(self):
     self.resources.enter_context(patch('aiosmtpd.main.pwd', None))
     stderr = StringIO()
     self.resources.enter_context(patch('sys.stderr', stderr))
     with self.assertRaises(SystemExit) as cm:
         main(args=())
     self.assertEqual(cm.exception.code, 1)
     # On Python 3.8 on Linux, a bunch of "RuntimeWarning: coroutine
     # 'AsyncMockMixin._execute_mock_call' was never awaited" messages
     # gets mixed up into stderr causing test fail.
     # Therefore, we use assertIn instead of assertEqual here, because
     # the string DOES appear in stderr, just buried.
     self.assertIn(
         'Cannot import module "pwd"; try running with -n option.\n',
         stderr.getvalue(),
     )
Ejemplo n.º 21
0
 def test_loop(self):
     main(('-n', ))
     # create_server() is called with a partial as the factory, and a
     # socket object.
     self.assertEqual(self.create_server.call_count, 1)
     positional, keywords = self.create_server.call_args
     self.assertEqual(positional[0].func, SMTP)
     self.assertEqual(len(positional[0].args), 1)
     self.assertIsInstance(positional[0].args[0], Debugging)
     self.assertEqual(positional[0].keywords,
                      dict(data_size_limit=None, enable_SMTPUTF8=False))
     self.assertEqual(sorted(keywords), ['host', 'port'])
     # run_until_complete() was called once.  The argument isn't important.
     self.assertTrue(self.run_until_complete.called)
     # add_signal_handler() is called with two arguments.
     self.assertEqual(self.add_signal_handler.call_count, 1)
     signal_number, callback = self.add_signal_handler.call_args[0]
     self.assertEqual(signal_number, signal.SIGINT)
     self.assertEqual(callback, self.loop.stop)
     # run_forever() was called once.
     self.assertEqual(self.run_forever.call_count, 1)
Ejemplo n.º 22
0
 def test_loop(self):
     main(('-n',))
     # create_server() is called with a partial as the factory, and a
     # socket object.
     self.assertEqual(self.create_server.call_count, 1)
     positional, keywords = self.create_server.call_args
     self.assertEqual(positional[0].func, SMTP)
     self.assertEqual(len(positional[0].args), 1)
     self.assertIsInstance(positional[0].args[0], Debugging)
     self.assertEqual(positional[0].keywords, dict(
         data_size_limit=None,
         enable_SMTPUTF8=False))
     self.assertEqual(sorted(keywords), ['host', 'port'])
     # run_until_complete() was called once.  The argument isn't important.
     self.assertTrue(self.run_until_complete.called)
     # add_signal_handler() is called with two arguments.
     self.assertEqual(self.add_signal_handler.call_count, 1)
     signal_number, callback = self.add_signal_handler.call_args[0]
     self.assertEqual(signal_number, signal.SIGINT)
     self.assertEqual(callback, self.loop.stop)
     # run_forever() was called once.
     self.assertEqual(self.run_forever.call_count, 1)
Ejemplo n.º 23
0
 def test_debug_3(self):
     # Mock the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         main(('-n', '-ddd'))
         self.assertEqual(log.getEffectiveLevel(), logging.DEBUG)
         self.assertTrue(asyncio.get_event_loop().get_debug())
Ejemplo n.º 24
0
from aiosmtpd.main import main


if __name__ == '__main__':
    main()
Ejemplo n.º 25
0
 def test_debug_1(self):
     # Mock the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         main(('-n', '-d'))
         self.assertEqual(log.getEffectiveLevel(), logging.INFO)
Ejemplo n.º 26
0
 def test_debug_3(self):
     # Mock the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         main(('-n', '-ddd'))
         self.assertEqual(log.getEffectiveLevel(), logging.DEBUG)
         self.assertTrue(asyncio.get_event_loop().get_debug())
Ejemplo n.º 27
0
 def test_setuid(self):
     with patch('os.setuid') as mock:
         main(args=())
         mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)
Ejemplo n.º 28
0
 def test_nosetuid(self, setuid):
     with pytest.raises(RuntimeError):
         main(("--nosetuid",))
Ejemplo n.º 29
0
 def test_setuid(self, nobody_uid, mocker):
     mock = mocker.patch("os.setuid")
     main(args=())
     mock.assert_called_with(nobody_uid)
Ejemplo n.º 30
0
def main_n(*args):
    main(("-n",) + args)
Ejemplo n.º 31
0
# Copyright 2014-2021 The aiosmtpd Developers
# SPDX-License-Identifier: Apache-2.0

from aiosmtpd.main import main

if __name__ == '__main__':
    main()
Ejemplo n.º 32
0
 def test_debug_1(self):
     # Mock the logger to eliminate console noise.
     with patch.object(logging.getLogger('mail.log'), 'info'):
         main(('-n', '-d'))
         self.assertEqual(log.getEffectiveLevel(), logging.INFO)
Ejemplo n.º 33
0
 def test_setuid(self):
     with patch('os.setuid') as mock:
         main(args=())
         mock.assert_called_with(pwd.getpwnam('nobody').pw_uid)