|
| 1 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +""" |
| 5 | +Testing the SecurityTXT Expert Bot |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | + |
| 10 | +import requests_mock |
| 11 | + |
| 12 | +import intelmq.lib.test as test |
| 13 | +from intelmq.bots.experts.securitytxt.expert import SecurityTXTExpertBot |
| 14 | + |
| 15 | +EXAMPLE_INPUT_IP = {"__type": "Event", |
| 16 | + "source.ip": "192.168.123.123"} |
| 17 | + |
| 18 | +EXPECTED_OUTPUT_IP = {"__type": "Event", |
| 19 | + "source.ip": "192.168.123.123", |
| 20 | + "source.account": 'test@test.local'} |
| 21 | + |
| 22 | +EXAMPLE_INPUT_FQDN = {"__type": "Event", |
| 23 | + "source.fqdn": "test.local"} |
| 24 | + |
| 25 | +EXPECTED_OUTPUT_FQDN = {"__type": "Event", |
| 26 | + "source.fqdn": "test.local", |
| 27 | + "source.abuse_contact": 'test.local/whitehat'} |
| 28 | + |
| 29 | +EXPECTED_OUTPUT_FQDN_NO_CONTACT = {"__type": "Event", |
| 30 | + "source.fqdn": "test.local"} |
| 31 | + |
| 32 | +@requests_mock.Mocker() |
| 33 | +class TestSecurityTXTExpertBot(test.BotTestCase, unittest.TestCase): |
| 34 | + """ |
| 35 | + A TestCase for the SecurityTXT Expert Bot |
| 36 | + """ |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def set_bot(cls): |
| 40 | + cls.bot_reference = SecurityTXTExpertBot |
| 41 | + |
| 42 | + def test_ip(self, m: requests_mock.Mocker): |
| 43 | + self._run_generic_test(securitytxt_url=f"https://{EXAMPLE_INPUT_IP['source.ip']}/.well-known/security.txt", |
| 44 | + securitytxt=f"Contact: {EXPECTED_OUTPUT_IP['source.account']}", |
| 45 | + input_message=EXAMPLE_INPUT_IP, |
| 46 | + output_message=EXPECTED_OUTPUT_IP, |
| 47 | + config={'url_field': 'source.ip', 'contact_field': 'source.account', |
| 48 | + 'only_email_address': False}, |
| 49 | + m=m) |
| 50 | + |
| 51 | + def test_fqdn(self, m: requests_mock.Mocker): |
| 52 | + self._run_generic_test(securitytxt_url=f"https://{EXAMPLE_INPUT_FQDN['source.fqdn']}/.well-known/security.txt", |
| 53 | + securitytxt=f"Contact: {EXPECTED_OUTPUT_FQDN['source.abuse_contact']}", |
| 54 | + input_message=EXAMPLE_INPUT_FQDN, |
| 55 | + output_message=EXPECTED_OUTPUT_FQDN, |
| 56 | + config={'url_field': 'source.fqdn', 'contact_field': 'source.abuse_contact', |
| 57 | + 'only_email_address': False}, |
| 58 | + m=m) |
| 59 | + |
| 60 | + def test_only_email_address_true(self, m: requests_mock.Mocker): |
| 61 | + self._run_generic_test(securitytxt_url=f"https://{EXAMPLE_INPUT_FQDN['source.fqdn']}/.well-known/security.txt", |
| 62 | + securitytxt=f"Contact: {EXPECTED_OUTPUT_FQDN['source.abuse_contact']}", |
| 63 | + input_message=EXAMPLE_INPUT_FQDN, |
| 64 | + output_message=EXPECTED_OUTPUT_FQDN_NO_CONTACT, |
| 65 | + config={'url_field': 'source.fqdn', 'contact_field': 'source.abuse_contact', |
| 66 | + 'only_email_address': True}, |
| 67 | + m=m) |
| 68 | + |
| 69 | + def test_expired(self, m: requests_mock.Mocker): |
| 70 | + self._run_generic_test(securitytxt_url=f"https://{EXAMPLE_INPUT_FQDN['source.fqdn']}/.well-known/security.txt", |
| 71 | + securitytxt=f"Contact: {EXPECTED_OUTPUT_FQDN['source.abuse_contact']}\nExpires: 1900-12-31T18:37:07.000Z", |
| 72 | + input_message=EXAMPLE_INPUT_FQDN, |
| 73 | + output_message=EXPECTED_OUTPUT_FQDN_NO_CONTACT, |
| 74 | + config={'url_field': 'source.fqdn', 'contact_field': 'source.abuse_contact', |
| 75 | + 'only_email_address': False, 'check_expired': True}, |
| 76 | + m=m) |
| 77 | + |
| 78 | + def test_not_expired(self, m: requests_mock.Mocker): |
| 79 | + self._run_generic_test(securitytxt_url=f"https://{EXAMPLE_INPUT_FQDN['source.fqdn']}/.well-known/security.txt", |
| 80 | + securitytxt=f"Contact: {EXPECTED_OUTPUT_FQDN['source.abuse_contact']}\nExpires: 3000-12-31T18:37:07.000Z", |
| 81 | + input_message=EXAMPLE_INPUT_FQDN, |
| 82 | + output_message=EXPECTED_OUTPUT_FQDN, |
| 83 | + config={'url_field': 'source.fqdn', 'contact_field': 'source.abuse_contact', |
| 84 | + 'only_email_address': False, 'check_expired': True}, |
| 85 | + m=m) |
| 86 | + |
| 87 | + def _run_generic_test(self, m: requests_mock.Mocker, config: dict, securitytxt_url: str, securitytxt: str, |
| 88 | + input_message: dict, output_message: dict): |
| 89 | + self.sysconfig = config |
| 90 | + self.prepare_bot() |
| 91 | + m.get(requests_mock.ANY, status_code=404) |
| 92 | + m.get(securitytxt_url, text=securitytxt) |
| 93 | + self.input_message = input_message |
| 94 | + self.run_bot() |
| 95 | + self.assertMessageEqual(0, output_message) |
0 commit comments