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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Test support library for request payloads.
#include "chrome/browser/policy/messaging_layer/util/test_request_payload.h"
#include <algorithm>
#include <string>
#include <string_view>
#include "base/json/json_reader.h"
#include "base/strings/string_util.h"
#include "chrome/browser/policy/messaging_layer/upload/encrypted_reporting_client.h"
#include "chrome/browser/policy/messaging_layer/upload/record_upload_request_builder.h"
#include "components/reporting/util/encrypted_reporting_json_keys.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
namespace reporting {
// Return true if s a properly formatted positive integer, i.e., is not empty,
// contains digits only and does not start with 0.
static bool IsPositiveInteger(std::string_view s) {
if (s.empty()) {
return false;
}
if (s.size() == 1) {
return absl::ascii_isdigit(static_cast<unsigned char>(s[0]));
}
return s[0] != '0' && s.find_first_not_of("0123456789") == std::string::npos;
}
// Get the record list. If it can't, print the message to listener and return a
// null pointer.
static const base::Value::List* GetRecordList(const base::Value::Dict& arg,
MatchResultListener* listener) {
const auto* const record_list = arg.FindList(json_keys::kEncryptedRecordList);
if (record_list == nullptr) {
*listener << "No key named \"encryptedRecord\" in the argument or the "
"value is not a list.";
return nullptr;
}
return record_list;
}
bool AttachEncryptionSettingsMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
const auto attach_encryption_settings =
arg.FindBool(json_keys::kAttachEncryptionSettings);
if (!attach_encryption_settings) {
*listener << "No key named \"attachEncryptionSettings\" in the argument or "
"the value is not of bool type.";
return false;
}
if (!attach_encryption_settings.value()) {
*listener << "The value of \"attachEncryptionSettings\" is false.";
return false;
}
return true;
}
void AttachEncryptionSettingsMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid attachEncryptionSettings field.";
}
void AttachEncryptionSettingsMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "has an invalid attachEncryptionSettings field.";
}
std::string AttachEncryptionSettingsMatcher::Name() const {
return "attach-encryption-settings-matcher";
}
bool NoAttachEncryptionSettingsMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
if (arg.Find(json_keys::kAttachEncryptionSettings) != nullptr) {
*listener << "Found \"attachEncryptionSettings\" in the argument.";
return false;
}
return true;
}
void NoAttachEncryptionSettingsMatcher::DescribeTo(std::ostream* os) const {
*os << "expectedly has no attachEncryptionSettings field.";
}
void NoAttachEncryptionSettingsMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "unexpectedly has an attachEncryptionSettings field.";
}
std::string NoAttachEncryptionSettingsMatcher::Name() const {
return "no-attach-encryption-settings-matcher";
}
bool ConfigurationFileVersionMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
auto* attach_configuration_file =
arg.Find(json_keys::kConfigurationFileVersion);
if (!attach_configuration_file->GetIfInt().has_value()) {
*listener << "No key named \"configurationFileVersion\" in the argument or "
"the value is not of int type.";
return false;
}
return true;
}
void ConfigurationFileVersionMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid configurationFileVersion field.";
}
void ConfigurationFileVersionMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "has an invalid configurationFileVersion field.";
}
std::string ConfigurationFileVersionMatcher::Name() const {
return "configuration-file-version-matcher";
}
bool NoConfigurationFileVersionMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
if (arg.Find(json_keys::kConfigurationFileVersion) != nullptr) {
*listener << "Found \"configurationFileVersion\" in the argument.";
return false;
}
return true;
}
void NoConfigurationFileVersionMatcher::DescribeTo(std::ostream* os) const {
*os << "expectedly has no configurationFileVersion field.";
}
void NoConfigurationFileVersionMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "unexpectedly has an configurationFileVersion field.";
}
std::string NoConfigurationFileVersionMatcher::Name() const {
return "no-configuration-file-version-matcher";
}
bool SourceMatcher::MatchAndExplain(const base::Value::Dict& arg,
MatchResultListener* listener) const {
if (arg.FindString(json_keys::kSource) == nullptr) {
*listener << "No key named \"source\" or the value "
"is not a string in the argument.";
return false;
}
return true;
}
void SourceMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid source field.";
}
void SourceMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "has an invalid source field.";
}
std::string SourceMatcher::Name() const {
return "source-test-matcher";
}
bool NoSourceMatcher::MatchAndExplain(const base::Value::Dict& arg,
MatchResultListener* listener) const {
if (arg.Find(json_keys::kSource) != nullptr) {
*listener << "Found \"source\" in the argument.";
return false;
}
return true;
}
void NoSourceMatcher::DescribeTo(std::ostream* os) const {
*os << "expectedly has no source field.";
}
void NoSourceMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "unexpectedly has an Source field.";
}
std::string NoSourceMatcher::Name() const {
return "source-test-matcher";
}
void CompressionInformationMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid compression information field.";
}
void CompressionInformationMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "has an invalid compression information field.";
}
std::string CompressionInformationMatcher::Name() const {
return "encrypted-record-matcher";
}
bool EncryptedRecordMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
const auto* const record_list = GetRecordList(arg, listener);
return record_list != nullptr;
}
void EncryptedRecordMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid encryptedRecord field.";
}
void EncryptedRecordMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "has an invalid encryptedRecord field.";
}
std::string EncryptedRecordMatcher::Name() const {
return "encrypted-record-matcher";
}
bool RequestIdMatcher::MatchAndExplain(const base::Value::Dict& arg,
MatchResultListener* listener) const {
const auto* const request_id = arg.FindString(json_keys::kRequestId);
if (request_id == nullptr) {
*listener << "No key named \"requestId\" in the argument or the value "
"is not a string.";
return false;
}
if (request_id->empty()) {
*listener << "Request ID is empty.";
return false;
}
if (!std::ranges::all_of(*request_id, base::IsHexDigit<char>)) {
*listener << "Request ID is not a hexadecimal number.";
return false;
}
return true;
}
void RequestIdMatcher::DescribeTo(std::ostream* os) const {
*os << "has a valid request ID.";
}
void RequestIdMatcher::DescribeNegationTo(std::ostream* os) const {
*os << "has an invalid request ID.";
}
std::string RequestIdMatcher::Name() const {
return "request-id-matcher";
}
bool RecordMatcher::MatchAndExplain(const base::Value::Dict& arg,
MatchResultListener* listener) const {
switch (mode_) {
case Mode::FullRequest: {
const auto* record_list = GetRecordList(arg, listener);
if (record_list == nullptr) {
return false;
}
for (const auto& record : *record_list) {
const auto* record_dict = record.GetIfDict();
if (record_dict == nullptr) {
*listener << "Record " << record << " is not a dict.";
return false;
}
if (!this->MatchAndExplainRecord(*record_dict, listener)) {
return false;
}
}
return true;
}
case Mode::RecordOnly: {
return this->MatchAndExplainRecord(arg, listener);
}
default: {
// Should never reach here.
*listener << "Invalid mode of RecordMatcher encountered: "
<< static_cast<std::underlying_type_t<Mode>>(mode_);
return false;
}
}
}
RecordMatcher& RecordMatcher::SetMode(RecordMatcher::Mode mode) {
mode_ = mode;
return *this;
}
bool CompressionInformationMatcher::MatchAndExplainRecord(
const base::Value::Dict& record,
MatchResultListener* listener) const {
const auto* const compression_info =
record.FindDict(json_keys::kCompressionInformation);
if (compression_info == nullptr) {
*listener << "No key named \"compressionInformation\" or the value is "
"not a dict in record "
<< record << '.';
return false;
}
const auto compression_algorithm =
compression_info->FindInt(json_keys::kCompressionAlgorithm);
if (!compression_algorithm.has_value()) {
*listener << "No key named \"compressionAlgorithm\" under "
"compressionInformation "
"or the value is not an integer in record "
<< record << '.';
return false;
}
if (compression_algorithm.value() > 1 || compression_algorithm < 0) {
*listener << "The value of \"compressionInformation/compressionAlgorithm\" "
"is neither 0 nor 1 in record "
<< record << '.';
return false;
}
return true;
}
bool EncryptedWrappedRecordRecordMatcher::MatchAndExplainRecord(
const base::Value::Dict& record,
MatchResultListener* listener) const {
if (record.FindString(json_keys::kEncryptedWrappedRecord) == nullptr) {
*listener << "No key named \"encryptedWrappedRecord\" or the value "
"is not a string in record "
<< record << '.';
return false;
}
return true;
}
void EncryptedWrappedRecordRecordMatcher::DescribeTo(std::ostream* os) const {
*os << "has valid encrypted wrapped records.";
}
void EncryptedWrappedRecordRecordMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "has at least one invalid encrypted wrapped records or has missing "
"encrypted wrapped records.";
}
std::string EncryptedWrappedRecordRecordMatcher::Name() const {
return "encrypted-wrapped-record-record-matcher";
}
bool NoEncryptedWrappedRecordRecordMatcher::MatchAndExplainRecord(
const base::Value::Dict& record,
MatchResultListener* listener) const {
if (record.Find(json_keys::kEncryptedWrappedRecord) != nullptr) {
*listener << "Found \"encryptedWrappedRecord\" in record " << record << '.';
return false;
}
return true;
}
void NoEncryptedWrappedRecordRecordMatcher::DescribeTo(std::ostream* os) const {
*os << "expectedly has no encrypted wrapped record.";
}
void NoEncryptedWrappedRecordRecordMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "has at least one encrypted wrapped record that it should not have.";
}
std::string NoEncryptedWrappedRecordRecordMatcher::Name() const {
return "no-encrypted-wrapped-record-record-matcher";
}
bool SequenceInformationRecordMatcher::MatchAndExplainRecord(
const base::Value::Dict& record,
MatchResultListener* listener) const {
const auto* const sequence_information =
record.FindDict(json_keys::kSequenceInformation);
if (sequence_information == nullptr) {
*listener << "No key named \"sequenceInformation\" or the value is "
"not a dict in record "
<< record << '.';
return false;
}
if (!sequence_information->FindInt(json_keys::kPriority).has_value()) {
*listener << "No key named \"sequenceInformation/priority\" or the "
"value is not an integer in record "
<< record << '.';
return false;
}
for (const char* id : {json_keys::kSequencingId, json_keys::kGenerationId}) {
const auto* const id_val = sequence_information->FindString(id);
if (id_val == nullptr) {
*listener << "No key named \"sequenceInformation/" << id
<< "\" or the value is not a string in record " << record
<< '.';
return false;
}
if (!IsPositiveInteger(*id_val)) {
*listener << "The value of \"sequenceInformation/" << id
<< "\" is not a properly formatted positive integer in record "
<< record << '.';
return false;
}
}
#if BUILDFLAG(IS_CHROMEOS)
if (EncryptedReportingClient::GenerationGuidIsRequired()) {
const auto* generation_guid =
sequence_information->FindString(json_keys::kGenerationGuid);
if ((!generation_guid || generation_guid->empty())) {
*listener << "No key named \"sequenceInformation/generationGuid\" or the "
"value is not a string in record "
<< record << '.';
return false;
}
}
#endif // BUILDFLAG(IS_CHROMEOS)
return true;
}
void SequenceInformationRecordMatcher::DescribeTo(std::ostream* os) const {
*os << "has valid sequence information.";
}
void SequenceInformationRecordMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "has invalid sequence information.";
}
std::string SequenceInformationRecordMatcher::Name() const {
return "sequence-information-record-matcher";
}
bool RequestContainingRecordMatcher::IsSubDict(const base::Value::Dict& sub,
const base::Value::Dict& super) {
for (auto&& [key, sub_value] : sub) {
const auto* super_value = super.Find(key);
if (super_value == nullptr || *super_value != sub_value) {
return false;
}
}
return true;
}
RequestContainingRecordMatcher::RequestContainingRecordMatcher(
std::string_view matched_record_json)
: matched_record_json_(matched_record_json) {}
bool RequestContainingRecordMatcher::MatchAndExplain(
const base::Value::Dict& arg,
MatchResultListener* listener) const {
const auto* record_list = GetRecordList(arg, listener);
if (record_list == nullptr) {
return false;
}
const auto matched_record = base::JSONReader::Read(matched_record_json_);
if (!matched_record.has_value()) {
*listener << "The specified record cannot be parsed as a JSON object.";
return false;
}
const auto* matched_record_dict = matched_record->GetIfDict();
if (matched_record_dict == nullptr) {
*listener
<< "The specified record must be a Dict itself because each record "
"is a Dict.";
return false;
}
for (const auto& record : *record_list) {
const auto* record_dict = record.GetIfDict();
if (!record_dict) {
continue;
}
// Match each key and value of matched_record with those of the iterated
// record_dict. In this way, users can specify part of a record instead
// of the full record.
if (IsSubDict(*matched_record_dict, *record_dict)) {
return true;
}
}
*listener << "The specified record is not found in the argument.";
return false;
}
void RequestContainingRecordMatcher::DescribeTo(std::ostream* os) const {
*os << "contains the specified record.";
}
void RequestContainingRecordMatcher::DescribeNegationTo(
std::ostream* os) const {
*os << "does not contain the specified record or there are other failed "
"conditions.";
}
} // namespace reporting
|