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

PoC: tests for defuse_stdlib #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions defusedxml/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ class NotSupportedError(DefusedXmlException):
def _apply_defusing(defused_mod):
assert defused_mod is sys.modules[defused_mod.__name__]
stdlib_name = defused_mod.__origin__
__import__(stdlib_name, {}, {}, ["*"])
stdlib_mod = sys.modules[stdlib_name]
stdlib_names = set(dir(stdlib_mod))
for name, obj in vars(defused_mod).items():
if name.startswith("_") or name not in stdlib_names:
continue
setattr(stdlib_mod, name, obj)
if PY3:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from unittest.mock import patch
for name, obj in vars(defused_mod).items():
if name.startswith("_"):
continue
try:
patcher = patch(stdlib_name + '.' + name, obj)
patcher.start()
except AttributeError:
pass
stdlib_mod = patcher
else:
__import__(stdlib_name, {}, {}, ["*"])
stdlib_mod = sys.modules[stdlib_name]
stdlib_names = set(dir(stdlib_mod))
for name, obj in vars(defused_mod).items():
if name.startswith("_") or name not in stdlib_names:
continue
setattr(stdlib_mod, name, obj)
return stdlib_mod


Expand Down
33 changes: 31 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,26 @@ def test_defused_gzip_response(self):
self.decode_response(response, 4095, 8192)


def get_std_module(defused_module):
name = defused_module.__origin__
obj = __import__(name, globals(), locals(), [], 0)
for part in name.split('.')[1:]:
obj = getattr(obj, part)
return obj


class TestStdElementTree(TestDefusedElementTree):
module = get_std_module(ElementTree)


class TestStdMinidom(TestDefusedMinidom):
module = get_std_module(minidom)


class TestStdPulldom(TestDefusedPulldom):
module = get_std_module(pulldom)


def test_main():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(TestDefusedcElementTree))
Expand All @@ -497,9 +517,18 @@ def test_main():
return suite


def test_origin():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(TestStdElementTree))
suite.addTests(unittest.makeSuite(TestStdMinidom))
return suite


if __name__ == "__main__":
suite = test_main()
result = unittest.TextTestRunner(verbosity=1).run(suite)
# TODO: test that it actually works
defuse_stdlib()
sys.exit(not result.wasSuccessful())
suite = test_origin()
result_std = unittest.TextTestRunner(verbosity=1).run(suite)
success = result.wasSuccessful() and result_std.wasSuccessful()
sys.exit(not success)