|
| 1 | +import codecs |
| 2 | + |
| 3 | +from helpers import ( |
| 4 | + ADD_TIME, |
| 5 | + DISABLE_VALUE, |
| 6 | + SET_TIME, |
| 7 | + generate_from_device_data, |
| 8 | + test_accepted_validator, |
| 9 | + test_how_many_days_validator, |
| 10 | + test_name, |
| 11 | +) |
| 12 | + |
| 13 | +from test.simulators.device_simulator import DeviceSimulator |
| 14 | + |
| 15 | +# This tests the device simulator against the full test procedure |
| 16 | + |
| 17 | + |
| 18 | +def run_core_token_tests(device_data, device_simulator): |
| 19 | + test = "We enter an invalid token" |
| 20 | + token_g1 = "123 456 789" |
| 21 | + test_accepted_validator(device_simulator, "G1", token_g1, -1, description=test) |
| 22 | + |
| 23 | + test = "We enter a valid token for setting one day" |
| 24 | + device_data, token_g2 = generate_from_device_data( |
| 25 | + device_data, token_type=SET_TIME, value_days=1 |
| 26 | + ) |
| 27 | + test_how_many_days_validator(device_simulator, "G2", token_g2, 1, description=test) |
| 28 | + |
| 29 | + test = "We enter a valid token for adding one day" |
| 30 | + device_data, token_g3 = generate_from_device_data( |
| 31 | + device_data, token_type=ADD_TIME, value_days=1 |
| 32 | + ) |
| 33 | + test_how_many_days_validator(device_simulator, "G3", token_g3, 2, description=test) |
| 34 | + |
| 35 | + test = ( |
| 36 | + "We enter the same Add Time token for 1 day, the days should not be " |
| 37 | + "added and the device should signal that the token was already used" |
| 38 | + ) |
| 39 | + test_how_many_days_validator( |
| 40 | + device_simulator, |
| 41 | + "G4A", |
| 42 | + token_g3, |
| 43 | + 2, |
| 44 | + description=test, |
| 45 | + accepted_but_used=True, |
| 46 | + ) |
| 47 | + test = ( |
| 48 | + "We enter the older Set Time token for 1 day, the days should not " |
| 49 | + "change and the device should signal that the token was already used" |
| 50 | + ) |
| 51 | + test_how_many_days_validator( |
| 52 | + device_simulator, |
| 53 | + "G4B", |
| 54 | + token_g2, |
| 55 | + 2, |
| 56 | + description=test, |
| 57 | + accepted_but_used=True, |
| 58 | + ) |
| 59 | + |
| 60 | + test = ( |
| 61 | + "We enter a valid token for setting 30 days and ensures it sets and does " |
| 62 | + "not add to the existing" |
| 63 | + ) |
| 64 | + device_data, token_g5 = generate_from_device_data( |
| 65 | + device_data, token_type=SET_TIME, value_days=30 |
| 66 | + ) |
| 67 | + test_how_many_days_validator(device_simulator, "G5", token_g5, 30, description=test) |
| 68 | + |
| 69 | + test = ( |
| 70 | + "We enter a valid token for setting 0 days and ensures the device is " |
| 71 | + "inactive with the outputs disabled immediately" |
| 72 | + ) |
| 73 | + device_data, token_g6 = generate_from_device_data( |
| 74 | + device_data, token_type=SET_TIME, value_days=0 |
| 75 | + ) |
| 76 | + test_how_many_days_validator(device_simulator, "G6", token_g6, 0, description=test) |
| 77 | + |
| 78 | + test = ( |
| 79 | + "We enter 3 consecutive Add Time tokens with the maximum amount of days and " |
| 80 | + "ensure that they cumulate properly" |
| 81 | + ) |
| 82 | + for i in range(1, 3 + 1): |
| 83 | + device_data, token_g7 = generate_from_device_data( |
| 84 | + device_data, token_type=ADD_TIME, value_raw=995 |
| 85 | + ) |
| 86 | + test_how_many_days_validator( |
| 87 | + device_simulator, |
| 88 | + test_name("G7", i), |
| 89 | + token_g7, |
| 90 | + value_raw=995 * i, |
| 91 | + device_data=device_data, |
| 92 | + description=test, |
| 93 | + ) |
| 94 | + test = "" |
| 95 | + |
| 96 | + test = ( |
| 97 | + "We enter 21 consecutive Set Time tokens for 1, 2, 3, … 21 days " |
| 98 | + "each with a count 30 higher than the other. The validation of the " |
| 99 | + "token should not take more than 5 seconds" |
| 100 | + ) |
| 101 | + for i in range(1, 21 + 1): |
| 102 | + device_data, token_g8 = generate_from_device_data( |
| 103 | + device_data, |
| 104 | + token_type=SET_TIME, |
| 105 | + value_days=i, |
| 106 | + token_count=device_data["token_count"] + 29, |
| 107 | + ) |
| 108 | + test_how_many_days_validator( |
| 109 | + device_simulator, |
| 110 | + test_name("G8", i), |
| 111 | + token_g8, |
| 112 | + value_days=i, |
| 113 | + device_data=device_data, |
| 114 | + description=test, |
| 115 | + ) |
| 116 | + test = "" |
| 117 | + |
| 118 | + test = "We enter a PAYG Disable token into the device" |
| 119 | + device_data, token_g9 = generate_from_device_data( |
| 120 | + device_data, token_type=SET_TIME, value_raw=DISABLE_VALUE |
| 121 | + ) |
| 122 | + test_how_many_days_validator( |
| 123 | + device_simulator, "G9", token_g9, None, description=test |
| 124 | + ) |
| 125 | + |
| 126 | + test = "We enter a Set Time token for 0 day, it should relock the device" |
| 127 | + device_data, token_g10 = generate_from_device_data( |
| 128 | + device_data, token_type=SET_TIME, value_days=0 |
| 129 | + ) |
| 130 | + test_how_many_days_validator( |
| 131 | + device_simulator, "G10", token_g10, 0, description=test |
| 132 | + ) |
| 133 | + |
| 134 | + test = ( |
| 135 | + "We enter a PAYG Disable token to relock the device, then enter a Add " |
| 136 | + "Time token with 0 day, it should NOT relock the device (Optional)" |
| 137 | + ) |
| 138 | + device_data, token_g11a = generate_from_device_data( |
| 139 | + device_data, token_type=SET_TIME, value_raw=DISABLE_VALUE |
| 140 | + ) |
| 141 | + test_how_many_days_validator( |
| 142 | + device_simulator, "G11A", token_g11a, None, description=test |
| 143 | + ) |
| 144 | + device_data, token_g11b = generate_from_device_data( |
| 145 | + device_data, token_type=ADD_TIME, value_days=0 |
| 146 | + ) |
| 147 | + test_how_many_days_validator(device_simulator, "G11B", token_g11b, None) |
| 148 | + |
| 149 | + test = ( |
| 150 | + "We deactivate the device with a Set Time of 0 days. We then wait " |
| 151 | + "48 hours before entering a Add Time of 1 day and ensuring that the " |
| 152 | + "days late are not considered in the activation time" |
| 153 | + ) |
| 154 | + device_data, token_g12a = generate_from_device_data( |
| 155 | + device_data, token_type=SET_TIME, value_days=0 |
| 156 | + ) |
| 157 | + test_how_many_days_validator( |
| 158 | + device_simulator, "G12A", token_g12a, 0, description=test |
| 159 | + ) |
| 160 | + device_data, token_g12b = generate_from_device_data( |
| 161 | + device_data, token_type=ADD_TIME, value_days=1 |
| 162 | + ) |
| 163 | + test_how_many_days_validator(device_simulator, "G12B", token_g12b, 1) |
| 164 | + |
| 165 | + return device_data |
| 166 | + |
| 167 | + |
| 168 | +def run_unordered_entry_tests(device_data, device_simulator): |
| 169 | + test = ( |
| 170 | + "We generate 3 Add Time tokens, then enter the 3rd, then first, then " |
| 171 | + "second and ensure the days are added properly" |
| 172 | + ) |
| 173 | + device_data, token_u1a = generate_from_device_data( |
| 174 | + device_data, token_type=SET_TIME, value_days=60 |
| 175 | + ) |
| 176 | + test_how_many_days_validator( |
| 177 | + device_simulator, "U1A", token_u1a, 60, description=test |
| 178 | + ) |
| 179 | + device_data, token_u1b = generate_from_device_data( |
| 180 | + device_data, token_type=ADD_TIME, value_days=10 |
| 181 | + ) |
| 182 | + device_data, token_u1c = generate_from_device_data( |
| 183 | + device_data, token_type=ADD_TIME, value_days=5 |
| 184 | + ) |
| 185 | + device_data, token_u1d = generate_from_device_data( |
| 186 | + device_data, token_type=ADD_TIME, value_days=1 |
| 187 | + ) |
| 188 | + test_how_many_days_validator(device_simulator, "U1B", token_u1d, 61) |
| 189 | + test_how_many_days_validator(device_simulator, "U1C", token_u1b, 71) |
| 190 | + test_how_many_days_validator(device_simulator, "U1D", token_u1c, 76) |
| 191 | + |
| 192 | + test = ( |
| 193 | + "We generate an Add Time, a Set Time and another Add Time token. We " |
| 194 | + "enter the set time and ensure that the older Add Time does not work " |
| 195 | + "but the newer does" |
| 196 | + ) |
| 197 | + device_data, token_u2a = generate_from_device_data( |
| 198 | + device_data, token_type=ADD_TIME, value_days=5 |
| 199 | + ) |
| 200 | + device_data, token_u2b = generate_from_device_data( |
| 201 | + device_data, token_type=SET_TIME, value_days=10 |
| 202 | + ) |
| 203 | + device_data, token_u2c = generate_from_device_data( |
| 204 | + device_data, token_type=ADD_TIME, value_days=3 |
| 205 | + ) |
| 206 | + test_how_many_days_validator( |
| 207 | + device_simulator, "U2A", token_u2b, 10, description=test |
| 208 | + ) |
| 209 | + test_how_many_days_validator( |
| 210 | + device_simulator, "U2B", token_u2a, 10, accepted_but_used=True |
| 211 | + ) |
| 212 | + test_how_many_days_validator(device_simulator, "U2C", token_u2c, 13) |
| 213 | + |
| 214 | + test = ( |
| 215 | + "We generate an Add Time token and a Disable PAYG token, we enter the " |
| 216 | + "Disable PAYG token and then the Add Time token should be refused" |
| 217 | + ) |
| 218 | + device_data, token_u3a = generate_from_device_data( |
| 219 | + device_data, token_type=ADD_TIME, value_days=1 |
| 220 | + ) |
| 221 | + device_data, token_u3b = generate_from_device_data( |
| 222 | + device_data, token_type=ADD_TIME, value_raw=DISABLE_VALUE |
| 223 | + ) |
| 224 | + test_how_many_days_validator( |
| 225 | + device_simulator, "U3A", token_u3b, None, description=test |
| 226 | + ) |
| 227 | + test_how_many_days_validator( |
| 228 | + device_simulator, "U3B", token_u3a, None, accepted_but_used=True |
| 229 | + ) |
| 230 | + |
| 231 | + return device_data |
| 232 | + |
| 233 | + |
| 234 | +if __name__ == "__main__": |
| 235 | + device_data = { |
| 236 | + "serial_number": "XXX", |
| 237 | + "starting_code": 516959010, |
| 238 | + "key": "bc41ec9530f6dac86b1a29ab82edc5fb", |
| 239 | + "restricted_digit_set": False, |
| 240 | + "time_divider": 1, |
| 241 | + "token_count": 1, |
| 242 | + } |
| 243 | + device_simulator = DeviceSimulator( |
| 244 | + device_data["starting_code"], |
| 245 | + codecs.decode(device_data["key"], "hex"), |
| 246 | + restricted_digit_set=device_data["restricted_digit_set"], |
| 247 | + waiting_period_enabled=False, |
| 248 | + ) |
| 249 | + device_data = run_core_token_tests(device_data, device_simulator) |
| 250 | + device_data = run_unordered_entry_tests(device_data, device_simulator) |
0 commit comments