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