Debian Codegraph @

Lib/test/test_wmi.py

raw
1 # Test the internal _wmi module on Windows
2 # This is used by the platform module, and potentially others
3
4 import unittest
5 from test import support
6 from test.support import import_helper
7
8
9 # Do this first so test will be skipped if module doesn't exist
10 _wmi = import_helper.import_module('_wmi', required_on=['win'])
11
12
13 def wmi_exec_query(query):
14 # gh-112278: WMI maybe slow response when first call.
15 for _ in support.sleeping_retry(support.LONG_TIMEOUT):
16 try:
17 return _wmi.exec_query(query)
18 except BrokenPipeError:
19 pass
20 # retry on pipe error
21 except WindowsError as exc:
22 if exc.winerror != 258:
23 raise
24 # retry on timeout
25
26
27 class WmiTests(unittest.TestCase):
28 def test_wmi_query_os_version(self):
29 r = wmi_exec_query("SELECT Version FROM Win32_OperatingSystem").split("\0")
30 self.assertEqual(1, len(r))
31 k, eq, v = r[0].partition("=")
32 self.assertEqual("=", eq, r[0])
33 self.assertEqual("Version", k, r[0])
34 # Best we can check for the version is that it's digits, dot, digits, anything
35 # Otherwise, we are likely checking the result of the query against itself
36 self.assertRegex(v, r"\d+\.\d+.+$", r[0])
37
38 def test_wmi_query_repeated(self):
39 # Repeated queries should not break
40 for _ in range(10):
41 self.test_wmi_query_os_version()
42
43 def test_wmi_query_error(self):
44 # Invalid queries fail with OSError
45 try:
46 wmi_exec_query("SELECT InvalidColumnName FROM InvalidTableName")
47 except OSError as ex:
48 if ex.winerror & 0xFFFFFFFF == 0x80041010:
49 # This is the expected error code. All others should fail the test
50 return
51 self.fail("Expected OSError")
52
53 def test_wmi_query_repeated_error(self):
54 for _ in range(10):
55 self.test_wmi_query_error()
56
57 def test_wmi_query_not_select(self):
58 # Queries other than SELECT are blocked to avoid potential exploits
59 with self.assertRaises(ValueError):
60 wmi_exec_query("not select, just in case someone tries something")
61
62 @support.requires_resource('cpu')
63 def test_wmi_query_overflow(self):
64 # Ensure very big queries fail
65 # Test multiple times to ensure consistency
66 for _ in range(2):
67 with self.assertRaises(OSError):
68 wmi_exec_query("SELECT * FROM CIM_DataFile")
69
70 def test_wmi_query_multiple_rows(self):
71 # Multiple instances should have an extra null separator
72 r = wmi_exec_query("SELECT ProcessId FROM Win32_Process WHERE ProcessId < 1000")
73 self.assertNotStartsWith(r, "\0")
74 self.assertNotEndsWith(r, "\0")
75 it = iter(r.split("\0"))
76 try:
77 while True:
78 self.assertRegex(next(it), r"ProcessId=\d+")
79 self.assertEqual("", next(it))
80 except StopIteration:
81 pass
82
83 def test_wmi_query_threads(self):
84 from concurrent.futures import ThreadPoolExecutor
85 query = "SELECT ProcessId FROM Win32_Process WHERE ProcessId < 1000"
86 with ThreadPoolExecutor(4) as pool:
87 task = [pool.submit(wmi_exec_query, query) for _ in range(32)]
88 for t in task:
89 self.assertRegex(t.result(), "ProcessId=")