File: test_msi.py

package info (click to toggle)
liquidctl 1.15.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,312 kB
  • sloc: python: 13,599; sh: 712; xml: 84; makefile: 4
file content (332 lines) | stat: -rw-r--r-- 10,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# uses the psf/black style

from struct import pack
from datetime import datetime

import pytest
from _testutils import MockHidapiDevice, Report

from liquidctl.driver.msi import MpgCooler, _REPORT_LENGTH, _DEFAULT_FEATURE_DATA, _LightingMode
from liquidctl.error import UnsafeFeaturesNotEnabled


@pytest.fixture
def mpgCoreLiquidK360Device():
    description = "Mock MPG CoreLiquid K360"
    device = _MockCoreLiquid(vendor_id=0xFFFF, product_id=0xB130)
    dev = MpgCooler(device, description)

    dev.connect()
    return dev


@pytest.fixture
def mpgCoreLiquidDeviceExperimental():
    _, pid, desc, kwargs = MpgCooler._MATCHES[-1]
    description = "Mock " + desc
    unsafe = kwargs["_unsafe"]
    device = _MockCoreLiquid(vendor_id=0xFFFF, product_id=pid)
    dev = MpgCooler(device, description, **kwargs, unsafe=unsafe)

    dev.connect(unsafe=unsafe)
    return dev


@pytest.fixture
def mpgCoreLiquidK360DeviceInvalid():
    description = "Mock MPG CoreLiquid K360"
    device = _MockCoreLiquidInvalid(vendor_id=0xFFFF, product_id=0xB130)
    dev = MpgCooler(device, description)

    dev.connect()
    return dev


class _MockCoreLiquid(MockHidapiDevice):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._fan_configs = (4, 20, 40, 50, 60, 70, 80, 90)
        self._fan_temp_configs = (4, 30, 40, 50, 60, 70, 80, 90)
        self._model_idx = 255  # TODO: check the correct model index from device
        self._feature_data = Report(_DEFAULT_FEATURE_DATA[0], _DEFAULT_FEATURE_DATA[1:])

        self.preload_read(self._feature_data)

    def write(self, data):
        reply = bytearray(_REPORT_LENGTH)
        reply[0:2] = data[0:2]
        if list(data[:2]) == [0x01, 0xB1]:  # get current model idx request
            reply[2] = self._model_idx
        elif list(data[:2]) == [0xD0, 0x31]:  # get status request
            reply[2:23] = (
                pack("<h", 496)
                + pack("<h", 517)
                + pack("<h", 509)
                + pack("<h", 1045)
                + pack("<h", 1754)
                + bytearray([0, 0, 0, 0, 0x7D, 0, 0x7D, 0, 0, 0])
                + pack("<h", 20)
                + pack("<h", 20)
                + pack("<h", 20)
                + pack("<h", 20)
                + pack("<h", 50)
            )
            self.preload_read(Report(0, reply))
        elif list(data[:2]) == [0xD0, 0x32]:  # get fan config request
            for i in (2, 10, 18, 26, 34):
                reply[i : i + 10] = self._fan_configs
            self.preload_read(Report(0, reply))
        elif list(data[:2]) == [0xD0, 0x33]:  # get fan T config request
            reply[1] = 0x32
            for i in (2, 10, 18, 26, 34):
                reply[i : i + 10] = self._fan_temp_configs
            self.preload_read(Report(0, reply))
        elif list(data[1:3]) == [0xB0, 0xCC]:  # get ldrom fw
            self.preload_read(Report(0, reply))
        elif list(data[1:3]) == [0xB6, 0xCC]:  # get aprom fw
            self.preload_read(Report(0, reply))
        elif data[1] == 0xF1:  # get screen fw
            self.preload_read(Report(0, reply))
        return super().write(data)


class _MockCoreLiquidInvalid(MockHidapiDevice):
    def read(self, length):
        buf = bytearray([0xD0, 0x32] + (62 * [0]))
        return buf[:length]


def test_mpg_core_liquid_k360_initializes(mpgCoreLiquidK360Device):
    mpgCoreLiquidK360Device.initialize()

    writes = len(mpgCoreLiquidK360Device.device.sent)
    assert (
        writes
        == 9
        # 1 get fan config,
        # 2 get fan T config,
        # 3 get aprom fw,
        # 4 get ldrom fw,
        # 5 get display fw,
        # 6 set screen settings,
        # 7 set fan config,
        # 8 set fan T config,
        # 9 set safe control temperature
    )


def test_mpg_core_liquid_k360_get_status(mpgCoreLiquidK360Device):
    dev = mpgCoreLiquidK360Device
    (
        fan1,
        fan1d,
        fan2,
        fan2d,
        fan3,
        fan3d,
        wbfan,
        wbfand,
        pump,
        pumpd,
    ) = dev.get_status()
    assert fan1[1] == 496
    assert fan1d[1] == 20
    assert fan2[1] == 517
    assert fan2d[1] == 20
    assert fan3[1] == 509
    assert fan3d[1] == 20
    assert wbfan[1] == 1045
    assert wbfand[1] == 20
    assert pump[1] == 1754
    assert pumpd[1] == 50
    assert dev.device.sent[-1].number == 0xD0
    assert dev.device.sent[-1].data[0] == 0x31


def test_mpg_core_liquid_k360_get_status_invalid_read(mpgCoreLiquidK360DeviceInvalid):
    dev = mpgCoreLiquidK360DeviceInvalid
    with pytest.raises(AssertionError):
        dev.get_status()


def test_mpg_core_liquid_k360_set_fixed_speeds(mpgCoreLiquidK360Device):
    mpgCoreLiquidK360Device.set_fixed_speed("pump", 65)

    fan_report, T_report = mpgCoreLiquidK360Device.device.sent[-2:]
    assert fan_report.data[33:41] == [3] + [65] * 7


def test_mpg_core_liquid_k360_set_speed_profile(mpgCoreLiquidK360Device):
    duties = [20, 30, 34, 40, 50]
    temps = [30, 50, 80, 90, 100]
    curve_profile = zip(duties, temps)

    mpgCoreLiquidK360Device.set_speed_profile("fans", curve_profile)

    fan_report, T_report = mpgCoreLiquidK360Device.device.sent[-3:-1]

    # fan 1
    assert fan_report.data[1:9] == [3] + duties + [0] * 2
    assert T_report.data[1:9] == [3] + temps + [0] * 2
    # fan 2
    assert fan_report.data[9:17] == [3] + duties + [0] * 2
    assert T_report.data[9:17] == [3] + temps + [0] * 2
    # fan 3
    assert fan_report.data[17:25] == [3] + duties + [0] * 2
    assert T_report.data[17:25] == [3] + temps + [0] * 2


def test_mpg_core_liquid_k360_set_color(mpgCoreLiquidK360Device):
    colors = [[255, 255, 0], [0, 255, 255]]
    mode = "clock"
    speed = 2
    brightness = 5
    use_color = 1

    mpgCoreLiquidK360Device.set_color(
        "sync", mode, colors, speed=speed, brightness=brightness, color_selection=use_color
    )
    report = mpgCoreLiquidK360Device.device.sent[-1]

    assert report.data[30] == 20  # mode
    assert report.data[34] == (brightness << 2) | speed  # brightness, speed
    assert report.data[31:34] == colors[0]
    assert report.data[35:38] == colors[1]


def test_mpg_core_liquid_k360_not_totally_broken(mpgCoreLiquidK360Device):
    """Reasonable calls to untested APIs do not raise exceptions"""
    dev = mpgCoreLiquidK360Device
    dev.initialize()
    _ = dev.get_status()

    profile = ((0, 30), (25, 40), (60, 60), (100, 75))
    dev.set_speed_profile("pump", profile)
    dev.set_fixed_speed("waterblock-fan", 42)
    dev.set_screen("lcd", "image", "0;4")
    dev.set_screen("lcd", "banner", "1;0;Hello, world")
    dev.set_screen("lcd", "disable", "")
    dev.set_screen("lcd", "clock", "0")
    dev.set_screen("lcd", "hardware", "cpu_temp;cpu_freq")


def test_mpg_core_liquid_k360_set_clock(mpgCoreLiquidK360Device):
    time = datetime(2012, 12, 21, 9, 54, 20)

    mpgCoreLiquidK360Device.set_time(time)

    report = mpgCoreLiquidK360Device.device.sent[-1]
    assert report.data[:8] == [131, 12, 12, 21, 4, 9, 54, 20]


def test_mpg_core_liquid_k360_set_hw_status(mpgCoreLiquidK360Device):
    cpu_freq = 3500.0
    cpu_T = 54.0
    gpu_f = 7000

    mpgCoreLiquidK360Device.set_hardware_status(cpu_T, cpu_f=cpu_freq, gpu_f=gpu_f)

    cpu_report = mpgCoreLiquidK360Device.device.sent[-2]
    assert cpu_report.data[:5] == [133, 172, 13, 54, 0]

    gpu_report = mpgCoreLiquidK360Device.device.sent[-1]
    assert gpu_report.data[:5] == [134, 88, 27, 0, 0]


def test_unsafe_core_liquid_get_status(mpgCoreLiquidDeviceExperimental):
    status = mpgCoreLiquidDeviceExperimental.get_status()
    assert status == []

    status = mpgCoreLiquidDeviceExperimental.get_status(unsafe=["other"])
    assert status == []

    status = mpgCoreLiquidDeviceExperimental.get_status(unsafe=["experimental_coreliquid_cooler"])
    assert mpgCoreLiquidDeviceExperimental.device.sent[-1].number == 0xD0


def test_unsafe_core_liquid_set_fixed_speed(mpgCoreLiquidDeviceExperimental):
    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_fixed_speed("pump", 65)

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_fixed_speed("pump", 65, unsafe=["other"])

    mpgCoreLiquidDeviceExperimental.set_fixed_speed(
        "pump", 65, unsafe=["experimental_coreliquid_cooler"]
    )


def test_unsafe_core_liquid_set_speed_profile(mpgCoreLiquidDeviceExperimental):
    duties = [20, 30, 34, 40, 50]
    temps = [30, 50, 80, 90, 100]
    curve_profile = zip(duties, temps)
    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_speed_profile("fans", curve_profile)

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_speed_profile("fans", curve_profile, unsafe=["other"])

    mpgCoreLiquidDeviceExperimental.set_speed_profile(
        "fans", curve_profile, unsafe=["experimental_coreliquid_cooler"]
    )


def test_unsafe_core_liquid_set_color(mpgCoreLiquidDeviceExperimental):
    colors = [[255, 255, 0], [0, 255, 255]]
    mode = "clock"
    speed = 2
    brightness = 5
    use_color = 1

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_color(
            "sync", mode, colors, speed=speed, brightness=brightness, color_selection=use_color
        )

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_color(
            "sync",
            mode,
            colors,
            speed=speed,
            brightness=brightness,
            color_selection=use_color,
            unsafe=["other"],
        )

    mpgCoreLiquidDeviceExperimental.set_color(
        "sync",
        mode,
        colors,
        speed=speed,
        brightness=brightness,
        color_selection=use_color,
        unsafe=["experimental_coreliquid_cooler"],
    )


def test_unsafe_core_liquid_set_clock(mpgCoreLiquidDeviceExperimental):
    time = datetime(2012, 12, 21, 9, 54, 20)

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_time(time)

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_time(time, unsafe=["other"])

    mpgCoreLiquidDeviceExperimental.set_time(time, unsafe=["experimental_coreliquid_cooler"])


def test_unsafe_core_liquid_set_hw_status(mpgCoreLiquidDeviceExperimental):
    cpu_freq = 3500.0
    cpu_T = 54.0

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_hardware_status(cpu_T, cpu_f=cpu_freq)

    with pytest.raises(UnsafeFeaturesNotEnabled):
        mpgCoreLiquidDeviceExperimental.set_hardware_status(cpu_T, cpu_f=cpu_freq, unsafe=["other"])

    mpgCoreLiquidDeviceExperimental.set_hardware_status(
        cpu_T, cpu_f=cpu_freq, unsafe=["experimental_coreliquid_cooler"]
    )