-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixes #13 with unit tests; improves Jep bridge
- Loading branch information
1 parent
84a9077
commit fdf6c45
Showing
7 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,6 @@ dist/** | |
*.exe | ||
.gradle/** | ||
/bin/ | ||
|
||
# Python ignore | ||
**/__pycache__/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (C) 2022 Mandiant, Inc. All Rights Reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at: [package root]/LICENSE.txt | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License | ||
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
"""Used to configure builtins from Java""" | ||
|
||
|
||
def jep_set_builtin(attr, o): | ||
setattr(__builtins__, attr, o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (C) 2022 Mandiant, Inc. All Rights Reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at: [package root]/LICENSE.txt | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License | ||
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
"""Proxy Python isinstance and issubclass calls | ||
See Jep: https://github.com/ninia/jep/issues/438 | ||
Note: we should remove this code when Jep https://github.com/ninia/jep/pull/440 is merged and released | ||
""" | ||
|
||
import builtins | ||
|
||
_saved_isinstance = builtins.isinstance | ||
_saved_issubclass = builtins.issubclass | ||
|
||
|
||
def _proxy_isinstance(obj, classinfo): | ||
if "jep.PyJClass" in str(type(classinfo)): | ||
classinfo = classinfo.__pytype__ | ||
return _saved_isinstance(obj, classinfo) | ||
|
||
|
||
def _proxy_issubclass(obj, classinfo): | ||
if "jep.PyJClass" in str(type(obj)): | ||
obj = obj.__pytype__ | ||
if "jep.PyJClass" in str(type(classinfo)): | ||
classinfo = classinfo.__pytype__ | ||
return _saved_issubclass(obj, classinfo) | ||
|
||
|
||
builtins.isinstance = _proxy_isinstance | ||
builtins.issubclass = _proxy_issubclass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Run Ghidrathon unit tests. | ||
# @author Mike Hunhoff ([email protected]) | ||
# @category Python 3 | ||
# Copyright (C) 2022 Mandiant, Inc. All Rights Reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at: [package root]/LICENSE.txt | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License | ||
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
"""Light harness used to run Python tests | ||
Note: you must run this harness from the Ghidra script manager or headless mode | ||
""" | ||
|
||
import unittest | ||
import pathlib | ||
|
||
|
||
def main(): | ||
loader = unittest.TestLoader() | ||
result = unittest.TestResult() | ||
|
||
directory = str(pathlib.Path(__file__).resolve().parent) | ||
|
||
suite = loader.discover(directory, pattern="test_*.py") | ||
_ = unittest.TextTestRunner(verbosity=2, failfast=True).run(suite) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (C) 2022 Mandiant, Inc. All Rights Reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at: [package root]/LICENSE.txt | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License | ||
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and limitations under the License. | ||
|
||
"""Unit tests to verify Ghidra Jep bridge | ||
Note: you must run these tests from the Ghidra script manager or headless mode | ||
""" | ||
|
||
import unittest | ||
|
||
|
||
class TestJepBridge(unittest.TestCase): | ||
def assertIsJavaObject(self, o): | ||
from java.lang import Object | ||
|
||
if not (o is None or isinstance(o, Object)): | ||
raise AssertionError("Object %s is not valid" % str(o)) | ||
|
||
def test_type_instance(self): | ||
# see Jep: https://github.com/ninia/jep/blob/15e36a7ba54eb7d8f7ffd85f16675fa4fd54eb1d/src/test/python/test_import.py#L54-L65 | ||
from java.lang import Object | ||
from java.io import Serializable | ||
from java.util import Date | ||
from ghidra.program.database import ProgramDB | ||
|
||
self.assertIsInstance(Date(), Object.__pytype__) | ||
self.assertIsInstance(Date(), Serializable.__pytype__) | ||
self.assertTrue(issubclass(Date.__pytype__, Object.__pytype__)) | ||
self.assertTrue(issubclass(Date.__pytype__, Serializable.__pytype__)) | ||
self.assertIsInstance(Date(), Object) | ||
self.assertIsInstance(Date(), Serializable) | ||
self.assertTrue(issubclass(Date, Object)) | ||
self.assertTrue(issubclass(Date, Serializable)) | ||
self.assertIsInstance(currentProgram, ProgramDB) | ||
|
||
def test_ghidra_script_variables(self): | ||
self.assertIsJavaObject(currentProgram) | ||
self.assertIsJavaObject(currentLocation) | ||
self.assertIsJavaObject(currentHighlight) | ||
self.assertIsJavaObject(currentSelection) | ||
|
||
def test_ghidra_script_methods(self): | ||
self.assertIsInstance(getGhidraVersion(), str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters