blob: 5f8f68c454ab0374bb6af733f77bd74ee74bde7a [file] [log] [blame]
Bob King386d33f2019-12-26 17:28:56 +08001/**
Bob King0dcbdf52020-01-20 17:19:39 +08002 * Copyright c 2020 IBM Corporation
Bob King386d33f2019-12-26 17:28:56 +08003 *
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 Kinged009652020-02-20 14:54:13 +080019#include <sys/stat.h>
Bob King386d33f2019-12-26 17:28:56 +080020#include <sys/wait.h>
21
Bob King386d33f2019-12-26 17:28:56 +080022#include <nlohmann/json.hpp>
23
Bob King0dcbdf52020-01-20 17:19:39 +080024#include <fstream>
25
Bob King386d33f2019-12-26 17:28:56 +080026#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
38using json = nlohmann::json;
39
40const 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 Kingb3e48bc2020-02-18 09:59:09 +080055 },
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 King386d33f2019-12-26 17:28:56 +080069 }
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 Kinga57e0812020-03-12 10:47:42 +0800106class TmpFile
Bob King386d33f2019-12-26 17:28:56 +0800107{
Bob Kinga57e0812020-03-12 10:47:42 +0800108 public:
109 TmpFile()
Bob King386d33f2019-12-26 17:28:56 +0800110 {
Bob Kinga57e0812020-03-12 10:47:42 +0800111 int fd = mkstemp(fileName);
112 if (fd == -1)
113 {
114 perror("Can't create temporary file");
115 }
116 close(fd);
Bob King386d33f2019-12-26 17:28:56 +0800117 }
Bob Kinga57e0812020-03-12 10:47:42 +0800118
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 King386d33f2019-12-26 17:28:56 +0800132
133std::string getValidationToolCommand(const std::string& configFileName)
134{
Bob Kinga57e0812020-03-12 10:47:42 +0800135 std::string command =
136 "../phosphor-regulators/tools/validate-regulators-config.py -s \
137 ../phosphor-regulators/schema/config_schema.json -c ";
Bob King386d33f2019-12-26 17:28:56 +0800138 command += configFileName;
139 return command;
140}
141
Bob Kinga57e0812020-03-12 10:47:42 +0800142int runToolForOutputWithCommand(std::string command,
143 std::string& standardOutput,
144 std::string& standardError)
Bob King386d33f2019-12-26 17:28:56 +0800145{
146 // run the validation tool with the temporary file and return the output
147 // of the validation tool.
Bob Kinga57e0812020-03-12 10:47:42 +0800148 TmpFile tmpFile;
149 command += " 2> " + tmpFile.getName();
Bob King386d33f2019-12-26 17:28:56 +0800150 // get the jsonschema print from validation tool.
151 char buffer[256];
Bob Kinga57e0812020-03-12 10:47:42 +0800152 std::string result;
Bob King386d33f2019-12-26 17:28:56 +0800153 // 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 Kinga57e0812020-03-12 10:47:42 +0800174 standardOutput = firstLine;
Bob King386d33f2019-12-26 17:28:56 +0800175 // Get command exit status from return value
176 int exitStatus = WEXITSTATUS(returnValue);
Bob Kinga57e0812020-03-12 10:47:42 +0800177
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 King386d33f2019-12-26 17:28:56 +0800187 return exitStatus;
188}
189
Bob Kinged009652020-02-20 14:54:13 +0800190int runToolForOutput(const std::string& configFileName, std::string& output,
Bob Kinga57e0812020-03-12 10:47:42 +0800191 std::string& error)
Bob Kinged009652020-02-20 14:54:13 +0800192{
193 std::string command = getValidationToolCommand(configFileName);
Bob Kinga57e0812020-03-12 10:47:42 +0800194 return runToolForOutputWithCommand(command, output, error);
Bob Kinged009652020-02-20 14:54:13 +0800195}
196
Bob King386d33f2019-12-26 17:28:56 +0800197void expectFileValid(const std::string& configFileName)
198{
Bob Kinged009652020-02-20 14:54:13 +0800199 std::string errorMessage;
200 std::string outputMessage;
Bob Kinga57e0812020-03-12 10:47:42 +0800201 EXPECT_EQ(runToolForOutput(configFileName, outputMessage, errorMessage), 0);
Bob King386d33f2019-12-26 17:28:56 +0800202 EXPECT_EQ(errorMessage, "");
203 EXPECT_EQ(outputMessage, "");
204}
205
206void expectFileInvalid(const std::string& configFileName,
207 const std::string& expectedErrorMessage,
208 const std::string& expectedOutputMessage)
209{
Bob Kinged009652020-02-20 14:54:13 +0800210 std::string errorMessage;
211 std::string outputMessage;
Bob Kinga57e0812020-03-12 10:47:42 +0800212 EXPECT_EQ(runToolForOutput(configFileName, outputMessage, errorMessage), 1);
Bob King386d33f2019-12-26 17:28:56 +0800213 EXPECT_EQ(errorMessage, expectedErrorMessage);
214 EXPECT_EQ(outputMessage, expectedOutputMessage);
215}
216
Bob Kinga57e0812020-03-12 10:47:42 +0800217void writeDataToFile(const json configFileJson, std::string fileName)
Bob King386d33f2019-12-26 17:28:56 +0800218{
Bob King386d33f2019-12-26 17:28:56 +0800219 std::string jsonData = configFileJson.dump();
220 std::ofstream out(fileName);
221 out << jsonData;
222 out.close();
Bob Kinged009652020-02-20 14:54:13 +0800223}
224
225void expectJsonValid(const json configFileJson)
226{
227 std::string fileName;
Bob Kinga57e0812020-03-12 10:47:42 +0800228 TmpFile tmpFile;
229 fileName = tmpFile.getName();
230 writeDataToFile(configFileJson, fileName);
Bob Kinged009652020-02-20 14:54:13 +0800231
Bob King386d33f2019-12-26 17:28:56 +0800232 EXPECT_FILE_VALID(fileName);
Bob King386d33f2019-12-26 17:28:56 +0800233}
234
235void expectJsonInvalid(const json configFileJson,
236 const std::string& expectedErrorMessage,
237 const std::string& expectedOutputMessage)
238{
239 std::string fileName;
Bob Kinga57e0812020-03-12 10:47:42 +0800240 TmpFile tmpFile;
241 fileName = tmpFile.getName();
242 writeDataToFile(configFileJson, fileName);
Bob King386d33f2019-12-26 17:28:56 +0800243
244 EXPECT_FILE_INVALID(fileName, expectedErrorMessage, expectedOutputMessage);
Bob King386d33f2019-12-26 17:28:56 +0800245}
246
Bob Kinged009652020-02-20 14:54:13 +0800247void 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 Kinga57e0812020-03-12 10:47:42 +0800253 EXPECT_EQ(runToolForOutputWithCommand(command, outputMessage, errorMessage),
254 status);
Bob Kinged009652020-02-20 14:54:13 +0800255 EXPECT_EQ(errorMessage, expectedErrorMessage);
256 EXPECT_EQ(outputMessage, expectedOutputMessage);
257}
258
Bob Kingbeaf6532020-01-21 11:03:49 +0800259TEST(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 King358c4172020-03-16 13:57:08 +0800302 "True is not of type 'array'");
Bob Kingbeaf6532020-01-21 11:03:49 +0800303 }
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 King358c4172020-03-16 13:57:08 +0800316 "'foo' is not of type 'object'");
Bob Kingbeaf6532020-01-21 11:03:49 +0800317 }
318}
Bob King3728f562020-01-21 11:35:31 +0800319TEST(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 King358c4172020-03-16 13:57:08 +0800338 "'number' is a required property");
Bob King3728f562020-01-21 11:35:31 +0800339 }
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 King358c4172020-03-16 13:57:08 +0800345 "True is not of type 'array'");
Bob King3728f562020-01-21 11:35:31 +0800346 }
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 King358c4172020-03-16 13:57:08 +0800352 "1.3 is not of type 'integer'");
Bob King3728f562020-01-21 11:35:31 +0800353 }
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 King358c4172020-03-16 13:57:08 +0800359 "True is not of type 'array'");
Bob King3728f562020-01-21 11:35:31 +0800360 }
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 Kingbf1cbea2020-01-21 11:08:50 +0800383TEST(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 King358c4172020-03-16 13:57:08 +0800401 "'fru' is a required property");
Bob Kingbf1cbea2020-01-21 11:08:50 +0800402 }
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 King358c4172020-03-16 13:57:08 +0800409 "'' is too short");
Bob Kingbf1cbea2020-01-21 11:08:50 +0800410 }
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 King358c4172020-03-16 13:57:08 +0800417 "'value' is a required property");
Bob Kingbf1cbea2020-01-21 11:08:50 +0800418 }
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 King358c4172020-03-16 13:57:08 +0800425 "'1' is not of type 'boolean'");
Bob Kingbf1cbea2020-01-21 11:08:50 +0800426 }
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 King358c4172020-03-16 13:57:08 +0800433 "1 is not of type 'string'");
Bob Kingbf1cbea2020-01-21 11:08:50 +0800434 }
435}
Bob Kingf8b77a02020-01-21 11:09:47 +0800436TEST(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 King358c4172020-03-16 13:57:08 +0800455 "'fru' is a required property");
Bob Kingf8b77a02020-01-21 11:09:47 +0800456 }
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 King358c4172020-03-16 13:57:08 +0800463 "'keyword' is a required property");
Bob Kingf8b77a02020-01-21 11:09:47 +0800464 }
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 King358c4172020-03-16 13:57:08 +0800471 "'value' is a required property");
Bob Kingf8b77a02020-01-21 11:09:47 +0800472 }
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 King358c4172020-03-16 13:57:08 +0800479 "1 is not of type 'string'");
Bob Kingf8b77a02020-01-21 11:09:47 +0800480 }
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 King358c4172020-03-16 13:57:08 +0800487 "'' is too short");
Bob Kingf8b77a02020-01-21 11:09:47 +0800488 }
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 King358c4172020-03-16 13:57:08 +0800497 "'Number' is not one of ['CCIN', "
498 "'Manufacturer', 'Model', 'PartNumber']");
Bob Kingf8b77a02020-01-21 11:09:47 +0800499 }
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 King358c4172020-03-16 13:57:08 +0800506 "1 is not of type 'string'");
Bob Kingf8b77a02020-01-21 11:09:47 +0800507 }
508}
Bob King20057412020-03-16 16:50:17 +0800509TEST(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 King4c67a3a2020-02-07 09:48:11 +0800601TEST(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 King78793102020-03-13 13:16:09 +0800613 configFile["chassis"][0]["devices"][0]["configuration"]["comments"][1] =
614 "test multiple array elements in comments.";
Bob King4c67a3a2020-02-07 09:48:11 +0800615 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 King358c4172020-03-16 13:57:08 +0800658 "1 is not of type 'array'");
Bob King4c67a3a2020-02-07 09:48:11 +0800659 }
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 King358c4172020-03-16 13:57:08 +0800670 "{'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 King4c67a3a2020-02-07 09:48:11 +0800675 }
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 King358c4172020-03-16 13:57:08 +0800682 "'rule_id' is a required property");
Bob King4c67a3a2020-02-07 09:48:11 +0800683 }
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 King358c4172020-03-16 13:57:08 +0800689 "True is not of type 'number'");
Bob King4c67a3a2020-02-07 09:48:11 +0800690 }
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 King358c4172020-03-16 13:57:08 +0800697 "True is not of type 'string'");
Bob King4c67a3a2020-02-07 09:48:11 +0800698 }
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 King358c4172020-03-16 13:57:08 +0800707 "True is not of type 'array'");
Bob King4c67a3a2020-02-07 09:48:11 +0800708 }
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 King358c4172020-03-16 13:57:08 +0800723 "'id!' does not match '^[A-Za-z0-9_]+$'");
Bob King4c67a3a2020-02-07 09:48:11 +0800724 }
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 Kinga2ba2df2020-02-04 14:38:44 +0800736TEST(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 King358c4172020-03-16 13:57:08 +0800758 "'id' is a required property");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800759 }
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 King358c4172020-03-16 13:57:08 +0800765 "'is_regulator' is a required property");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800766 }
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 King358c4172020-03-16 13:57:08 +0800772 "'fru' is a required property");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800773 }
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 King358c4172020-03-16 13:57:08 +0800779 "'i2c_interface' is a required property");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800780 }
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 King358c4172020-03-16 13:57:08 +0800786 "True is not of type 'array'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800787 }
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 King358c4172020-03-16 13:57:08 +0800793 "True is not of type 'string'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800794 }
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 King358c4172020-03-16 13:57:08 +0800800 "1 is not of type 'boolean'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800801 }
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 King358c4172020-03-16 13:57:08 +0800807 "True is not of type 'string'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800808 }
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 King358c4172020-03-16 13:57:08 +0800814 "True is not of type 'object'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800815 }
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 King358c4172020-03-16 13:57:08 +0800822 "True is not of type 'object'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800823 }
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 King358c4172020-03-16 13:57:08 +0800829 "True is not of type 'object'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800830 }
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 King358c4172020-03-16 13:57:08 +0800836 "True is not of type 'array'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800837 }
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 King358c4172020-03-16 13:57:08 +0800850 "'' is too short");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800851 }
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 King358c4172020-03-16 13:57:08 +0800857 "'id#' does not match '^[A-Za-z0-9_]+$'");
Bob Kinga2ba2df2020-02-04 14:38:44 +0800858 }
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 King4ab8cbb2020-01-21 11:10:48 +0800867TEST(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 King358c4172020-03-16 13:57:08 +0800886 "'register' is a required property");
Bob King4ab8cbb2020-01-21 11:10:48 +0800887 }
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 King358c4172020-03-16 13:57:08 +0800894 "'position' is a required property");
Bob King4ab8cbb2020-01-21 11:10:48 +0800895 }
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 King358c4172020-03-16 13:57:08 +0800901 "'value' is a required property");
Bob King4ab8cbb2020-01-21 11:10:48 +0800902 }
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 King358c4172020-03-16 13:57:08 +0800908 "1 is not of type 'string'");
Bob King4ab8cbb2020-01-21 11:10:48 +0800909 }
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 King358c4172020-03-16 13:57:08 +0800916 "'0xA00' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King4ab8cbb2020-01-21 11:10:48 +0800917 }
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 King358c4172020-03-16 13:57:08 +0800924 "3.1 is not of type 'integer'");
Bob King4ab8cbb2020-01-21 11:10:48 +0800925 }
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 King358c4172020-03-16 13:57:08 +0800946 "'1' is not of type 'integer'");
Bob King4ab8cbb2020-01-21 11:10:48 +0800947 }
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 King514023d2020-01-21 11:13:05 +0800963TEST(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 King358c4172020-03-16 13:57:08 +0800989 "'register' is a required property");
Bob King514023d2020-01-21 11:13:05 +0800990 }
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 King358c4172020-03-16 13:57:08 +0800996 "'value' is a required property");
Bob King514023d2020-01-21 11:13:05 +0800997 }
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 King358c4172020-03-16 13:57:08 +08001004 "1 is not of type 'string'");
Bob King514023d2020-01-21 11:13:05 +08001005 }
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 King358c4172020-03-16 13:57:08 +08001011 "1 is not of type 'string'");
Bob King514023d2020-01-21 11:13:05 +08001012 }
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 King358c4172020-03-16 13:57:08 +08001018 "1 is not of type 'string'");
Bob King514023d2020-01-21 11:13:05 +08001019 }
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 King358c4172020-03-16 13:57:08 +08001027 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001028 }
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 King358c4172020-03-16 13:57:08 +08001036 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001037 }
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 King358c4172020-03-16 13:57:08 +08001044 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001045 }
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 King358c4172020-03-16 13:57:08 +08001053 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001054 }
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 King358c4172020-03-16 13:57:08 +08001062 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001063 }
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 King358c4172020-03-16 13:57:08 +08001070 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001071 }
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 King358c4172020-03-16 13:57:08 +08001078 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001079 }
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 King358c4172020-03-16 13:57:08 +08001086 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001087 }
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 King358c4172020-03-16 13:57:08 +08001093 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001094 }
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 King358c4172020-03-16 13:57:08 +08001101 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001102 }
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 King358c4172020-03-16 13:57:08 +08001109 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001110 }
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 King358c4172020-03-16 13:57:08 +08001117 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King514023d2020-01-21 11:13:05 +08001118 }
1119}
Bob Kingfb162bb2020-01-21 11:28:07 +08001120TEST(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 King358c4172020-03-16 13:57:08 +08001147 "'register' is a required property");
Bob Kingfb162bb2020-01-21 11:28:07 +08001148 }
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 King358c4172020-03-16 13:57:08 +08001155 "'values' is a required property");
Bob Kingfb162bb2020-01-21 11:28:07 +08001156 }
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 King358c4172020-03-16 13:57:08 +08001179 "1 is not of type 'string'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001180 }
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 King358c4172020-03-16 13:57:08 +08001186 "1 is not of type 'array'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001187 }
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 King358c4172020-03-16 13:57:08 +08001193 "1 is not of type 'array'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001194 }
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 King358c4172020-03-16 13:57:08 +08001202 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001203 }
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 King358c4172020-03-16 13:57:08 +08001211 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001212 }
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 King358c4172020-03-16 13:57:08 +08001220 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001221 }
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 King358c4172020-03-16 13:57:08 +08001229 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001230 }
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 King358c4172020-03-16 13:57:08 +08001238 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001239 }
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 King358c4172020-03-16 13:57:08 +08001247 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001248 }
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 King358c4172020-03-16 13:57:08 +08001255 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001256 }
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 King358c4172020-03-16 13:57:08 +08001263 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001264 }
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 King358c4172020-03-16 13:57:08 +08001271 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001272 }
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 King358c4172020-03-16 13:57:08 +08001279 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001280 }
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 King358c4172020-03-16 13:57:08 +08001287 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001288 }
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 King358c4172020-03-16 13:57:08 +08001295 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingfb162bb2020-01-21 11:28:07 +08001296 }
1297}
Bob Kingca93f1f2020-01-31 11:21:16 +08001298TEST(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 King358c4172020-03-16 13:57:08 +08001310 "'bus' is a required property");
Bob Kingca93f1f2020-01-31 11:21:16 +08001311 }
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 King358c4172020-03-16 13:57:08 +08001318 "'address' is a required property");
Bob Kingca93f1f2020-01-31 11:21:16 +08001319 }
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 King358c4172020-03-16 13:57:08 +08001325 "True is not of type 'integer'");
Bob Kingca93f1f2020-01-31 11:21:16 +08001326 }
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 King358c4172020-03-16 13:57:08 +08001334 "True is not of type 'string'");
Bob Kingca93f1f2020-01-31 11:21:16 +08001335 }
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 King358c4172020-03-16 13:57:08 +08001351 "'0x700' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob Kingca93f1f2020-01-31 11:21:16 +08001352 }
1353}
Bob King188db7d2020-01-31 13:01:01 +08001354TEST(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 King358c4172020-03-16 13:57:08 +08001371 "'register' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001372 }
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 King358c4172020-03-16 13:57:08 +08001378 "'position' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001379 }
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 King358c4172020-03-16 13:57:08 +08001385 "'value' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001386 }
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 King358c4172020-03-16 13:57:08 +08001392 "1 is not of type 'string'");
Bob King188db7d2020-01-31 13:01:01 +08001393 }
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 King358c4172020-03-16 13:57:08 +08001400 "'0xA00' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001401 }
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 King358c4172020-03-16 13:57:08 +08001407 "3.1 is not of type 'integer'");
Bob King188db7d2020-01-31 13:01:01 +08001408 }
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 King358c4172020-03-16 13:57:08 +08001428 "'1' is not of type 'integer'");
Bob King188db7d2020-01-31 13:01:01 +08001429 }
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}
1445TEST(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 King358c4172020-03-16 13:57:08 +08001471 "'register' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001472 }
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 King358c4172020-03-16 13:57:08 +08001478 "'value' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001479 }
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 King358c4172020-03-16 13:57:08 +08001485 "1 is not of type 'string'");
Bob King188db7d2020-01-31 13:01:01 +08001486 }
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 King358c4172020-03-16 13:57:08 +08001492 "1 is not of type 'string'");
Bob King188db7d2020-01-31 13:01:01 +08001493 }
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 King358c4172020-03-16 13:57:08 +08001499 "1 is not of type 'string'");
Bob King188db7d2020-01-31 13:01:01 +08001500 }
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 King358c4172020-03-16 13:57:08 +08001508 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001509 }
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 King358c4172020-03-16 13:57:08 +08001517 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001518 }
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 King358c4172020-03-16 13:57:08 +08001525 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001526 }
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 King358c4172020-03-16 13:57:08 +08001534 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001535 }
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 King358c4172020-03-16 13:57:08 +08001542 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001543 }
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 King358c4172020-03-16 13:57:08 +08001549 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001550 }
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 King358c4172020-03-16 13:57:08 +08001557 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001558 }
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 King358c4172020-03-16 13:57:08 +08001564 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001565 }
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 King358c4172020-03-16 13:57:08 +08001571 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001572 }
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 King358c4172020-03-16 13:57:08 +08001579 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001580 }
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 King358c4172020-03-16 13:57:08 +08001587 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001588 }
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 King358c4172020-03-16 13:57:08 +08001594 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001595 }
1596}
1597TEST(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 King358c4172020-03-16 13:57:08 +08001623 "'register' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001624 }
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 King358c4172020-03-16 13:57:08 +08001630 "'values' is a required property");
Bob King188db7d2020-01-31 13:01:01 +08001631 }
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 King358c4172020-03-16 13:57:08 +08001653 "1 is not of type 'string'");
Bob King188db7d2020-01-31 13:01:01 +08001654 }
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 King358c4172020-03-16 13:57:08 +08001660 "1 is not of type 'array'");
Bob King188db7d2020-01-31 13:01:01 +08001661 }
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 King358c4172020-03-16 13:57:08 +08001667 "1 is not of type 'array'");
Bob King188db7d2020-01-31 13:01:01 +08001668 }
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 King358c4172020-03-16 13:57:08 +08001676 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001677 }
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 King358c4172020-03-16 13:57:08 +08001685 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001686 }
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 King358c4172020-03-16 13:57:08 +08001694 "'0x820' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001695 }
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 King358c4172020-03-16 13:57:08 +08001703 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001704 }
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 King358c4172020-03-16 13:57:08 +08001712 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001713 }
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 King358c4172020-03-16 13:57:08 +08001721 "'0x8' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001722 }
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 King358c4172020-03-16 13:57:08 +08001729 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001730 }
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 King358c4172020-03-16 13:57:08 +08001737 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001738 }
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 King358c4172020-03-16 13:57:08 +08001745 "'82' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001746 }
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 King358c4172020-03-16 13:57:08 +08001753 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001754 }
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 King358c4172020-03-16 13:57:08 +08001761 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001762 }
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 King358c4172020-03-16 13:57:08 +08001769 "'0xG1' does not match '^0x[0-9A-Fa-f]{2}$'");
Bob King188db7d2020-01-31 13:01:01 +08001770 }
1771}
Bob Kingead0b052020-01-21 11:29:03 +08001772TEST(ValidateRegulatorsConfigTest, If)
1773{
1774 json ifFile = validConfigFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001775 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 Kingead0b052020-01-21 11:29:03 +08001782 // 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 King2d27dcf2020-02-11 15:00:50 +08001790 configFile["rules"][2]["actions"][0]["if"].erase("else");
Bob Kingead0b052020-01-21 11:29:03 +08001791 EXPECT_JSON_VALID(configFile);
1792 }
1793 // Invalid: test if with no property condition.
1794 {
1795 json configFile = ifFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001796 configFile["rules"][2]["actions"][0]["if"].erase("condition");
Bob Kingead0b052020-01-21 11:29:03 +08001797 EXPECT_JSON_INVALID(configFile, "Validation failed.",
Bob King358c4172020-03-16 13:57:08 +08001798 "'condition' is a required property");
Bob Kingead0b052020-01-21 11:29:03 +08001799 }
1800 // Invalid: test if with no property then.
1801 {
1802 json configFile = ifFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001803 configFile["rules"][2]["actions"][0]["if"].erase("then");
Bob Kingead0b052020-01-21 11:29:03 +08001804 EXPECT_JSON_INVALID(configFile, "Validation failed.",
Bob King358c4172020-03-16 13:57:08 +08001805 "'then' is a required property");
Bob Kingead0b052020-01-21 11:29:03 +08001806 }
1807 // Invalid: test if with property then empty array.
1808 {
1809 json configFile = ifFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001810 configFile["rules"][2]["actions"][0]["if"]["then"] = json::array();
Bob Kingead0b052020-01-21 11:29:03 +08001811 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 King2d27dcf2020-02-11 15:00:50 +08001817 configFile["rules"][2]["actions"][0]["if"]["else"] = json::array();
Bob Kingead0b052020-01-21 11:29:03 +08001818 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 King2d27dcf2020-02-11 15:00:50 +08001824 configFile["rules"][2]["actions"][0]["if"]["condition"] = 1;
Bob Kingead0b052020-01-21 11:29:03 +08001825 EXPECT_JSON_INVALID(configFile, "Validation failed.",
Bob King358c4172020-03-16 13:57:08 +08001826 "1 is not of type 'object'");
Bob Kingead0b052020-01-21 11:29:03 +08001827 }
1828 // Invalid: test if with property then wrong type.
1829 {
1830 json configFile = ifFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001831 configFile["rules"][2]["actions"][0]["if"]["then"] = 1;
Bob Kingead0b052020-01-21 11:29:03 +08001832 EXPECT_JSON_INVALID(configFile, "Validation failed.",
Bob King358c4172020-03-16 13:57:08 +08001833 "1 is not of type 'array'");
Bob Kingead0b052020-01-21 11:29:03 +08001834 }
1835 // Invalid: test if with property else wrong type.
1836 {
1837 json configFile = ifFile;
Bob King2d27dcf2020-02-11 15:00:50 +08001838 configFile["rules"][2]["actions"][0]["if"]["else"] = 1;
Bob Kingead0b052020-01-21 11:29:03 +08001839 EXPECT_JSON_INVALID(configFile, "Validation failed.",
Bob King358c4172020-03-16 13:57:08 +08001840 "1 is not of type 'array'");
Bob Kingead0b052020-01-21 11:29:03 +08001841 }
1842}
Bob Kingbfe9fe72020-01-21 11:29:57 +08001843TEST(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 King358c4172020-03-16 13:57:08 +08001860 "1 is not of type 'object'");
Bob Kingbfe9fe72020-01-21 11:29:57 +08001861 }
1862}
Bob Kingcfc29d02020-01-21 11:30:50 +08001863TEST(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 King358c4172020-03-16 13:57:08 +08001891 "1 is not of type 'array'");
Bob Kingcfc29d02020-01-21 11:30:50 +08001892 }
1893}
Bob Kingd6618092020-01-21 11:31:46 +08001894TEST(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 King358c4172020-03-16 13:57:08 +08001922 "'type' is a required property");
Bob Kingd6618092020-01-21 11:31:46 +08001923 }
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 King358c4172020-03-16 13:57:08 +08001930 "'command' is a required property");
Bob Kingd6618092020-01-21 11:31:46 +08001931 }
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 King358c4172020-03-16 13:57:08 +08001938 "'format' is a required property");
Bob Kingd6618092020-01-21 11:31:46 +08001939 }
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 King358c4172020-03-16 13:57:08 +08001945 EXPECT_JSON_INVALID(configFile, "Validation failed.",
1946 "True is not of type 'string'");
Bob Kingd6618092020-01-21 11:31:46 +08001947 }
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 King358c4172020-03-16 13:57:08 +08001954 "True is not of type 'string'");
Bob Kingd6618092020-01-21 11:31:46 +08001955 }
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 King358c4172020-03-16 13:57:08 +08001962 "True is not of type 'string'");
Bob Kingd6618092020-01-21 11:31:46 +08001963 }
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 King358c4172020-03-16 13:57:08 +08001970 "True is not of type 'integer'");
Bob Kingd6618092020-01-21 11:31:46 +08001971 }
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 King358c4172020-03-16 13:57:08 +08001979 "'foo' is not one of ['iout', 'iout_peak', 'iout_valley', "
1980 "'pout', 'temperature', 'temperature_peak', 'vout', "
1981 "'vout_peak', 'vout_valley']");
Bob Kingd6618092020-01-21 11:31:46 +08001982 }
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 King358c4172020-03-16 13:57:08 +08001989 "'0x8B0' does not match '^0x[0-9a-fA-F]{2}$'");
Bob Kingd6618092020-01-21 11:31:46 +08001990 }
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 King358c4172020-03-16 13:57:08 +08001996 EXPECT_JSON_INVALID(configFile, "Validation failed.",
1997 "'foo' is not one of ['linear_11', 'linear_16']");
Bob Kingd6618092020-01-21 11:31:46 +08001998 }
1999}
Bob King02179c62020-01-21 11:32:36 +08002000TEST(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 King358c4172020-03-16 13:57:08 +08002033 "'format' is a required property");
Bob King02179c62020-01-21 11:32:36 +08002034 }
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 King358c4172020-03-16 13:57:08 +08002041 "True is not of type 'number'");
Bob King02179c62020-01-21 11:32:36 +08002042 }
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 King358c4172020-03-16 13:57:08 +08002049 "True is not of type 'string'");
Bob King02179c62020-01-21 11:32:36 +08002050 }
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 King358c4172020-03-16 13:57:08 +08002057 "1.3 is not of type 'integer'");
Bob King02179c62020-01-21 11:32:36 +08002058 }
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 King358c4172020-03-16 13:57:08 +08002066 "1 is not of type 'boolean'");
Bob King02179c62020-01-21 11:32:36 +08002067 }
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 King358c4172020-03-16 13:57:08 +08002074 "'foo' is not one of ['linear']");
Bob King02179c62020-01-21 11:32:36 +08002075 }
2076}
Bob King99d8fa12020-01-31 11:23:40 +08002077TEST(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 Kingf4ff1162020-02-11 15:13:38 +08002084 ["rule_id"] = "set_voltage_rule";
Bob King99d8fa12020-01-31 11:23:40 +08002085 // 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 King358c4172020-03-16 13:57:08 +08002114 "{'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 King99d8fa12020-01-31 11:23:40 +08002119 }
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 King358c4172020-03-16 13:57:08 +08002126 "'rule_id' is a required property");
Bob King99d8fa12020-01-31 11:23:40 +08002127 }
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 King358c4172020-03-16 13:57:08 +08002134 "True is not of type 'array'");
Bob King99d8fa12020-01-31 11:23:40 +08002135 }
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 King358c4172020-03-16 13:57:08 +08002142 "True is not of type 'string'");
Bob King99d8fa12020-01-31 11:23:40 +08002143 }
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 King358c4172020-03-16 13:57:08 +08002152 "True is not of type 'array'");
Bob King99d8fa12020-01-31 11:23:40 +08002153 }
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 King358c4172020-03-16 13:57:08 +08002160 "'id@' does not match '^[A-Za-z0-9_]+$'");
Bob King99d8fa12020-01-31 11:23:40 +08002161 }
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 Kinge9260b52020-01-21 11:46:13 +08002181TEST(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 King358c4172020-03-16 13:57:08 +08002203 "'id' is a required property");
Bob Kinge9260b52020-01-21 11:46:13 +08002204 }
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 King358c4172020-03-16 13:57:08 +08002210 "True is not of type 'array'");
Bob Kinge9260b52020-01-21 11:46:13 +08002211 }
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 King358c4172020-03-16 13:57:08 +08002217 "True is not of type 'string'");
Bob Kinge9260b52020-01-21 11:46:13 +08002218 }
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 King358c4172020-03-16 13:57:08 +08002225 "True is not of type 'object'");
Bob Kinge9260b52020-01-21 11:46:13 +08002226 }
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 King358c4172020-03-16 13:57:08 +08002233 "True is not of type 'object'");
Bob Kinge9260b52020-01-21 11:46:13 +08002234 }
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 King358c4172020-03-16 13:57:08 +08002248 "'id~' does not match '^[A-Za-z0-9_]+$'");
Bob Kinge9260b52020-01-21 11:46:13 +08002249 }
2250}
Bob King64df7da2020-01-31 12:04:12 +08002251TEST(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 King358c4172020-03-16 13:57:08 +08002272 "1 is not of type 'string'");
Bob King64df7da2020-01-31 12:04:12 +08002273 }
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 King358c4172020-03-16 13:57:08 +08002280 "'id' is a required property");
Bob King64df7da2020-01-31 12:04:12 +08002281 }
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 King358c4172020-03-16 13:57:08 +08002288 "True is not of type 'string'");
Bob King64df7da2020-01-31 12:04:12 +08002289 }
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 King358c4172020-03-16 13:57:08 +08002296 "'foo%' does not match '^[A-Za-z0-9_]+$'");
Bob King64df7da2020-01-31 12:04:12 +08002297 }
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 King358c4172020-03-16 13:57:08 +08002304 "'actions' is a required property");
Bob King64df7da2020-01-31 12:04:12 +08002305 }
2306
2307 // valid test rule with multiple actions
2308 {
2309 json configFile = validConfigFile;
Bob King63d795f2020-02-11 15:22:09 +08002310 configFile["rules"][0]["actions"][1]["run_rule"] = "read_sensors_rule";
Bob King64df7da2020-01-31 12:04:12 +08002311 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 King358c4172020-03-16 13:57:08 +08002319 "1 is not of type 'array'");
Bob King64df7da2020-01-31 12:04:12 +08002320 }
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 King358c4172020-03-16 13:57:08 +08002327 "'foo' is not of type 'object'");
Bob King64df7da2020-01-31 12:04:12 +08002328 }
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 Kinge86c2e52020-01-21 11:33:32 +08002338TEST(ValidateRegulatorsConfigTest, RunRule)
2339{
2340 json runRuleFile = validConfigFile;
Bob King7d3a9f12020-02-11 15:34:52 +08002341 runRuleFile["rules"][0]["actions"][1]["run_rule"] = "read_sensors_rule";
Bob Kinge86c2e52020-01-21 11:33:32 +08002342 // 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 King358c4172020-03-16 13:57:08 +08002352 "True is not of type 'string'");
Bob Kinge86c2e52020-01-21 11:33:32 +08002353 }
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 King358c4172020-03-16 13:57:08 +08002360 "'set_voltage_rule%' does not match '^[A-Za-z0-9_]+$'");
Bob Kinge86c2e52020-01-21 11:33:32 +08002361 }
2362}
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002363TEST(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 King358c4172020-03-16 13:57:08 +08002395 "{'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 Kingfcc2a2f2020-01-31 11:29:45 +08002399 }
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 King358c4172020-03-16 13:57:08 +08002406 "'rule_id' is a required property");
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002407 }
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 King358c4172020-03-16 13:57:08 +08002414 "True is not of type 'array'");
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002415 }
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 King358c4172020-03-16 13:57:08 +08002422 "True is not of type 'string'");
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002423 }
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 King358c4172020-03-16 13:57:08 +08002432 "True is not of type 'array'");
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002433 }
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 King358c4172020-03-16 13:57:08 +08002440 "'id@' does not match '^[A-Za-z0-9_]+$'");
Bob Kingfcc2a2f2020-01-31 11:29:45 +08002441 }
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 King68230aa2020-01-21 11:34:33 +08002461TEST(ValidateRegulatorsConfigTest, SetDevice)
2462{
2463 json setDeviceFile = validConfigFile;
Bob King7d3a9f12020-02-11 15:34:52 +08002464 setDeviceFile["rules"][0]["actions"][1]["set_device"] = "vdd_regulator";
Bob King68230aa2020-01-21 11:34:33 +08002465 // 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 King358c4172020-03-16 13:57:08 +08002475 "True is not of type 'string'");
Bob King68230aa2020-01-21 11:34:33 +08002476 }
2477 // Invalid: test set_device wrong format.
2478 {
2479 json configFile = setDeviceFile;
2480 configFile["rules"][0]["actions"][1]["set_device"] = "io_expander2%";
Bob King358c4172020-03-16 13:57:08 +08002481 EXPECT_JSON_INVALID(configFile, "Validation failed.",
2482 "'io_expander2%' does not match '^[A-Za-z0-9_]+$'");
Bob King68230aa2020-01-21 11:34:33 +08002483 }
2484}
Bob King3643cc02020-01-31 11:32:56 +08002485TEST(ValidateRegulatorsConfigTest, DuplicateRuleID)
2486{
2487 // Invalid: test duplicate ID in rule.
2488 {
2489 json configFile = validConfigFile;
Bob Kingb3e48bc2020-02-18 09:59:09 +08002490 configFile["rules"][2]["id"] = "set_voltage_rule";
2491 configFile["rules"][2]["actions"][0]["pmbus_write_vout_command"]
Bob King3643cc02020-01-31 11:32:56 +08002492 ["format"] = "linear";
2493 EXPECT_JSON_INVALID(configFile, "Error: Duplicate rule ID.", "");
2494 }
2495}
2496TEST(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}
2505TEST(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}
2520TEST(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 King78793102020-03-13 13:16:09 +08002529TEST(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 King3643cc02020-01-31 11:32:56 +08002554TEST(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 Kingf88203a2020-02-18 13:26:07 +08002587TEST(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 King13b2ad92020-02-18 13:31:39 +08002597TEST(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 King21b09be2020-02-18 13:33:09 +08002607TEST(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 Kingdc72b622020-02-18 13:36:18 +08002634TEST(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 Kinged009652020-02-20 14:54:13 +08002663TEST(ValidateRegulatorsConfigTest, CommandLineSyntax)
2664{
Bob Kinga57e0812020-03-12 10:47:42 +08002665 std::string validateTool =
2666 " ../phosphor-regulators/tools/validate-regulators-config.py ";
Bob Kinged009652020-02-20 14:54:13 +08002667 std::string schema = " -s ";
Bob Kinga57e0812020-03-12 10:47:42 +08002668 std::string schemaFile =
2669 " ../phosphor-regulators/schema/config_schema.json ";
Bob Kinged009652020-02-20 14:54:13 +08002670 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 Kinga57e0812020-03-12 10:47:42 +08002679 TmpFile tmpFile;
2680 fileName = tmpFile.getName();
2681 writeDataToFile(validConfigFile, fileName);
Bob Kinged009652020-02-20 14:54:13 +08002682 // 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 Kinga57e0812020-03-12 10:47:42 +08002765 std::string wrongFormatFileName;
2766 TmpFile wrongFormatFile;
2767 wrongFormatFileName = wrongFormatFile.getName();
2768 std::ofstream out(wrongFormatFileName);
Bob Kinged009652020-02-20 14:54:13 +08002769 out << "foo";
2770 out.close();
Bob Kinga57e0812020-03-12 10:47:42 +08002771 command = validateTool + schema + wrongFormatFileName + configuration +
2772 fileName;
Bob Kinged009652020-02-20 14:54:13 +08002773 expectCommandLineSyntax(
2774 "Traceback (most recent call last):", outputMessage, command, 1);
Bob Kinged009652020-02-20 14:54:13 +08002775 }
2776 // Invalid: File specified after -c is not readable
2777 {
Bob Kinga57e0812020-03-12 10:47:42 +08002778 std::string notReadableFileName;
2779 TmpFile notReadableFile;
2780 notReadableFileName = notReadableFile.getName();
2781 writeDataToFile(validConfigFile, notReadableFileName);
Bob Kinged009652020-02-20 14:54:13 +08002782 command = validateTool + schema + schemaFile + configuration +
Bob Kinga57e0812020-03-12 10:47:42 +08002783 notReadableFileName;
2784 chmod(notReadableFileName.c_str(), 0222);
Bob Kinged009652020-02-20 14:54:13 +08002785 expectCommandLineSyntax(
2786 "Traceback (most recent call last):", outputMessage, command, 1);
Bob Kinged009652020-02-20 14:54:13 +08002787 }
2788 // Invalid: File specified after -s is not readable
2789 {
Bob Kinga57e0812020-03-12 10:47:42 +08002790 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 Kinged009652020-02-20 14:54:13 +08002797 expectCommandLineSyntax(
2798 "Traceback (most recent call last):", outputMessage, command, 1);
Bob Kinged009652020-02-20 14:54:13 +08002799 }
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 Kinged009652020-02-20 14:54:13 +08002806}