| 1 |
"""Test AsusRouter devicemap endpoint module.""" |
| 2 |
|
| 3 |
from collections.abc import Generator |
| 4 |
from datetime import datetime, timedelta, timezone |
| 5 |
from typing import Any |
| 6 |
from unittest.mock import ANY, MagicMock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from asusrouter.config import ARConfigKey as ARConfKey |
| 11 |
from asusrouter.modules.data import AsusData, AsusDataState |
| 12 |
from asusrouter.modules.endpoint import devicemap |
| 13 |
|
| 14 |
|
| 15 |
def generate_xml_content(groups: dict[str, Any]) -> str: |
| 16 |
"""Generate XML content based on input parameters.""" |
| 17 |
|
| 18 |
content = "<devicemap>\n" |
| 19 |
for group, keys in groups.items(): |
| 20 |
content += f" <{group}>\n" |
| 21 |
for key, value in keys.items(): |
| 22 |
content += f" <{key}>{value}</{key}>\n" |
| 23 |
content += f" </{group}>\n" |
| 24 |
content += "</devicemap>\n" |
| 25 |
return content |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
"content", |
| 30 |
[ |
| 31 |
"<devicemap></devicemap>", # Empty devicemap |
| 32 |
"non-xml", # Invalid XML |
| 33 |
], |
| 34 |
) |
| 35 |
def test_read_invalid(content: str) -> None: |
| 36 |
"""Test read function with empty devicemap.""" |
| 37 |
|
| 38 |
assert devicemap.read(content) == {} |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture |
| 42 |
def common_group() -> dict[str, dict[str, str]]: |
| 43 |
"""Return the common test data intermediate.""" |
| 44 |
|
| 45 |
return { |
| 46 |
"group1": {"key1": "value1"}, |
| 47 |
"group2": {"key2": "value2"}, |
| 48 |
"group3": {"key3": "value3_test"}, |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def common_test_data_result() -> dict[str, dict[str, str]]: |
| 54 |
"""Return the common test data result.""" |
| 55 |
|
| 56 |
return { |
| 57 |
"group1": {"key1": "value1"}, |
| 58 |
"group2": {"key2": "value2"}, |
| 59 |
"group3": {"key3": "value3"}, |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
@pytest.fixture |
| 64 |
def mock_functions() -> Generator[dict[str, MagicMock | Any], None, None]: |
| 65 |
"""Return the mock functions.""" |
| 66 |
|
| 67 |
with ( |
| 68 |
patch( |
| 69 |
"asusrouter.modules.endpoint.devicemap.read_index", |
| 70 |
return_value={ |
| 71 |
"group1": {"key1": "value1"}, |
| 72 |
"group3": {"key3": "value3_test"}, |
| 73 |
}, |
| 74 |
) as mock_read_index, |
| 75 |
patch( |
| 76 |
"asusrouter.modules.endpoint.devicemap.read_key", |
| 77 |
return_value={"group2": {"key2": "value2"}}, |
| 78 |
) as mock_read_key, |
| 79 |
patch( |
| 80 |
"asusrouter.modules.endpoint.devicemap.merge_dicts", |
| 81 |
side_effect=lambda x, y: {**x, **y}, |
| 82 |
) as mock_merge_dicts, |
| 83 |
patch( |
| 84 |
"asusrouter.modules.endpoint.devicemap.clean_dict", |
| 85 |
side_effect=lambda x: x, |
| 86 |
) as mock_clean_dict, |
| 87 |
patch( |
| 88 |
"asusrouter.modules.endpoint.devicemap.clean_dict_key_prefix", |
| 89 |
side_effect=lambda x, _: x, |
| 90 |
) as mock_clean_dict_key_prefix, |
| 91 |
patch( |
| 92 |
"asusrouter.modules.endpoint.devicemap.DEVICEMAP_CLEAR", |
| 93 |
new={ |
| 94 |
"group3": {"key3": "_test", "key4": "_test"}, |
| 95 |
"group4": {"key5": "_test"}, |
| 96 |
}, |
| 97 |
) as mock_devicemap_clear, |
| 98 |
): |
| 99 |
yield { |
| 100 |
"read_index": mock_read_index, |
| 101 |
"read_key": mock_read_key, |
| 102 |
"merge_dicts": mock_merge_dicts, |
| 103 |
"clean_dict": mock_clean_dict, |
| 104 |
"clean_dict_key_prefix": mock_clean_dict_key_prefix, |
| 105 |
"devicemap_clear": mock_devicemap_clear, |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
def test_read_with_data( |
| 110 |
mock_functions: dict[str, MagicMock | Any], # pylint: disable=redefined-outer-name |
| 111 |
common_test_data_result: dict[str, dict[str, str]], # pylint: disable=redefined-outer-name |
| 112 |
common_group: dict[str, dict[str, str]], # pylint: disable=redefined-outer-name |
| 113 |
) -> None: |
| 114 |
"""Test read function.""" |
| 115 |
|
| 116 |
# Test data |
| 117 |
content = generate_xml_content(common_group) |
| 118 |
expected_devicemap = common_test_data_result |
| 119 |
|
| 120 |
# Call the function |
| 121 |
result = devicemap.read(content) |
| 122 |
|
| 123 |
# Check the result |
| 124 |
assert result == expected_devicemap |
| 125 |
|
| 126 |
# Check the calls to the mocked functions |
| 127 |
mock_functions["read_index"].assert_called_with(common_group) |
| 128 |
mock_functions["read_key"].assert_called_with(common_group) |
| 129 |
assert mock_functions["merge_dicts"].call_count == 2 # noqa: PLR2004 |
| 130 |
mock_functions["clean_dict"].assert_called_with(expected_devicemap) |
| 131 |
assert mock_functions["clean_dict_key_prefix"].call_count == 3 # noqa: PLR2004 |
| 132 |
|
| 133 |
|
| 134 |
@pytest.fixture |
| 135 |
def const_devicemap() -> list[tuple[str, str, list[str]]]: |
| 136 |
"""Return the const devicemap.""" |
| 137 |
|
| 138 |
return [ |
| 139 |
("output_group1", "group1", ["input_value1"]), |
| 140 |
("output_group2", "group2", ["input_value3", "input_value4"]), |
| 141 |
("output_group3", "group3", ["input_value5"]), |
| 142 |
("output_group4", "group4", ["input_value6"]), |
| 143 |
("output_group5", "group5", ["input_value2"]), |
| 144 |
("output_group6", "group6", ["input_value7"]), |
| 145 |
] |
| 146 |
|
| 147 |
|
| 148 |
@pytest.fixture |
| 149 |
def const_devicemap_result() -> dict[str, dict[str, str]]: |
| 150 |
"""Return the const devicemap result.""" |
| 151 |
|
| 152 |
return { |
| 153 |
"output_group1": {"input_value1": "value1"}, |
| 154 |
"output_group2": {"input_value3": "value3", "input_value4": "value4"}, |
| 155 |
"output_group3": {"input_value5": "value5"}, |
| 156 |
"output_group4": {"input_value6": "value6"}, |
| 157 |
"output_group5": {}, |
| 158 |
"output_group6": {}, |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
@pytest.fixture |
| 163 |
def input_data() -> dict[str, list[str]]: |
| 164 |
"""Return the input data for the tests.""" |
| 165 |
|
| 166 |
return { |
| 167 |
"group1": ["value1"], |
| 168 |
"group2": ["value3", "value4"], |
| 169 |
"group3": ["value5"], |
| 170 |
"group4": ["value6"], |
| 171 |
# "group5": [], |
| 172 |
"group6": [], |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
@pytest.fixture |
| 177 |
def input_data_key() -> dict[str, Any]: |
| 178 |
"""Return the input data for the read_key test.""" |
| 179 |
|
| 180 |
return { |
| 181 |
"group1": ["input_value1=value1"], |
| 182 |
"group2": ["input_value3=value3", "input_value4=value4"], |
| 183 |
"group3": "input_value5=value5", |
| 184 |
"group4": "input_value6=value6", |
| 185 |
"group5": None, |
| 186 |
"group6": [], |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
def test_read_index( |
| 191 |
const_devicemap: list[tuple[str, str, list[str]]], # pylint: disable=redefined-outer-name |
| 192 |
const_devicemap_result: dict[str, dict[str, str]], # pylint: disable=redefined-outer-name |
| 193 |
input_data: dict[str, list[str]], # pylint: disable=redefined-outer-name |
| 194 |
) -> None: |
| 195 |
"""Test read_index function.""" |
| 196 |
|
| 197 |
with patch.object(devicemap, "DEVICEMAP_BY_INDEX", new=const_devicemap): |
| 198 |
# Call the function |
| 199 |
result = devicemap.read_index(input_data) |
| 200 |
|
| 201 |
# Check the result |
| 202 |
assert result == const_devicemap_result |
| 203 |
|
| 204 |
|
| 205 |
def test_read_key( |
| 206 |
const_devicemap: list[tuple[str, str, list[str]]], # pylint: disable=redefined-outer-name |
| 207 |
const_devicemap_result: dict[str, dict[str, str]], # pylint: disable=redefined-outer-name |
| 208 |
input_data_key: dict[str, Any], # pylint: disable=redefined-outer-name |
| 209 |
) -> None: |
| 210 |
"""Test read_key function.""" |
| 211 |
|
| 212 |
with patch.object(devicemap, "DEVICEMAP_BY_KEY", new=const_devicemap): |
| 213 |
# Call the function |
| 214 |
result = devicemap.read_key(input_data_key) |
| 215 |
|
| 216 |
# Check the result |
| 217 |
assert result == const_devicemap_result |
| 218 |
|
| 219 |
|
| 220 |
def test_read_special( |
| 221 |
input_data: dict[str, list[str]], # pylint: disable=redefined-outer-name |
| 222 |
) -> None: |
| 223 |
"""Test read_special function.""" |
| 224 |
|
| 225 |
result = devicemap.read_special(input_data) |
| 226 |
assert result == {} # pylint: disable=C1803 |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.parametrize( |
| 230 |
("content", "result"), |
| 231 |
[ |
| 232 |
# Test with a valid content string |
| 233 |
( |
| 234 |
"Thu, 16 Nov 2023 07:17:45 +0100(219355 secs since boot)", |
| 235 |
( |
| 236 |
datetime( |
| 237 |
2023, |
| 238 |
11, |
| 239 |
16, |
| 240 |
7, |
| 241 |
17, |
| 242 |
45, |
| 243 |
tzinfo=timezone(timedelta(hours=1)), |
| 244 |
) |
| 245 |
- timedelta(seconds=219355), |
| 246 |
219355, |
| 247 |
), |
| 248 |
), |
| 249 |
# Test with an invalid content string (no seconds) |
| 250 |
("Thu, 16 Nov 2023 07:17:45 +0100(no secs since boot)", (None, None)), |
| 251 |
# Test with an invalid content string (bad format) |
| 252 |
("bad format", (None, None)), |
| 253 |
# Test with a content string that has an invalid date |
| 254 |
("Not a date (219355 secs since boot)", (None, 219355)), |
| 255 |
# Test with a content string that has an invalid number of seconds |
| 256 |
( |
| 257 |
"Thu, 16 Nov 2023 07:17:45 +0100(not a number secs since boot)", |
| 258 |
(None, None), |
| 259 |
), |
| 260 |
], |
| 261 |
) |
| 262 |
def test_read_uptime_string( |
| 263 |
content: str, result: tuple[datetime | None, int | None] |
| 264 |
) -> None: |
| 265 |
"""Test read_uptime_string function.""" |
| 266 |
|
| 267 |
assert devicemap.read_uptime_string(content) == result |
| 268 |
|
| 269 |
|
| 270 |
@pytest.mark.parametrize( |
| 271 |
("raw_seconds", "diff_seconds", "result_seconds"), |
| 272 |
[ |
| 273 |
(15, 0, 14), |
| 274 |
(15, 1, 14), |
| 275 |
(15, 2, 12), |
| 276 |
(16, 0, 16), |
| 277 |
(16, 1, 14), |
| 278 |
(16, 2, 14), |
| 279 |
], |
| 280 |
) |
| 281 |
def test_read_uptime_string_robust( |
| 282 |
raw_seconds: int, |
| 283 |
diff_seconds: int, |
| 284 |
result_seconds: int, |
| 285 |
) -> None: |
| 286 |
"""Test read_uptime_string with robust boottime enabled.""" |
| 287 |
|
| 288 |
# Mock the ARConfig to enable robust boottime |
| 289 |
with patch( |
| 290 |
"asusrouter.modules.endpoint.devicemap.ARConfig" |
| 291 |
) as mock_config: |
| 292 |
mock_config.get.side_effect = ( |
| 293 |
lambda key: key == ARConfKey.ROBUST_BOOTTIME |
| 294 |
) |
| 295 |
# Test with a valid content string |
| 296 |
content = f"Sat, 8 Aug 2025 08:08:{raw_seconds} " |
| 297 |
content += f"+0100({diff_seconds} secs since boot)" |
| 298 |
result = devicemap.read_uptime_string(content) |
| 299 |
assert result == ( |
| 300 |
datetime( |
| 301 |
2025, |
| 302 |
8, |
| 303 |
8, |
| 304 |
8, |
| 305 |
8, |
| 306 |
result_seconds, |
| 307 |
tzinfo=timezone(timedelta(hours=1)), |
| 308 |
), |
| 309 |
diff_seconds, |
| 310 |
) |
| 311 |
|
| 312 |
|
| 313 |
@pytest.mark.parametrize( |
| 314 |
("boottime_return", "expected_flags"), |
| 315 |
[ |
| 316 |
(("boottime", False), {}), |
| 317 |
(("boottime", True), {"reboot": True}), |
| 318 |
], |
| 319 |
) |
| 320 |
@patch("asusrouter.modules.endpoint.devicemap.process_boottime") |
| 321 |
@patch("asusrouter.modules.endpoint.devicemap.process_ovpn") |
| 322 |
def test_process( |
| 323 |
mock_process_ovpn: MagicMock, |
| 324 |
mock_process_boottime: MagicMock, |
| 325 |
boottime_return: tuple[str, bool], |
| 326 |
expected_flags: dict[str, bool], |
| 327 |
) -> None: |
| 328 |
"""Test process function.""" |
| 329 |
|
| 330 |
# Prepare the mock functions |
| 331 |
mock_process_boottime.return_value = boottime_return |
| 332 |
mock_process_ovpn.return_value = "openvpn" |
| 333 |
|
| 334 |
# Prepare the test data |
| 335 |
data = { |
| 336 |
"history": {AsusData.BOOTTIME: AsusDataState(data="prev_boottime")} |
| 337 |
} |
| 338 |
|
| 339 |
# Call the function with the test data |
| 340 |
result = devicemap.process(data) |
| 341 |
|
| 342 |
# Check the result |
| 343 |
assert result == { |
| 344 |
AsusData.DEVICEMAP: data, |
| 345 |
AsusData.BOOTTIME: boottime_return[0], |
| 346 |
AsusData.OPENVPN: "openvpn", |
| 347 |
AsusData.FLAGS: expected_flags, |
| 348 |
} |
| 349 |
|
| 350 |
# Check that the mock functions were called with the correct arguments |
| 351 |
mock_process_boottime.assert_called_once_with(data, "prev_boottime") |
| 352 |
mock_process_ovpn.assert_called_once_with(data) |
| 353 |
|
| 354 |
|
| 355 |
@pytest.mark.parametrize( |
| 356 |
("prev_boottime_delta", "expected_result"), |
| 357 |
[ |
| 358 |
(timedelta(seconds=1), ({"datetime": ANY, "uptime": 2}, False)), |
| 359 |
(timedelta(seconds=3), ({"datetime": ANY, "uptime": 2}, True)), |
| 360 |
], |
| 361 |
) |
| 362 |
@patch("asusrouter.modules.endpoint.devicemap.read_uptime_string") |
| 363 |
def test_process_boottime( |
| 364 |
mock_read_uptime_string: MagicMock, |
| 365 |
prev_boottime_delta: timedelta, |
| 366 |
expected_result: tuple[dict[str, Any], bool], |
| 367 |
) -> None: |
| 368 |
"""Test process_boottime function.""" |
| 369 |
|
| 370 |
# Prepare the mock function |
| 371 |
mock_read_uptime_string.return_value = (datetime.now(), 2) |
| 372 |
|
| 373 |
# Prepare the test data |
| 374 |
devicemap_data = {"sys": {"uptimeStr": "uptime string"}} |
| 375 |
prev_boottime = {"datetime": datetime.now() - prev_boottime_delta} |
| 376 |
|
| 377 |
# Call the function with the test data |
| 378 |
result = devicemap.process_boottime(devicemap_data, prev_boottime) |
| 379 |
|
| 380 |
# Check the result |
| 381 |
assert result == expected_result |
| 382 |
|
| 383 |
# Check that the mock function was called with the correct argument |
| 384 |
mock_read_uptime_string.assert_called_once_with("uptime string") |
| 385 |
|
| 386 |
|
| 387 |
@patch("asusrouter.modules.endpoint.devicemap.AsusOVPNClient") |
| 388 |
@patch("asusrouter.modules.endpoint.devicemap.AsusOVPNServer") |
| 389 |
@patch("asusrouter.modules.endpoint.devicemap.safe_int") |
| 390 |
def test_process_ovpn( |
| 391 |
mock_safe_int: MagicMock, |
| 392 |
mock_asusovpnserver: MagicMock, |
| 393 |
mock_asusovnclient: MagicMock, |
| 394 |
) -> None: |
| 395 |
"""Test process_ovpn function.""" |
| 396 |
|
| 397 |
# Prepare the mock functions |
| 398 |
mock_asusovnclient.return_value = MagicMock() |
| 399 |
mock_asusovpnserver.return_value = MagicMock() |
| 400 |
mock_safe_int.return_value = 0 |
| 401 |
|
| 402 |
# Prepare the test data |
| 403 |
devicemap_data = { |
| 404 |
"vpn": { |
| 405 |
"client1_state": "state", |
| 406 |
"client1_errno": "errno", |
| 407 |
"server1_state": "state", |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
# Call the function with the test data |
| 412 |
result = devicemap.process_ovpn(devicemap_data) |
| 413 |
|
| 414 |
# Check the result |
| 415 |
expected_result = { |
| 416 |
"client": { |
| 417 |
1: { |
| 418 |
"state": mock_asusovnclient.return_value, |
| 419 |
"errno": 0, |
| 420 |
} |
| 421 |
}, |
| 422 |
"server": { |
| 423 |
1: { |
| 424 |
"state": mock_asusovpnserver.return_value, |
| 425 |
} |
| 426 |
}, |
| 427 |
} |
| 428 |
assert result == expected_result |
| 429 |
|
| 430 |
# Check that the mock functions were called with the correct arguments |
| 431 |
mock_asusovnclient.assert_called_once_with(0) |
| 432 |
mock_asusovpnserver.assert_called_once_with(0) |
| 433 |
mock_safe_int.assert_any_call("state", default=0) |
| 434 |
mock_safe_int.assert_any_call("errno") |