|
7 | 7 |
|
8 | 8 | Software engineering by BSI & Intevation GmbH |
9 | 9 |
|
10 | | -This tests IntelMQ bots in library mode (IEP007) |
| 10 | +This file tests IntelMQ bots in library mode (IEP007) |
11 | 11 | """ |
12 | 12 | import json |
13 | 13 | import unittest |
|
28 | 28 | 'source.urlpath': '/', |
29 | 29 | 'protocol.application': 'http', |
30 | 30 | 'protocol.transport': 'tcp'} |
| 31 | +EXAMPLE_IP_INPUT = {"source.ip": "192.0.43.7", # icann.org. |
| 32 | + "destination.ip": "192.0.43.8", # iana.org. |
| 33 | + "time.observation": "2015-01-01T00:00:00+00:00", |
| 34 | + } |
31 | 35 |
|
32 | 36 |
|
33 | 37 | class BrokenInitExpertBot(ExpertBot): |
34 | 38 | def init(self): |
35 | 39 | raise ValueError('This initialization intionally raises an error!') |
36 | 40 |
|
37 | 41 |
|
| 42 | +class RaisesOnFirstRunExpertBot(ExpertBot): |
| 43 | + counter = 0 |
| 44 | + |
| 45 | + def init(self): |
| 46 | + self.counter = 0 |
| 47 | + |
| 48 | + def process(self): |
| 49 | + event = self.receive_message() |
| 50 | + self.counter += 1 |
| 51 | + if self.counter == 1: |
| 52 | + raise ValueError('This initialization intionally raises an error!') |
| 53 | + self.send_message(event) |
| 54 | + self.acknowledge_message() |
| 55 | + |
| 56 | + |
38 | 57 | def assertMessageEqual(actual, expected): |
39 | 58 | """ |
40 | 59 | Compare two messages as dicts. |
@@ -109,5 +128,18 @@ def test_bot_multi_message(): |
109 | 128 | assert queues['output'] == [EXAMPLE_DATA_URL_OUT] * 2 |
110 | 129 |
|
111 | 130 |
|
| 131 | +def test_bot_raises_and_second_message(): |
| 132 | + """ |
| 133 | + The first message raises an error and the second message |
| 134 | + This test is based on an issue where the exception-raising message was not cleared from the internal message store of the Bot/Pipeline instance and thus re-used on the second run |
| 135 | + """ |
| 136 | + raises_on_first_run = RaisesOnFirstRunExpertBot('raises', settings=BotLibSettings) |
| 137 | + with raises(ValueError): |
| 138 | + raises_on_first_run.process_message(EXAMPLE_DATA_URL) |
| 139 | + queues = raises_on_first_run.process_message(EXAMPLE_IP_INPUT) |
| 140 | + assert len(queues['output']) == 1 |
| 141 | + assertMessageEqual(queues['output'][0], EXAMPLE_IP_INPUT) |
| 142 | + |
| 143 | + |
112 | 144 | if __name__ == '__main__': # pragma: no cover |
113 | 145 | unittest.main() |
0 commit comments