From 59a84685d7c686f05f8143b960e906951b3d4e6e Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 20 Jun 2018 09:24:52 +0200 Subject: [PATCH] Add dummy stop_scan method to be implemented in the wrapper. Add test testScanAndResult.py calling --- ospd/ospd.py | 5 +++++ tests/testScanAndResult.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ospd/ospd.py b/ospd/ospd.py index a789d4ce..cbb7b8a9 100644 --- a/ospd/ospd.py +++ b/ospd/ospd.py @@ -448,6 +448,7 @@ def handle_stop_scan_command(self, scan_et): raise OSPDError('Scan already stopped or finished.', 'stop_scan') logger.info('{0}: Scan stopping {1}.'.format(scan_id, scan_process.ident)) + self.stop_scan(scan_id) scan_process.terminate() os.killpg(os.getpgid(scan_process.ident), 15) scan_process.join() @@ -456,6 +457,10 @@ def handle_stop_scan_command(self, scan_et): logger.info('{0}: Scan stopped.'.format(scan_id)) return simple_response_str('stop_scan', 200, 'OK') + def stop_scan(self, scan_id): + """ Should be implemented by subclass in case of a clean up before + terminating is needed. """ + def exec_scan(self, scan_id, target): """ Asserts to False. Should be implemented by subclass. """ raise NotImplementedError diff --git a/tests/testScanAndResult.py b/tests/testScanAndResult.py index 72847113..07f79dbe 100644 --- a/tests/testScanAndResult.py +++ b/tests/testScanAndResult.py @@ -129,3 +129,21 @@ def testiScanWithError(self): response = ET.fromstring(daemon.handle_command('' % scan_id)) self.assertEqual(response.get('status'), '200') print(ET.tostring(response)) + + def testStopScan(self): + daemon = DummyWrapper([]) + response = ET.fromstring( + daemon.handle_command('' + + '')) + print(ET.tostring(response)) + scan_id = response.findtext('id') + time.sleep(0.01) + + response = daemon.stop_scan(scan_id) + self.assertEqual(response, None) + + response = ET.fromstring(daemon.handle_command( + '' % scan_id)) + self.assertEqual(response.get('status'), '200') + print(ET.tostring(response))