Skip to content

Commit

Permalink
Ever more tests! 🤖
Browse files Browse the repository at this point in the history
  • Loading branch information
leonjza committed Oct 10, 2017
1 parent 2c817e3 commit d11bb58
Show file tree
Hide file tree
Showing 14 changed files with 1,045 additions and 2 deletions.
Empty file added tests/state/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/state/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

from objection.state.app import app_state


class TestApp(unittest.TestCase):
def test_app_should_not_debug_hooks_by_default(self):
self.assertFalse(app_state.should_debug_hooks())
34 changes: 34 additions & 0 deletions tests/state/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest

from objection.state.connection import state_connection


class TestConnection(unittest.TestCase):
def setUp(self):
pass

def test_default_type_is_usb(self):
comms_type = state_connection.get_comms_type()
comms_type_string = state_connection.get_comms_type_string()

self.assertEqual(comms_type, 0)
self.assertEqual(comms_type_string, 'usb')

def test_sets_type_to_network(self):
state_connection.use_network()

comms_type = state_connection.get_comms_type()
comms_type_string = state_connection.get_comms_type_string()

self.assertEqual(comms_type, 1)
self.assertEqual(comms_type_string, 'net')

def test_sets_type_usb_after_setting_type_network(self):
state_connection.use_network()
state_connection.use_usb()

comms_type = state_connection.get_comms_type()
comms_type_string = state_connection.get_comms_type_string()

self.assertEqual(comms_type, 0)
self.assertEqual(comms_type_string, 'usb')
11 changes: 11 additions & 0 deletions tests/state/test_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest

from objection.state.device import device_state


class TestDevice(unittest.TestCase):
def test_device_representation(self):
device_state.device_type = 'ios'
device_state.frida_version = '10.6.1'

self.assertEqual(repr(device_state), '<Type: ios Frida Version:10.6.1>')
25 changes: 25 additions & 0 deletions tests/state/test_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from objection.state.jobs import job_manager_state


class TestJobManager(unittest.TestCase):
def tearDown(self):
job_manager_state.jobs = []

def test_job_manager_starts_with_empty_jobs(self):
self.assertEqual(len(job_manager_state.jobs), 0)

def test_adds_jobs(self):
job_manager_state.add_job('foo')

self.assertEqual(len(job_manager_state.jobs), 1)

def test_removes_jobs(self):
job_manager_state.add_job('foo')
job_manager_state.add_job('bar')

job_manager_state.remove_job('foo')
job_manager_state.remove_job('bar')

self.assertEqual(len(job_manager_state.jobs), 0)
56 changes: 56 additions & 0 deletions tests/state/test_sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
from unittest import mock

from objection.state.sqlite import sqlite_manager_state
from ..helpers import capture


class TestSQLite(unittest.TestCase):
def tearDown(self):
sqlite_manager_state.file = sqlite_manager_state.temp_file = None

def test_reports_not_connected_by_default(self):
status = sqlite_manager_state.is_connected()

self.assertFalse(status)

def test_reports_connected_with_file_and_tempfile_set(self):
sqlite_manager_state.file = 'foo'
sqlite_manager_state.temp_file = 'bar'

status = sqlite_manager_state.is_connected()

self.assertTrue(status)

@mock.patch('objection.state.sqlite.tempfile')
def test_gets_new_cache_directory_for_temp_storage(self, mock_tempfile):
mock_tempfile.mkstemp.return_value = 1, '/tmp/foo'

directory = sqlite_manager_state.get_cache_dir()

self.assertEqual(directory, '/tmp/foo')

def test_gets_existing_temp_directory_for_temp_storage(self):
sqlite_manager_state.temp_file = '/foo/bar'

directory = sqlite_manager_state.get_cache_dir()

self.assertEqual(directory, '/foo/bar')

@mock.patch('objection.state.sqlite.os')
def test_will_cleanup_when_connected(self, mock_os):
mock_os.remove.return_value = None

sqlite_manager_state.file = 'foo'
sqlite_manager_state.temp_file = 'bar'

with capture(sqlite_manager_state.cleanup) as o:
output = o

self.assertEqual(output, '[sqlite manager] Removing cached copy of SQLite database: foo at bar\n')
self.assertIsNone(sqlite_manager_state.file)
self.assertIsNone(sqlite_manager_state.temp_file)
self.assertIsNone(sqlite_manager_state.full_remote_file)

def test_representation(self):
self.assertEqual(repr(sqlite_manager_state), '<File:None LocalTemp:None>')
Empty file.
137 changes: 137 additions & 0 deletions tests/utils/patchers/test_android.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import os
import unittest
from unittest import mock

from objection.utils.patchers.android import AndroidGadget, AndroidPatcher


class TestAndroidGadget(unittest.TestCase):
@mock.patch('objection.utils.patchers.android.Github')
@mock.patch('objection.utils.patchers.android.os')
def setUp(self, github, mock_os):
mock_os.path.exists.return_value = True

self.android_gadget = AndroidGadget(github)

self.github_get_assets_sample = [
{
"url": "https://api.github.com/repos/frida/frida/releases/assets/5005221",
"id": 5005221,
"name": "frida-gadget-10.6.8-android-x86.so.xz",
"label": "",
"uploader": {
"id": 735197,
},
"state": "uploaded",
"size": 12912624,
"download_count": 1,
"created_at": "2017-10-07T00:01:10Z",
"updated_at": "2017-10-07T00:01:17Z",
"browser_download_url": "https://github.com/frida/frida/releases/download/"
"10.6.8/frida-gadget-10.6.8-android-x86.so.xz"
}
]

def test_sets_architecture(self):
self.android_gadget.set_architecture('x86')
self.assertEqual(self.android_gadget.architecture, 'x86')

def test_raises_exception_with_invalid_architecture(self):
with self.assertRaises(Exception) as _:
self.android_gadget.set_architecture('foo')

def test_sets_architecture_and_returns_context(self):
result = self.android_gadget.set_architecture('x86')
self.assertEqual(type(result), AndroidGadget)

def test_gets_architecture_when_set(self):
self.android_gadget.set_architecture('x86')
architecture = self.android_gadget.get_architecture()

self.assertEqual(architecture, 'x86')

def test_gets_frida_library_path(self):
self.android_gadget.set_architecture('x86')

frida_path = self.android_gadget.get_frida_library_path()
self.assertTrue('.objection/android/x86/libfrida-gadget.so' in frida_path)

def test_fails_to_get_frida_library_path_without_architecture(self):
with self.assertRaises(Exception) as _:
self.android_gadget.get_frida_library_path()

@mock.patch('objection.utils.patchers.android.os')
def test_checks_if_gadget_exists_if_it_really_exists(self, mock_os):
mock_os.path.exists.return_value = True
self.android_gadget.set_architecture('x86')

status = self.android_gadget.gadget_exists()

self.assertTrue(status)

@mock.patch('objection.utils.patchers.android.os')
def test_checks_if_gadget_exists_if_it_really_does_not_exist(self, mock_os):
mock_os.path.exists.return_value = False
self.android_gadget.set_architecture('x86')

status = self.android_gadget.gadget_exists()

self.assertFalse(status)

def test_check_if_gadget_exists_fails_without_architecture(self):
with self.assertRaises(Exception) as _:
self.android_gadget.gadget_exists()

def test_can_find_download_url_for_gadget(self):
mock_github = mock.MagicMock()
mock_github.get_assets.return_value = self.github_get_assets_sample

self.android_gadget.github = mock_github
self.android_gadget.architecture = 'x86'

# the method we actually testing here!
url = self.android_gadget._get_download_url()

self.assertEqual(url, 'https://github.com/frida/frida/releases/download/'
'10.6.8/frida-gadget-10.6.8-android-x86.so.xz')

def test_throws_exception_when_download_url_could_not_be_determined(self):
mock_github = mock.MagicMock()
mock_github.get_assets.return_value = self.github_get_assets_sample

self.android_gadget.github = mock_github
self.android_gadget.architecture = 'arm'

# the method we actually testing here!
with self.assertRaises(Exception) as _:
self.android_gadget._get_download_url()


class TestAndroidPatcher(unittest.TestCase):
@mock.patch('objection.utils.patchers.android.BasePlatformPatcher.__init__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.android.AndroidPatcher.__del__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.android.tempfile')
def test_inits_patcher(self, tempfile):
tempfile.mkdtemp.return_value = '/tmp/test'

patcher = AndroidPatcher()

self.assertIsNone(patcher.apk_source)
self.assertEqual(patcher.apk_temp_directory, '/tmp/test')
self.assertEqual(patcher.apk_temp_frida_patched, '/tmp/test.objection.apk')
self.assertFalse(patcher.skip_cleanup)
self.assertTrue('objection/utils/patchers/../assets/objection.jks' in patcher.keystore)
self.assertTrue(os.path.exists(patcher.keystore))

@mock.patch('objection.utils.patchers.android.AndroidPatcher.__init__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.android.AndroidPatcher.__del__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.android.tempfile')
@mock.patch('objection.utils.patchers.android.os')
def test_set_android_apk_source(self, _, mock_os):
mock_os.path.exists.return_value = True
patcher = AndroidPatcher()

source = patcher.set_apk_source('foo.apk')

self.assertEqual(type(source), AndroidPatcher)
self.assertEqual(patcher.apk_source, 'foo.apk')
77 changes: 77 additions & 0 deletions tests/utils/patchers/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest
from unittest import mock

from objection.utils.patchers.base import BasePlatformGadget, BasePlatformPatcher
from ...helpers import capture


class TestBasePlatformGadget(unittest.TestCase):
@mock.patch('objection.utils.patchers.base.Github')
def setUp(self, mock_github):
self.gadget = BasePlatformGadget(github=mock_github)

@mock.patch('objection.utils.patchers.base.os')
def test_sets_version_to_zero_if_no_local_record_is_found(self, mock_os):
mock_os.path.exists.return_value = False
version = self.gadget.get_local_version('test')

self.assertEqual(version, '0')


class TestBasePlatformPatcher(unittest.TestCase):
def setUp(self):
pass

@mock.patch('objection.utils.patchers.base.BasePlatformPatcher._check_commands', mock.Mock(return_value=True))
def test_inits_base_patcher(self):
base_patcher = BasePlatformPatcher()

self.assertTrue(base_patcher.have_all_commands)
self.assertEqual(base_patcher.command_run_timeout, 300)

@mock.patch('objection.utils.patchers.base.BasePlatformPatcher._check_commands', mock.Mock(return_value=True))
def test_are_requirements_met_returns_true_if_met(self):
base_patcher = BasePlatformPatcher()

self.assertTrue(base_patcher.are_requirements_met())

@mock.patch('objection.utils.patchers.base.BasePlatformPatcher._check_commands', mock.Mock(return_value=False))
def test_are_requirements_met_returns_false_if_not_met(self):
base_patcher = BasePlatformPatcher()

self.assertFalse(base_patcher.are_requirements_met())

@mock.patch('objection.utils.patchers.base.BasePlatformPatcher.__init__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.base.shutil')
def test_check_commands_finds_commands_and_sets_location(self, mock_shutil):
mock_shutil.which.return_value = '/bin/test'

base_patcher = BasePlatformPatcher()
base_patcher.required_commands = {
'aapt': {
'installation': 'apt install aapt (Kali Linux)'
}
}

check_result = base_patcher._check_commands()

self.assertTrue(check_result)
self.assertEqual(base_patcher.required_commands['aapt']['location'], '/bin/test')

@mock.patch('objection.utils.patchers.base.BasePlatformPatcher.__init__', mock.Mock(return_value=None))
@mock.patch('objection.utils.patchers.base.shutil')
def test_check_commands_fails_to_find_command_and_displays_error(self, mock_shutil):
mock_shutil.which.return_value = None

base_patcher = BasePlatformPatcher()
base_patcher.required_commands = {
'aapt': {
'installation': 'apt install aapt (Kali Linux)'
}
}

with capture(base_patcher._check_commands) as o:
output = o

self.assertEqual(output, 'Unable to find aapt. Install it with:'
' apt install aapt (Kali Linux) before continuing.\n')
Loading

0 comments on commit d11bb58

Please sign in to comment.