| 1 |
"""Test AsusRouter readers tools.""" |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from asusrouter.const import ContentType |
| 9 |
from asusrouter.tools import readers |
| 10 |
from asusrouter.tools.units import ( |
| 11 |
DataRateUnitConverter, |
| 12 |
UnitConverterBase, |
| 13 |
UnitOfDataRate, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class MockConverter(UnitConverterBase): |
| 18 |
"""Mock converter for testing.""" |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.parametrize( |
| 22 |
("value", "result"), |
| 23 |
[ |
| 24 |
# Any float-compatible value should pass if not |
| 25 |
# smaller than zero |
| 26 |
(1.0, True), |
| 27 |
("0", True), |
| 28 |
(123132, True), |
| 29 |
# Negatives should fail |
| 30 |
(-1.0, False), |
| 31 |
("-1", False), |
| 32 |
# Non-comparable types should pass, since not negative |
| 33 |
(None, True), |
| 34 |
(object(), True), |
| 35 |
({}, True), |
| 36 |
], |
| 37 |
) |
| 38 |
def test_is_non_negative(value: Any, result: bool) -> None: |
| 39 |
"""Test is_non_negative method.""" |
| 40 |
|
| 41 |
assert readers.is_non_negative(value) is result |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
("dict1", "dict2", "expected"), |
| 46 |
[ |
| 47 |
# Test non-nested dictionaries |
| 48 |
({"a": 1, "b": 2}, {"b": 3, "c": 4}, {"a": 1, "b": 2, "c": 4}), |
| 49 |
# Test nested dictionaries |
| 50 |
( |
| 51 |
{"a": 1, "b": {"x": 2}}, |
| 52 |
{"b": {"y": 3}, "c": 4}, |
| 53 |
{"a": 1, "b": {"x": 2, "y": 3}, "c": 4}, |
| 54 |
), |
| 55 |
# Test with None values |
| 56 |
({"a": None, "b": 2}, {"a": 1, "b": None}, {"a": 1, "b": 2}), |
| 57 |
({"a": None}, {"a": {"b": 1}}, {"a": {"b": 1}}), |
| 58 |
], |
| 59 |
) |
| 60 |
def test_merge_dicts( |
| 61 |
dict1: dict[str, Any], dict2: dict[str, Any], expected: dict[str, Any] |
| 62 |
) -> None: |
| 63 |
"""Test merge_dicts method.""" |
| 64 |
|
| 65 |
assert readers.merge_dicts(dict1, dict2) == expected |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize( |
| 69 |
("content", "expected"), |
| 70 |
[ |
| 71 |
("Test string", "test_string"), # Upper case |
| 72 |
( |
| 73 |
"test with special ^$@ characters", |
| 74 |
"test_with_special_characters", |
| 75 |
), # Special characters |
| 76 |
("snake_case", "snake_case"), # Already snake case |
| 77 |
], |
| 78 |
) |
| 79 |
def test_read_as_snake_case(content: str, expected: str) -> None: |
| 80 |
"""Test read_as_snake_case method.""" |
| 81 |
|
| 82 |
assert readers.read_as_snake_case(content) == expected |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
("headers", "expected"), |
| 87 |
[ |
| 88 |
({"content-type": "application/json;charset=UTF-8"}, ContentType.JSON), |
| 89 |
({"content-type": "application/xml"}, ContentType.XML), |
| 90 |
({"content-type": "text/html"}, ContentType.HTML), |
| 91 |
({"content-type": "text/plain"}, ContentType.TEXT), |
| 92 |
({"content-type": "application/octet-stream"}, ContentType.BINARY), |
| 93 |
({"content-type": "application/unknown"}, ContentType.UNKNOWN), |
| 94 |
({}, ContentType.UNKNOWN), |
| 95 |
], |
| 96 |
) |
| 97 |
def test_read_content_type( |
| 98 |
headers: dict[str, str], expected: ContentType |
| 99 |
) -> None: |
| 100 |
"""Test read_content_type method.""" |
| 101 |
|
| 102 |
assert readers.read_content_type(headers) == expected |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.parametrize( |
| 106 |
("content", "expected"), |
| 107 |
[ |
| 108 |
# JS from the active temperature sensors |
| 109 |
( |
| 110 |
'curr_coreTmp_wl0_raw = "44°C";', |
| 111 |
{"curr_coreTmp_wl0_raw": "44°C"}, |
| 112 |
), |
| 113 |
# JS from the disabled temperature sensors |
| 114 |
( |
| 115 |
'curr_coreTmp_wl2_raw = "<i>disabled</i>";', |
| 116 |
{"curr_coreTmp_wl2_raw": "<i>disabled</i>"}, |
| 117 |
), |
| 118 |
# JS from the VPN |
| 119 |
('vpn_client1_status = "None";', {"vpn_client1_status": "None"}), |
| 120 |
# JSON variables |
| 121 |
( |
| 122 |
"get_onboardinglist = [{}][0];", |
| 123 |
{"get_onboardinglist": [{}]}, |
| 124 |
), |
| 125 |
# Value with array index, fallback to string |
| 126 |
("arr = value[0];", {"arr": "value"}), |
| 127 |
# Quoted string, not valid JSON or Python literal |
| 128 |
("foo = 'bar';", {"foo": "bar"}), |
| 129 |
('baz = "qux";', {"baz": "qux"}), |
| 130 |
# Unquoted string, not valid JSON or Python literal |
| 131 |
("plain = not_json;", {"plain": "not_json"}), |
| 132 |
# Extra quotes |
| 133 |
('extra = ""quoted"";', {"extra": '"quoted"'}), |
| 134 |
# Multi-line variable |
| 135 |
( |
| 136 |
( |
| 137 |
"multi_line = {\n" |
| 138 |
' "key1": "value1",\n' |
| 139 |
" \n" |
| 140 |
' "key2": "value2"\n' |
| 141 |
"};" |
| 142 |
), |
| 143 |
{ |
| 144 |
"multi_line": { |
| 145 |
"key1": "value1", |
| 146 |
"key2": "value2", |
| 147 |
} |
| 148 |
}, |
| 149 |
), |
| 150 |
( |
| 151 |
('if (true) {\n conditional = "value";\n};'), |
| 152 |
{"conditional": "value"}, |
| 153 |
), |
| 154 |
# Weird formatting |
| 155 |
(("something var=42;"), {"var": 42}), |
| 156 |
(("something var='4;2';"), {"var": "4;2"}), |
| 157 |
(('var="val42";'), {"var": "val42"}), |
| 158 |
(('var="val;42";'), {"var": "val;42"}), |
| 159 |
(('if(True){var="val42";}'), {"var": "val42"}), |
| 160 |
('if(True){ var="value;amp"; }', {"var": "value;amp"}), |
| 161 |
( |
| 162 |
('if(True){var="val;42";}'), |
| 163 |
{"var": "val;42"}, |
| 164 |
), |
| 165 |
# Other cases |
| 166 |
('var="val\\"42";', {"var": 'val"42'}), |
| 167 |
("var=42; // comment", {"var": 42}), |
| 168 |
('var="foo"; /* block comment */', {"var": "foo"}), |
| 169 |
("a=1; b=2;", {"a": 1, "b": 2}), |
| 170 |
(' var = "foo" ; ', {"var": "foo"}), |
| 171 |
("var=;", {"var": ""}), |
| 172 |
# Non-string values |
| 173 |
("num=123;", {"num": 123}), |
| 174 |
("flag=true;", {"flag": True}), |
| 175 |
], |
| 176 |
) |
| 177 |
def test_read_js_variables(content: str, expected: dict[str, Any]) -> None: |
| 178 |
"""Test read_js_variables method.""" |
| 179 |
|
| 180 |
assert readers.read_js_variables(content) == expected |
| 181 |
|
| 182 |
|
| 183 |
@pytest.mark.parametrize( |
| 184 |
("content", "expected"), |
| 185 |
[ |
| 186 |
# JSON from ethernet ports |
| 187 |
( |
| 188 |
( |
| 189 |
'{ "portSpeed": { "WAN 0": "G", "LAN 1": "G", "LAN 2": "G", ' |
| 190 |
'"LAN 3": "G", "LAN 4": "X", "LAN 5": "X", "LAN 6": "X", ' |
| 191 |
'"LAN 7": "X", "LAN 8": "G" }, "portCount": { "wanCount": 1, ' |
| 192 |
'"lanCount": 8 } }' |
| 193 |
), |
| 194 |
{ |
| 195 |
"portSpeed": { |
| 196 |
"WAN 0": "G", |
| 197 |
"LAN 1": "G", |
| 198 |
"LAN 2": "G", |
| 199 |
"LAN 3": "G", |
| 200 |
"LAN 4": "X", |
| 201 |
"LAN 5": "X", |
| 202 |
"LAN 6": "X", |
| 203 |
"LAN 7": "X", |
| 204 |
"LAN 8": "G", |
| 205 |
}, |
| 206 |
"portCount": {"wanCount": 1, "lanCount": 8}, |
| 207 |
}, |
| 208 |
), |
| 209 |
# JSON from sysinfo |
| 210 |
( |
| 211 |
( |
| 212 |
'{"wlc_0_arr":["11", "11", "11"],' |
| 213 |
'"wlc_1_arr":["2", "2", "2"],' |
| 214 |
'"wlc_2_arr":["0", "0", "0"],' |
| 215 |
'"wlc_3_arr":["0", "0", "0"],' |
| 216 |
'"conn_stats_arr":["394","56"],' |
| 217 |
'"mem_stats_arr":["882.34", "395.23",' |
| 218 |
'"0.00", "52.64", "0.00", "0.00", "85343", "7.61 / 63.00 MB"],' |
| 219 |
'"cpu_stats_arr":["2.18", "2.09", "2.03"]}' |
| 220 |
), |
| 221 |
{ |
| 222 |
"wlc_0_arr": ["11", "11", "11"], |
| 223 |
"wlc_1_arr": ["2", "2", "2"], |
| 224 |
"wlc_2_arr": ["0", "0", "0"], |
| 225 |
"wlc_3_arr": ["0", "0", "0"], |
| 226 |
"conn_stats_arr": ["394", "56"], |
| 227 |
"mem_stats_arr": [ |
| 228 |
"882.34", |
| 229 |
"395.23", |
| 230 |
"0.00", |
| 231 |
"52.64", |
| 232 |
"0.00", |
| 233 |
"0.00", |
| 234 |
"85343", |
| 235 |
"7.61 / 63.00 MB", |
| 236 |
], |
| 237 |
"cpu_stats_arr": ["2.18", "2.09", "2.03"], |
| 238 |
}, |
| 239 |
), |
| 240 |
# Test valid JSON content |
| 241 |
('{"key": "value"}', {"key": "value"}), |
| 242 |
# Test empty content |
| 243 |
(None, {}), |
| 244 |
# Test invalid JSON content |
| 245 |
("not a json", {}), |
| 246 |
# Test missing values |
| 247 |
( |
| 248 |
'{ , "key1": "value1", , "key2": "value2", }', |
| 249 |
{"key1": "value1", "key2": "value2"}, |
| 250 |
), |
| 251 |
# Test keys without values |
| 252 |
( |
| 253 |
'{"key1": "value1", "key2": , "key3": "value3", "key4": ,}', |
| 254 |
{"key1": "value1", "key2": None, "key3": "value3", "key4": None}, |
| 255 |
), |
| 256 |
], |
| 257 |
) |
| 258 |
def test_read_json_content( |
| 259 |
content: str | None, expected: dict[str, Any] |
| 260 |
) -> None: |
| 261 |
"""Test read_json_content method.""" |
| 262 |
|
| 263 |
assert readers.read_json_content(content) == expected |
| 264 |
|
| 265 |
|
| 266 |
def test_read_json_content_fail() -> None: |
| 267 |
"""Test read_json_content method with invalid JSON response.""" |
| 268 |
|
| 269 |
with patch( |
| 270 |
"asusrouter.tools.readers.json.loads", return_value="some value" |
| 271 |
): |
| 272 |
assert readers.read_json_content("invalid json") == {} |
| 273 |
|
| 274 |
|
| 275 |
@pytest.mark.parametrize( |
| 276 |
("content", "expected"), |
| 277 |
[ |
| 278 |
# Test valid MAC addresses |
| 279 |
("01:23:45:67:89:AB", True), |
| 280 |
("01-23-45-67-89-AB", True), |
| 281 |
# Test invalid MAC addresses |
| 282 |
("01:23:45:67:89-87-65", False), |
| 283 |
("01-23-45-67-89", False), |
| 284 |
("01:23:45:67:89:ZZ", False), |
| 285 |
(" ", False), |
| 286 |
# Test non-string input |
| 287 |
(1234567890, False), |
| 288 |
(None, False), |
| 289 |
], |
| 290 |
) |
| 291 |
def test_readable_mac(content: str | None, expected: bool) -> None: |
| 292 |
"""Test readable_mac method.""" |
| 293 |
|
| 294 |
assert readers.readable_mac(content) == expected |
| 295 |
|
| 296 |
|
| 297 |
def test_read_units_as_base_wrong_converter() -> None: |
| 298 |
"""Test read_units_as_base with a wrong converter.""" |
| 299 |
|
| 300 |
with pytest.raises(TypeError, match="Converter must be"): |
| 301 |
readers.read_units_as_base("not a converter", "some_units") # type: ignore[arg-type] |
| 302 |
|
| 303 |
|
| 304 |
@pytest.mark.parametrize("mode", ["none", "single", "list"]) |
| 305 |
def test_read_units_as_base_checks_pass(mode: str) -> None: |
| 306 |
"""Reader should call converter.convert_to_base when checks pass.""" |
| 307 |
|
| 308 |
check_calls: Any |
| 309 |
|
| 310 |
with patch.object( |
| 311 |
MockConverter, "convert_to_base", return_value=1 |
| 312 |
) as mock_convert_to_base: |
| 313 |
if mode == "none": |
| 314 |
check_calls = None |
| 315 |
elif mode == "single": |
| 316 |
|
| 317 |
def is_positive(v: float) -> bool: |
| 318 |
return v > 0 |
| 319 |
|
| 320 |
check_calls = is_positive |
| 321 |
else: |
| 322 |
|
| 323 |
def is_positive(v: float) -> bool: |
| 324 |
return v > 0 |
| 325 |
|
| 326 |
def less_than_1(v: float) -> bool: |
| 327 |
return v < 1 |
| 328 |
|
| 329 |
check_calls = [is_positive, less_than_1] |
| 330 |
|
| 331 |
reader = readers.read_units_as_base( |
| 332 |
MockConverter, "u", check_calls=check_calls, fallback_value=0.0 |
| 333 |
) |
| 334 |
|
| 335 |
result = reader(0.5) |
| 336 |
mock_convert_to_base.assert_called_once_with(0.5, "u") |
| 337 |
assert result == 1 |
| 338 |
|
| 339 |
|
| 340 |
def test_read_units_as_base_checks_fail() -> None: |
| 341 |
"""Reader should return fallback value when checks fail.""" |
| 342 |
|
| 343 |
fallback_value = 0.0 |
| 344 |
|
| 345 |
with patch.object( |
| 346 |
MockConverter, "convert_to_base", return_value=1 |
| 347 |
) as mock_convert_to_base: |
| 348 |
check_calls = [lambda v: v > 0, lambda v: v < 1] |
| 349 |
|
| 350 |
reader = readers.read_units_as_base( |
| 351 |
MockConverter, |
| 352 |
"u", |
| 353 |
check_calls=check_calls, |
| 354 |
fallback_value=fallback_value, |
| 355 |
) |
| 356 |
|
| 357 |
result = reader(1.5) |
| 358 |
mock_convert_to_base.assert_not_called() |
| 359 |
assert result == fallback_value |
| 360 |
|
| 361 |
|
| 362 |
def test_read_units_data_rate() -> None: |
| 363 |
"""Test read_units_data_rate method.""" |
| 364 |
|
| 365 |
input_value = 42.0 |
| 366 |
|
| 367 |
def return_function(x: Any) -> Any: |
| 368 |
"""Return the input value.""" |
| 369 |
|
| 370 |
return x |
| 371 |
|
| 372 |
with patch( |
| 373 |
"asusrouter.tools.readers.read_units_as_base", |
| 374 |
return_value=return_function, |
| 375 |
) as mock_read_units_as_base: |
| 376 |
result = readers.read_units_data_rate( |
| 377 |
UnitOfDataRate.MEBIBIT_PER_SECOND |
| 378 |
) |
| 379 |
|
| 380 |
assert input_value == result(input_value) |
| 381 |
mock_read_units_as_base.assert_called_once_with( |
| 382 |
DataRateUnitConverter, |
| 383 |
UnitOfDataRate.MEBIBIT_PER_SECOND, |
| 384 |
readers.is_non_negative, |
| 385 |
0.0, |
| 386 |
) |