| 1 |
"""Tests for the Firmware endpoint module.""" |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from asusrouter.modules.data import AsusData |
| 8 |
from asusrouter.modules.endpoint.firmware import process, read |
| 9 |
from asusrouter.modules.firmware import ( |
| 10 |
Firmware, |
| 11 |
WebsError, |
| 12 |
WebsFlag, |
| 13 |
WebsUpdate, |
| 14 |
WebsUpgrade, |
| 15 |
) |
| 16 |
from asusrouter.tools.readers import read_js_variables |
| 17 |
|
| 18 |
|
| 19 |
def test_read() -> None: |
| 20 |
"""Test read function.""" |
| 21 |
|
| 22 |
# Check if 'read' is the same as 'read_js_variables' |
| 23 |
assert read == read_js_variables |
| 24 |
|
| 25 |
|
| 26 |
# Case 1: No input |
| 27 |
# Check the default values and make sure that |
| 28 |
# the function does not crash even on no input. |
| 29 |
test_case_1_input: dict[str, Any] = {} |
| 30 |
test_case_1: dict[str, Any] = { |
| 31 |
"current": None, |
| 32 |
"state": False, |
| 33 |
"available": None, |
| 34 |
"state_beta": False, |
| 35 |
"available_beta": None, |
| 36 |
"webs": { |
| 37 |
"update": WebsUpdate.UNKNOWN, |
| 38 |
"upgrade": WebsUpgrade.INACTIVE, |
| 39 |
"available": None, |
| 40 |
"available_beta": None, |
| 41 |
"required": None, |
| 42 |
"error": WebsError.UNKNOWN, |
| 43 |
"flag": WebsFlag.UNKNOWN, |
| 44 |
"level": None, |
| 45 |
}, |
| 46 |
"cfg": { |
| 47 |
"check": None, |
| 48 |
"upgrade": None, |
| 49 |
}, |
| 50 |
"sig": { |
| 51 |
"update": None, |
| 52 |
"upgrade": None, |
| 53 |
"version": None, |
| 54 |
"error": None, |
| 55 |
"flag": None, |
| 56 |
}, |
| 57 |
"hndwr": { |
| 58 |
"status": None, |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
# Case 2: Stable firmware available |
| 63 |
# Check that a stable available firmware is properly |
| 64 |
# processed and reported as available update. |
| 65 |
test_case_2_input: dict[str, Any] = { |
| 66 |
"webs_state_info": "3.0.0.4.388.8_0", |
| 67 |
"firmware": Firmware("3.0.0.4.388.7_0"), |
| 68 |
} |
| 69 |
|
| 70 |
test_case_2 = test_case_1.copy() |
| 71 |
test_case_2_webs = test_case_2["webs"].copy() |
| 72 |
test_case_2_webs.update( |
| 73 |
{ |
| 74 |
"available": Firmware("3.0.0.4.388.8_0"), |
| 75 |
} |
| 76 |
) |
| 77 |
test_case_2.update( |
| 78 |
{ |
| 79 |
"current": Firmware("3.0.0.4.388.7_0"), |
| 80 |
"state": True, |
| 81 |
"available": Firmware("3.0.0.4.388.8_0"), |
| 82 |
"webs": test_case_2_webs, |
| 83 |
} |
| 84 |
) |
| 85 |
|
| 86 |
# Case 3: Beta firmware available |
| 87 |
# Check that a beta available firmware is properly |
| 88 |
# processed and reported as available update. |
| 89 |
test_case_3_input: dict[str, Any] = { |
| 90 |
"webs_state_info": "3.0.0.4.388.8_0", |
| 91 |
"webs_state_info_beta": "3.0.0.4.388.8_2beta1", |
| 92 |
"firmware": Firmware("3.0.0.4.388.8_0"), |
| 93 |
} |
| 94 |
|
| 95 |
test_case_3 = test_case_1.copy() |
| 96 |
test_case_3_webs = test_case_3["webs"].copy() |
| 97 |
test_case_3_webs.update( |
| 98 |
{ |
| 99 |
"available": Firmware("3.0.0.4.388.8_0"), |
| 100 |
"available_beta": Firmware("3.0.0.4.388.8_2beta1"), |
| 101 |
} |
| 102 |
) |
| 103 |
test_case_3.update( |
| 104 |
{ |
| 105 |
"current": Firmware("3.0.0.4.388.8_0"), |
| 106 |
"state_beta": True, |
| 107 |
"available_beta": Firmware("3.0.0.4.388.8_2beta1"), |
| 108 |
"webs": test_case_3_webs, |
| 109 |
} |
| 110 |
) |
| 111 |
|
| 112 |
# Case 4: Stable and beta firmware available |
| 113 |
# Check that both stable and beta available firmware are |
| 114 |
# properly processed and reported as available updates. |
| 115 |
test_case_4_input: dict[str, Any] = { |
| 116 |
"webs_state_info": "3.0.0.4.388.8_0", |
| 117 |
"webs_state_info_beta": "3.0.0.4.388.8_2beta1", |
| 118 |
"firmware": Firmware("3.0.0.4.388.7_0"), |
| 119 |
} |
| 120 |
|
| 121 |
test_case_4 = test_case_1.copy() |
| 122 |
test_case_4_webs = test_case_4["webs"].copy() |
| 123 |
test_case_4_webs.update( |
| 124 |
{ |
| 125 |
"available": Firmware("3.0.0.4.388.8_0"), |
| 126 |
"available_beta": Firmware("3.0.0.4.388.8_2beta1"), |
| 127 |
} |
| 128 |
) |
| 129 |
test_case_4.update( |
| 130 |
{ |
| 131 |
"current": Firmware("3.0.0.4.388.7_0"), |
| 132 |
"state": True, |
| 133 |
"available": Firmware("3.0.0.4.388.8_0"), |
| 134 |
"state_beta": True, |
| 135 |
"available_beta": Firmware("3.0.0.4.388.8_2beta1"), |
| 136 |
"webs": test_case_4_webs, |
| 137 |
} |
| 138 |
) |
| 139 |
|
| 140 |
# Case 5: ROG flag (Merlin firmware) |
| 141 |
# Check that the ROG flag is properly ignored. |
| 142 |
test_case_5_input: dict[str, Any] = { |
| 143 |
"webs_state_info": "3.0.0.4.388.7_0", |
| 144 |
"firmware": Firmware("3.0.0.4.388.7_0_rog"), |
| 145 |
} |
| 146 |
|
| 147 |
test_case_5 = test_case_1.copy() |
| 148 |
test_case_5_webs = test_case_5["webs"].copy() |
| 149 |
test_case_5_webs.update( |
| 150 |
{ |
| 151 |
"available": Firmware("3.0.0.4.388.7_0"), |
| 152 |
} |
| 153 |
) |
| 154 |
test_case_5.update( |
| 155 |
{ |
| 156 |
"current": Firmware("3.0.0.4.388.7_0_rog"), |
| 157 |
"webs": test_case_5_webs, |
| 158 |
} |
| 159 |
) |
| 160 |
|
| 161 |
# Case 6: Rog flag reversed (Merlin firmware) |
| 162 |
# Check that the ROG flag is properly ignored. |
| 163 |
test_case_6_input: dict[str, Any] = { |
| 164 |
"webs_state_info": "3.0.0.4.388.7_0_rog", |
| 165 |
"firmware": Firmware("3.0.0.4.388.7_0"), |
| 166 |
} |
| 167 |
|
| 168 |
test_case_6 = test_case_1.copy() |
| 169 |
test_case_6_webs = test_case_6["webs"].copy() |
| 170 |
test_case_6_webs.update( |
| 171 |
{ |
| 172 |
"available": Firmware("3.0.0.4.388.7_0_rog"), |
| 173 |
} |
| 174 |
) |
| 175 |
test_case_6.update( |
| 176 |
{ |
| 177 |
"current": Firmware("3.0.0.4.388.7_0"), |
| 178 |
"webs": test_case_6_webs, |
| 179 |
} |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
test_cases = [ |
| 184 |
( |
| 185 |
test_case_1_input, |
| 186 |
{AsusData.FIRMWARE: test_case_1}, |
| 187 |
), |
| 188 |
( |
| 189 |
test_case_2_input, |
| 190 |
{AsusData.FIRMWARE: test_case_2}, |
| 191 |
), |
| 192 |
( |
| 193 |
test_case_3_input, |
| 194 |
{AsusData.FIRMWARE: test_case_3}, |
| 195 |
), |
| 196 |
( |
| 197 |
test_case_4_input, |
| 198 |
{AsusData.FIRMWARE: test_case_4}, |
| 199 |
), |
| 200 |
( |
| 201 |
test_case_5_input, |
| 202 |
{AsusData.FIRMWARE: test_case_5}, |
| 203 |
), |
| 204 |
( |
| 205 |
test_case_6_input, |
| 206 |
{AsusData.FIRMWARE: test_case_6}, |
| 207 |
), |
| 208 |
] |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.parametrize(("input_data", "expected"), test_cases) |
| 212 |
def test_process(input_data: dict[str, Any], expected: dict[str, Any]) -> None: |
| 213 |
"""Test process function.""" |
| 214 |
|
| 215 |
result = process(input_data) |
| 216 |
assert result == expected |