Skip to content

Commit

Permalink
test tanner-parse-response (#192)
Browse files Browse the repository at this point in the history
* test tanner-parse-response

* test separated

* minor changes

* Increased coverage final

* updated changes

* 100% reached
  • Loading branch information
rjt-gupta authored and rnehra01 committed Mar 28, 2019
1 parent 775e327 commit b00eb70
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
88 changes: 82 additions & 6 deletions snare/tests/test_parse_tanner_response.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,6 +40,8 @@ def setUp(self):
self.detection = None
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}
Expand All @@ -54,6 +56,34 @@ 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'<html><body></body></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.page_content, self.content_type, {}, 200]
self.assertCountEqual(real_result, expected_result)

def test_parse_type_one_error(self):
self.detection = {"type": 1}
self.requested_name = 'something/'
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,
Expand All @@ -73,6 +103,52 @@ 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": {
"page": "",
"value": "test.png",
"headers": {
"content-type": "multipart/form-data"
}
}
}
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())
real_result = [self.res1, self.res2, self.res3, self.res4]
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": "/something",
"value": "test"
}
}
self.expected_content = b'<html><body><div>test</div></body></html>'
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,
Expand Down
11 changes: 11 additions & 0 deletions snare/tests/test_submit_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import yarl
import aiohttp
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
Expand Down Expand Up @@ -65,6 +66,16 @@ 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):
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: 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())

Expand Down

0 comments on commit b00eb70

Please sign in to comment.