def test_serialize(self): created = datetime.fromtimestamp(1628512774) message_id = UUID('63026767-029d-417e-9148-77f4da49f49a') group_id = UUID('866350e8-1492-497e-b12b-c079287d51dd') message = ResultMessage( created=created, message_id=message_id, group_id=group_id, scan_id='scan_1', host_ip='1.1.1.1', host_name='foo', oid='1.2.3.4.5', value='A Vulnerability has been found', uri='file://foo/bar', ) serialized = message.serialize() self.assertEqual(serialized['created'], 1628512774.0) self.assertEqual( serialized['message_id'], '63026767-029d-417e-9148-77f4da49f49a' ) self.assertEqual( serialized['group_id'], '866350e8-1492-497e-b12b-c079287d51dd' ) self.assertEqual(serialized['message_type'], 'result.scan') self.assertEqual(serialized['scan_id'], 'scan_1') self.assertEqual(serialized['host_ip'], '1.1.1.1') self.assertEqual(serialized['host_name'], 'foo') self.assertEqual(serialized['oid'], '1.2.3.4.5') self.assertEqual(serialized['value'], 'A Vulnerability has been found') self.assertEqual(serialized['uri'], 'file://foo/bar') self.assertEqual(serialized['port'], 'package') self.assertEqual(serialized['result_type'], 'ALARM')
def test_constructor(self): message = ResultMessage( scan_id='scan_1', host_ip='1.1.1.1', host_name='foo', oid='1.2.3.4.5', value='A Vulnerability has been found', uri='file://foo/bar', ) self.assertIsInstance(message.message_id, UUID) self.assertIsInstance(message.group_id, str) self.assertIsInstance(message.created, datetime) self.assertEqual(message.message_type, MessageType.RESULT) self.assertEqual(message.topic, 'scanner/scan/info') self.assertEqual(message.scan_id, 'scan_1') self.assertEqual(message.host_ip, '1.1.1.1') self.assertEqual(message.host_name, 'foo') self.assertEqual(message.oid, '1.2.3.4.5') self.assertEqual(message.value, 'A Vulnerability has been found') self.assertEqual(message.result_type, ResultType.ALARM) self.assertEqual(message.port, 'package') self.assertEqual(message.uri, 'file://foo/bar')
def test_notus_fail_cases(self): def start(self): self.function(*self.args, **self.kwargs) mock_report_func = mock.MagicMock(return_value=False) logging.Logger.warning = mock.MagicMock() notus = NotusResultHandler(mock_report_func) res_msg = ResultMessage( scan_id='scan_1', host_ip='1.1.1.1', host_name='foo', oid='1.2.3.4.5', value='A Vulnerability has been found', port="42", uri='file://foo/bar', ) with mock.patch.object(threading.Timer, 'start', start): notus.result_handler(res_msg) logging.Logger.warning.assert_called_with( # pylint: disable=no-member "Unable to report %d notus results for scan id %s.", 1, "scan_1" )
def test_deserialize_invalid_result_type(self): data = { 'message_id': '63026767-029d-417e-9148-77f4da49f49a', 'group_id': '866350e8-1492-497e-b12b-c079287d51dd', 'created': 1628512774.0, 'message_type': 'result.scan', 'scan_id': 'scan_1', 'host_ip': '1.1.1.1', 'host_name': 'foo', 'oid': '1.2.3.4.5', 'value': 'A Vulnerability has been found', 'uri': 'file://foo/bar', 'port': 'package', 'result_type': 'foo', } with self.assertRaisesRegex( ValueError, "'foo' is not a valid ResultType" ): ResultMessage.deserialize(data)
def test_deserialize_invalid_message_type(self): data = { 'message_id': '63026767-029d-417e-9148-77f4da49f49a', 'group_id': '866350e8-1492-497e-b12b-c079287d51dd', 'created': 1628512774.0, 'message_type': 'scan.status', 'scan_id': 'scan_1', 'host_ip': '1.1.1.1', 'host_name': 'foo', 'oid': '1.2.3.4.5', 'value': 'A Vulnerability has been found', 'uri': 'file://foo/bar', 'port': 'package', 'result_type': 'ALARM', } with self.assertRaisesRegex( ValueError, "Invalid message type MessageType.SCAN_STATUS for " "ResultMessage. Must be MessageType.RESULT.", ): ResultMessage.deserialize(data)
def test_subscribe(self): client = mock.MagicMock() callback = mock.MagicMock() callback.__name__ = "callback_name" subscriber = MQTTSubscriber(client) message = ResultMessage( scan_id='scan_1', host_ip='1.1.1.1', host_name='foo', oid='1.2.3.4.5', value='A Vulnerability has been found', uri='file://foo/bar', ) subscriber.subscribe(message, callback) client.subscribe.assert_called_with('scanner/scan/info', qos=1)
def test_publish(self): client = mock.MagicMock() publisher = MQTTPublisher(client) created = datetime.fromtimestamp(1628512774) message_id = UUID('63026767-029d-417e-9148-77f4da49f49a') group_id = UUID('866350e8-1492-497e-b12b-c079287d51dd') message = ResultMessage( created=created, message_id=message_id, group_id=group_id, scan_id='scan_1', host_ip='1.1.1.1', host_name='foo', oid='1.2.3.4.5', value='A Vulnerability has been found', port='42', uri='file://foo/bar', ) publisher.publish(message) client.publish.assert_called_with( 'scanner/scan/info', '{"message_id": "63026767-029d-417e-9148-77f4da49f49a", ' '"message_type": "result.scan", ' '"group_id": "866350e8-1492-497e-b12b-c079287d51dd", ' '"created": 1628512774.0, ' '"scan_id": "scan_1", ' '"host_ip": "1.1.1.1", ' '"host_name": "foo", ' '"oid": "1.2.3.4.5", ' '"value": "A Vulnerability has been found", ' '"port": "42", ' '"uri": "file://foo/bar", ' '"result_type": "ALARM"}', qos=1, )
def test_deserialize(self): data = { 'message_id': '63026767-029d-417e-9148-77f4da49f49a', 'group_id': '866350e8-1492-497e-b12b-c079287d51dd', 'created': 1628512774.0, 'message_type': 'result.scan', 'scan_id': 'scan_1', 'host_ip': '1.1.1.1', 'host_name': 'foo', 'oid': '1.2.3.4.5', 'value': 'A Vulnerability has been found', 'uri': 'file://foo/bar', 'port': 'package', 'result_type': 'ALARM', } message = ResultMessage.deserialize(data) self.assertEqual( message.message_id, UUID('63026767-029d-417e-9148-77f4da49f49a') ) self.assertEqual( message.group_id, '866350e8-1492-497e-b12b-c079287d51dd' ) self.assertEqual( message.created, datetime.fromtimestamp(1628512774.0, tz=timezone.utc), ) self.assertEqual(message.message_type, MessageType.RESULT) self.assertEqual(message.scan_id, 'scan_1') self.assertEqual(message.host_ip, '1.1.1.1') self.assertEqual(message.host_name, 'foo') self.assertEqual(message.oid, '1.2.3.4.5') self.assertEqual(message.value, 'A Vulnerability has been found') self.assertEqual(message.uri, 'file://foo/bar') self.assertEqual(message.port, 'package') self.assertEqual(message.result_type, ResultType.ALARM)