blob: 41f9fae10223182b94897235ccce53e9a92807fa [file] [log] [blame]
Steve Dowerb82e17e2019-05-23 08:45:22 -07001"""Tests for sys.audit and sys.addaudithook
2"""
3
Steve Dowerb82e17e2019-05-23 08:45:22 -07004import subprocess
5import sys
6import unittest
7from test import support
8
9if not hasattr(sys, "addaudithook") or not hasattr(sys, "audit"):
10 raise unittest.SkipTest("test only relevant when sys.audit is available")
11
Steve Dower9ddc4162019-05-29 08:20:35 -070012AUDIT_TESTS_PY = support.findfile("audit-tests.py")
Steve Dowerb82e17e2019-05-23 08:45:22 -070013
14
15class AuditTest(unittest.TestCase):
Steve Dower9ddc4162019-05-29 08:20:35 -070016 def do_test(self, *args):
Steve Dowerb82e17e2019-05-23 08:45:22 -070017 with subprocess.Popen(
Steve Dower9ddc4162019-05-29 08:20:35 -070018 [sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
Steve Dowerb82e17e2019-05-23 08:45:22 -070019 encoding="utf-8",
20 stdout=subprocess.PIPE,
21 stderr=subprocess.PIPE,
22 ) as p:
23 p.wait()
Steve Dower9ddc4162019-05-29 08:20:35 -070024 sys.stdout.writelines(p.stdout)
25 sys.stderr.writelines(p.stderr)
26 if p.returncode:
27 self.fail(''.join(p.stderr))
28
29 def test_basic(self):
30 self.do_test("test_basic")
31
32 def test_block_add_hook(self):
33 self.do_test("test_block_add_hook")
34
35 def test_block_add_hook_baseexception(self):
36 self.do_test("test_block_add_hook_baseexception")
37
38 def test_finalize_hooks(self):
39 events = []
40 with subprocess.Popen(
41 [sys.executable, "-X utf8", AUDIT_TESTS_PY, "test_finalize_hooks"],
42 encoding="utf-8",
43 stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE,
45 ) as p:
46 p.wait()
47 for line in p.stdout:
Steve Dowerb82e17e2019-05-23 08:45:22 -070048 events.append(line.strip().partition(" "))
Steve Dower9ddc4162019-05-29 08:20:35 -070049 sys.stderr.writelines(p.stderr)
50 if p.returncode:
51 self.fail(''.join(p.stderr))
52
Steve Dowerb82e17e2019-05-23 08:45:22 -070053 firstId = events[0][2]
54 self.assertSequenceEqual(
55 [
56 ("Created", " ", firstId),
57 ("cpython._PySys_ClearAuditHooks", " ", firstId),
58 ],
59 events,
60 )
61
62 def test_pickle(self):
Steve Dower9ddc4162019-05-29 08:20:35 -070063 support.import_module("pickle")
Steve Dowerb82e17e2019-05-23 08:45:22 -070064
Steve Dower9ddc4162019-05-29 08:20:35 -070065 self.do_test("test_pickle")
Steve Dowerb82e17e2019-05-23 08:45:22 -070066
67 def test_monkeypatch(self):
Steve Dower9ddc4162019-05-29 08:20:35 -070068 self.do_test("test_monkeypatch")
Steve Dowerb82e17e2019-05-23 08:45:22 -070069
70 def test_open(self):
Steve Dower9ddc4162019-05-29 08:20:35 -070071 self.do_test("test_open", support.TESTFN)
Steve Dowerb82e17e2019-05-23 08:45:22 -070072
73 def test_cantrace(self):
Steve Dower9ddc4162019-05-29 08:20:35 -070074 self.do_test("test_cantrace")
Steve Dowerb82e17e2019-05-23 08:45:22 -070075
Steve Dower6c794772019-06-21 09:45:13 -070076 def test_mmap(self):
77 self.do_test("test_mmap")
78
Steve Dowerb82e17e2019-05-23 08:45:22 -070079
80if __name__ == "__main__":
Steve Dowerb82e17e2019-05-23 08:45:22 -070081 unittest.main()