Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 1 | /** |
Bob King | 0dcbdf5 | 2020-01-20 17:19:39 +0800 | [diff] [blame] | 2 | * Copyright c 2020 IBM Corporation |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <errno.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 20 | #include <sys/wait.h> |
| 21 | |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
| 23 | |
Bob King | 0dcbdf5 | 2020-01-20 17:19:39 +0800 | [diff] [blame] | 24 | #include <fstream> |
| 25 | |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #define EXPECT_FILE_VALID(configFile) expectFileValid(configFile) |
| 29 | #define EXPECT_FILE_INVALID(configFile, expectedErrorMessage, \ |
| 30 | expectedOutputMessage) \ |
| 31 | expectFileInvalid(configFile, expectedErrorMessage, expectedOutputMessage) |
| 32 | #define EXPECT_JSON_VALID(configFileJson) expectJsonValid(configFileJson) |
| 33 | #define EXPECT_JSON_INVALID(configFileJson, expectedErrorMessage, \ |
| 34 | expectedOutputMessage) \ |
| 35 | expectJsonInvalid(configFileJson, expectedErrorMessage, \ |
| 36 | expectedOutputMessage) |
| 37 | |
| 38 | using json = nlohmann::json; |
| 39 | |
| 40 | const json validConfigFile = R"( |
| 41 | { |
| 42 | "comments": [ "Config file for a FooBar one-chassis system" ], |
| 43 | |
| 44 | "rules": [ |
| 45 | { |
| 46 | "comments": [ "Sets output voltage for a PMBus regulator rail" ], |
| 47 | "id": "set_voltage_rule", |
| 48 | "actions": [ |
| 49 | { |
| 50 | "pmbus_write_vout_command": { |
| 51 | "format": "linear" |
| 52 | } |
| 53 | } |
| 54 | ] |
Bob King | b3e48bc | 2020-02-18 09:59:09 +0800 | [diff] [blame] | 55 | }, |
| 56 | { |
| 57 | "comments": [ "Reads sensors from a PMBus regulator rail" ], |
| 58 | "id": "read_sensors_rule", |
| 59 | "actions": [ |
| 60 | { |
| 61 | "comments": [ "Read output voltage from READ_VOUT." ], |
| 62 | "pmbus_read_sensor": { |
| 63 | "type": "vout", |
| 64 | "command": "0x8B", |
| 65 | "format": "linear_16" |
| 66 | } |
| 67 | } |
| 68 | ] |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 69 | } |
| 70 | ], |
| 71 | |
| 72 | "chassis": [ |
| 73 | { |
| 74 | "comments": [ "Chassis number 1 containing CPUs and memory" ], |
| 75 | "number": 1, |
| 76 | "devices": [ |
| 77 | { |
| 78 | "comments": [ "IR35221 regulator producing the Vdd rail" ], |
| 79 | "id": "vdd_regulator", |
| 80 | "is_regulator": true, |
| 81 | "fru": "/system/chassis/motherboard/regulator1", |
| 82 | "i2c_interface": { |
| 83 | "bus": 1, |
| 84 | "address": "0x70" |
| 85 | }, |
| 86 | "rails": [ |
| 87 | { |
| 88 | "comments": [ "Vdd rail" ], |
| 89 | "id": "vdd", |
| 90 | "configuration": { |
| 91 | "volts": 1.03, |
| 92 | "rule_id": "set_voltage_rule" |
| 93 | }, |
| 94 | "sensor_monitoring": { |
| 95 | "rule_id": "read_sensors_rule" |
| 96 | } |
| 97 | } |
| 98 | ] |
| 99 | } |
| 100 | ] |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | )"_json; |
| 105 | |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 106 | class TmpFile |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 107 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 108 | public: |
| 109 | TmpFile() |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 110 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 111 | int fd = mkstemp(fileName); |
| 112 | if (fd == -1) |
| 113 | { |
| 114 | perror("Can't create temporary file"); |
| 115 | } |
| 116 | close(fd); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 117 | } |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 118 | |
| 119 | std::string getName() |
| 120 | { |
| 121 | return fileName; |
| 122 | } |
| 123 | |
| 124 | ~TmpFile() |
| 125 | { |
| 126 | unlink(fileName); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | char fileName[17] = "/tmp/temp-XXXXXX"; |
| 131 | }; |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 132 | |
| 133 | std::string getValidationToolCommand(const std::string& configFileName) |
| 134 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 135 | std::string command = |
| 136 | "../phosphor-regulators/tools/validate-regulators-config.py -s \ |
| 137 | ../phosphor-regulators/schema/config_schema.json -c "; |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 138 | command += configFileName; |
| 139 | return command; |
| 140 | } |
| 141 | |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 142 | int runToolForOutputWithCommand(std::string command, |
| 143 | std::string& standardOutput, |
| 144 | std::string& standardError) |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 145 | { |
| 146 | // run the validation tool with the temporary file and return the output |
| 147 | // of the validation tool. |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 148 | TmpFile tmpFile; |
| 149 | command += " 2> " + tmpFile.getName(); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 150 | // get the jsonschema print from validation tool. |
| 151 | char buffer[256]; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 152 | std::string result; |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 153 | // to get the stdout from the validation tool. |
| 154 | FILE* pipe = popen(command.c_str(), "r"); |
| 155 | if (!pipe) |
| 156 | { |
| 157 | throw std::runtime_error("popen() failed!"); |
| 158 | } |
| 159 | while (!std::feof(pipe)) |
| 160 | { |
| 161 | if (fgets(buffer, sizeof buffer, pipe) != NULL) |
| 162 | { |
| 163 | result += buffer; |
| 164 | } |
| 165 | } |
| 166 | int returnValue = pclose(pipe); |
| 167 | // Check if pclose() failed |
| 168 | if (returnValue == -1) |
| 169 | { |
| 170 | // unable to close pipe. Print error and exit function. |
| 171 | throw std::runtime_error("pclose() failed!"); |
| 172 | } |
| 173 | std::string firstLine = result.substr(0, result.find('\n')); |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 174 | standardOutput = firstLine; |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 175 | // Get command exit status from return value |
| 176 | int exitStatus = WEXITSTATUS(returnValue); |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 177 | |
| 178 | // Read the standardError from tmpFile. |
| 179 | std::ifstream input(tmpFile.getName().c_str()); |
| 180 | std::string line; |
| 181 | |
| 182 | if (std::getline(input, line)) |
| 183 | { |
| 184 | standardError = line; |
| 185 | } |
| 186 | |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 187 | return exitStatus; |
| 188 | } |
| 189 | |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 190 | int runToolForOutput(const std::string& configFileName, std::string& output, |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 191 | std::string& error) |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 192 | { |
| 193 | std::string command = getValidationToolCommand(configFileName); |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 194 | return runToolForOutputWithCommand(command, output, error); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 195 | } |
| 196 | |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 197 | void expectFileValid(const std::string& configFileName) |
| 198 | { |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 199 | std::string errorMessage; |
| 200 | std::string outputMessage; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 201 | EXPECT_EQ(runToolForOutput(configFileName, outputMessage, errorMessage), 0); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 202 | EXPECT_EQ(errorMessage, ""); |
| 203 | EXPECT_EQ(outputMessage, ""); |
| 204 | } |
| 205 | |
| 206 | void expectFileInvalid(const std::string& configFileName, |
| 207 | const std::string& expectedErrorMessage, |
| 208 | const std::string& expectedOutputMessage) |
| 209 | { |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 210 | std::string errorMessage; |
| 211 | std::string outputMessage; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 212 | EXPECT_EQ(runToolForOutput(configFileName, outputMessage, errorMessage), 1); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 213 | EXPECT_EQ(errorMessage, expectedErrorMessage); |
| 214 | EXPECT_EQ(outputMessage, expectedOutputMessage); |
| 215 | } |
| 216 | |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 217 | void writeDataToFile(const json configFileJson, std::string fileName) |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 218 | { |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 219 | std::string jsonData = configFileJson.dump(); |
| 220 | std::ofstream out(fileName); |
| 221 | out << jsonData; |
| 222 | out.close(); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void expectJsonValid(const json configFileJson) |
| 226 | { |
| 227 | std::string fileName; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 228 | TmpFile tmpFile; |
| 229 | fileName = tmpFile.getName(); |
| 230 | writeDataToFile(configFileJson, fileName); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 231 | |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 232 | EXPECT_FILE_VALID(fileName); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void expectJsonInvalid(const json configFileJson, |
| 236 | const std::string& expectedErrorMessage, |
| 237 | const std::string& expectedOutputMessage) |
| 238 | { |
| 239 | std::string fileName; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 240 | TmpFile tmpFile; |
| 241 | fileName = tmpFile.getName(); |
| 242 | writeDataToFile(configFileJson, fileName); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 243 | |
| 244 | EXPECT_FILE_INVALID(fileName, expectedErrorMessage, expectedOutputMessage); |
Bob King | 386d33f | 2019-12-26 17:28:56 +0800 | [diff] [blame] | 245 | } |
| 246 | |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 247 | void expectCommandLineSyntax(const std::string& expectedErrorMessage, |
| 248 | const std::string& expectedOutputMessage, |
| 249 | std::string command, int status) |
| 250 | { |
| 251 | std::string errorMessage; |
| 252 | std::string outputMessage; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 253 | EXPECT_EQ(runToolForOutputWithCommand(command, outputMessage, errorMessage), |
| 254 | status); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 255 | EXPECT_EQ(errorMessage, expectedErrorMessage); |
| 256 | EXPECT_EQ(outputMessage, expectedOutputMessage); |
| 257 | } |
| 258 | |
Bob King | beaf653 | 2020-01-21 11:03:49 +0800 | [diff] [blame] | 259 | TEST(ValidateRegulatorsConfigTest, And) |
| 260 | { |
| 261 | // Valid. |
| 262 | { |
| 263 | json configFile = validConfigFile; |
| 264 | json andAction = |
| 265 | R"( |
| 266 | { |
| 267 | "and": [ |
| 268 | { "i2c_compare_byte": { "register": "0xA0", "value": "0x00" } }, |
| 269 | { "i2c_compare_byte": { "register": "0xA1", "value": "0x00" } } |
| 270 | ] |
| 271 | } |
| 272 | )"_json; |
| 273 | configFile["rules"][0]["actions"].push_back(andAction); |
| 274 | EXPECT_JSON_VALID(configFile); |
| 275 | } |
| 276 | |
| 277 | // Invalid: actions property value is an empty array. |
| 278 | { |
| 279 | json configFile = validConfigFile; |
| 280 | json andAction = |
| 281 | R"( |
| 282 | { |
| 283 | "and": [] |
| 284 | } |
| 285 | )"_json; |
| 286 | configFile["rules"][0]["actions"].push_back(andAction); |
| 287 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 288 | "[] is too short"); |
| 289 | } |
| 290 | |
| 291 | // Invalid: actions property has incorrect value data type. |
| 292 | { |
| 293 | json configFile = validConfigFile; |
| 294 | json andAction = |
| 295 | R"( |
| 296 | { |
| 297 | "and": true |
| 298 | } |
| 299 | )"_json; |
| 300 | configFile["rules"][0]["actions"].push_back(andAction); |
| 301 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 302 | "True is not of type 'array'"); |
Bob King | beaf653 | 2020-01-21 11:03:49 +0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Invalid: actions property value contains wrong element type |
| 306 | { |
| 307 | json configFile = validConfigFile; |
| 308 | json andAction = |
| 309 | R"( |
| 310 | { |
| 311 | "and": ["foo"] |
| 312 | } |
| 313 | )"_json; |
| 314 | configFile["rules"][0]["actions"].push_back(andAction); |
| 315 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 316 | "'foo' is not of type 'object'"); |
Bob King | beaf653 | 2020-01-21 11:03:49 +0800 | [diff] [blame] | 317 | } |
| 318 | } |
Bob King | 3728f56 | 2020-01-21 11:35:31 +0800 | [diff] [blame] | 319 | TEST(ValidateRegulatorsConfigTest, Chassis) |
| 320 | { |
| 321 | // Valid: test chassis. |
| 322 | { |
| 323 | json configFile = validConfigFile; |
| 324 | EXPECT_JSON_VALID(configFile); |
| 325 | } |
| 326 | // Valid: test chassis with required properties. |
| 327 | { |
| 328 | json configFile = validConfigFile; |
| 329 | configFile["chassis"][0].erase("comments"); |
| 330 | configFile["chassis"][0].erase("devices"); |
| 331 | EXPECT_JSON_VALID(configFile); |
| 332 | } |
| 333 | // Invalid: test chassis with no number. |
| 334 | { |
| 335 | json configFile = validConfigFile; |
| 336 | configFile["chassis"][0].erase("number"); |
| 337 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 338 | "'number' is a required property"); |
Bob King | 3728f56 | 2020-01-21 11:35:31 +0800 | [diff] [blame] | 339 | } |
| 340 | // Invalid: test chassis with property comments wrong type. |
| 341 | { |
| 342 | json configFile = validConfigFile; |
| 343 | configFile["chassis"][0]["comments"] = true; |
| 344 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 345 | "True is not of type 'array'"); |
Bob King | 3728f56 | 2020-01-21 11:35:31 +0800 | [diff] [blame] | 346 | } |
| 347 | // Invalid: test chassis with property number wrong type. |
| 348 | { |
| 349 | json configFile = validConfigFile; |
| 350 | configFile["chassis"][0]["number"] = 1.3; |
| 351 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 352 | "1.3 is not of type 'integer'"); |
Bob King | 3728f56 | 2020-01-21 11:35:31 +0800 | [diff] [blame] | 353 | } |
| 354 | // Invalid: test chassis with property devices wrong type. |
| 355 | { |
| 356 | json configFile = validConfigFile; |
| 357 | configFile["chassis"][0]["devices"] = true; |
| 358 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 359 | "True is not of type 'array'"); |
Bob King | 3728f56 | 2020-01-21 11:35:31 +0800 | [diff] [blame] | 360 | } |
| 361 | // Invalid: test chassis with property comments empty array. |
| 362 | { |
| 363 | json configFile = validConfigFile; |
| 364 | configFile["chassis"][0]["comments"] = json::array(); |
| 365 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 366 | "[] is too short"); |
| 367 | } |
| 368 | // Invalid: test chassis with property devices empty array. |
| 369 | { |
| 370 | json configFile = validConfigFile; |
| 371 | configFile["chassis"][0]["devices"] = json::array(); |
| 372 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 373 | "[] is too short"); |
| 374 | } |
| 375 | // Invalid: test chassis with property number less than 1. |
| 376 | { |
| 377 | json configFile = validConfigFile; |
| 378 | configFile["chassis"][0]["number"] = 0; |
| 379 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 380 | "0 is less than the minimum of 1"); |
| 381 | } |
| 382 | } |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 383 | TEST(ValidateRegulatorsConfigTest, ComparePresence) |
| 384 | { |
| 385 | json comparePresenceFile = validConfigFile; |
| 386 | comparePresenceFile["rules"][0]["actions"][1]["compare_presence"]["fru"] = |
| 387 | "/system/chassis/motherboard/regulator2"; |
| 388 | comparePresenceFile["rules"][0]["actions"][1]["compare_presence"]["value"] = |
| 389 | true; |
| 390 | // Valid. |
| 391 | { |
| 392 | json configFile = comparePresenceFile; |
| 393 | EXPECT_JSON_VALID(configFile); |
| 394 | } |
| 395 | |
| 396 | // Invalid: no FRU property. |
| 397 | { |
| 398 | json configFile = comparePresenceFile; |
| 399 | configFile["rules"][0]["actions"][1]["compare_presence"].erase("fru"); |
| 400 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 401 | "'fru' is a required property"); |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // Invalid: FRU property length is string less than 1. |
| 405 | { |
| 406 | json configFile = comparePresenceFile; |
| 407 | configFile["rules"][0]["actions"][1]["compare_presence"]["fru"] = ""; |
| 408 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 409 | "'' is too short"); |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | // Invalid: no value property. |
| 413 | { |
| 414 | json configFile = comparePresenceFile; |
| 415 | configFile["rules"][0]["actions"][1]["compare_presence"].erase("value"); |
| 416 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 417 | "'value' is a required property"); |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Invalid: value property type is not boolean. |
| 421 | { |
| 422 | json configFile = comparePresenceFile; |
| 423 | configFile["rules"][0]["actions"][1]["compare_presence"]["value"] = "1"; |
| 424 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 425 | "'1' is not of type 'boolean'"); |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // Invalid: FRU property type is not string. |
| 429 | { |
| 430 | json configFile = comparePresenceFile; |
| 431 | configFile["rules"][0]["actions"][1]["compare_presence"]["fru"] = 1; |
| 432 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 433 | "1 is not of type 'string'"); |
Bob King | bf1cbea | 2020-01-21 11:08:50 +0800 | [diff] [blame] | 434 | } |
| 435 | } |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 436 | TEST(ValidateRegulatorsConfigTest, CompareVpd) |
| 437 | { |
| 438 | json compareVpdFile = validConfigFile; |
| 439 | compareVpdFile["rules"][0]["actions"][1]["compare_vpd"]["fru"] = |
| 440 | "/system/chassis/motherboard/regulator2"; |
| 441 | compareVpdFile["rules"][0]["actions"][1]["compare_vpd"]["keyword"] = "CCIN"; |
| 442 | compareVpdFile["rules"][0]["actions"][1]["compare_vpd"]["value"] = "2D35"; |
| 443 | |
| 444 | // Valid. |
| 445 | { |
| 446 | json configFile = compareVpdFile; |
| 447 | EXPECT_JSON_VALID(configFile); |
| 448 | } |
| 449 | |
| 450 | // Invalid: no FRU property. |
| 451 | { |
| 452 | json configFile = compareVpdFile; |
| 453 | configFile["rules"][0]["actions"][1]["compare_vpd"].erase("fru"); |
| 454 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 455 | "'fru' is a required property"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Invalid: no keyword property. |
| 459 | { |
| 460 | json configFile = compareVpdFile; |
| 461 | configFile["rules"][0]["actions"][1]["compare_vpd"].erase("keyword"); |
| 462 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 463 | "'keyword' is a required property"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Invalid: no value property. |
| 467 | { |
| 468 | json configFile = compareVpdFile; |
| 469 | configFile["rules"][0]["actions"][1]["compare_vpd"].erase("value"); |
| 470 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 471 | "'value' is a required property"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Invalid: property FRU wrong type. |
| 475 | { |
| 476 | json configFile = compareVpdFile; |
| 477 | configFile["rules"][0]["actions"][1]["compare_vpd"]["fru"] = 1; |
| 478 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 479 | "1 is not of type 'string'"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // Invalid: property FRU is string less than 1. |
| 483 | { |
| 484 | json configFile = compareVpdFile; |
| 485 | configFile["rules"][0]["actions"][1]["compare_vpd"]["fru"] = ""; |
| 486 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 487 | "'' is too short"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // Invalid: property keyword is not "CCIN", "Manufacturer", "Model", |
| 491 | // "PartNumber" |
| 492 | { |
| 493 | json configFile = compareVpdFile; |
| 494 | configFile["rules"][0]["actions"][1]["compare_vpd"]["keyword"] = |
| 495 | "Number"; |
| 496 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 497 | "'Number' is not one of ['CCIN', " |
| 498 | "'Manufacturer', 'Model', 'PartNumber']"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Invalid: property value wrong type. |
| 502 | { |
| 503 | json configFile = compareVpdFile; |
| 504 | configFile["rules"][0]["actions"][1]["compare_vpd"]["value"] = 1; |
| 505 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 506 | "1 is not of type 'string'"); |
Bob King | f8b77a0 | 2020-01-21 11:09:47 +0800 | [diff] [blame] | 507 | } |
| 508 | } |
Bob King | 2005741 | 2020-03-16 16:50:17 +0800 | [diff] [blame^] | 509 | TEST(ValidateRegulatorsConfigTest, ConfigFile) |
| 510 | { |
| 511 | // Valid: Only required properties specified |
| 512 | { |
| 513 | json configFile; |
| 514 | configFile["chassis"][0]["number"] = 1; |
| 515 | EXPECT_JSON_VALID(configFile); |
| 516 | } |
| 517 | // Valid: All properties specified |
| 518 | { |
| 519 | json configFile = validConfigFile; |
| 520 | EXPECT_JSON_VALID(configFile); |
| 521 | } |
| 522 | // Invalid: Required chassis property not specified |
| 523 | { |
| 524 | json configFile = validConfigFile; |
| 525 | configFile.erase("chassis"); |
| 526 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 527 | "'chassis' is a required property"); |
| 528 | } |
| 529 | // Invalid: Wrong data type for comments |
| 530 | { |
| 531 | json configFile = validConfigFile; |
| 532 | configFile["comments"] = true; |
| 533 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 534 | "True is not of type 'array'"); |
| 535 | } |
| 536 | // Invalid: Wrong data type for rules |
| 537 | { |
| 538 | json configFile = validConfigFile; |
| 539 | configFile["rules"] = true; |
| 540 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 541 | "True is not of type 'array'"); |
| 542 | } |
| 543 | // Invalid: Wrong data type for chassis |
| 544 | { |
| 545 | json configFile = validConfigFile; |
| 546 | configFile["chassis"] = true; |
| 547 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 548 | "True is not of type 'array'"); |
| 549 | } |
| 550 | // Invalid: Empty comments array; |
| 551 | { |
| 552 | json configFile = validConfigFile; |
| 553 | configFile["comments"] = json::array(); |
| 554 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 555 | "[] is too short"); |
| 556 | } |
| 557 | // Invalid: Empty rules array |
| 558 | { |
| 559 | json configFile = validConfigFile; |
| 560 | configFile["rules"] = json::array(); |
| 561 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 562 | "[] is too short"); |
| 563 | } |
| 564 | // Invalid: Empty chassis array |
| 565 | { |
| 566 | json configFile = validConfigFile; |
| 567 | configFile["chassis"] = json::array(); |
| 568 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 569 | "[] is too short"); |
| 570 | } |
| 571 | // Invalid: Comments array has wrong element type (should be string) |
| 572 | { |
| 573 | json configFile = validConfigFile; |
| 574 | configFile["comments"][0] = true; |
| 575 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 576 | "True is not of type 'string'"); |
| 577 | } |
| 578 | // Invalid: Rules array has wrong element type (should be rule) |
| 579 | { |
| 580 | json configFile = validConfigFile; |
| 581 | configFile["rules"][0] = true; |
| 582 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 583 | "True is not of type 'object'"); |
| 584 | } |
| 585 | // Invalid: Chassis array has wrong element type (should be chassis) |
| 586 | { |
| 587 | json configFile = validConfigFile; |
| 588 | configFile["chassis"][0] = true; |
| 589 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 590 | "True is not of type 'object'"); |
| 591 | } |
| 592 | // Invalid: Unexpected property specified |
| 593 | { |
| 594 | json configFile = validConfigFile; |
| 595 | configFile["foo"] = json::array(); |
| 596 | EXPECT_JSON_INVALID( |
| 597 | configFile, "Validation failed.", |
| 598 | "Additional properties are not allowed ('foo' was unexpected)"); |
| 599 | } |
| 600 | } |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 601 | TEST(ValidateRegulatorsConfigTest, Configuration) |
| 602 | { |
| 603 | json configurationFile = validConfigFile; |
| 604 | configurationFile["chassis"][0]["devices"][0]["configuration"]["comments"] |
| 605 | [0] = "Set rail to 1.25V using standard rule"; |
| 606 | configurationFile["chassis"][0]["devices"][0]["configuration"]["volts"] = |
| 607 | 1.25; |
| 608 | configurationFile["chassis"][0]["devices"][0]["configuration"]["rule_id"] = |
| 609 | "set_voltage_rule"; |
| 610 | // Valid: test configuration with property rule_id and with no actions. |
| 611 | { |
| 612 | json configFile = configurationFile; |
Bob King | 7879310 | 2020-03-13 13:16:09 +0800 | [diff] [blame] | 613 | configFile["chassis"][0]["devices"][0]["configuration"]["comments"][1] = |
| 614 | "test multiple array elements in comments."; |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 615 | EXPECT_JSON_VALID(configFile); |
| 616 | } |
| 617 | // Valid: test configuration with property actions and with no rule_id. |
| 618 | { |
| 619 | json configFile = configurationFile; |
| 620 | configFile["chassis"][0]["devices"][0]["configuration"].erase( |
| 621 | "rule_id"); |
| 622 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"][0] |
| 623 | ["compare_presence"]["fru"] = |
| 624 | "/system/chassis/motherboard/cpu3"; |
| 625 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"][0] |
| 626 | ["compare_presence"]["value"] = true; |
| 627 | EXPECT_JSON_VALID(configFile); |
| 628 | } |
| 629 | // Valid: comments not specified (optional property). |
| 630 | { |
| 631 | json configFile = configurationFile; |
| 632 | configFile["chassis"][0]["devices"][0]["configuration"].erase( |
| 633 | "comments"); |
| 634 | EXPECT_JSON_VALID(configFile); |
| 635 | } |
| 636 | // Valid: volts not specified (optional property). |
| 637 | { |
| 638 | json configFile = configurationFile; |
| 639 | configFile["chassis"][0]["devices"][0]["configuration"].erase("volts"); |
| 640 | EXPECT_JSON_VALID(configFile); |
| 641 | } |
| 642 | // Valid: configuration is property of a rail (vs. a device). |
| 643 | { |
| 644 | json configFile = validConfigFile; |
| 645 | configFile["chassis"][0]["devices"][0]["rails"][0]["configuration"] |
| 646 | ["comments"][0] = "Set rail to 1.25V using standard rule"; |
| 647 | configFile["chassis"][0]["devices"][0]["rails"][0]["configuration"] |
| 648 | ["volts"] = 1.25; |
| 649 | configFile["chassis"][0]["devices"][0]["rails"][0]["configuration"] |
| 650 | ["rule_id"] = "set_voltage_rule"; |
| 651 | EXPECT_JSON_VALID(configFile); |
| 652 | } |
| 653 | // Invalid: comments property has wrong data type (not an array). |
| 654 | { |
| 655 | json configFile = configurationFile; |
| 656 | configFile["chassis"][0]["devices"][0]["configuration"]["comments"] = 1; |
| 657 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 658 | "1 is not of type 'array'"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 659 | } |
| 660 | // Invalid: test configuration with both actions and rule_id properties. |
| 661 | { |
| 662 | json configFile = configurationFile; |
| 663 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"][0] |
| 664 | ["compare_presence"]["fru"] = |
| 665 | "/system/chassis/motherboard/cpu3"; |
| 666 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"][0] |
| 667 | ["compare_presence"]["value"] = true; |
| 668 | EXPECT_JSON_INVALID( |
| 669 | configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 670 | "{'actions': [{'compare_presence': {'fru': " |
| 671 | "'/system/chassis/motherboard/cpu3', 'value': True}}], 'comments': " |
| 672 | "['Set rail to 1.25V using standard rule'], 'rule_id': " |
| 673 | "'set_voltage_rule', 'volts': 1.25} is valid under each of " |
| 674 | "{'required': ['actions']}, {'required': ['rule_id']}"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 675 | } |
| 676 | // Invalid: test configuration with no rule_id and actions. |
| 677 | { |
| 678 | json configFile = configurationFile; |
| 679 | configFile["chassis"][0]["devices"][0]["configuration"].erase( |
| 680 | "rule_id"); |
| 681 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 682 | "'rule_id' is a required property"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 683 | } |
| 684 | // Invalid: test configuration with property volts wrong type. |
| 685 | { |
| 686 | json configFile = configurationFile; |
| 687 | configFile["chassis"][0]["devices"][0]["configuration"]["volts"] = true; |
| 688 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 689 | "True is not of type 'number'"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 690 | } |
| 691 | // Invalid: test configuration with property rule_id wrong type. |
| 692 | { |
| 693 | json configFile = configurationFile; |
| 694 | configFile["chassis"][0]["devices"][0]["configuration"]["rule_id"] = |
| 695 | true; |
| 696 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 697 | "True is not of type 'string'"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 698 | } |
| 699 | // Invalid: test configuration with property actions wrong type. |
| 700 | { |
| 701 | json configFile = configurationFile; |
| 702 | configFile["chassis"][0]["devices"][0]["configuration"].erase( |
| 703 | "rule_id"); |
| 704 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"] = |
| 705 | true; |
| 706 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 707 | "True is not of type 'array'"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 708 | } |
| 709 | // Invalid: test configuration with property comments empty array. |
| 710 | { |
| 711 | json configFile = configurationFile; |
| 712 | configFile["chassis"][0]["devices"][0]["configuration"]["comments"] = |
| 713 | json::array(); |
| 714 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 715 | "[] is too short"); |
| 716 | } |
| 717 | // Invalid: test configuration with property rule_id wrong format. |
| 718 | { |
| 719 | json configFile = configurationFile; |
| 720 | configFile["chassis"][0]["devices"][0]["configuration"]["rule_id"] = |
| 721 | "id!"; |
| 722 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 723 | "'id!' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | 4c67a3a | 2020-02-07 09:48:11 +0800 | [diff] [blame] | 724 | } |
| 725 | // Invalid: test configuration with property actions empty array. |
| 726 | { |
| 727 | json configFile = configurationFile; |
| 728 | configFile["chassis"][0]["devices"][0]["configuration"].erase( |
| 729 | "rule_id"); |
| 730 | configFile["chassis"][0]["devices"][0]["configuration"]["actions"] = |
| 731 | json::array(); |
| 732 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 733 | "[] is too short"); |
| 734 | } |
| 735 | } |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 736 | TEST(ValidateRegulatorsConfigTest, Device) |
| 737 | { |
| 738 | |
| 739 | // Valid: test devices. |
| 740 | { |
| 741 | json configFile = validConfigFile; |
| 742 | EXPECT_JSON_VALID(configFile); |
| 743 | } |
| 744 | // Valid: test devices with required properties. |
| 745 | { |
| 746 | json configFile = validConfigFile; |
| 747 | configFile["chassis"][0]["devices"][0].erase("comments"); |
| 748 | configFile["chassis"][0]["devices"][0].erase("presence_detection"); |
| 749 | configFile["chassis"][0]["devices"][0].erase("configuration"); |
| 750 | configFile["chassis"][0]["devices"][0].erase("rails"); |
| 751 | EXPECT_JSON_VALID(configFile); |
| 752 | } |
| 753 | // Invalid: test devices with no id. |
| 754 | { |
| 755 | json configFile = validConfigFile; |
| 756 | configFile["chassis"][0]["devices"][0].erase("id"); |
| 757 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 758 | "'id' is a required property"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 759 | } |
| 760 | // Invalid: test devices with no is_regulator. |
| 761 | { |
| 762 | json configFile = validConfigFile; |
| 763 | configFile["chassis"][0]["devices"][0].erase("is_regulator"); |
| 764 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 765 | "'is_regulator' is a required property"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 766 | } |
| 767 | // Invalid: test devices with no fru. |
| 768 | { |
| 769 | json configFile = validConfigFile; |
| 770 | configFile["chassis"][0]["devices"][0].erase("fru"); |
| 771 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 772 | "'fru' is a required property"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 773 | } |
| 774 | // Invalid: test devices with no i2c_interface. |
| 775 | { |
| 776 | json configFile = validConfigFile; |
| 777 | configFile["chassis"][0]["devices"][0].erase("i2c_interface"); |
| 778 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 779 | "'i2c_interface' is a required property"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 780 | } |
| 781 | // Invalid: test devices with property comments wrong type. |
| 782 | { |
| 783 | json configFile = validConfigFile; |
| 784 | configFile["chassis"][0]["devices"][0]["comments"] = true; |
| 785 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 786 | "True is not of type 'array'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 787 | } |
| 788 | // Invalid: test devices with property id wrong type. |
| 789 | { |
| 790 | json configFile = validConfigFile; |
| 791 | configFile["chassis"][0]["devices"][0]["id"] = true; |
| 792 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 793 | "True is not of type 'string'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 794 | } |
| 795 | // Invalid: test devices with property is_regulator wrong type. |
| 796 | { |
| 797 | json configFile = validConfigFile; |
| 798 | configFile["chassis"][0]["devices"][0]["is_regulator"] = 1; |
| 799 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 800 | "1 is not of type 'boolean'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 801 | } |
| 802 | // Invalid: test devices with property fru wrong type. |
| 803 | { |
| 804 | json configFile = validConfigFile; |
| 805 | configFile["chassis"][0]["devices"][0]["fru"] = true; |
| 806 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 807 | "True is not of type 'string'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 808 | } |
| 809 | // Invalid: test devices with property i2c_interface wrong type. |
| 810 | { |
| 811 | json configFile = validConfigFile; |
| 812 | configFile["chassis"][0]["devices"][0]["i2c_interface"] = true; |
| 813 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 814 | "True is not of type 'object'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 815 | } |
| 816 | // Invalid: test devices with property presence_detection wrong |
| 817 | // type. |
| 818 | { |
| 819 | json configFile = validConfigFile; |
| 820 | configFile["chassis"][0]["devices"][0]["presence_detection"] = true; |
| 821 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 822 | "True is not of type 'object'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 823 | } |
| 824 | // Invalid: test devices with property configuration wrong type. |
| 825 | { |
| 826 | json configFile = validConfigFile; |
| 827 | configFile["chassis"][0]["devices"][0]["configuration"] = true; |
| 828 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 829 | "True is not of type 'object'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 830 | } |
| 831 | // Invalid: test devices with property rails wrong type. |
| 832 | { |
| 833 | json configFile = validConfigFile; |
| 834 | configFile["chassis"][0]["devices"][0]["rails"] = true; |
| 835 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 836 | "True is not of type 'array'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 837 | } |
| 838 | // Invalid: test devices with property comments empty array. |
| 839 | { |
| 840 | json configFile = validConfigFile; |
| 841 | configFile["chassis"][0]["devices"][0]["comments"] = json::array(); |
| 842 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 843 | "[] is too short"); |
| 844 | } |
| 845 | // Invalid: test devices with property fru length less than 1. |
| 846 | { |
| 847 | json configFile = validConfigFile; |
| 848 | configFile["chassis"][0]["devices"][0]["fru"] = ""; |
| 849 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 850 | "'' is too short"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 851 | } |
| 852 | // Invalid: test devices with property id wrong format. |
| 853 | { |
| 854 | json configFile = validConfigFile; |
| 855 | configFile["chassis"][0]["devices"][0]["id"] = "id#"; |
| 856 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 857 | "'id#' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | a2ba2df | 2020-02-04 14:38:44 +0800 | [diff] [blame] | 858 | } |
| 859 | // Invalid: test devices with property rails empty array. |
| 860 | { |
| 861 | json configFile = validConfigFile; |
| 862 | configFile["chassis"][0]["devices"][0]["rails"] = json::array(); |
| 863 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 864 | "[] is too short"); |
| 865 | } |
| 866 | } |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 867 | TEST(ValidateRegulatorsConfigTest, I2CCompareBit) |
| 868 | { |
| 869 | json i2cCompareBitFile = validConfigFile; |
| 870 | i2cCompareBitFile["rules"][0]["actions"][1]["i2c_compare_bit"]["register"] = |
| 871 | "0xA0"; |
| 872 | i2cCompareBitFile["rules"][0]["actions"][1]["i2c_compare_bit"]["position"] = |
| 873 | 3; |
| 874 | i2cCompareBitFile["rules"][0]["actions"][1]["i2c_compare_bit"]["value"] = 1; |
| 875 | // Valid: test rule actions i2c_compare_bit. |
| 876 | { |
| 877 | json configFile = i2cCompareBitFile; |
| 878 | EXPECT_JSON_VALID(configFile); |
| 879 | } |
| 880 | // Invalid: test i2c_compare_bit with no register. |
| 881 | { |
| 882 | json configFile = i2cCompareBitFile; |
| 883 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"].erase( |
| 884 | "register"); |
| 885 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 886 | "'register' is a required property"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 887 | } |
| 888 | // Invalid: test i2c_compare_bit with no position. |
| 889 | { |
| 890 | json configFile = i2cCompareBitFile; |
| 891 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"].erase( |
| 892 | "position"); |
| 893 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 894 | "'position' is a required property"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 895 | } |
| 896 | // Invalid: test i2c_compare_bit with no value. |
| 897 | { |
| 898 | json configFile = i2cCompareBitFile; |
| 899 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"].erase("value"); |
| 900 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 901 | "'value' is a required property"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 902 | } |
| 903 | // Invalid: test i2c_compare_bit with register wrong type. |
| 904 | { |
| 905 | json configFile = i2cCompareBitFile; |
| 906 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["register"] = 1; |
| 907 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 908 | "1 is not of type 'string'"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 909 | } |
| 910 | // Invalid: test i2c_compare_bit with register wrong format. |
| 911 | { |
| 912 | json configFile = i2cCompareBitFile; |
| 913 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["register"] = |
| 914 | "0xA00"; |
| 915 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 916 | "'0xA00' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 917 | } |
| 918 | // Invalid: test i2c_compare_bit with position wrong type. |
| 919 | { |
| 920 | json configFile = i2cCompareBitFile; |
| 921 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["position"] = |
| 922 | 3.1; |
| 923 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 924 | "3.1 is not of type 'integer'"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 925 | } |
| 926 | // Invalid: test i2c_compare_bit with position greater than 7. |
| 927 | { |
| 928 | json configFile = i2cCompareBitFile; |
| 929 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["position"] = 8; |
| 930 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 931 | "8 is greater than the maximum of 7"); |
| 932 | } |
| 933 | // Invalid: test i2c_compare_bit with position less than 0. |
| 934 | { |
| 935 | json configFile = i2cCompareBitFile; |
| 936 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["position"] = |
| 937 | -1; |
| 938 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 939 | "-1 is less than the minimum of 0"); |
| 940 | } |
| 941 | // Invalid: test i2c_compare_bit with value wrong type. |
| 942 | { |
| 943 | json configFile = i2cCompareBitFile; |
| 944 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["value"] = "1"; |
| 945 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 946 | "'1' is not of type 'integer'"); |
Bob King | 4ab8cbb | 2020-01-21 11:10:48 +0800 | [diff] [blame] | 947 | } |
| 948 | // Invalid: test i2c_compare_bit with value greater than 1. |
| 949 | { |
| 950 | json configFile = i2cCompareBitFile; |
| 951 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["value"] = 2; |
| 952 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 953 | "2 is greater than the maximum of 1"); |
| 954 | } |
| 955 | // Invalid: test i2c_compare_bit with value less than 0. |
| 956 | { |
| 957 | json configFile = i2cCompareBitFile; |
| 958 | configFile["rules"][0]["actions"][1]["i2c_compare_bit"]["value"] = -1; |
| 959 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 960 | "-1 is less than the minimum of 0"); |
| 961 | } |
| 962 | } |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 963 | TEST(ValidateRegulatorsConfigTest, I2CCompareByte) |
| 964 | { |
| 965 | json i2cCompareByteFile = validConfigFile; |
| 966 | i2cCompareByteFile["rules"][0]["actions"][1]["i2c_compare_byte"] |
| 967 | ["register"] = "0x82"; |
| 968 | i2cCompareByteFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = |
| 969 | "0x40"; |
| 970 | i2cCompareByteFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = |
| 971 | "0x7F"; |
| 972 | // Valid: test i2c_compare_byte with all properties. |
| 973 | { |
| 974 | json configFile = i2cCompareByteFile; |
| 975 | EXPECT_JSON_VALID(configFile); |
| 976 | } |
| 977 | // Valid: test i2c_compare_byte with all required properties. |
| 978 | { |
| 979 | json configFile = i2cCompareByteFile; |
| 980 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"].erase("mask"); |
| 981 | EXPECT_JSON_VALID(configFile); |
| 982 | } |
| 983 | // Invalid: test i2c_compare_byte with no register. |
| 984 | { |
| 985 | json configFile = i2cCompareByteFile; |
| 986 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"].erase( |
| 987 | "register"); |
| 988 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 989 | "'register' is a required property"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 990 | } |
| 991 | // Invalid: test i2c_compare_byte with no value. |
| 992 | { |
| 993 | json configFile = i2cCompareByteFile; |
| 994 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"].erase("value"); |
| 995 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 996 | "'value' is a required property"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 997 | } |
| 998 | // Invalid: test i2c_compare_byte with property register wrong type. |
| 999 | { |
| 1000 | json configFile = i2cCompareByteFile; |
| 1001 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] = |
| 1002 | 1; |
| 1003 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1004 | "1 is not of type 'string'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1005 | } |
| 1006 | // Invalid: test i2c_compare_byte with property value wrong type. |
| 1007 | { |
| 1008 | json configFile = i2cCompareByteFile; |
| 1009 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = 1; |
| 1010 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1011 | "1 is not of type 'string'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1012 | } |
| 1013 | // Invalid: test i2c_compare_byte with property mask wrong type. |
| 1014 | { |
| 1015 | json configFile = i2cCompareByteFile; |
| 1016 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = 1; |
| 1017 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1018 | "1 is not of type 'string'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1019 | } |
| 1020 | // Invalid: test i2c_compare_byte with property register more than 2 hex |
| 1021 | // digits. |
| 1022 | { |
| 1023 | json configFile = i2cCompareByteFile; |
| 1024 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] = |
| 1025 | "0x820"; |
| 1026 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1027 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1028 | } |
| 1029 | // Invalid: test i2c_compare_byte with property value more than 2 hex |
| 1030 | // digits. |
| 1031 | { |
| 1032 | json configFile = i2cCompareByteFile; |
| 1033 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = |
| 1034 | "0x820"; |
| 1035 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1036 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1037 | } |
| 1038 | // Invalid: test i2c_compare_byte with property mask more than 2 hex digits. |
| 1039 | { |
| 1040 | json configFile = i2cCompareByteFile; |
| 1041 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = |
| 1042 | "0x820"; |
| 1043 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1044 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1045 | } |
| 1046 | // Invalid: test i2c_compare_byte with property register less than 2 hex |
| 1047 | // digits. |
| 1048 | { |
| 1049 | json configFile = i2cCompareByteFile; |
| 1050 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] = |
| 1051 | "0x8"; |
| 1052 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1053 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1054 | } |
| 1055 | // Invalid: test i2c_compare_byte with property value less than 2 hex |
| 1056 | // digits. |
| 1057 | { |
| 1058 | json configFile = i2cCompareByteFile; |
| 1059 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = |
| 1060 | "0x8"; |
| 1061 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1062 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1063 | } |
| 1064 | // Invalid: test i2c_compare_byte with property mask less than 2 hex digits. |
| 1065 | { |
| 1066 | json configFile = i2cCompareByteFile; |
| 1067 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = |
| 1068 | "0x8"; |
| 1069 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1070 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1071 | } |
| 1072 | // Invalid: test i2c_compare_byte with property register no leading prefix. |
| 1073 | { |
| 1074 | json configFile = i2cCompareByteFile; |
| 1075 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] = |
| 1076 | "82"; |
| 1077 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1078 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1079 | } |
| 1080 | // Invalid: test i2c_compare_byte with property value no leading prefix. |
| 1081 | { |
| 1082 | json configFile = i2cCompareByteFile; |
| 1083 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = |
| 1084 | "82"; |
| 1085 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1086 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1087 | } |
| 1088 | // Invalid: test i2c_compare_byte with property mask no leading prefix. |
| 1089 | { |
| 1090 | json configFile = i2cCompareByteFile; |
| 1091 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = "82"; |
| 1092 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1093 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1094 | } |
| 1095 | // Invalid: test i2c_compare_byte with property register invalid hex digit. |
| 1096 | { |
| 1097 | json configFile = i2cCompareByteFile; |
| 1098 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["register"] = |
| 1099 | "0xG1"; |
| 1100 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1101 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1102 | } |
| 1103 | // Invalid: test i2c_compare_byte with property value invalid hex digit. |
| 1104 | { |
| 1105 | json configFile = i2cCompareByteFile; |
| 1106 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["value"] = |
| 1107 | "0xG1"; |
| 1108 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1109 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1110 | } |
| 1111 | // Invalid: test i2c_compare_byte with property mask invalid hex digit. |
| 1112 | { |
| 1113 | json configFile = i2cCompareByteFile; |
| 1114 | configFile["rules"][0]["actions"][1]["i2c_compare_byte"]["mask"] = |
| 1115 | "0xG1"; |
| 1116 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1117 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 514023d | 2020-01-21 11:13:05 +0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1120 | TEST(ValidateRegulatorsConfigTest, I2CCompareBytes) |
| 1121 | { |
| 1122 | json i2cCompareBytesFile = validConfigFile; |
| 1123 | i2cCompareBytesFile["rules"][0]["actions"][1]["i2c_compare_bytes"] |
| 1124 | ["register"] = "0x82"; |
| 1125 | i2cCompareBytesFile["rules"][0]["actions"][1]["i2c_compare_bytes"] |
| 1126 | ["values"] = {"0x02", "0x73"}; |
| 1127 | i2cCompareBytesFile["rules"][0]["actions"][1]["i2c_compare_bytes"] |
| 1128 | ["masks"] = {"0x7F", "0x7F"}; |
| 1129 | // Valid: test i2c_compare_bytes. |
| 1130 | { |
| 1131 | json configFile = i2cCompareBytesFile; |
| 1132 | EXPECT_JSON_VALID(configFile); |
| 1133 | } |
| 1134 | // Valid: test i2c_compare_bytes with all required properties. |
| 1135 | { |
| 1136 | json configFile = i2cCompareBytesFile; |
| 1137 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"].erase( |
| 1138 | "masks"); |
| 1139 | EXPECT_JSON_VALID(configFile); |
| 1140 | } |
| 1141 | // Invalid: test i2c_compare_bytes with no register. |
| 1142 | { |
| 1143 | json configFile = i2cCompareBytesFile; |
| 1144 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"].erase( |
| 1145 | "register"); |
| 1146 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1147 | "'register' is a required property"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1148 | } |
| 1149 | // Invalid: test i2c_compare_bytes with no values. |
| 1150 | { |
| 1151 | json configFile = i2cCompareBytesFile; |
| 1152 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"].erase( |
| 1153 | "values"); |
| 1154 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1155 | "'values' is a required property"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1156 | } |
| 1157 | // Invalid: test i2c_compare_bytes with property values as empty array. |
| 1158 | { |
| 1159 | json configFile = i2cCompareBytesFile; |
| 1160 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"] = |
| 1161 | json::array(); |
| 1162 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1163 | "[] is too short"); |
| 1164 | } |
| 1165 | // Invalid: test i2c_compare_bytes with property masks as empty array. |
| 1166 | { |
| 1167 | json configFile = i2cCompareBytesFile; |
| 1168 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"] = |
| 1169 | json::array(); |
| 1170 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1171 | "[] is too short"); |
| 1172 | } |
| 1173 | // Invalid: test i2c_compare_bytes with property register wrong type. |
| 1174 | { |
| 1175 | json configFile = i2cCompareBytesFile; |
| 1176 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 1177 | 1; |
| 1178 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1179 | "1 is not of type 'string'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1180 | } |
| 1181 | // Invalid: test i2c_compare_bytes with property values wrong type. |
| 1182 | { |
| 1183 | json configFile = i2cCompareBytesFile; |
| 1184 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"] = 1; |
| 1185 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1186 | "1 is not of type 'array'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1187 | } |
| 1188 | // Invalid: test i2c_compare_bytes with property masks wrong type. |
| 1189 | { |
| 1190 | json configFile = i2cCompareBytesFile; |
| 1191 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"] = 1; |
| 1192 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1193 | "1 is not of type 'array'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1194 | } |
| 1195 | // Invalid: test i2c_compare_bytes with property register more than 2 hex |
| 1196 | // digits. |
| 1197 | { |
| 1198 | json configFile = i2cCompareBytesFile; |
| 1199 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 1200 | "0x820"; |
| 1201 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1202 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1203 | } |
| 1204 | // Invalid: test i2c_compare_bytes with property values more than 2 hex |
| 1205 | // digits. |
| 1206 | { |
| 1207 | json configFile = i2cCompareBytesFile; |
| 1208 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"][0] = |
| 1209 | "0x820"; |
| 1210 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1211 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1212 | } |
| 1213 | // Invalid: test i2c_compare_bytes with property masks more than 2 hex |
| 1214 | // digits. |
| 1215 | { |
| 1216 | json configFile = i2cCompareBytesFile; |
| 1217 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"][0] = |
| 1218 | "0x820"; |
| 1219 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1220 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1221 | } |
| 1222 | // Invalid: test i2c_compare_bytes with property register less than 2 hex |
| 1223 | // digits. |
| 1224 | { |
| 1225 | json configFile = i2cCompareBytesFile; |
| 1226 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 1227 | "0x8"; |
| 1228 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1229 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1230 | } |
| 1231 | // Invalid: test i2c_compare_bytes with property values less than 2 hex |
| 1232 | // digits. |
| 1233 | { |
| 1234 | json configFile = i2cCompareBytesFile; |
| 1235 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"][0] = |
| 1236 | "0x8"; |
| 1237 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1238 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1239 | } |
| 1240 | // Invalid: test i2c_compare_bytes with property masks less than 2 hex |
| 1241 | // digits. |
| 1242 | { |
| 1243 | json configFile = i2cCompareBytesFile; |
| 1244 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"][0] = |
| 1245 | "0x8"; |
| 1246 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1247 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1248 | } |
| 1249 | // Invalid: test i2c_compare_bytes with property register no leading prefix. |
| 1250 | { |
| 1251 | json configFile = i2cCompareBytesFile; |
| 1252 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 1253 | "82"; |
| 1254 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1255 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1256 | } |
| 1257 | // Invalid: test i2c_compare_bytes with property values no leading prefix. |
| 1258 | { |
| 1259 | json configFile = i2cCompareBytesFile; |
| 1260 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"][0] = |
| 1261 | "82"; |
| 1262 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1263 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1264 | } |
| 1265 | // Invalid: test i2c_compare_bytes with property masks no leading prefix. |
| 1266 | { |
| 1267 | json configFile = i2cCompareBytesFile; |
| 1268 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"][0] = |
| 1269 | "82"; |
| 1270 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1271 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1272 | } |
| 1273 | // Invalid: test i2c_compare_bytes with property register invalid hex digit. |
| 1274 | { |
| 1275 | json configFile = i2cCompareBytesFile; |
| 1276 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 1277 | "0xG1"; |
| 1278 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1279 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1280 | } |
| 1281 | // Invalid: test i2c_compare_bytes with property values invalid hex digit. |
| 1282 | { |
| 1283 | json configFile = i2cCompareBytesFile; |
| 1284 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"][0] = |
| 1285 | "0xG1"; |
| 1286 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1287 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1288 | } |
| 1289 | // Invalid: test i2c_compare_bytes with property masks invalid hex digit. |
| 1290 | { |
| 1291 | json configFile = i2cCompareBytesFile; |
| 1292 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"][0] = |
| 1293 | "0xG1"; |
| 1294 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1295 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | fb162bb | 2020-01-21 11:28:07 +0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1298 | TEST(ValidateRegulatorsConfigTest, I2CInterface) |
| 1299 | { |
| 1300 | // Valid: test i2c_interface. |
| 1301 | { |
| 1302 | json configFile = validConfigFile; |
| 1303 | EXPECT_JSON_VALID(configFile); |
| 1304 | } |
| 1305 | // Invalid: testi2c_interface with no bus. |
| 1306 | { |
| 1307 | json configFile = validConfigFile; |
| 1308 | configFile["chassis"][0]["devices"][0]["i2c_interface"].erase("bus"); |
| 1309 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1310 | "'bus' is a required property"); |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1311 | } |
| 1312 | // Invalid: test i2c_interface with no address. |
| 1313 | { |
| 1314 | json configFile = validConfigFile; |
| 1315 | configFile["chassis"][0]["devices"][0]["i2c_interface"].erase( |
| 1316 | "address"); |
| 1317 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1318 | "'address' is a required property"); |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1319 | } |
| 1320 | // Invalid: test i2c_interface with property bus wrong type. |
| 1321 | { |
| 1322 | json configFile = validConfigFile; |
| 1323 | configFile["chassis"][0]["devices"][0]["i2c_interface"]["bus"] = true; |
| 1324 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1325 | "True is not of type 'integer'"); |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1326 | } |
| 1327 | // Invalid: test i2c_interface with property address wrong |
| 1328 | // type. |
| 1329 | { |
| 1330 | json configFile = validConfigFile; |
| 1331 | configFile["chassis"][0]["devices"][0]["i2c_interface"]["address"] = |
| 1332 | true; |
| 1333 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1334 | "True is not of type 'string'"); |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1335 | } |
| 1336 | // Invalid: test i2c_interface with property bus less than |
| 1337 | // 0. |
| 1338 | { |
| 1339 | json configFile = validConfigFile; |
| 1340 | configFile["chassis"][0]["devices"][0]["i2c_interface"]["bus"] = -1; |
| 1341 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1342 | "-1 is less than the minimum of 0"); |
| 1343 | } |
| 1344 | // Invalid: test i2c_interface with property address wrong |
| 1345 | // format. |
| 1346 | { |
| 1347 | json configFile = validConfigFile; |
| 1348 | configFile["chassis"][0]["devices"][0]["i2c_interface"]["address"] = |
| 1349 | "0x700"; |
| 1350 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1351 | "'0x700' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | ca93f1f | 2020-01-31 11:21:16 +0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1354 | TEST(ValidateRegulatorsConfigTest, I2CWriteBit) |
| 1355 | { |
| 1356 | json i2cWriteBitFile = validConfigFile; |
| 1357 | i2cWriteBitFile["rules"][0]["actions"][1]["i2c_write_bit"]["register"] = |
| 1358 | "0xA0"; |
| 1359 | i2cWriteBitFile["rules"][0]["actions"][1]["i2c_write_bit"]["position"] = 3; |
| 1360 | i2cWriteBitFile["rules"][0]["actions"][1]["i2c_write_bit"]["value"] = 1; |
| 1361 | // Valid: test rule actions i2c_write_bit. |
| 1362 | { |
| 1363 | json configFile = i2cWriteBitFile; |
| 1364 | EXPECT_JSON_VALID(configFile); |
| 1365 | } |
| 1366 | // Invalid: test i2c_write_bit with no register. |
| 1367 | { |
| 1368 | json configFile = i2cWriteBitFile; |
| 1369 | configFile["rules"][0]["actions"][1]["i2c_write_bit"].erase("register"); |
| 1370 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1371 | "'register' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1372 | } |
| 1373 | // Invalid: test i2c_write_bit with no position. |
| 1374 | { |
| 1375 | json configFile = i2cWriteBitFile; |
| 1376 | configFile["rules"][0]["actions"][1]["i2c_write_bit"].erase("position"); |
| 1377 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1378 | "'position' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1379 | } |
| 1380 | // Invalid: test i2c_write_bit with no value. |
| 1381 | { |
| 1382 | json configFile = i2cWriteBitFile; |
| 1383 | configFile["rules"][0]["actions"][1]["i2c_write_bit"].erase("value"); |
| 1384 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1385 | "'value' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1386 | } |
| 1387 | // Invalid: test i2c_write_bit with register wrong type. |
| 1388 | { |
| 1389 | json configFile = i2cWriteBitFile; |
| 1390 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["register"] = 1; |
| 1391 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1392 | "1 is not of type 'string'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1393 | } |
| 1394 | // Invalid: test i2c_write_bit with register wrong format. |
| 1395 | { |
| 1396 | json configFile = i2cWriteBitFile; |
| 1397 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["register"] = |
| 1398 | "0xA00"; |
| 1399 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1400 | "'0xA00' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1401 | } |
| 1402 | // Invalid: test i2c_write_bit with position wrong type. |
| 1403 | { |
| 1404 | json configFile = i2cWriteBitFile; |
| 1405 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["position"] = 3.1; |
| 1406 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1407 | "3.1 is not of type 'integer'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1408 | } |
| 1409 | // Invalid: test i2c_write_bit with position greater than 7. |
| 1410 | { |
| 1411 | json configFile = i2cWriteBitFile; |
| 1412 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["position"] = 8; |
| 1413 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1414 | "8 is greater than the maximum of 7"); |
| 1415 | } |
| 1416 | // Invalid: test i2c_write_bit with position less than 0. |
| 1417 | { |
| 1418 | json configFile = i2cWriteBitFile; |
| 1419 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["position"] = -1; |
| 1420 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1421 | "-1 is less than the minimum of 0"); |
| 1422 | } |
| 1423 | // Invalid: test i2c_write_bit with value wrong type. |
| 1424 | { |
| 1425 | json configFile = i2cWriteBitFile; |
| 1426 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["value"] = "1"; |
| 1427 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1428 | "'1' is not of type 'integer'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1429 | } |
| 1430 | // Invalid: test i2c_write_bit with value greater than 1. |
| 1431 | { |
| 1432 | json configFile = i2cWriteBitFile; |
| 1433 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["value"] = 2; |
| 1434 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1435 | "2 is greater than the maximum of 1"); |
| 1436 | } |
| 1437 | // Invalid: test i2c_write_bit with value less than 0. |
| 1438 | { |
| 1439 | json configFile = i2cWriteBitFile; |
| 1440 | configFile["rules"][0]["actions"][1]["i2c_write_bit"]["value"] = -1; |
| 1441 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1442 | "-1 is less than the minimum of 0"); |
| 1443 | } |
| 1444 | } |
| 1445 | TEST(ValidateRegulatorsConfigTest, I2CWriteByte) |
| 1446 | { |
| 1447 | json i2cWriteByteFile = validConfigFile; |
| 1448 | i2cWriteByteFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = |
| 1449 | "0x82"; |
| 1450 | i2cWriteByteFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = |
| 1451 | "0x40"; |
| 1452 | i2cWriteByteFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = |
| 1453 | "0x7F"; |
| 1454 | // Valid: test i2c_write_byte with all properties. |
| 1455 | { |
| 1456 | json configFile = i2cWriteByteFile; |
| 1457 | EXPECT_JSON_VALID(configFile); |
| 1458 | } |
| 1459 | // Valid: test i2c_write_byte with all required properties. |
| 1460 | { |
| 1461 | json configFile = i2cWriteByteFile; |
| 1462 | configFile["rules"][0]["actions"][1]["i2c_write_byte"].erase("mask"); |
| 1463 | EXPECT_JSON_VALID(configFile); |
| 1464 | } |
| 1465 | // Invalid: test i2c_write_byte with no register. |
| 1466 | { |
| 1467 | json configFile = i2cWriteByteFile; |
| 1468 | configFile["rules"][0]["actions"][1]["i2c_write_byte"].erase( |
| 1469 | "register"); |
| 1470 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1471 | "'register' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1472 | } |
| 1473 | // Invalid: test i2c_write_byte with no value. |
| 1474 | { |
| 1475 | json configFile = i2cWriteByteFile; |
| 1476 | configFile["rules"][0]["actions"][1]["i2c_write_byte"].erase("value"); |
| 1477 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1478 | "'value' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1479 | } |
| 1480 | // Invalid: test i2c_write_byte with property register wrong type. |
| 1481 | { |
| 1482 | json configFile = i2cWriteByteFile; |
| 1483 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = 1; |
| 1484 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1485 | "1 is not of type 'string'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1486 | } |
| 1487 | // Invalid: test i2c_write_byte with property value wrong type. |
| 1488 | { |
| 1489 | json configFile = i2cWriteByteFile; |
| 1490 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = 1; |
| 1491 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1492 | "1 is not of type 'string'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1493 | } |
| 1494 | // Invalid: test i2c_write_byte with property mask wrong type. |
| 1495 | { |
| 1496 | json configFile = i2cWriteByteFile; |
| 1497 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = 1; |
| 1498 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1499 | "1 is not of type 'string'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1500 | } |
| 1501 | // Invalid: test i2c_write_byte with property register more than 2 hex |
| 1502 | // digits. |
| 1503 | { |
| 1504 | json configFile = i2cWriteByteFile; |
| 1505 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = |
| 1506 | "0x820"; |
| 1507 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1508 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1509 | } |
| 1510 | // Invalid: test i2c_write_byte with property value more than 2 hex |
| 1511 | // digits. |
| 1512 | { |
| 1513 | json configFile = i2cWriteByteFile; |
| 1514 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = |
| 1515 | "0x820"; |
| 1516 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1517 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1518 | } |
| 1519 | // Invalid: test i2c_write_byte with property mask more than 2 hex digits. |
| 1520 | { |
| 1521 | json configFile = i2cWriteByteFile; |
| 1522 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = |
| 1523 | "0x820"; |
| 1524 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1525 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1526 | } |
| 1527 | // Invalid: test i2c_write_byte with property register less than 2 hex |
| 1528 | // digits. |
| 1529 | { |
| 1530 | json configFile = i2cWriteByteFile; |
| 1531 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = |
| 1532 | "0x8"; |
| 1533 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1534 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1535 | } |
| 1536 | // Invalid: test i2c_write_byte with property value less than 2 hex |
| 1537 | // digits. |
| 1538 | { |
| 1539 | json configFile = i2cWriteByteFile; |
| 1540 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = "0x8"; |
| 1541 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1542 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1543 | } |
| 1544 | // Invalid: test i2c_write_byte with property mask less than 2 hex digits. |
| 1545 | { |
| 1546 | json configFile = i2cWriteByteFile; |
| 1547 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = "0x8"; |
| 1548 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1549 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1550 | } |
| 1551 | // Invalid: test i2c_write_byte with property register no leading prefix. |
| 1552 | { |
| 1553 | json configFile = i2cWriteByteFile; |
| 1554 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = |
| 1555 | "82"; |
| 1556 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1557 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1558 | } |
| 1559 | // Invalid: test i2c_write_byte with property value no leading prefix. |
| 1560 | { |
| 1561 | json configFile = i2cWriteByteFile; |
| 1562 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = "82"; |
| 1563 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1564 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1565 | } |
| 1566 | // Invalid: test i2c_write_byte with property mask no leading prefix. |
| 1567 | { |
| 1568 | json configFile = i2cWriteByteFile; |
| 1569 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = "82"; |
| 1570 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1571 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1572 | } |
| 1573 | // Invalid: test i2c_write_byte with property register invalid hex digit. |
| 1574 | { |
| 1575 | json configFile = i2cWriteByteFile; |
| 1576 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["register"] = |
| 1577 | "0xG1"; |
| 1578 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1579 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1580 | } |
| 1581 | // Invalid: test i2c_write_byte with property value invalid hex digit. |
| 1582 | { |
| 1583 | json configFile = i2cWriteByteFile; |
| 1584 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["value"] = |
| 1585 | "0xG1"; |
| 1586 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1587 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1588 | } |
| 1589 | // Invalid: test i2c_write_byte with property mask invalid hex digit. |
| 1590 | { |
| 1591 | json configFile = i2cWriteByteFile; |
| 1592 | configFile["rules"][0]["actions"][1]["i2c_write_byte"]["mask"] = "0xG1"; |
| 1593 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1594 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | TEST(ValidateRegulatorsConfigTest, I2CWriteBytes) |
| 1598 | { |
| 1599 | json i2cWriteBytesFile = validConfigFile; |
| 1600 | i2cWriteBytesFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 1601 | "0x82"; |
| 1602 | i2cWriteBytesFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"] = { |
| 1603 | "0x02", "0x73"}; |
| 1604 | i2cWriteBytesFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"] = { |
| 1605 | "0x7F", "0x7F"}; |
| 1606 | // Valid: test i2c_write_bytes. |
| 1607 | { |
| 1608 | json configFile = i2cWriteBytesFile; |
| 1609 | EXPECT_JSON_VALID(configFile); |
| 1610 | } |
| 1611 | // Valid: test i2c_write_bytes with all required properties. |
| 1612 | { |
| 1613 | json configFile = i2cWriteBytesFile; |
| 1614 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"].erase("masks"); |
| 1615 | EXPECT_JSON_VALID(configFile); |
| 1616 | } |
| 1617 | // Invalid: test i2c_write_bytes with no register. |
| 1618 | { |
| 1619 | json configFile = i2cWriteBytesFile; |
| 1620 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"].erase( |
| 1621 | "register"); |
| 1622 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1623 | "'register' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1624 | } |
| 1625 | // Invalid: test i2c_write_bytes with no values. |
| 1626 | { |
| 1627 | json configFile = i2cWriteBytesFile; |
| 1628 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"].erase("values"); |
| 1629 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1630 | "'values' is a required property"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1631 | } |
| 1632 | // Invalid: test i2c_write_bytes with property values as empty array. |
| 1633 | { |
| 1634 | json configFile = i2cWriteBytesFile; |
| 1635 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"] = |
| 1636 | json::array(); |
| 1637 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1638 | "[] is too short"); |
| 1639 | } |
| 1640 | // Invalid: test i2c_write_bytes with property masks as empty array. |
| 1641 | { |
| 1642 | json configFile = i2cWriteBytesFile; |
| 1643 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"] = |
| 1644 | json::array(); |
| 1645 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1646 | "[] is too short"); |
| 1647 | } |
| 1648 | // Invalid: test i2c_write_bytes with property register wrong type. |
| 1649 | { |
| 1650 | json configFile = i2cWriteBytesFile; |
| 1651 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = 1; |
| 1652 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1653 | "1 is not of type 'string'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1654 | } |
| 1655 | // Invalid: test i2c_write_bytes with property values wrong type. |
| 1656 | { |
| 1657 | json configFile = i2cWriteBytesFile; |
| 1658 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"] = 1; |
| 1659 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1660 | "1 is not of type 'array'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1661 | } |
| 1662 | // Invalid: test i2c_write_bytes with property masks wrong type. |
| 1663 | { |
| 1664 | json configFile = i2cWriteBytesFile; |
| 1665 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"] = 1; |
| 1666 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1667 | "1 is not of type 'array'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1668 | } |
| 1669 | // Invalid: test i2c_write_bytes with property register more than 2 hex |
| 1670 | // digits. |
| 1671 | { |
| 1672 | json configFile = i2cWriteBytesFile; |
| 1673 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 1674 | "0x820"; |
| 1675 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1676 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1677 | } |
| 1678 | // Invalid: test i2c_write_bytes with property values more than 2 hex |
| 1679 | // digits. |
| 1680 | { |
| 1681 | json configFile = i2cWriteBytesFile; |
| 1682 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"][0] = |
| 1683 | "0x820"; |
| 1684 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1685 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1686 | } |
| 1687 | // Invalid: test i2c_write_bytes with property masks more than 2 hex |
| 1688 | // digits. |
| 1689 | { |
| 1690 | json configFile = i2cWriteBytesFile; |
| 1691 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"][0] = |
| 1692 | "0x820"; |
| 1693 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1694 | "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1695 | } |
| 1696 | // Invalid: test i2c_write_bytes with property register less than 2 hex |
| 1697 | // digits. |
| 1698 | { |
| 1699 | json configFile = i2cWriteBytesFile; |
| 1700 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 1701 | "0x8"; |
| 1702 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1703 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1704 | } |
| 1705 | // Invalid: test i2c_write_bytes with property values less than 2 hex |
| 1706 | // digits. |
| 1707 | { |
| 1708 | json configFile = i2cWriteBytesFile; |
| 1709 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"][0] = |
| 1710 | "0x8"; |
| 1711 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1712 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1713 | } |
| 1714 | // Invalid: test i2c_write_bytes with property masks less than 2 hex |
| 1715 | // digits. |
| 1716 | { |
| 1717 | json configFile = i2cWriteBytesFile; |
| 1718 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"][0] = |
| 1719 | "0x8"; |
| 1720 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1721 | "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1722 | } |
| 1723 | // Invalid: test i2c_write_bytes with property register no leading prefix. |
| 1724 | { |
| 1725 | json configFile = i2cWriteBytesFile; |
| 1726 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 1727 | "82"; |
| 1728 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1729 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1730 | } |
| 1731 | // Invalid: test i2c_write_bytes with property values no leading prefix. |
| 1732 | { |
| 1733 | json configFile = i2cWriteBytesFile; |
| 1734 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"][0] = |
| 1735 | "82"; |
| 1736 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1737 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1738 | } |
| 1739 | // Invalid: test i2c_write_bytes with property masks no leading prefix. |
| 1740 | { |
| 1741 | json configFile = i2cWriteBytesFile; |
| 1742 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"][0] = |
| 1743 | "82"; |
| 1744 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1745 | "'82' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1746 | } |
| 1747 | // Invalid: test i2c_write_bytes with property register invalid hex digit. |
| 1748 | { |
| 1749 | json configFile = i2cWriteBytesFile; |
| 1750 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 1751 | "0xG1"; |
| 1752 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1753 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1754 | } |
| 1755 | // Invalid: test i2c_write_bytes with property values invalid hex digit. |
| 1756 | { |
| 1757 | json configFile = i2cWriteBytesFile; |
| 1758 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"][0] = |
| 1759 | "0xG1"; |
| 1760 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1761 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1762 | } |
| 1763 | // Invalid: test i2c_write_bytes with property masks invalid hex digit. |
| 1764 | { |
| 1765 | json configFile = i2cWriteBytesFile; |
| 1766 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"][0] = |
| 1767 | "0xG1"; |
| 1768 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1769 | "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'"); |
Bob King | 188db7d | 2020-01-31 13:01:01 +0800 | [diff] [blame] | 1770 | } |
| 1771 | } |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1772 | TEST(ValidateRegulatorsConfigTest, If) |
| 1773 | { |
| 1774 | json ifFile = validConfigFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1775 | ifFile["rules"][2]["actions"][0]["if"]["condition"]["run_rule"] = |
| 1776 | "set_voltage_rule"; |
| 1777 | ifFile["rules"][2]["actions"][0]["if"]["then"][0]["run_rule"] = |
| 1778 | "read_sensors_rule"; |
| 1779 | ifFile["rules"][2]["actions"][0]["if"]["else"][0]["run_rule"] = |
| 1780 | "read_sensors_rule"; |
| 1781 | ifFile["rules"][2]["id"] = "rule_if"; |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1782 | // Valid: test if. |
| 1783 | { |
| 1784 | json configFile = ifFile; |
| 1785 | EXPECT_JSON_VALID(configFile); |
| 1786 | } |
| 1787 | // Valid: test if with required properties. |
| 1788 | { |
| 1789 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1790 | configFile["rules"][2]["actions"][0]["if"].erase("else"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1791 | EXPECT_JSON_VALID(configFile); |
| 1792 | } |
| 1793 | // Invalid: test if with no property condition. |
| 1794 | { |
| 1795 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1796 | configFile["rules"][2]["actions"][0]["if"].erase("condition"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1797 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1798 | "'condition' is a required property"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1799 | } |
| 1800 | // Invalid: test if with no property then. |
| 1801 | { |
| 1802 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1803 | configFile["rules"][2]["actions"][0]["if"].erase("then"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1804 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1805 | "'then' is a required property"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1806 | } |
| 1807 | // Invalid: test if with property then empty array. |
| 1808 | { |
| 1809 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1810 | configFile["rules"][2]["actions"][0]["if"]["then"] = json::array(); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1811 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1812 | "[] is too short"); |
| 1813 | } |
| 1814 | // Invalid: test if with property else empty array. |
| 1815 | { |
| 1816 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1817 | configFile["rules"][2]["actions"][0]["if"]["else"] = json::array(); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1818 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1819 | "[] is too short"); |
| 1820 | } |
| 1821 | // Invalid: test if with property condition wrong type. |
| 1822 | { |
| 1823 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1824 | configFile["rules"][2]["actions"][0]["if"]["condition"] = 1; |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1825 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1826 | "1 is not of type 'object'"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1827 | } |
| 1828 | // Invalid: test if with property then wrong type. |
| 1829 | { |
| 1830 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1831 | configFile["rules"][2]["actions"][0]["if"]["then"] = 1; |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1832 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1833 | "1 is not of type 'array'"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1834 | } |
| 1835 | // Invalid: test if with property else wrong type. |
| 1836 | { |
| 1837 | json configFile = ifFile; |
Bob King | 2d27dcf | 2020-02-11 15:00:50 +0800 | [diff] [blame] | 1838 | configFile["rules"][2]["actions"][0]["if"]["else"] = 1; |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1839 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1840 | "1 is not of type 'array'"); |
Bob King | ead0b05 | 2020-01-21 11:29:03 +0800 | [diff] [blame] | 1841 | } |
| 1842 | } |
Bob King | bfe9fe7 | 2020-01-21 11:29:57 +0800 | [diff] [blame] | 1843 | TEST(ValidateRegulatorsConfigTest, Not) |
| 1844 | { |
| 1845 | json notFile = validConfigFile; |
| 1846 | notFile["rules"][0]["actions"][1]["not"]["i2c_compare_byte"]["register"] = |
| 1847 | "0xA0"; |
| 1848 | notFile["rules"][0]["actions"][1]["not"]["i2c_compare_byte"]["value"] = |
| 1849 | "0xFF"; |
| 1850 | // Valid: test not. |
| 1851 | { |
| 1852 | json configFile = notFile; |
| 1853 | EXPECT_JSON_VALID(configFile); |
| 1854 | } |
| 1855 | // Invalid: test not with wrong type. |
| 1856 | { |
| 1857 | json configFile = notFile; |
| 1858 | configFile["rules"][0]["actions"][1]["not"] = 1; |
| 1859 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1860 | "1 is not of type 'object'"); |
Bob King | bfe9fe7 | 2020-01-21 11:29:57 +0800 | [diff] [blame] | 1861 | } |
| 1862 | } |
Bob King | cfc29d0 | 2020-01-21 11:30:50 +0800 | [diff] [blame] | 1863 | TEST(ValidateRegulatorsConfigTest, Or) |
| 1864 | { |
| 1865 | json orFile = validConfigFile; |
| 1866 | orFile["rules"][0]["actions"][1]["or"][0]["i2c_compare_byte"]["register"] = |
| 1867 | "0xA0"; |
| 1868 | orFile["rules"][0]["actions"][1]["or"][0]["i2c_compare_byte"]["value"] = |
| 1869 | "0x00"; |
| 1870 | orFile["rules"][0]["actions"][1]["or"][1]["i2c_compare_byte"]["register"] = |
| 1871 | "0xA1"; |
| 1872 | orFile["rules"][0]["actions"][1]["or"][1]["i2c_compare_byte"]["value"] = |
| 1873 | "0x00"; |
| 1874 | // Valid: test or. |
| 1875 | { |
| 1876 | json configFile = orFile; |
| 1877 | EXPECT_JSON_VALID(configFile); |
| 1878 | } |
| 1879 | // Invalid: test or with empty array. |
| 1880 | { |
| 1881 | json configFile = orFile; |
| 1882 | configFile["rules"][0]["actions"][1]["or"] = json::array(); |
| 1883 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1884 | "[] is too short"); |
| 1885 | } |
| 1886 | // Invalid: test or with wrong type. |
| 1887 | { |
| 1888 | json configFile = orFile; |
| 1889 | configFile["rules"][0]["actions"][1]["or"] = 1; |
| 1890 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1891 | "1 is not of type 'array'"); |
Bob King | cfc29d0 | 2020-01-21 11:30:50 +0800 | [diff] [blame] | 1892 | } |
| 1893 | } |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1894 | TEST(ValidateRegulatorsConfigTest, PmbusReadSensor) |
| 1895 | { |
| 1896 | json pmbusReadSensorFile = validConfigFile; |
| 1897 | pmbusReadSensorFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["type"] = |
| 1898 | "vout"; |
| 1899 | pmbusReadSensorFile["rules"][0]["actions"][1]["pmbus_read_sensor"] |
| 1900 | ["command"] = "0x8B"; |
| 1901 | pmbusReadSensorFile["rules"][0]["actions"][1]["pmbus_read_sensor"] |
| 1902 | ["format"] = "linear_16"; |
| 1903 | pmbusReadSensorFile["rules"][0]["actions"][1]["pmbus_read_sensor"] |
| 1904 | ["exponent"] = -8; |
| 1905 | // Valid: test pmbus_read_sensor. |
| 1906 | { |
| 1907 | json configFile = pmbusReadSensorFile; |
| 1908 | EXPECT_JSON_VALID(configFile); |
| 1909 | } |
| 1910 | // Valid: test pmbus_read_sensor with required properties. |
| 1911 | { |
| 1912 | json configFile = pmbusReadSensorFile; |
| 1913 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"].erase( |
| 1914 | "exponent"); |
| 1915 | EXPECT_JSON_VALID(configFile); |
| 1916 | } |
| 1917 | // Invalid: test pmbus_read_sensor with no type. |
| 1918 | { |
| 1919 | json configFile = pmbusReadSensorFile; |
| 1920 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"].erase("type"); |
| 1921 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1922 | "'type' is a required property"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1923 | } |
| 1924 | // Invalid: test pmbus_read_sensor with no command. |
| 1925 | { |
| 1926 | json configFile = pmbusReadSensorFile; |
| 1927 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"].erase( |
| 1928 | "command"); |
| 1929 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1930 | "'command' is a required property"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1931 | } |
| 1932 | // Invalid: test pmbus_read_sensor with no format. |
| 1933 | { |
| 1934 | json configFile = pmbusReadSensorFile; |
| 1935 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"].erase( |
| 1936 | "format"); |
| 1937 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1938 | "'format' is a required property"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1939 | } |
| 1940 | // Invalid: test pmbus_read_sensor with property type wrong type. |
| 1941 | { |
| 1942 | json configFile = pmbusReadSensorFile; |
| 1943 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["type"] = |
| 1944 | true; |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1945 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1946 | "True is not of type 'string'"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1947 | } |
| 1948 | // Invalid: test pmbus_read_sensor with property command wrong type. |
| 1949 | { |
| 1950 | json configFile = pmbusReadSensorFile; |
| 1951 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["command"] = |
| 1952 | true; |
| 1953 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1954 | "True is not of type 'string'"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1955 | } |
| 1956 | // Invalid: test pmbus_read_sensor with property format wrong type. |
| 1957 | { |
| 1958 | json configFile = pmbusReadSensorFile; |
| 1959 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["format"] = |
| 1960 | true; |
| 1961 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1962 | "True is not of type 'string'"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1963 | } |
| 1964 | // Invalid: test pmbus_read_sensor with property exponent wrong type. |
| 1965 | { |
| 1966 | json configFile = pmbusReadSensorFile; |
| 1967 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["exponent"] = |
| 1968 | true; |
| 1969 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1970 | "True is not of type 'integer'"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1971 | } |
| 1972 | // Invalid: test pmbus_read_sensor with property type wrong format. |
| 1973 | { |
| 1974 | json configFile = pmbusReadSensorFile; |
| 1975 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["type"] = |
| 1976 | "foo"; |
| 1977 | EXPECT_JSON_INVALID( |
| 1978 | configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1979 | "'foo' is not one of ['iout', 'iout_peak', 'iout_valley', " |
| 1980 | "'pout', 'temperature', 'temperature_peak', 'vout', " |
| 1981 | "'vout_peak', 'vout_valley']"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1982 | } |
| 1983 | // Invalid: test pmbus_read_sensor with property command wrong format. |
| 1984 | { |
| 1985 | json configFile = pmbusReadSensorFile; |
| 1986 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["command"] = |
| 1987 | "0x8B0"; |
| 1988 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1989 | "'0x8B0' does not match '^0x[0-9a-fA-F]{2}$'"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1990 | } |
| 1991 | // Invalid: test pmbus_read_sensor with property format wrong format. |
| 1992 | { |
| 1993 | json configFile = pmbusReadSensorFile; |
| 1994 | configFile["rules"][0]["actions"][1]["pmbus_read_sensor"]["format"] = |
| 1995 | "foo"; |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 1996 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 1997 | "'foo' is not one of ['linear_11', 'linear_16']"); |
Bob King | d661809 | 2020-01-21 11:31:46 +0800 | [diff] [blame] | 1998 | } |
| 1999 | } |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2000 | TEST(ValidateRegulatorsConfigTest, PmbusWriteVoutCommand) |
| 2001 | { |
| 2002 | json pmbusWriteVoutCommandFile = validConfigFile; |
| 2003 | pmbusWriteVoutCommandFile["rules"][0]["actions"][1] |
| 2004 | ["pmbus_write_vout_command"]["volts"] = 1.03; |
| 2005 | pmbusWriteVoutCommandFile["rules"][0]["actions"][1] |
| 2006 | ["pmbus_write_vout_command"]["format"] = "linear"; |
| 2007 | pmbusWriteVoutCommandFile["rules"][0]["actions"][1] |
| 2008 | ["pmbus_write_vout_command"]["exponent"] = -8; |
| 2009 | pmbusWriteVoutCommandFile["rules"][0]["actions"][1] |
| 2010 | ["pmbus_write_vout_command"]["is_verified"] = true; |
| 2011 | // Valid: test pmbus_write_vout_command. |
| 2012 | { |
| 2013 | json configFile = pmbusWriteVoutCommandFile; |
| 2014 | EXPECT_JSON_VALID(configFile); |
| 2015 | } |
| 2016 | // Valid: test pmbus_write_vout_command with required properties. |
| 2017 | { |
| 2018 | json configFile = pmbusWriteVoutCommandFile; |
| 2019 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"].erase( |
| 2020 | "volts"); |
| 2021 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"].erase( |
| 2022 | "exponent"); |
| 2023 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"].erase( |
| 2024 | "is_verified"); |
| 2025 | EXPECT_JSON_VALID(configFile); |
| 2026 | } |
| 2027 | // Invalid: test pmbus_write_vout_command with no format. |
| 2028 | { |
| 2029 | json configFile = pmbusWriteVoutCommandFile; |
| 2030 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"].erase( |
| 2031 | "format"); |
| 2032 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2033 | "'format' is a required property"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2034 | } |
| 2035 | // Invalid: test pmbus_write_vout_command with property volts wrong type. |
| 2036 | { |
| 2037 | json configFile = pmbusWriteVoutCommandFile; |
| 2038 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"] |
| 2039 | ["volts"] = true; |
| 2040 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2041 | "True is not of type 'number'"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2042 | } |
| 2043 | // Invalid: test pmbus_write_vout_command with property format wrong type. |
| 2044 | { |
| 2045 | json configFile = pmbusWriteVoutCommandFile; |
| 2046 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"] |
| 2047 | ["format"] = true; |
| 2048 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2049 | "True is not of type 'string'"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2050 | } |
| 2051 | // Invalid: test pmbus_write_vout_command with property exponent wrong type. |
| 2052 | { |
| 2053 | json configFile = pmbusWriteVoutCommandFile; |
| 2054 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"] |
| 2055 | ["exponent"] = 1.3; |
| 2056 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2057 | "1.3 is not of type 'integer'"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2058 | } |
| 2059 | // Invalid: test pmbus_write_vout_command with property is_verified wrong |
| 2060 | // type. |
| 2061 | { |
| 2062 | json configFile = pmbusWriteVoutCommandFile; |
| 2063 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"] |
| 2064 | ["is_verified"] = 1; |
| 2065 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2066 | "1 is not of type 'boolean'"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2067 | } |
| 2068 | // Invalid: test pmbus_write_vout_command with property format wrong format. |
| 2069 | { |
| 2070 | json configFile = pmbusWriteVoutCommandFile; |
| 2071 | configFile["rules"][0]["actions"][1]["pmbus_write_vout_command"] |
| 2072 | ["format"] = "foo"; |
| 2073 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2074 | "'foo' is not one of ['linear']"); |
Bob King | 02179c6 | 2020-01-21 11:32:36 +0800 | [diff] [blame] | 2075 | } |
| 2076 | } |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2077 | TEST(ValidateRegulatorsConfigTest, PresenceDetection) |
| 2078 | { |
| 2079 | json presenceDetectionFile = validConfigFile; |
| 2080 | presenceDetectionFile |
| 2081 | ["chassis"][0]["devices"][0]["presence_detection"]["comments"][0] = |
| 2082 | "Regulator is only present on the FooBar backplane"; |
| 2083 | presenceDetectionFile["chassis"][0]["devices"][0]["presence_detection"] |
Bob King | f4ff116 | 2020-02-11 15:13:38 +0800 | [diff] [blame] | 2084 | ["rule_id"] = "set_voltage_rule"; |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2085 | // Valid: test presence_detection with only property rule_id. |
| 2086 | { |
| 2087 | json configFile = presenceDetectionFile; |
| 2088 | EXPECT_JSON_VALID(configFile); |
| 2089 | } |
| 2090 | // Valid: test presence_detection with only property actions. |
| 2091 | { |
| 2092 | json configFile = presenceDetectionFile; |
| 2093 | configFile["chassis"][0]["devices"][0]["presence_detection"].erase( |
| 2094 | "rule_id"); |
| 2095 | configFile["chassis"][0]["devices"][0]["presence_detection"]["actions"] |
| 2096 | [0]["compare_presence"]["fru"] = |
| 2097 | "/system/chassis/motherboard/cpu3"; |
| 2098 | configFile["chassis"][0]["devices"][0]["presence_detection"]["actions"] |
| 2099 | [0]["compare_presence"]["value"] = true; |
| 2100 | configFile["chassis"][0]["devices"][0]["presence_detection"].erase( |
| 2101 | "comments"); |
| 2102 | EXPECT_JSON_VALID(configFile); |
| 2103 | } |
| 2104 | // Invalid: test presence_detection with both property rule_id and actions. |
| 2105 | { |
| 2106 | json configFile = presenceDetectionFile; |
| 2107 | configFile["chassis"][0]["devices"][0]["presence_detection"]["actions"] |
| 2108 | [0]["compare_presence"]["fru"] = |
| 2109 | "/system/chassis/motherboard/cpu3"; |
| 2110 | configFile["chassis"][0]["devices"][0]["presence_detection"]["actions"] |
| 2111 | [0]["compare_presence"]["value"] = true; |
| 2112 | EXPECT_JSON_INVALID( |
| 2113 | configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2114 | "{'actions': [{'compare_presence': {'fru': " |
| 2115 | "'/system/chassis/motherboard/cpu3', 'value': True}}], 'comments': " |
| 2116 | "['Regulator is only present on the FooBar backplane'], 'rule_id': " |
| 2117 | "'set_voltage_rule'} is valid under each of {'required': " |
| 2118 | "['actions']}, {'required': ['rule_id']}"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2119 | } |
| 2120 | // Invalid: test presence_detection with no rule_id and actions. |
| 2121 | { |
| 2122 | json configFile = presenceDetectionFile; |
| 2123 | configFile["chassis"][0]["devices"][0]["presence_detection"].erase( |
| 2124 | "rule_id"); |
| 2125 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2126 | "'rule_id' is a required property"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2127 | } |
| 2128 | // Invalid: test presence_detection with property comments wrong type. |
| 2129 | { |
| 2130 | json configFile = presenceDetectionFile; |
| 2131 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2132 | ["comments"] = true; |
| 2133 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2134 | "True is not of type 'array'"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2135 | } |
| 2136 | // Invalid: test presence_detection with property rule_id wrong type. |
| 2137 | { |
| 2138 | json configFile = presenceDetectionFile; |
| 2139 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2140 | ["rule_id"] = true; |
| 2141 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2142 | "True is not of type 'string'"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2143 | } |
| 2144 | // Invalid: test presence_detection with property actions wrong type. |
| 2145 | { |
| 2146 | json configFile = presenceDetectionFile; |
| 2147 | configFile["chassis"][0]["devices"][0]["presence_detection"].erase( |
| 2148 | "rule_id"); |
| 2149 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2150 | ["actions"] = true; |
| 2151 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2152 | "True is not of type 'array'"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2153 | } |
| 2154 | // Invalid: test presence_detection with property rule_id wrong format. |
| 2155 | { |
| 2156 | json configFile = presenceDetectionFile; |
| 2157 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2158 | ["rule_id"] = "id@"; |
| 2159 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2160 | "'id@' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | 99d8fa1 | 2020-01-31 11:23:40 +0800 | [diff] [blame] | 2161 | } |
| 2162 | // Invalid: test presence_detection with property comments empty array. |
| 2163 | { |
| 2164 | json configFile = presenceDetectionFile; |
| 2165 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2166 | ["comments"] = json::array(); |
| 2167 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2168 | "[] is too short"); |
| 2169 | } |
| 2170 | // Invalid: test presence_detection with property actions empty array. |
| 2171 | { |
| 2172 | json configFile = presenceDetectionFile; |
| 2173 | configFile["chassis"][0]["devices"][0]["presence_detection"].erase( |
| 2174 | "rule_id"); |
| 2175 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2176 | ["actions"] = json::array(); |
| 2177 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2178 | "[] is too short"); |
| 2179 | } |
| 2180 | } |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2181 | TEST(ValidateRegulatorsConfigTest, Rail) |
| 2182 | { |
| 2183 | // Valid: test rail. |
| 2184 | { |
| 2185 | json configFile = validConfigFile; |
| 2186 | EXPECT_JSON_VALID(configFile); |
| 2187 | } |
| 2188 | // Valid: test rail with required properties. |
| 2189 | { |
| 2190 | json configFile = validConfigFile; |
| 2191 | configFile["chassis"][0]["devices"][0]["rails"][0].erase("comments"); |
| 2192 | configFile["chassis"][0]["devices"][0]["rails"][0].erase( |
| 2193 | "configuration"); |
| 2194 | configFile["chassis"][0]["devices"][0]["rails"][0].erase( |
| 2195 | "sensor_monitoring"); |
| 2196 | EXPECT_JSON_VALID(configFile); |
| 2197 | } |
| 2198 | // Invalid: test rail with no id. |
| 2199 | { |
| 2200 | json configFile = validConfigFile; |
| 2201 | configFile["chassis"][0]["devices"][0]["rails"][0].erase("id"); |
| 2202 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2203 | "'id' is a required property"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2204 | } |
| 2205 | // Invalid: test rail with comments wrong type. |
| 2206 | { |
| 2207 | json configFile = validConfigFile; |
| 2208 | configFile["chassis"][0]["devices"][0]["rails"][0]["comments"] = true; |
| 2209 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2210 | "True is not of type 'array'"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2211 | } |
| 2212 | // Invalid: test rail with id wrong type. |
| 2213 | { |
| 2214 | json configFile = validConfigFile; |
| 2215 | configFile["chassis"][0]["devices"][0]["rails"][0]["id"] = true; |
| 2216 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2217 | "True is not of type 'string'"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2218 | } |
| 2219 | // Invalid: test rail with configuration wrong type. |
| 2220 | { |
| 2221 | json configFile = validConfigFile; |
| 2222 | configFile["chassis"][0]["devices"][0]["rails"][0]["configuration"] = |
| 2223 | true; |
| 2224 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2225 | "True is not of type 'object'"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2226 | } |
| 2227 | // Invalid: test rail with sensor_monitoring wrong type. |
| 2228 | { |
| 2229 | json configFile = validConfigFile; |
| 2230 | configFile["chassis"][0]["devices"][0]["rails"][0] |
| 2231 | ["sensor_monitoring"] = true; |
| 2232 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2233 | "True is not of type 'object'"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2234 | } |
| 2235 | // Invalid: test rail with comments empty array. |
| 2236 | { |
| 2237 | json configFile = validConfigFile; |
| 2238 | configFile["chassis"][0]["devices"][0]["rails"][0]["comments"] = |
| 2239 | json::array(); |
| 2240 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2241 | "[] is too short"); |
| 2242 | } |
| 2243 | // Invalid: test rail with id wrong format. |
| 2244 | { |
| 2245 | json configFile = validConfigFile; |
| 2246 | configFile["chassis"][0]["devices"][0]["rails"][0]["id"] = "id~"; |
| 2247 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2248 | "'id~' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | e9260b5 | 2020-01-21 11:46:13 +0800 | [diff] [blame] | 2249 | } |
| 2250 | } |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2251 | TEST(ValidateRegulatorsConfigTest, Rule) |
| 2252 | { |
| 2253 | // valid test comments property, id property, |
| 2254 | // action property specified. |
| 2255 | { |
| 2256 | json configFile = validConfigFile; |
| 2257 | EXPECT_JSON_VALID(configFile); |
| 2258 | } |
| 2259 | |
| 2260 | // valid test rule with no comments |
| 2261 | { |
| 2262 | json configFile = validConfigFile; |
| 2263 | configFile["rules"][0].erase("comments"); |
| 2264 | EXPECT_JSON_VALID(configFile); |
| 2265 | } |
| 2266 | |
| 2267 | // invalid test comments property has invalid value type |
| 2268 | { |
| 2269 | json configFile = validConfigFile; |
| 2270 | configFile["rules"][0]["comments"] = {1}; |
| 2271 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2272 | "1 is not of type 'string'"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | // invalid test rule with no ID |
| 2276 | { |
| 2277 | json configFile = validConfigFile; |
| 2278 | configFile["rules"][0].erase("id"); |
| 2279 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2280 | "'id' is a required property"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | // invalid test id property has invalid value type (not string) |
| 2284 | { |
| 2285 | json configFile = validConfigFile; |
| 2286 | configFile["rules"][0]["id"] = true; |
| 2287 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2288 | "True is not of type 'string'"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | // invalid test id property has invalid value |
| 2292 | { |
| 2293 | json configFile = validConfigFile; |
| 2294 | configFile["rules"][0]["id"] = "foo%"; |
| 2295 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2296 | "'foo%' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | // invalid test rule with no actions property |
| 2300 | { |
| 2301 | json configFile = validConfigFile; |
| 2302 | configFile["rules"][0].erase("actions"); |
| 2303 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2304 | "'actions' is a required property"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | // valid test rule with multiple actions |
| 2308 | { |
| 2309 | json configFile = validConfigFile; |
Bob King | 63d795f | 2020-02-11 15:22:09 +0800 | [diff] [blame] | 2310 | configFile["rules"][0]["actions"][1]["run_rule"] = "read_sensors_rule"; |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2311 | EXPECT_JSON_VALID(configFile); |
| 2312 | } |
| 2313 | |
| 2314 | // invalid test actions property has invalid value type (not an array) |
| 2315 | { |
| 2316 | json configFile = validConfigFile; |
| 2317 | configFile["rules"][0]["actions"] = 1; |
| 2318 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2319 | "1 is not of type 'array'"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | // invalid test actions property has invalid value of action |
| 2323 | { |
| 2324 | json configFile = validConfigFile; |
| 2325 | configFile["rules"][0]["actions"][0] = "foo"; |
| 2326 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2327 | "'foo' is not of type 'object'"); |
Bob King | 64df7da | 2020-01-31 12:04:12 +0800 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | // invalid test actions property has empty array |
| 2331 | { |
| 2332 | json configFile = validConfigFile; |
| 2333 | configFile["rules"][0]["actions"] = json::array(); |
| 2334 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2335 | "[] is too short"); |
| 2336 | } |
| 2337 | } |
Bob King | e86c2e5 | 2020-01-21 11:33:32 +0800 | [diff] [blame] | 2338 | TEST(ValidateRegulatorsConfigTest, RunRule) |
| 2339 | { |
| 2340 | json runRuleFile = validConfigFile; |
Bob King | 7d3a9f1 | 2020-02-11 15:34:52 +0800 | [diff] [blame] | 2341 | runRuleFile["rules"][0]["actions"][1]["run_rule"] = "read_sensors_rule"; |
Bob King | e86c2e5 | 2020-01-21 11:33:32 +0800 | [diff] [blame] | 2342 | // Valid: test run_rule. |
| 2343 | { |
| 2344 | json configFile = runRuleFile; |
| 2345 | EXPECT_JSON_VALID(configFile); |
| 2346 | } |
| 2347 | // Invalid: test run_rule wrong type. |
| 2348 | { |
| 2349 | json configFile = runRuleFile; |
| 2350 | configFile["rules"][0]["actions"][1]["run_rule"] = true; |
| 2351 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2352 | "True is not of type 'string'"); |
Bob King | e86c2e5 | 2020-01-21 11:33:32 +0800 | [diff] [blame] | 2353 | } |
| 2354 | // Invalid: test run_rule wrong format. |
| 2355 | { |
| 2356 | json configFile = runRuleFile; |
| 2357 | configFile["rules"][0]["actions"][1]["run_rule"] = "set_voltage_rule%"; |
| 2358 | EXPECT_JSON_INVALID( |
| 2359 | configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2360 | "'set_voltage_rule%' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | e86c2e5 | 2020-01-21 11:33:32 +0800 | [diff] [blame] | 2361 | } |
| 2362 | } |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2363 | TEST(ValidateRegulatorsConfigTest, SensorMonitoring) |
| 2364 | { |
| 2365 | // Valid: test rails sensor_monitoring with only property rule id. |
| 2366 | { |
| 2367 | json configFile = validConfigFile; |
| 2368 | EXPECT_JSON_VALID(configFile); |
| 2369 | } |
| 2370 | // Valid: test rails sensor_monitoring with only property actions. |
| 2371 | { |
| 2372 | json configFile = validConfigFile; |
| 2373 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2374 | .erase("rule_id"); |
| 2375 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2376 | ["actions"][0]["compare_presence"]["fru"] = |
| 2377 | "/system/chassis/motherboard/cpu3"; |
| 2378 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2379 | ["actions"][0]["compare_presence"]["value"] = true; |
| 2380 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2381 | ["comments"][0] = "comments"; |
| 2382 | EXPECT_JSON_VALID(configFile); |
| 2383 | } |
| 2384 | // Invalid: test rails sensor_monitoring with both property rule_id and |
| 2385 | // actions. |
| 2386 | { |
| 2387 | json configFile = validConfigFile; |
| 2388 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2389 | ["actions"][0]["compare_presence"]["fru"] = |
| 2390 | "/system/chassis/motherboard/cpu3"; |
| 2391 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2392 | ["actions"][0]["compare_presence"]["value"] = true; |
| 2393 | EXPECT_JSON_INVALID( |
| 2394 | configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2395 | "{'actions': [{'compare_presence': {'fru': " |
| 2396 | "'/system/chassis/motherboard/cpu3', 'value': True}}], 'rule_id': " |
| 2397 | "'read_sensors_rule'} is valid under each of {'required': " |
| 2398 | "['actions']}, {'required': ['rule_id']}"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2399 | } |
| 2400 | // Invalid: test rails sensor_monitoring with no rule_id and actions. |
| 2401 | { |
| 2402 | json configFile = validConfigFile; |
| 2403 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2404 | .erase("rule_id"); |
| 2405 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2406 | "'rule_id' is a required property"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2407 | } |
| 2408 | // Invalid: test rails sensor_monitoring with property comments wrong type. |
| 2409 | { |
| 2410 | json configFile = validConfigFile; |
| 2411 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2412 | ["comments"] = true; |
| 2413 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2414 | "True is not of type 'array'"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2415 | } |
| 2416 | // Invalid: test rails sensor_monitoring with property rule_id wrong type. |
| 2417 | { |
| 2418 | json configFile = validConfigFile; |
| 2419 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2420 | ["rule_id"] = true; |
| 2421 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2422 | "True is not of type 'string'"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2423 | } |
| 2424 | // Invalid: test rails sensor_monitoring with property actions wrong type. |
| 2425 | { |
| 2426 | json configFile = validConfigFile; |
| 2427 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2428 | .erase("rule_id"); |
| 2429 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2430 | ["actions"] = true; |
| 2431 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2432 | "True is not of type 'array'"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2433 | } |
| 2434 | // Invalid: test rails sensor_monitoring with property rule_id wrong format. |
| 2435 | { |
| 2436 | json configFile = validConfigFile; |
| 2437 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2438 | ["rule_id"] = "id@"; |
| 2439 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2440 | "'id@' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | fcc2a2f | 2020-01-31 11:29:45 +0800 | [diff] [blame] | 2441 | } |
| 2442 | // Invalid: test rails sensor_monitoring with property comments empty array. |
| 2443 | { |
| 2444 | json configFile = validConfigFile; |
| 2445 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2446 | ["comments"] = json::array(); |
| 2447 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2448 | "[] is too short"); |
| 2449 | } |
| 2450 | // Invalid: test rails sensor_monitoring with property actions empty array. |
| 2451 | { |
| 2452 | json configFile = validConfigFile; |
| 2453 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2454 | .erase("rule_id"); |
| 2455 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2456 | ["actions"] = json::array(); |
| 2457 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2458 | "[] is too short"); |
| 2459 | } |
| 2460 | } |
Bob King | 68230aa | 2020-01-21 11:34:33 +0800 | [diff] [blame] | 2461 | TEST(ValidateRegulatorsConfigTest, SetDevice) |
| 2462 | { |
| 2463 | json setDeviceFile = validConfigFile; |
Bob King | 7d3a9f1 | 2020-02-11 15:34:52 +0800 | [diff] [blame] | 2464 | setDeviceFile["rules"][0]["actions"][1]["set_device"] = "vdd_regulator"; |
Bob King | 68230aa | 2020-01-21 11:34:33 +0800 | [diff] [blame] | 2465 | // Valid: test set_device. |
| 2466 | { |
| 2467 | json configFile = setDeviceFile; |
| 2468 | EXPECT_JSON_VALID(configFile); |
| 2469 | } |
| 2470 | // Invalid: test set_device wrong type. |
| 2471 | { |
| 2472 | json configFile = setDeviceFile; |
| 2473 | configFile["rules"][0]["actions"][1]["set_device"] = true; |
| 2474 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2475 | "True is not of type 'string'"); |
Bob King | 68230aa | 2020-01-21 11:34:33 +0800 | [diff] [blame] | 2476 | } |
| 2477 | // Invalid: test set_device wrong format. |
| 2478 | { |
| 2479 | json configFile = setDeviceFile; |
| 2480 | configFile["rules"][0]["actions"][1]["set_device"] = "io_expander2%"; |
Bob King | 358c417 | 2020-03-16 13:57:08 +0800 | [diff] [blame] | 2481 | EXPECT_JSON_INVALID(configFile, "Validation failed.", |
| 2482 | "'io_expander2%' does not match '^[A-Za-z0-9_]+$'"); |
Bob King | 68230aa | 2020-01-21 11:34:33 +0800 | [diff] [blame] | 2483 | } |
| 2484 | } |
Bob King | 3643cc0 | 2020-01-31 11:32:56 +0800 | [diff] [blame] | 2485 | TEST(ValidateRegulatorsConfigTest, DuplicateRuleID) |
| 2486 | { |
| 2487 | // Invalid: test duplicate ID in rule. |
| 2488 | { |
| 2489 | json configFile = validConfigFile; |
Bob King | b3e48bc | 2020-02-18 09:59:09 +0800 | [diff] [blame] | 2490 | configFile["rules"][2]["id"] = "set_voltage_rule"; |
| 2491 | configFile["rules"][2]["actions"][0]["pmbus_write_vout_command"] |
Bob King | 3643cc0 | 2020-01-31 11:32:56 +0800 | [diff] [blame] | 2492 | ["format"] = "linear"; |
| 2493 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate rule ID.", ""); |
| 2494 | } |
| 2495 | } |
| 2496 | TEST(ValidateRegulatorsConfigTest, DuplicateChassisNumber) |
| 2497 | { |
| 2498 | // Invalid: test duplicate number in chassis. |
| 2499 | { |
| 2500 | json configFile = validConfigFile; |
| 2501 | configFile["chassis"][1]["number"] = 1; |
| 2502 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate chassis number.", ""); |
| 2503 | } |
| 2504 | } |
| 2505 | TEST(ValidateRegulatorsConfigTest, DuplicateDeviceID) |
| 2506 | { |
| 2507 | // Invalid: test duplicate ID in device. |
| 2508 | { |
| 2509 | json configFile = validConfigFile; |
| 2510 | configFile["chassis"][0]["devices"][1]["id"] = "vdd_regulator"; |
| 2511 | configFile["chassis"][0]["devices"][1]["is_regulator"] = true; |
| 2512 | configFile["chassis"][0]["devices"][1]["fru"] = |
| 2513 | "/system/chassis/motherboard/regulator1"; |
| 2514 | configFile["chassis"][0]["devices"][1]["i2c_interface"]["bus"] = 2; |
| 2515 | configFile["chassis"][0]["devices"][1]["i2c_interface"]["address"] = |
| 2516 | "0x71"; |
| 2517 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate device ID.", ""); |
| 2518 | } |
| 2519 | } |
| 2520 | TEST(ValidateRegulatorsConfigTest, DuplicateRailID) |
| 2521 | { |
| 2522 | // Invalid: test duplicate ID in rail. |
| 2523 | { |
| 2524 | json configFile = validConfigFile; |
| 2525 | configFile["chassis"][0]["devices"][0]["rails"][1]["id"] = "vdd"; |
| 2526 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate rail ID.", ""); |
| 2527 | } |
| 2528 | } |
Bob King | 7879310 | 2020-03-13 13:16:09 +0800 | [diff] [blame] | 2529 | TEST(ValidateRegulatorsConfigTest, DuplicateObjectID) |
| 2530 | { |
| 2531 | // Invalid: test duplicate object ID in device and rail. |
| 2532 | { |
| 2533 | json configFile = validConfigFile; |
| 2534 | configFile["chassis"][0]["devices"][0]["rails"][1]["id"] = |
| 2535 | "vdd_regulator"; |
| 2536 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate ID.", ""); |
| 2537 | } |
| 2538 | // Invalid: test duplicate object ID in device and rule. |
| 2539 | { |
| 2540 | json configFile = validConfigFile; |
| 2541 | configFile["rules"][2]["id"] = "vdd_regulator"; |
| 2542 | configFile["rules"][2]["actions"][0]["pmbus_write_vout_command"] |
| 2543 | ["format"] = "linear"; |
| 2544 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate ID.", ""); |
| 2545 | } |
| 2546 | // Invalid: test duplicate object ID in rule and rail. |
| 2547 | { |
| 2548 | json configFile = validConfigFile; |
| 2549 | configFile["chassis"][0]["devices"][0]["rails"][1]["id"] = |
| 2550 | "set_voltage_rule"; |
| 2551 | EXPECT_JSON_INVALID(configFile, "Error: Duplicate ID.", ""); |
| 2552 | } |
| 2553 | } |
Bob King | 3643cc0 | 2020-01-31 11:32:56 +0800 | [diff] [blame] | 2554 | TEST(ValidateRegulatorsConfigTest, InfiniteLoops) |
| 2555 | { |
| 2556 | // Invalid: test run_rule with infinite loop (rules run each other). |
| 2557 | { |
| 2558 | json configFile = validConfigFile; |
| 2559 | configFile["rules"][2]["actions"][0]["run_rule"] = "set_voltage_rule2"; |
| 2560 | configFile["rules"][2]["id"] = "set_voltage_rule1"; |
| 2561 | configFile["rules"][3]["actions"][0]["run_rule"] = "set_voltage_rule1"; |
| 2562 | configFile["rules"][3]["id"] = "set_voltage_rule2"; |
| 2563 | EXPECT_JSON_INVALID(configFile, |
| 2564 | "Infinite loop caused by run_rule actions.", ""); |
| 2565 | } |
| 2566 | // Invalid: test run_rule with infinite loop (rule runs itself). |
| 2567 | { |
| 2568 | json configFile = validConfigFile; |
| 2569 | configFile["rules"][2]["actions"][0]["run_rule"] = "set_voltage_rule1"; |
| 2570 | configFile["rules"][2]["id"] = "set_voltage_rule1"; |
| 2571 | EXPECT_JSON_INVALID(configFile, |
| 2572 | "Infinite loop caused by run_rule actions.", ""); |
| 2573 | } |
| 2574 | // Invalid: test run_rule with infinite loop (indirect loop). |
| 2575 | { |
| 2576 | json configFile = validConfigFile; |
| 2577 | configFile["rules"][2]["actions"][0]["run_rule"] = "set_voltage_rule2"; |
| 2578 | configFile["rules"][2]["id"] = "set_voltage_rule1"; |
| 2579 | configFile["rules"][3]["actions"][0]["run_rule"] = "set_voltage_rule3"; |
| 2580 | configFile["rules"][3]["id"] = "set_voltage_rule2"; |
| 2581 | configFile["rules"][4]["actions"][0]["run_rule"] = "set_voltage_rule1"; |
| 2582 | configFile["rules"][4]["id"] = "set_voltage_rule3"; |
| 2583 | EXPECT_JSON_INVALID(configFile, |
| 2584 | "Infinite loop caused by run_rule actions.", ""); |
| 2585 | } |
| 2586 | } |
Bob King | f88203a | 2020-02-18 13:26:07 +0800 | [diff] [blame] | 2587 | TEST(ValidateRegulatorsConfigTest, RunRuleValueExists) |
| 2588 | { |
| 2589 | // Invalid: test run_rule actions specify a rule ID that does not exist. |
| 2590 | { |
| 2591 | json configFile = validConfigFile; |
| 2592 | configFile["rules"][2]["actions"][0]["run_rule"] = "set_voltage_rule2"; |
| 2593 | configFile["rules"][2]["id"] = "set_voltage_rule1"; |
| 2594 | EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", ""); |
| 2595 | } |
| 2596 | } |
Bob King | 13b2ad9 | 2020-02-18 13:31:39 +0800 | [diff] [blame] | 2597 | TEST(ValidateRegulatorsConfigTest, SetDeviceValueExists) |
| 2598 | { |
| 2599 | // Invalid: test set_device actions specify a device ID that does not exist. |
| 2600 | { |
| 2601 | json configFile = validConfigFile; |
| 2602 | configFile["rules"][2]["actions"][0]["set_device"] = "vdd_regulator2"; |
| 2603 | configFile["rules"][2]["id"] = "set_voltage_rule1"; |
| 2604 | EXPECT_JSON_INVALID(configFile, "Error: Device ID does not exist.", ""); |
| 2605 | } |
| 2606 | } |
Bob King | 21b09be | 2020-02-18 13:33:09 +0800 | [diff] [blame] | 2607 | TEST(ValidateRegulatorsConfigTest, RuleIDExists) |
| 2608 | { |
| 2609 | // Invalid: test rule_id property in configuration specifies a rule ID that |
| 2610 | // does not exist. |
| 2611 | { |
| 2612 | json configFile = validConfigFile; |
| 2613 | configFile["chassis"][0]["devices"][0]["configuration"]["rule_id"] = |
| 2614 | "set_voltage_rule2"; |
| 2615 | EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", ""); |
| 2616 | } |
| 2617 | // Invalid: test rule_id property in presence_detection specifies a rule ID |
| 2618 | // that does not exist. |
| 2619 | { |
| 2620 | json configFile = validConfigFile; |
| 2621 | configFile["chassis"][0]["devices"][0]["presence_detection"] |
| 2622 | ["rule_id"] = "set_voltage_rule2"; |
| 2623 | EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", ""); |
| 2624 | } |
| 2625 | // Invalid: test rule_id property in sensor_monitoring specifies a rule ID |
| 2626 | // that does not exist. |
| 2627 | { |
| 2628 | json configFile = validConfigFile; |
| 2629 | configFile["chassis"][0]["devices"][0]["rails"][0]["sensor_monitoring"] |
| 2630 | ["rule_id"] = "set_voltage_rule2"; |
| 2631 | EXPECT_JSON_INVALID(configFile, "Error: Rule ID does not exist.", ""); |
| 2632 | } |
| 2633 | } |
Bob King | dc72b62 | 2020-02-18 13:36:18 +0800 | [diff] [blame] | 2634 | TEST(ValidateRegulatorsConfigTest, NumberOfElementsInMasks) |
| 2635 | { |
| 2636 | // Invalid: test number of elements in masks not equal to number in values |
| 2637 | // in i2c_compare_bytes. |
| 2638 | { |
| 2639 | json configFile = validConfigFile; |
| 2640 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["register"] = |
| 2641 | "0x82"; |
| 2642 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["values"] = { |
| 2643 | "0x02", "0x73"}; |
| 2644 | configFile["rules"][0]["actions"][1]["i2c_compare_bytes"]["masks"] = { |
| 2645 | "0x7F"}; |
| 2646 | EXPECT_JSON_INVALID(configFile, |
| 2647 | "Error: Invalid i2c_compare_bytes action.", ""); |
| 2648 | } |
| 2649 | // Invalid: test number of elements in masks not equal to number in values |
| 2650 | // in i2c_write_bytes. |
| 2651 | { |
| 2652 | json configFile = validConfigFile; |
| 2653 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["register"] = |
| 2654 | "0x82"; |
| 2655 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["values"] = { |
| 2656 | "0x02", "0x73"}; |
| 2657 | configFile["rules"][0]["actions"][1]["i2c_write_bytes"]["masks"] = { |
| 2658 | "0x7F"}; |
| 2659 | EXPECT_JSON_INVALID(configFile, |
| 2660 | "Error: Invalid i2c_write_bytes action.", ""); |
| 2661 | } |
| 2662 | } |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2663 | TEST(ValidateRegulatorsConfigTest, CommandLineSyntax) |
| 2664 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2665 | std::string validateTool = |
| 2666 | " ../phosphor-regulators/tools/validate-regulators-config.py "; |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2667 | std::string schema = " -s "; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2668 | std::string schemaFile = |
| 2669 | " ../phosphor-regulators/schema/config_schema.json "; |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2670 | std::string configuration = " -c "; |
| 2671 | std::string command; |
| 2672 | std::string errorMessage; |
| 2673 | std::string outputMessage; |
| 2674 | std::string outputMessageHelp = |
| 2675 | "usage: validate-regulators-config.py [-h] [-s SCHEMA_FILE]"; |
| 2676 | int valid = 0; |
| 2677 | |
| 2678 | std::string fileName; |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2679 | TmpFile tmpFile; |
| 2680 | fileName = tmpFile.getName(); |
| 2681 | writeDataToFile(validConfigFile, fileName); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2682 | // Valid: -s specified |
| 2683 | { |
| 2684 | command = validateTool + "-s " + schemaFile + configuration + fileName; |
| 2685 | expectCommandLineSyntax(errorMessage, outputMessage, command, valid); |
| 2686 | } |
| 2687 | // Valid: --schema-file specified |
| 2688 | { |
| 2689 | command = validateTool + "--schema-file " + schemaFile + configuration + |
| 2690 | fileName; |
| 2691 | expectCommandLineSyntax(errorMessage, outputMessage, command, valid); |
| 2692 | } |
| 2693 | // Valid: -c specified |
| 2694 | { |
| 2695 | command = validateTool + schema + schemaFile + "-c " + fileName; |
| 2696 | expectCommandLineSyntax(errorMessage, outputMessage, command, valid); |
| 2697 | } |
| 2698 | // Valid: --configuration-file specified |
| 2699 | { |
| 2700 | command = validateTool + schema + schemaFile + "--configuration-file " + |
| 2701 | fileName; |
| 2702 | expectCommandLineSyntax(errorMessage, outputMessage, command, valid); |
| 2703 | } |
| 2704 | // Valid: -h specified |
| 2705 | { |
| 2706 | command = validateTool + "-h "; |
| 2707 | expectCommandLineSyntax(errorMessage, outputMessageHelp, command, |
| 2708 | valid); |
| 2709 | } |
| 2710 | // Valid: --help specified |
| 2711 | { |
| 2712 | command = validateTool + "--help "; |
| 2713 | expectCommandLineSyntax(errorMessage, outputMessageHelp, command, |
| 2714 | valid); |
| 2715 | } |
| 2716 | // Invalid: -c/--configuration-file not specified |
| 2717 | { |
| 2718 | command = validateTool + schema + schemaFile; |
| 2719 | expectCommandLineSyntax("Error: Configuration file is required.", |
| 2720 | outputMessageHelp, command, 1); |
| 2721 | } |
| 2722 | // Invalid: -s/--schema-file not specified |
| 2723 | { |
| 2724 | command = validateTool + configuration + fileName; |
| 2725 | expectCommandLineSyntax("Error: Schema file is required.", |
| 2726 | outputMessageHelp, command, 1); |
| 2727 | } |
| 2728 | // Invalid: -s specified more than once |
| 2729 | { |
| 2730 | command = |
| 2731 | validateTool + "-s -s " + schemaFile + configuration + fileName; |
| 2732 | expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2); |
| 2733 | } |
| 2734 | // Invalid: -c specified more than once |
| 2735 | { |
| 2736 | command = validateTool + schema + schemaFile + "-c -c " + fileName; |
| 2737 | expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2); |
| 2738 | } |
| 2739 | // Invalid: No file name specified after -c |
| 2740 | { |
| 2741 | command = validateTool + schema + schemaFile + configuration; |
| 2742 | expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2); |
| 2743 | } |
| 2744 | // Invalid: No file name specified after -s |
| 2745 | { |
| 2746 | command = validateTool + schema + configuration + fileName; |
| 2747 | expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2); |
| 2748 | } |
| 2749 | // Invalid: File specified after -c does not exist |
| 2750 | { |
| 2751 | command = validateTool + schema + schemaFile + configuration + |
| 2752 | "../notExistFile"; |
| 2753 | expectCommandLineSyntax( |
| 2754 | "Traceback (most recent call last):", outputMessage, command, 1); |
| 2755 | } |
| 2756 | // Invalid: File specified after -s does not exist |
| 2757 | { |
| 2758 | command = validateTool + schema + "../notExistFile " + configuration + |
| 2759 | fileName; |
| 2760 | expectCommandLineSyntax( |
| 2761 | "Traceback (most recent call last):", outputMessage, command, 1); |
| 2762 | } |
| 2763 | // Invalid: File specified after -s is not right data format |
| 2764 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2765 | std::string wrongFormatFileName; |
| 2766 | TmpFile wrongFormatFile; |
| 2767 | wrongFormatFileName = wrongFormatFile.getName(); |
| 2768 | std::ofstream out(wrongFormatFileName); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2769 | out << "foo"; |
| 2770 | out.close(); |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2771 | command = validateTool + schema + wrongFormatFileName + configuration + |
| 2772 | fileName; |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2773 | expectCommandLineSyntax( |
| 2774 | "Traceback (most recent call last):", outputMessage, command, 1); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2775 | } |
| 2776 | // Invalid: File specified after -c is not readable |
| 2777 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2778 | std::string notReadableFileName; |
| 2779 | TmpFile notReadableFile; |
| 2780 | notReadableFileName = notReadableFile.getName(); |
| 2781 | writeDataToFile(validConfigFile, notReadableFileName); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2782 | command = validateTool + schema + schemaFile + configuration + |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2783 | notReadableFileName; |
| 2784 | chmod(notReadableFileName.c_str(), 0222); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2785 | expectCommandLineSyntax( |
| 2786 | "Traceback (most recent call last):", outputMessage, command, 1); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2787 | } |
| 2788 | // Invalid: File specified after -s is not readable |
| 2789 | { |
Bob King | a57e081 | 2020-03-12 10:47:42 +0800 | [diff] [blame] | 2790 | std::string notReadableFileName; |
| 2791 | TmpFile notReadableFile; |
| 2792 | notReadableFileName = notReadableFile.getName(); |
| 2793 | writeDataToFile(validConfigFile, notReadableFileName); |
| 2794 | command = validateTool + schema + notReadableFileName + configuration + |
| 2795 | fileName; |
| 2796 | chmod(notReadableFileName.c_str(), 0222); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2797 | expectCommandLineSyntax( |
| 2798 | "Traceback (most recent call last):", outputMessage, command, 1); |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2799 | } |
| 2800 | // Invalid: Unexpected parameter specified (like -g) |
| 2801 | { |
| 2802 | command = validateTool + schema + schemaFile + configuration + |
| 2803 | fileName + " -g"; |
| 2804 | expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2); |
| 2805 | } |
Bob King | ed00965 | 2020-02-20 14:54:13 +0800 | [diff] [blame] | 2806 | } |