Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test tanner-parse-response #192

Merged
merged 6 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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