2019-05-23 12:45:22 -03:00
|
|
|
"""Tests for sys.audit and sys.addaudithook
|
|
|
|
"""
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from test import support
|
2020-08-07 18:55:35 -03:00
|
|
|
from test.support import import_helper
|
|
|
|
from test.support import os_helper
|
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
if not hasattr(sys, "addaudithook") or not hasattr(sys, "audit"):
|
|
|
|
raise unittest.SkipTest("test only relevant when sys.audit is available")
|
|
|
|
|
2019-05-29 12:20:35 -03:00
|
|
|
AUDIT_TESTS_PY = support.findfile("audit-tests.py")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
class AuditTest(unittest.TestCase):
|
2019-05-29 12:20:35 -03:00
|
|
|
def do_test(self, *args):
|
|
|
|
with subprocess.Popen(
|
|
|
|
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
|
|
|
|
encoding="utf-8",
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
) as p:
|
|
|
|
p.wait()
|
|
|
|
sys.stdout.writelines(p.stdout)
|
|
|
|
sys.stderr.writelines(p.stderr)
|
|
|
|
if p.returncode:
|
2019-11-28 12:46:11 -04:00
|
|
|
self.fail("".join(p.stderr))
|
|
|
|
|
|
|
|
def run_python(self, *args):
|
|
|
|
events = []
|
|
|
|
with subprocess.Popen(
|
|
|
|
[sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
|
|
|
|
encoding="utf-8",
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
) as p:
|
|
|
|
p.wait()
|
|
|
|
sys.stderr.writelines(p.stderr)
|
|
|
|
return (
|
|
|
|
p.returncode,
|
|
|
|
[line.strip().partition(" ") for line in p.stdout],
|
|
|
|
"".join(p.stderr),
|
|
|
|
)
|
2019-05-29 12:20:35 -03:00
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
def test_basic(self):
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_basic")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_block_add_hook(self):
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_block_add_hook")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_block_add_hook_baseexception(self):
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_block_add_hook_baseexception")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_pickle(self):
|
2020-08-07 18:55:35 -03:00
|
|
|
import_helper.import_module("pickle")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_pickle")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_monkeypatch(self):
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_monkeypatch")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_open(self):
|
2020-08-07 18:55:35 -03:00
|
|
|
self.do_test("test_open", os_helper.TESTFN)
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
def test_cantrace(self):
|
2019-05-29 12:20:35 -03:00
|
|
|
self.do_test("test_cantrace")
|
2019-05-23 12:45:22 -03:00
|
|
|
|
2019-06-21 12:31:59 -03:00
|
|
|
def test_mmap(self):
|
|
|
|
self.do_test("test_mmap")
|
|
|
|
|
2019-11-28 12:46:11 -04:00
|
|
|
def test_excepthook(self):
|
|
|
|
returncode, events, stderr = self.run_python("test_excepthook")
|
|
|
|
if not returncode:
|
|
|
|
self.fail(f"Expected fatal exception\n{stderr}")
|
|
|
|
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
[("sys.excepthook", " ", "RuntimeError('fatal-error')")], events
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unraisablehook(self):
|
|
|
|
returncode, events, stderr = self.run_python("test_unraisablehook")
|
|
|
|
if returncode:
|
|
|
|
self.fail(stderr)
|
|
|
|
|
|
|
|
self.assertEqual(events[0][0], "sys.unraisablehook")
|
|
|
|
self.assertEqual(
|
|
|
|
events[0][2],
|
|
|
|
"RuntimeError('nonfatal-error') Exception ignored for audit hook test",
|
|
|
|
)
|
|
|
|
|
2019-12-09 15:18:12 -04:00
|
|
|
def test_winreg(self):
|
2020-08-07 18:55:35 -03:00
|
|
|
import_helper.import_module("winreg")
|
2019-12-09 15:18:12 -04:00
|
|
|
returncode, events, stderr = self.run_python("test_winreg")
|
|
|
|
if returncode:
|
|
|
|
self.fail(stderr)
|
|
|
|
|
|
|
|
self.assertEqual(events[0][0], "winreg.OpenKey")
|
|
|
|
self.assertEqual(events[1][0], "winreg.OpenKey/result")
|
|
|
|
expected = events[1][2]
|
|
|
|
self.assertTrue(expected)
|
|
|
|
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 0"], events[2])
|
|
|
|
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 10000"], events[3])
|
|
|
|
self.assertSequenceEqual(["winreg.PyHKEY.Detach", " ", expected], events[4])
|
|
|
|
|
2020-03-31 08:38:53 -03:00
|
|
|
def test_socket(self):
|
2020-08-07 18:55:35 -03:00
|
|
|
import_helper.import_module("socket")
|
2020-03-31 08:38:53 -03:00
|
|
|
returncode, events, stderr = self.run_python("test_socket")
|
|
|
|
if returncode:
|
|
|
|
self.fail(stderr)
|
|
|
|
|
|
|
|
if support.verbose:
|
|
|
|
print(*events, sep='\n')
|
|
|
|
self.assertEqual(events[0][0], "socket.gethostname")
|
|
|
|
self.assertEqual(events[1][0], "socket.__new__")
|
|
|
|
self.assertEqual(events[2][0], "socket.bind")
|
|
|
|
self.assertTrue(events[2][2].endswith("('127.0.0.1', 8080)"))
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|