Beispiel #1
0
 def test_delay_and_mode(self):
     smtp = Smtp([])
     smtp.delay = '30'
     smtp.mode = 'accept'
     smtp._flags = {'delay': '20', 'mode': 'bounce'}
     assert smtp.delay == 20
     assert smtp.mode == 'bounce'
Beispiel #2
0
 def test_delay_range_and_mode(self):
     smtp = Smtp([])
     smtp.delay = '30'
     smtp.mode = 'accept'
     smtp._flags = {'delay': ['10', '20'], 'mode': 'bounce'}
     assert smtp.delay in (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
     assert smtp.mode == 'bounce'
Beispiel #3
0
 def test_headers_default(self):
     cfile = create_config(('', ))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == 'accept'
     smtp.process_header('x-blackhole-mode: bounce')
     assert smtp.mode == 'bounce'
Beispiel #4
0
 def test_mode_valid_overrides_config(self):
     cfile = create_config(("mode=bounce",))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == "bounce"
     smtp.mode = "accept"
     assert smtp.mode == "accept"
Beispiel #5
0
 def test_delay_range_and_mode(self):
     smtp = Smtp([])
     smtp.delay = "30"
     smtp.mode = "accept"
     smtp._flags = {"delay": ["10", "20"], "mode": "bounce"}
     assert smtp.delay in (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
     assert smtp.mode == "bounce"
Beispiel #6
0
 def test_delay_switch_range_overrides_config(self):
     cfile = create_config(("delay=30",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "40, 45"
     assert smtp.delay in [x for x in range(40, 46)]
     assert smtp.config.delay is 30
Beispiel #7
0
 def test_delay_switch_overrides_config_single(self):
     cfile = create_config(("delay=30",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "60"
     assert smtp.delay is 60
     assert smtp.config.delay is 30
Beispiel #8
0
 def test_headers_enabled(self):
     cfile = create_config(("dynamic_switch=true",))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == "accept"
     smtp.process_header("x-blackhole-mode: bounce")
     assert smtp.mode == "bounce"
Beispiel #9
0
 def test_headers_default(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == "accept"
     smtp.process_header("x-blackhole-mode: bounce")
     assert smtp.mode == "bounce"
Beispiel #10
0
 def test_delay_and_mode(self):
     smtp = Smtp([])
     smtp.delay = "30"
     smtp.mode = "accept"
     smtp._flags = {"delay": "20", "mode": "bounce"}
     assert smtp.delay == 20
     assert smtp.mode == "bounce"
Beispiel #11
0
 def test_headers_enabled(self):
     cfile = create_config(('dynamic_switch=true', ))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == 'accept'
     smtp.process_header('x-blackhole-mode: bounce')
     assert smtp.mode == 'bounce'
Beispiel #12
0
 def test_mode_valid_overrides_config(self):
     cfile = create_config(('mode=bounce', ))
     Config(cfile).load()
     smtp = Smtp([])
     assert smtp.mode == 'bounce'
     smtp.mode = 'accept'
     assert smtp.mode == 'accept'
Beispiel #13
0
def test_initiation():
    cfile = create_config(("",))
    with mock.patch("os.access", return_value=False), mock.patch(
        "socket.getfqdn", return_value="a.blackhole.io"
    ):
        conf = Config(cfile)
    conf.load()
    smtp = Smtp([])
    assert smtp.fqdn == "a.blackhole.io"
Beispiel #14
0
 def _run(self, ready_event):
     asyncio.set_event_loop(self.loop)
     conf = Config(None)
     conf.mailname = "blackhole.io"
     _server = self.loop.create_server(lambda: Smtp([]), sock=self.sock)
     self.server = self.loop.run_until_complete(_server)
     self.loop.call_soon(ready_event.set)
     self.loop.run_forever()
     self.server.close()
     self.loop.run_until_complete(self.server.wait_closed())
     self.loop.close()
     self.server = None
Beispiel #15
0
def test_unknown_handlers():
    # Protection against adding/removing without updating tests
    verbs = [
        "do_DATA",
        "do_EHLO",
        "do_ETRN",
        "do_EXPN",
        "do_HELO",
        "do_HELP",
        "do_MAIL",
        "do_NOOP",
        "do_NOT_IMPLEMENTED",
        "do_QUIT",
        "do_RCPT",
        "do_RSET",
        "do_STARTTLS",
        "do_UNKNOWN",
        "do_VRFY",
    ]
    helps = [
        "help_AUTH",
        "help_DATA",
        "help_EHLO",
        "help_ETRN",
        "help_EXPN",
        "help_HELO",
        "help_MAIL",
        "help_NOOP",
        "help_QUIT",
        "help_RCPT",
        "help_RSET",
        "help_UNKNOWN",
        "help_VRFY",
    ]
    auths = ["auth_CRAM_MD5", "auth_LOGIN", "auth_PLAIN", "auth_UNKNOWN"]
    smtp = Smtp([])
    for mem in inspect.getmembers(smtp, inspect.ismethod):
        f, _ = mem
        if f.startswith("do_"):
            assert f in verbs
        if f.startswith("help_"):
            assert f in helps
        if f.startswith("auth_"):
            assert f in auths
Beispiel #16
0
 def test_mode(self):
     smtp = Smtp([])
     smtp.mode = "accept"
     smtp._flags = {"mode": "bounce"}
     assert smtp.mode == "bounce"
Beispiel #17
0
 def test_invalid_mode_header(self):
     smtp = Smtp([])
     assert smtp.mode == "accept"
     smtp.process_header("x-blackhole-mode: help")
     assert smtp.mode == "accept"
Beispiel #18
0
def test_handler_lookup():
    smtp = Smtp([])
    assert smtp.lookup_handler("AUTH CRAM-MD5") == smtp.auth_CRAM_MD5
    assert smtp.lookup_handler("AUTH LOGIN") == smtp.auth_LOGIN
    assert smtp.lookup_handler("AUTH PLAIN") == smtp.auth_PLAIN
    assert smtp.lookup_handler("AUTH") == smtp.auth_UNKNOWN
    assert smtp.lookup_handler("AUTH KURA") == smtp.auth_UNKNOWN
    assert smtp.lookup_handler("HELP") == smtp.do_HELP
    assert smtp.lookup_handler("DATA") == smtp.do_DATA
    assert smtp.lookup_handler("EHLO") == smtp.do_EHLO
    assert smtp.lookup_handler("ETRN") == smtp.do_ETRN
    assert smtp.lookup_handler("EXPN") == smtp.do_EXPN
    assert smtp.lookup_handler("HELO") == smtp.do_HELO
    assert smtp.lookup_handler("MAIL") == smtp.do_MAIL
    assert smtp.lookup_handler("NOOP") == smtp.do_NOOP
    assert smtp.lookup_handler("QUIT") == smtp.do_QUIT
    assert smtp.lookup_handler("RCPT") == smtp.do_RCPT
    assert smtp.lookup_handler("RSET") == smtp.do_RSET
    assert smtp.lookup_handler("VRFY") == smtp.do_VRFY
    assert smtp.lookup_handler("STARTTLS") == smtp.do_STARTTLS
    assert smtp.lookup_handler("KURA") == smtp.do_UNKNOWN
    assert smtp.lookup_handler("") == smtp.do_UNKNOWN
    assert smtp.lookup_handler("STARTTLS") == smtp.do_STARTTLS
    assert smtp.lookup_handler("HELP DATA") == smtp.help_DATA
    assert smtp.lookup_handler("HELP EHLO") == smtp.help_EHLO
    assert smtp.lookup_handler("HELP ETRN") == smtp.help_ETRN
    assert smtp.lookup_handler("HELP EXPN") == smtp.help_EXPN
    assert smtp.lookup_handler("HELP HELO") == smtp.help_HELO
    assert smtp.lookup_handler("HELP MAIL") == smtp.help_MAIL
    assert smtp.lookup_handler("HELP NOOP") == smtp.help_NOOP
    assert smtp.lookup_handler("HELP QUIT") == smtp.help_QUIT
    assert smtp.lookup_handler("HELP RCPT") == smtp.help_RCPT
    assert smtp.lookup_handler("HELP RSET") == smtp.help_RSET
    assert smtp.lookup_handler("HELP VRFY") == smtp.help_VRFY
    assert smtp.lookup_handler("HELP KURA") == smtp.help_UNKNOWN
Beispiel #19
0
 def test_delay_switch_range_max_higher_than_60(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "59, 70"
     assert smtp.delay in [59, 60]
Beispiel #20
0
 def test_delay_switch_more_than_2(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "1, 2, 3"
     assert smtp.delay is None
Beispiel #21
0
def test_auth_mechanisms():
    smtp = Smtp([])
    assert smtp.get_auth_members() == ["CRAM-MD5", "LOGIN", "PLAIN"]
Beispiel #22
0
 def test_mode_valid(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.mode = "bounce"
     assert smtp.mode == "bounce"
Beispiel #23
0
 def test_delay_switch_not_above_max(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "90"
     assert smtp.delay is 60
Beispiel #24
0
 def test_delay_switch_invalid_max_range_value_no_config(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "15, eighteen"
     assert smtp.delay is None
Beispiel #25
0
 def test_invalid_mode_header2(self):
     smtp = Smtp([])
     assert smtp.mode == "accept"
     smtp.process_header("x-some-mode: bounce")
     assert smtp.mode == "accept"
Beispiel #26
0
 def test_delay_switch_invalid_single_negative_value(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "-10"
     assert smtp.delay is None
Beispiel #27
0
 def test_valid_single_delay(self):
     smtp = Smtp([])
     assert smtp.delay is None
     smtp.process_header("x-blackhole-delay: 30")
     assert smtp.delay is 30
Beispiel #28
0
 def test_valid_range_delay(self):
     smtp = Smtp([])
     assert smtp.delay is None
     smtp.process_header("x-blackhole-delay: 5, 10")
     assert smtp.delay in [5, 6, 7, 8, 9, 10]
Beispiel #29
0
 def test_invalid_range_delay(self):
     smtp = Smtp([])
     assert smtp.delay is None
     smtp.process_header("x-blackhole-delay: abc, def")
     assert smtp.delay is None
Beispiel #30
0
 def test_delay(self):
     smtp = Smtp([])
     smtp.delay = "30"
     smtp._flags = {"delay": 20}
     assert smtp.delay == 20
Beispiel #31
0
 def test_delay_switch_invalid_range_negatives(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "-10, -1"
     assert smtp.delay is None
Beispiel #32
0
 def test_delay_range(self):
     smtp = Smtp([])
     smtp.delay = "30"
     smtp._flags = {"delay": ["10", "20"]}
     assert smtp.delay in (10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
Beispiel #33
0
 def test_delay_switch_range_min_higher_than_max(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "20, 10"
     assert smtp.delay is None
Beispiel #34
0
 def test_delay_switch_invalid_single_value_config_60(self):
     cfile = create_config(("delay=30",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.delay = "fifteen"
     assert smtp.delay is 30
Beispiel #35
0
 def test_mode_invalid(self):
     cfile = create_config(("",))
     Config(cfile).load()
     smtp = Smtp([])
     smtp.mode = "kura"
     assert smtp.mode == "accept"