2 # SPDX-License-Identifier: GPL-2.0
9 import ioctl_opt as ioctl
15 # Artificial delay between set commands
19 class invalid_param(ctypes.Structure):
21 ("data", ctypes.c_uint8),
25 def system_is_secured() -> bool:
26 fused_part = glob.glob("/sys/bus/pci/drivers/ccp/**/fused_part")[0]
27 if os.path.exists(fused_part):
28 with open(fused_part, "r") as r:
29 return int(r.read()) == 1
33 class DynamicBoostControlTest(unittest.TestCase):
34 def __init__(self, data) -> None:
36 self.signature = b"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
37 self.uid = b"1111111111111111"
38 super().__init__(data)
40 def setUp(self) -> None:
41 self.d = open(DEVICE_NODE)
42 return super().setUp()
44 def tearDown(self) -> None:
47 return super().tearDown()
50 class TestUnsupportedSystem(DynamicBoostControlTest):
51 def setUp(self) -> None:
52 if os.path.exists(DEVICE_NODE):
53 self.skipTest("system is supported")
54 with self.assertRaises(FileNotFoundError) as error:
56 self.assertEqual(error.exception.errno, 2)
58 def test_unauthenticated_nonce(self) -> None:
59 """fetch unauthenticated nonce"""
60 with self.assertRaises(ValueError) as error:
61 get_nonce(self.d, None)
64 class TestInvalidIoctls(DynamicBoostControlTest):
65 def __init__(self, data) -> None:
66 self.data = invalid_param()
68 super().__init__(data)
70 def setUp(self) -> None:
71 if not os.path.exists(DEVICE_NODE):
72 self.skipTest("system is unsupported")
74 self.skipTest("unable to test IOCTLs without ioctl_opt")
76 return super().setUp()
78 def test_invalid_nonce_ioctl(self) -> None:
79 """tries to call get_nonce ioctl with invalid data structures"""
81 # 0x1 (get nonce), and invalid data
82 INVALID1 = ioctl.IOWR(ord("D"), 0x01, invalid_param)
83 with self.assertRaises(OSError) as error:
84 fcntl.ioctl(self.d, INVALID1, self.data, True)
85 self.assertEqual(error.exception.errno, 22)
87 def test_invalid_setuid_ioctl(self) -> None:
88 """tries to call set_uid ioctl with invalid data structures"""
90 # 0x2 (set uid), and invalid data
91 INVALID2 = ioctl.IOW(ord("D"), 0x02, invalid_param)
92 with self.assertRaises(OSError) as error:
93 fcntl.ioctl(self.d, INVALID2, self.data, True)
94 self.assertEqual(error.exception.errno, 22)
96 def test_invalid_setuid_rw_ioctl(self) -> None:
97 """tries to call set_uid ioctl with invalid data structures"""
99 # 0x2 as RW (set uid), and invalid data
100 INVALID3 = ioctl.IOWR(ord("D"), 0x02, invalid_param)
101 with self.assertRaises(OSError) as error:
102 fcntl.ioctl(self.d, INVALID3, self.data, True)
103 self.assertEqual(error.exception.errno, 22)
105 def test_invalid_param_ioctl(self) -> None:
106 """tries to call param ioctl with invalid data structures"""
107 # 0x3 (param), and invalid data
108 INVALID4 = ioctl.IOWR(ord("D"), 0x03, invalid_param)
109 with self.assertRaises(OSError) as error:
110 fcntl.ioctl(self.d, INVALID4, self.data, True)
111 self.assertEqual(error.exception.errno, 22)
113 def test_invalid_call_ioctl(self) -> None:
114 """tries to call the DBC ioctl with invalid data structures"""
115 # 0x4, and invalid data
116 INVALID5 = ioctl.IOWR(ord("D"), 0x04, invalid_param)
117 with self.assertRaises(OSError) as error:
118 fcntl.ioctl(self.d, INVALID5, self.data, True)
119 self.assertEqual(error.exception.errno, 22)
122 class TestInvalidSignature(DynamicBoostControlTest):
123 def setUp(self) -> None:
124 if not os.path.exists(DEVICE_NODE):
125 self.skipTest("system is unsupported")
126 if not system_is_secured():
127 self.skipTest("system is unfused")
128 return super().setUp()
130 def test_unauthenticated_nonce(self) -> None:
131 """fetch unauthenticated nonce"""
132 get_nonce(self.d, None)
134 def test_multiple_unauthenticated_nonce(self) -> None:
135 """ensure state machine always returns nonce"""
136 for count in range(0, 2):
137 get_nonce(self.d, None)
139 def test_authenticated_nonce(self) -> None:
140 """fetch authenticated nonce"""
141 with self.assertRaises(OSError) as error:
142 get_nonce(self.d, self.signature)
143 self.assertEqual(error.exception.errno, 1)
145 def test_set_uid(self) -> None:
147 with self.assertRaises(OSError) as error:
148 set_uid(self.d, self.uid, self.signature)
149 self.assertEqual(error.exception.errno, 1)
151 def test_get_param(self) -> None:
152 """fetch a parameter"""
153 with self.assertRaises(OSError) as error:
154 process_param(self.d, PARAM_GET_SOC_PWR_CUR, self.signature)
155 self.assertEqual(error.exception.errno, 1)
157 def test_set_param(self) -> None:
158 """set a parameter"""
159 with self.assertRaises(OSError) as error:
160 process_param(self.d, PARAM_SET_PWR_CAP, self.signature, 1000)
161 self.assertEqual(error.exception.errno, 1)
164 class TestUnFusedSystem(DynamicBoostControlTest):
165 def setup_identity(self) -> None:
166 """sets up the identity of the caller"""
167 # if already authenticated these may fail
169 get_nonce(self.d, None)
170 except PermissionError:
173 set_uid(self.d, self.uid, self.signature)
174 except BlockingIOError:
177 get_nonce(self.d, self.signature)
178 except PermissionError:
181 def setUp(self) -> None:
182 if not os.path.exists(DEVICE_NODE):
183 self.skipTest("system is unsupported")
184 if system_is_secured():
185 self.skipTest("system is fused")
187 self.setup_identity()
188 time.sleep(SET_DELAY)
190 def test_get_valid_param(self) -> None:
191 """fetch all possible parameters"""
193 soc_power_max = process_param(self.d, PARAM_GET_SOC_PWR_MAX, self.signature)
194 soc_power_min = process_param(self.d, PARAM_GET_SOC_PWR_MIN, self.signature)
195 self.assertGreater(soc_power_max[0], soc_power_min[0])
198 fmax_max = process_param(self.d, PARAM_GET_FMAX_MAX, self.signature)
199 fmax_min = process_param(self.d, PARAM_GET_FMAX_MIN, self.signature)
200 self.assertGreater(fmax_max[0], fmax_min[0])
204 "fmax-cap": PARAM_GET_FMAX_CAP,
205 "power-cap": PARAM_GET_PWR_CAP,
206 "current-temp": PARAM_GET_CURR_TEMP,
207 "soc-power-cur": PARAM_GET_SOC_PWR_CUR,
210 result = process_param(self.d, keys[k], self.signature)
211 self.assertGreater(result[0], 0)
213 def test_get_invalid_param(self) -> None:
214 """fetch an invalid parameter"""
216 set_uid(self.d, self.uid, self.signature)
219 with self.assertRaises(OSError) as error:
220 process_param(self.d, (0xF,), self.signature)
221 self.assertEqual(error.exception.errno, 22)
223 def test_set_fmax(self) -> None:
224 """get/set fmax limit"""
226 original = process_param(self.d, PARAM_GET_FMAX_CAP, self.signature)
229 target = original[0] - 100
230 process_param(self.d, PARAM_SET_FMAX_CAP, self.signature, target)
231 time.sleep(SET_DELAY)
232 new = process_param(self.d, PARAM_GET_FMAX_CAP, self.signature)
233 self.assertEqual(new[0], target)
235 # revert back to current
236 process_param(self.d, PARAM_SET_FMAX_CAP, self.signature, original[0])
237 time.sleep(SET_DELAY)
238 cur = process_param(self.d, PARAM_GET_FMAX_CAP, self.signature)
239 self.assertEqual(cur[0], original[0])
241 def test_set_power_cap(self) -> None:
242 """get/set power cap limit"""
244 original = process_param(self.d, PARAM_GET_PWR_CAP, self.signature)
247 target = original[0] - 10
248 process_param(self.d, PARAM_SET_PWR_CAP, self.signature, target)
249 time.sleep(SET_DELAY)
250 new = process_param(self.d, PARAM_GET_PWR_CAP, self.signature)
251 self.assertEqual(new[0], target)
253 # revert back to current
254 process_param(self.d, PARAM_SET_PWR_CAP, self.signature, original[0])
255 time.sleep(SET_DELAY)
256 cur = process_param(self.d, PARAM_GET_PWR_CAP, self.signature)
257 self.assertEqual(cur[0], original[0])
259 def test_set_3d_graphics_mode(self) -> None:
260 """set/get 3d graphics mode"""
261 # these aren't currently implemented but may be some day
262 # they are *expected* to fail
263 with self.assertRaises(OSError) as error:
264 process_param(self.d, PARAM_GET_GFX_MODE, self.signature)
265 self.assertEqual(error.exception.errno, 2)
267 time.sleep(SET_DELAY)
269 with self.assertRaises(OSError) as error:
270 process_param(self.d, PARAM_SET_GFX_MODE, self.signature, 1)
271 self.assertEqual(error.exception.errno, 2)
274 if __name__ == "__main__":