From 3d70820a3eae428c3cb288ad0f213bf69f0f728d Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Thu, 14 Mar 2019 00:21:48 +0530 Subject: [PATCH 1/6] test tanner-parse-response --- snare/tests/test_parse_tanner_response.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) mode change 100644 => 100755 snare/tests/test_parse_tanner_response.py diff --git a/snare/tests/test_parse_tanner_response.py b/snare/tests/test_parse_tanner_response.py old mode 100644 new mode 100755 index b932cfe8..aa3bedf0 --- a/snare/tests/test_parse_tanner_response.py +++ b/snare/tests/test_parse_tanner_response.py @@ -73,6 +73,27 @@ async def test(): expected_result = [self.expected_content, self.content_type, {}, 200] self.assertCountEqual(real_result, expected_result) + self.detection = { + "type": 2, + "payload": { + "page": "", + "value": "test.png", + "headers": { + "content-type": "multipart/form-data" + } + } + } + + async def test(): + (self.res1, self.res2, + self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) + + self.loop.run_until_complete(test()) + expected_header = {"content-type": "multipart/form-data"} + real_result = [self.res1, self.res2, self.res3, self.res4] + expected_result = [b'test.png', 'image/png', expected_header, 200] + self.assertCountEqual(real_result, expected_result) + def test_parse_type_three(self): self.detection = { "type": 3, From ba76782980c473466a0f651beff201a2fd7c1d14 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Thu, 14 Mar 2019 14:41:20 +0530 Subject: [PATCH 2/6] test separated --- snare/tests/test_parse_tanner_response.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snare/tests/test_parse_tanner_response.py b/snare/tests/test_parse_tanner_response.py index aa3bedf0..c142eb34 100755 --- a/snare/tests/test_parse_tanner_response.py +++ b/snare/tests/test_parse_tanner_response.py @@ -73,6 +73,8 @@ async def test(): expected_result = [self.expected_content, self.content_type, {}, 200] self.assertCountEqual(real_result, expected_result) + def test_parse_type_two_with_headers(self): + self.detection = { "type": 2, "payload": { From 8186a68c227b744dfc636ffadcd736c4bc473a08 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Thu, 14 Mar 2019 23:16:01 +0530 Subject: [PATCH 3/6] minor changes --- snare/tests/test_parse_tanner_response.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/snare/tests/test_parse_tanner_response.py b/snare/tests/test_parse_tanner_response.py index c142eb34..74128400 100755 --- a/snare/tests/test_parse_tanner_response.py +++ b/snare/tests/test_parse_tanner_response.py @@ -40,6 +40,7 @@ def setUp(self): self.detection = None self.expected_content = None self.call_content = None + self.expected_header = None def test_parse_type_one(self): self.detection = {"type": 1} @@ -85,15 +86,17 @@ def test_parse_type_two_with_headers(self): } } } + self.expected_content = b'test.png' + self.content_type = 'image/png' + self.expected_header = {"content-type": "multipart/form-data"} async def test(): (self.res1, self.res2, self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) self.loop.run_until_complete(test()) - expected_header = {"content-type": "multipart/form-data"} real_result = [self.res1, self.res2, self.res3, self.res4] - expected_result = [b'test.png', 'image/png', expected_header, 200] + expected_result = [self.expected_content, self.content_type, self.expected_header, 200] self.assertCountEqual(real_result, expected_result) def test_parse_type_three(self): From 127f99e3b2a1093f718750efdf47c3c15434c7ea Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Fri, 15 Mar 2019 02:03:45 +0530 Subject: [PATCH 4/6] Increased coverage final --- snare/tests/test_parse_tanner_response.py | 78 +++++++++++++++++------ 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/snare/tests/test_parse_tanner_response.py b/snare/tests/test_parse_tanner_response.py index 74128400..81f88fd1 100755 --- a/snare/tests/test_parse_tanner_response.py +++ b/snare/tests/test_parse_tanner_response.py @@ -24,12 +24,12 @@ def setUp(self): f.write(self.page_content) with open(os.path.join(self.main_page_path, "meta.json"), 'w') as f: json.dump(meta_content, f) - args = run_args.parse_args(['--page-dir', page_dir]) - args.index_page = '/index.html' - args.no_dorks = True - args.tanner = "tanner.mushmush.org" - uuid = "test_uuid" - self.handler = TannerHandler(args, meta_content, uuid) + self.args = run_args.parse_args(['--page-dir', page_dir]) + self.args.index_page = '/index.html' + self.args.no_dorks = True + self.args.tanner = "tanner.mushmush.org" + self.uuid = "test_uuid" + self.handler = TannerHandler(self.args, meta_content, self.uuid) self.requested_name = '/' self.loop = asyncio.get_event_loop() self.handler.html_handler.handle_content = AsyncMock(return_value=self.page_content) @@ -41,6 +41,7 @@ def setUp(self): self.expected_content = None self.call_content = None self.expected_header = None + self.expected_status_code = None def test_parse_type_one(self): self.detection = {"type": 1} @@ -55,6 +56,36 @@ async def test(): expected_result = [self.page_content, self.content_type, {}, 200] self.assertCountEqual(real_result, expected_result) + def test_parse_type_one_query(self): + self.requested_name = '/?' + self.detection = {"type": 1} + self.call_content = b'' + + async def test(): + (self.res1, self.res2, + self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) + + self.loop.run_until_complete(test()) + real_result = [self.res1, self.res2, self.res3, self.res4] + expected_result = [self.page_content, self.content_type, {}, 200] + self.assertCountEqual(real_result, expected_result) + + def test_parse_type_one_error(self): + self.detection = {"type": 1} + meta_content = {"/index.html": {}} + self.requested_name = 'something/' + self.handler = TannerHandler(self.args, meta_content, self.uuid) + self.expected_status_code = 404 + + async def test(): + (self.res1, self.res2, + self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) + + self.loop.run_until_complete(test()) + real_result = [self.res1, self.res2, self.res3, self.res4] + expected_result = [None, None, {}, self.expected_status_code] + self.assertCountEqual(real_result, expected_result) + def test_parse_type_two(self): self.detection = { "type": 2, @@ -99,6 +130,29 @@ async def test(): expected_result = [self.expected_content, self.content_type, self.expected_header, 200] self.assertCountEqual(real_result, expected_result) + def test_parse_type_two_error(self): + + self.detection = { + "type": 2, + "payload": { + "page": "/index.html", + "value": "test" + } + } + meta_content = {"/index.html": {}} + self.handler = TannerHandler(self.args, meta_content, self.uuid) + self.expected_content = b'
test
' + self.content_type = r'text\html' + + async def test(): + (self.res1, self.res2, + self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) + + self.loop.run_until_complete(test()) + real_result = [self.res1, self.res2, self.res3, self.res4] + expected_result = [self.expected_content, self.content_type, {}, 200] + self.assertCountEqual(real_result, expected_result) + def test_parse_type_three(self): self.detection = { "type": 3, @@ -131,17 +185,5 @@ async def test(): self.loop.run_until_complete(test()) self.handler.html_handler.handle_content.assert_called_with(self.call_content) - def test_parse_exception(self): - self.detection = {} - self.call_content = b'' - self.expected_content = self.page_content - - async def test(): - (self.res1, self.res2, - self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) - - with self.assertRaises(KeyError): - self.loop.run_until_complete(test()) - def tearDown(self): shutil.rmtree(self.main_page_path) From 9357b8d2d3782ee2beebb8ea44feb0f9dec80ed9 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Mon, 25 Mar 2019 21:17:14 +0530 Subject: [PATCH 5/6] updated changes --- snare/tests/test_parse_tanner_response.py | 20 ++++++++++++++------ snare/tests/test_submit_data.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/snare/tests/test_parse_tanner_response.py b/snare/tests/test_parse_tanner_response.py index 81f88fd1..acec4247 100755 --- a/snare/tests/test_parse_tanner_response.py +++ b/snare/tests/test_parse_tanner_response.py @@ -72,9 +72,7 @@ async def test(): def test_parse_type_one_error(self): self.detection = {"type": 1} - meta_content = {"/index.html": {}} self.requested_name = 'something/' - self.handler = TannerHandler(self.args, meta_content, self.uuid) self.expected_status_code = 404 async def test(): @@ -135,14 +133,12 @@ def test_parse_type_two_error(self): self.detection = { "type": 2, "payload": { - "page": "/index.html", + "page": "/something", "value": "test" } } - meta_content = {"/index.html": {}} - self.handler = TannerHandler(self.args, meta_content, self.uuid) self.expected_content = b'
test
' - self.content_type = r'text\html' + self.content_type = r'text/html' async def test(): (self.res1, self.res2, @@ -185,5 +181,17 @@ async def test(): self.loop.run_until_complete(test()) self.handler.html_handler.handle_content.assert_called_with(self.call_content) + def test_parse_exception(self): + self.detection = {} + self.call_content = b'' + self.expected_content = self.page_content + + async def test(): + (self.res1, self.res2, + self.res3, self.res4) = await self.handler.parse_tanner_response(self.requested_name, self.detection) + + with self.assertRaises(KeyError): + self.loop.run_until_complete(test()) + def tearDown(self): shutil.rmtree(self.main_page_path) diff --git a/snare/tests/test_submit_data.py b/snare/tests/test_submit_data.py index 0f494297..ecbca7c0 100644 --- a/snare/tests/test_submit_data.py +++ b/snare/tests/test_submit_data.py @@ -6,10 +6,14 @@ import json import yarl import aiohttp +import logging +from unittest import mock from snare.utils.asyncmock import AsyncMock from snare.tanner_handler import TannerHandler from snare.utils.page_path_generator import generate_unique_path +logger = logging.getLogger(__name__) + class TestSubmitData(unittest.TestCase): def setUp(self): @@ -65,6 +69,21 @@ async def test(): self.loop.run_until_complete(test()) self.assertEqual(self.result, dict(detection={'type': 1}, sess_uuid="test_uuid")) + def test_submit_data_error(self): + + async def mock_json_decode_error(data): + logger.error('Error submitting data: JSONDecodeError {}'.format(data)) + + self.handler = mock.Mock() + self.handler.submit_data = mock_json_decode_error + + async def test(): + self.result = await self.handler.submit_data(self.data) + + with self.assertLogs(level='ERROR') as log: + self.loop.run_until_complete(test()) + self.assertIn('Error submitting data: JSONDecodeError {}'.format(self.data), log.output[0]) + def test_event_result_exception(self): aiohttp.ClientResponse.json = AsyncMock(side_effect=Exception()) From 81e3d9530eab84bdcdda4eeb4a314f512f73a003 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Wed, 27 Mar 2019 19:34:21 +0530 Subject: [PATCH 6/6] 100% reached --- snare/tests/test_submit_data.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/snare/tests/test_submit_data.py b/snare/tests/test_submit_data.py index ecbca7c0..27b6f67c 100644 --- a/snare/tests/test_submit_data.py +++ b/snare/tests/test_submit_data.py @@ -6,14 +6,11 @@ import json import yarl import aiohttp -import logging -from unittest import mock +from json import JSONDecodeError from snare.utils.asyncmock import AsyncMock from snare.tanner_handler import TannerHandler from snare.utils.page_path_generator import generate_unique_path -logger = logging.getLogger(__name__) - class TestSubmitData(unittest.TestCase): def setUp(self): @@ -70,19 +67,14 @@ async def test(): self.assertEqual(self.result, dict(detection={'type': 1}, sess_uuid="test_uuid")) def test_submit_data_error(self): - - async def mock_json_decode_error(data): - logger.error('Error submitting data: JSONDecodeError {}'.format(data)) - - self.handler = mock.Mock() - self.handler.submit_data = mock_json_decode_error + aiohttp.ClientResponse.json = AsyncMock(side_effect=JSONDecodeError('ERROR', '', 0)) async def test(): self.result = await self.handler.submit_data(self.data) with self.assertLogs(level='ERROR') as log: self.loop.run_until_complete(test()) - self.assertIn('Error submitting data: JSONDecodeError {}'.format(self.data), log.output[0]) + self.assertIn('Error submitting data: ERROR: line 1 column 1 (char 0) {}'.format(self.data), log.output[0]) def test_event_result_exception(self): aiohttp.ClientResponse.json = AsyncMock(side_effect=Exception())