Skip to content

Patch tests, set request timeout to 0.1 sec for dead servers (just in case) #25

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

Merged
merged 1 commit into from
Jul 23, 2020
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
8 changes: 5 additions & 3 deletions src/scripts/other/network_usage/test_module.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python3

from unittest import TestCase
from .module import Runner
from http.server import HTTPServer, BaseHTTPRequestHandler
from random import randrange
from threading import Thread
from unittest import TestCase

from .module import Runner


class DefaultValues:
Expand All @@ -12,7 +14,7 @@ class DefaultValues:
"""

HOST = "127.0.0.1"
PORT = 1337
PORT = randrange(20_000, 65_635)


class TestClassHTTPRequestHandler(BaseHTTPRequestHandler):
Expand Down
38 changes: 35 additions & 3 deletions src/scripts/recon/allowed_methods/test_module.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python3

from unittest import TestCase
from .module import Runner
from http.server import HTTPServer, BaseHTTPRequestHandler
from random import randrange
from threading import Thread
from unittest import TestCase

from .module import Runner


class DefaultValues:
Expand All @@ -12,7 +14,7 @@ class DefaultValues:
"""

HOST = "127.0.0.1"
PORT = 1337
PORT = randrange(20_000, 65_635)


class TestClassHTTPRequestHandler(BaseHTTPRequestHandler):
Expand Down Expand Up @@ -209,6 +211,36 @@ def test_no_response(self):
"""
Tests module on offline server.
"""
original_run = self.runner.run

def patch_run(*args, **kwargs):
"""
Patch run function from module to provide request timeout, monkey patching boi!
:param args: original args
:param kwargs: original kwargs
:return: results of the original 'runner.run' function
"""
import src.scripts.recon.allowed_methods.module
original_request = src.scripts.recon.allowed_methods.module.request

def patch_request(*_args, **_kwargs):
"""
Patch Python 'requests' module 'request' method to provide timeout
:param _args: original args
:param _kwargs: original kwargs
:return: patched request
"""
return original_request(*_args, **_kwargs, timeout=0.1)

# Wrap runner in timeout request
src.scripts.recon.allowed_methods.module.request = patch_request
original_run_results = original_run(*args, **kwargs)
src.scripts.recon.allowed_methods.module.request = original_request

return original_run_results

self.runner.run = patch_run

result = self.runner.run(
url=f"http://{DefaultValues.HOST}:{DefaultValues.PORT}"
)
Expand Down
40 changes: 36 additions & 4 deletions src/scripts/recon/shodan_favicon_hash/test_module.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python3

from unittest import TestCase
from .module import Runner
from http.server import HTTPServer, BaseHTTPRequestHandler
from pathlib import Path
from random import randrange
from threading import Thread
from unittest import TestCase

from pathlib import Path
from .module import Runner


class DefaultValues:
Expand All @@ -14,7 +15,7 @@ class DefaultValues:
"""

HOST = "127.0.0.1"
PORT = 1337
PORT = randrange(20_000, 65_635)


class TestClassHTTPRequestHandler(BaseHTTPRequestHandler):
Expand Down Expand Up @@ -121,6 +122,37 @@ def test_no_response(self):
"""
Tests module on offline server.
"""

original_run = self.runner.run

def patch_run(*args, **kwargs):
"""
Patch run function from module to provide request timeout, monkey patching boi!
:param args: original args
:param kwargs: original kwargs
:return: results of the original 'runner.run' function
"""
import src.scripts.recon.shodan_favicon_hash.module
original_request = src.scripts.recon.shodan_favicon_hash.module.get

def patch_request(*_args, **_kwargs):
"""
Patch Python 'requests' module 'get' method to provide timeout
:param _args: original args
:param _kwargs: original kwargs
:return: patched request
"""
return original_request(*_args, **_kwargs, timeout=0.1)

# Wrap runner in timeout request
src.scripts.recon.shodan_favicon_hash.module.get = patch_request
original_run_results = original_run(*args, **kwargs)
src.scripts.recon.shodan_favicon_hash.module.get = original_request

return original_run_results

self.runner.run = patch_run

result = self.runner.run(
url=f"http://{DefaultValues.HOST}:{DefaultValues.PORT}"
)
Expand Down