-
Notifications
You must be signed in to change notification settings - Fork 1
/
probe_result_checker.cc
529 lines (457 loc) · 16.2 KB
/
probe_result_checker.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <base/strings/stringprintf.h>
#include <base/strings/string_number_conversions.h>
#include "runtime_probe/probe_result_checker.h"
#include "runtime_probe/utils/type_utils.h"
namespace {
using ValidatorOperator = runtime_probe::ValidatorOperator;
using ReturnCode = runtime_probe::FieldConverter::ReturnCode;
constexpr const char* GetPrefix(ValidatorOperator op) {
switch (op) {
case ValidatorOperator::NOP:
return "!nop ";
case ValidatorOperator::RE:
return "!re ";
case ValidatorOperator::EQ:
return "!eq ";
case ValidatorOperator::NE:
return "!ne ";
case ValidatorOperator::GT:
return "!gt ";
case ValidatorOperator::GE:
return "!ge ";
case ValidatorOperator::LT:
return "!lt ";
case ValidatorOperator::LE:
return "!le ";
default:
DCHECK(false) << "should never reach here";
}
return nullptr;
}
constexpr const char* ToString(ValidatorOperator op) {
switch (op) {
case ValidatorOperator::NOP:
return "NOP";
case ValidatorOperator::RE:
return "RE";
case ValidatorOperator::EQ:
return "EQ";
case ValidatorOperator::NE:
return "NE";
case ValidatorOperator::GT:
return "GT";
case ValidatorOperator::GE:
return "GE";
case ValidatorOperator::LT:
return "LT";
case ValidatorOperator::LE:
return "LE";
default:
DCHECK(false) << "should never reach here";
}
return nullptr;
}
bool SplitValidateRuleString(const base::StringPiece& validate_rule,
ValidatorOperator* operator_,
base::StringPiece* operand) {
if (validate_rule.empty()) {
*operator_ = ValidatorOperator::NOP;
*operand = "";
return true;
}
auto first_space_idx = validate_rule.find_first_of(' ');
auto prefix = validate_rule.substr(0, first_space_idx + 1);
auto rest = validate_rule.substr(first_space_idx + 1);
for (int i = 0; i < static_cast<int>(ValidatorOperator::NUM_OP); i++) {
auto op = static_cast<ValidatorOperator>(i);
if (prefix == GetPrefix(op)) {
*operator_ = op;
if (op != ValidatorOperator::NOP) // NOP shouldn't have operand.
*operand = rest;
return true;
}
}
return false;
}
template <typename ConverterType>
std::unique_ptr<ConverterType> BuildNumericConverter(
const base::StringPiece& validate_rule) {
ValidatorOperator op;
base::StringPiece rest;
if (SplitValidateRuleString(validate_rule, &op, &rest)) {
if (op == ValidatorOperator::NOP)
return std::make_unique<ConverterType>(op, 0);
if (op == ValidatorOperator::EQ || op == ValidatorOperator::NE ||
op == ValidatorOperator::GT || op == ValidatorOperator::GE ||
op == ValidatorOperator::LT || op == ValidatorOperator::LE) {
typename ConverterType::OperandType operand;
if (ConverterType::StringToOperand(rest.as_string(), &operand)) {
return std::make_unique<ConverterType>(op, operand);
} else {
LOG(ERROR) << "Can't convert to operand: " << rest.as_string();
}
}
}
LOG(ERROR) << "Invalid validate rule: " << validate_rule;
return nullptr;
}
template <typename ValueType>
ReturnCode CheckNumber(ValidatorOperator op,
const ValueType lhs,
const ValueType rhs) {
bool is_valid = true;
switch (op) {
case ValidatorOperator::NOP:
break;
case ValidatorOperator::EQ:
is_valid = lhs == rhs;
break;
case ValidatorOperator::GE:
is_valid = lhs >= rhs;
break;
case ValidatorOperator::GT:
is_valid = lhs > rhs;
break;
case ValidatorOperator::LE:
is_valid = lhs <= rhs;
break;
case ValidatorOperator::LT:
is_valid = lhs < rhs;
break;
case ValidatorOperator::NE:
is_valid = lhs != rhs;
break;
default:
return ReturnCode::UNSUPPORTED_OPERATOR;
}
return is_valid ? ReturnCode::OK : ReturnCode::INVALID_VALUE;
}
} // namespace
namespace runtime_probe {
using ReturnCode = FieldConverter::ReturnCode;
std::string StringFieldConverter::ToString() const {
return base::StringPrintf("StringFieldConverter(%s, %s)",
::ToString(operator_), operand_.c_str());
}
std::string IntegerFieldConverter::ToString() const {
return base::StringPrintf("IntegerFieldConverter(%s, %d)",
::ToString(operator_), operand_);
}
std::string HexFieldConverter::ToString() const {
return base::StringPrintf("IntegerFieldConverter(%s, 0x%x)",
::ToString(operator_), operand_);
}
std::string DoubleFieldConverter::ToString() const {
return base::StringPrintf("IntegerFieldConverter(%s, %f)",
::ToString(operator_), operand_);
}
std::unique_ptr<StringFieldConverter> StringFieldConverter::Build(
const base::StringPiece& validate_rule) {
ValidatorOperator op;
base::StringPiece pattern;
if (SplitValidateRuleString(validate_rule, &op, &pattern)) {
if (op == ValidatorOperator::NOP)
return std::make_unique<StringFieldConverter>(op, "");
if (op == ValidatorOperator::EQ || op == ValidatorOperator::NE) {
return std::make_unique<StringFieldConverter>(op, pattern);
}
if (op == ValidatorOperator::RE) {
auto instance = std::make_unique<StringFieldConverter>(op, pattern);
if (instance->regex_->error().empty()) {
// No error, the pattern is valid.
return instance;
}
// Error string is set to non-empty if there are errors.
LOG(ERROR) << "Invalid pattern: " << pattern;
LOG(ERROR) << instance->regex_->error();
}
}
LOG(ERROR) << "Invalid validate rule: " << validate_rule;
return nullptr;
}
std::unique_ptr<IntegerFieldConverter> IntegerFieldConverter::Build(
const base::StringPiece& validate_rule) {
return BuildNumericConverter<IntegerFieldConverter>(validate_rule);
}
std::unique_ptr<HexFieldConverter> HexFieldConverter::Build(
const base::StringPiece& validate_rule) {
return BuildNumericConverter<HexFieldConverter>(validate_rule);
}
std::unique_ptr<DoubleFieldConverter> DoubleFieldConverter::Build(
const base::StringPiece& validate_rule) {
return BuildNumericConverter<DoubleFieldConverter>(validate_rule);
}
ReturnCode StringFieldConverter::Convert(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value = dict_value->FindKey(field_name);
if (!value)
return ReturnCode::FIELD_NOT_FOUND;
switch (value->type()) {
case base::Value::Type::DOUBLE:
dict_value->SetStringKey(field_name, std::to_string(value->GetDouble()));
return ReturnCode::OK;
case base::Value::Type::INTEGER:
dict_value->SetStringKey(field_name, std::to_string(value->GetInt()));
return ReturnCode::OK;
case base::Value::Type::NONE:
dict_value->SetStringKey(field_name, "null");
return ReturnCode::OK;
case base::Value::Type::STRING:
return ReturnCode::OK;
default:
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
ReturnCode IntegerFieldConverter::Convert(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value = dict_value->FindKey(field_name);
if (!value)
return ReturnCode::FIELD_NOT_FOUND;
switch (value->type()) {
case base::Value::Type::DOUBLE:
dict_value->SetIntKey(field_name, static_cast<int>(value->GetDouble()));
return ReturnCode::OK;
case base::Value::Type::INTEGER:
return ReturnCode::OK;
case base::Value::Type::STRING: {
const auto& string_value = value->GetString();
int int_value;
if (StringToInt(string_value, &int_value)) {
dict_value->SetIntKey(field_name, int_value);
return ReturnCode::OK;
} else {
LOG(ERROR) << "Failed to convert '" << string_value << "' to integer.";
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
default:
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
ReturnCode HexFieldConverter::Convert(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value = dict_value->FindKey(field_name);
if (!value)
return ReturnCode::FIELD_NOT_FOUND;
switch (value->type()) {
case base::Value::Type::DOUBLE:
dict_value->SetIntKey(field_name, static_cast<int>(value->GetDouble()));
return ReturnCode::OK;
case base::Value::Type::INTEGER:
return ReturnCode::OK;
case base::Value::Type::STRING: {
const auto& string_value = value->GetString();
int int_value;
if (HexStringToInt(string_value, &int_value)) {
dict_value->SetIntKey(field_name, int_value);
return ReturnCode::OK;
} else {
LOG(ERROR) << "Failed to convert '" << string_value << "' to integer.";
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
default:
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
ReturnCode DoubleFieldConverter::Convert(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value = dict_value->FindKey(field_name);
if (!value)
return ReturnCode::FIELD_NOT_FOUND;
switch (value->type()) {
case base::Value::Type::DOUBLE:
return ReturnCode::OK;
case base::Value::Type::INTEGER:
dict_value->SetDoubleKey(field_name, value->GetDouble());
return ReturnCode::OK;
case base::Value::Type::STRING: {
const auto& string_value = value->GetString();
double double_value;
if (StringToDouble(string_value, &double_value)) {
dict_value->SetDoubleKey(field_name, double_value);
return ReturnCode::OK;
} else {
LOG(ERROR) << "Failed to convert '" << string_value << "' to double.";
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
default:
return ReturnCode::INCOMPATIBLE_VALUE;
}
}
ReturnCode StringFieldConverter::Validate(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value_ = dict_value->FindKey(field_name);
if (!value_)
return ReturnCode::FIELD_NOT_FOUND;
if (!value_->is_string())
return ReturnCode::INCOMPATIBLE_VALUE;
const auto& value = value_->GetString();
bool is_valid = true;
switch (operator_) {
case ValidatorOperator::NOP:
break;
case ValidatorOperator::EQ:
is_valid = value == operand_;
break;
case ValidatorOperator::RE:
is_valid = regex_->FullMatch(value);
break;
case ValidatorOperator::NE:
is_valid = value != operand_;
break;
default:
return ReturnCode::UNSUPPORTED_OPERATOR;
}
return is_valid ? ReturnCode::OK : ReturnCode::INVALID_VALUE;
}
ReturnCode IntegerFieldConverter::Validate(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value_ = dict_value->FindKey(field_name);
if (!value_)
return ReturnCode::FIELD_NOT_FOUND;
if (!value_->is_int())
return ReturnCode::INCOMPATIBLE_VALUE;
const auto value = value_->GetInt();
return CheckNumber(operator_, value, operand_);
}
ReturnCode HexFieldConverter::Validate(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value_ = dict_value->FindKey(field_name);
if (!value_)
return ReturnCode::FIELD_NOT_FOUND;
if (!value_->is_int())
return ReturnCode::INCOMPATIBLE_VALUE;
const auto value = value_->GetInt();
return CheckNumber(operator_, value, operand_);
}
ReturnCode DoubleFieldConverter::Validate(const std::string& field_name,
base::Value* dict_value) const {
CHECK(dict_value);
auto* value_ = dict_value->FindKey(field_name);
if (!value_)
return ReturnCode::FIELD_NOT_FOUND;
if (!value_->is_double() && !value_->is_int())
return ReturnCode::INCOMPATIBLE_VALUE;
const auto value = value_->GetDouble();
return CheckNumber(operator_, value, operand_);
}
std::unique_ptr<ProbeResultChecker> ProbeResultChecker::FromValue(
const base::Value& dict_value) {
std::unique_ptr<ProbeResultChecker> instance{new ProbeResultChecker()};
for (const auto& entry : dict_value.DictItems()) {
const auto& key = entry.first;
const auto& val = entry.second;
auto print_error_and_return = [&val]() {
LOG(ERROR) << "'expect' attribute should be a list whose values are"
<< "[<required:bool>, <expected_type:string>, "
<< "<optional_validate_rule:string>], got: " << val;
return nullptr;
};
const auto& list_value = val.GetList();
if (list_value.size() < 2 || list_value.size() > 3)
return print_error_and_return();
if (!list_value[0].is_bool())
return print_error_and_return();
bool required = list_value[0].GetBool();
auto* target =
required ? &instance->required_fields_ : &instance->optional_fields_;
if (!list_value[1].is_string())
return print_error_and_return();
const auto& expect_type = list_value[1].GetString();
std::string validate_rule;
if (list_value.size() == 3) {
if (!list_value[2].is_string())
return print_error_and_return();
validate_rule = list_value[2].GetString();
}
std::unique_ptr<FieldConverter> converter = nullptr;
if (expect_type == "str") {
converter = StringFieldConverter::Build(validate_rule);
} else if (expect_type == "int") {
converter = IntegerFieldConverter::Build(validate_rule);
} else if (expect_type == "double") {
converter = DoubleFieldConverter::Build(validate_rule);
} else if (expect_type == "hex") {
converter = HexFieldConverter::Build(validate_rule);
}
if (converter == nullptr) {
LOG(ERROR) << "Cannot build converter, 'expect_type': " << expect_type
<< ", 'validate_rule': " << validate_rule;
return nullptr;
} else {
(*target)[key] = std::move(converter);
}
}
return instance;
}
bool ProbeResultChecker::Apply(base::Value* probe_result) const {
bool success = true;
CHECK(probe_result != nullptr);
// Try to convert and validate each required fields.
// Any failures will cause the final result be |false|.
for (const auto& entry : required_fields_) {
const auto& key = entry.first;
const auto& converter = entry.second;
if (!probe_result->FindKey(key)) {
LOG(ERROR) << "Missing key: " << key;
success = false;
break;
}
auto return_code = converter->Convert(key, probe_result);
if (return_code != ReturnCode::OK) {
auto* value = probe_result->FindKey(key);
LOG(ERROR) << "Failed to apply " << converter->ToString() << " on "
<< *value << "(ReturnCode = " << static_cast<int>(return_code)
<< ")";
success = false;
break;
}
}
// |ProbeStatement| will remove this element from final results, there is no
// need to continue.
if (!success) {
VLOG(3) << "probe_result = " << *probe_result;
return false;
}
// Try to convert and validate each optional fields.
// For failures, just remove them from probe_result and continue.
for (const auto& entry : optional_fields_) {
const auto& key = entry.first;
const auto& converter = entry.second;
if (!probe_result->FindKey(key))
continue;
auto return_code = converter->Convert(key, probe_result);
if (return_code != ReturnCode::OK) {
VLOG(1) << "Optional field '" << key << "' has unexpected value, "
<< "remove it from probe result.";
probe_result->RemoveKey(key);
}
}
// Now all fields should have the correct type, let's validate them.
for (const auto& entry : required_fields_) {
auto return_code = entry.second->Validate(entry.first, probe_result);
if (return_code != ReturnCode::OK) {
success = false;
break;
}
}
// Optional fields shouldn't have expect value.
return success;
}
} // namespace runtime_probe