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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/autofill/automated_tests/cache_replayer.h"
#include <memory>
#include <optional>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_writer.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/proto/api_v1.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/google/compression_utils.h"
namespace autofill {
namespace test {
namespace {
// Only run these tests on Linux because there are issues with other platforms.
// Testing on one platform gives enough confidence.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
using base::JSONWriter;
using base::Value;
// Request Response Pair for the API server
using RequestResponsePair =
std::pair<AutofillPageQueryRequest, AutofillQueryResponse>;
constexpr char kTestHTTPResponseHeader[] = "Fake HTTP Response Header";
constexpr char kHTTPBodySep[] = "\r\n\r\n";
// The host name of the autofill server.
constexpr char kHostname[] = "content-autofill.googleapis.com";
struct LightField {
uint32_t signature;
uint32_t prediction;
};
struct LightForm {
uint64_t signature;
std::vector<LightField> fields;
};
std::string CreateQueryUrl(const std::string& base64_encoded_query) {
constexpr char base_url[] =
"https://content-autofill.googleapis.com/v1/pages:get";
if (base64_encoded_query.empty())
return base_url;
return base::StrCat({base_url, "/", base64_encoded_query});
}
bool GetServerResponseForQuery(const ServerCacheReplayer& cache_replayer,
const AutofillPageQueryRequest& query,
std::string* http_text) {
return cache_replayer.GetApiServerResponseForQuery(query, http_text);
}
RequestResponsePair MakeQueryRequestResponsePair(
const std::vector<LightForm>& forms) {
AutofillPageQueryRequest query;
AutofillQueryResponse response;
for (const auto& form : forms) {
auto* query_form = query.add_forms();
query_form->set_signature(form.signature);
auto* response_form = response.add_form_suggestions();
for (const auto& field : form.fields) {
query_form->add_fields()->set_signature(field.signature);
auto* response_field = response_form->add_field_suggestions();
response_field->set_field_signature(field.signature);
response_field->add_predictions()->set_type(field.prediction);
}
}
return RequestResponsePair({std::move(query), std::move(response)});
}
// Returns a query request URL. If |query| is not empty, the corresponding
// query is encoded into the URL.
bool MakeQueryRequestURL(std::optional<AutofillPageQueryRequest> query,
std::string* request_url) {
if (!query.has_value()) {
*request_url = CreateQueryUrl("");
return true;
}
std::string serialized_query;
if (!(*query).SerializeToString(&serialized_query)) {
VLOG(1) << "could not serialize Query proto";
return false;
}
*request_url = CreateQueryUrl(base::Base64Encode(serialized_query));
return true;
}
// Make HTTP request header given |url|.
inline std::string MakeRequestHeader(std::string_view url) {
return base::StrCat({"GET ", url, " ", "HTTP/1.1"});
}
// Makes string value for "SerializedRequest" json node that contains HTTP
// request content.
bool MakeSerializedRequest(const AutofillPageQueryRequest& query,
RequestType type,
std::string* serialized_request,
std::string* request_url) {
// Make body and query content for URL depending on the |type|.
std::string body;
std::optional<AutofillPageQueryRequest> query_for_url;
if (type == RequestType::kQueryProtoGET) {
query_for_url = std::move(query);
} else {
std::string serialized_query;
query.SerializeToString(&serialized_query);
// Wrap query payload in a request proto to interface with API Query method.
AutofillPageResourceQueryRequest request;
request.set_serialized_request(base::Base64Encode(serialized_query));
request.SerializeToString(&body);
query_for_url = std::nullopt;
}
// Make header according to query content for URL.
std::string url;
if (!MakeQueryRequestURL(std::move(query_for_url), &url)) {
return false;
}
*request_url = url;
std::string header = MakeRequestHeader(url);
// Fill HTTP text.
std::string http_text =
base::JoinString(std::vector<std::string>{header, body}, kHTTPBodySep);
*serialized_request = base::Base64Encode(http_text);
return true;
}
std::string MakeSerializedResponse(
const AutofillQueryResponse& query_response) {
std::string serialized_response;
query_response.SerializeToString(&serialized_response);
// The Api Environment expects the response body to be base64 encoded.
serialized_response = base::Base64Encode(serialized_response);
std::string compressed_query;
compression::GzipCompress(serialized_response, &compressed_query);
// TODO(vincb): Put a real header here.
std::string http_text = base::JoinString(
std::vector<std::string>{kTestHTTPResponseHeader, compressed_query},
kHTTPBodySep);
return base::Base64Encode(http_text);
}
// Write json node to file in text format.
bool WriteJSONNode(const base::FilePath& file_path, const base::Value& node) {
std::string json_text;
JSONWriter::WriteWithOptions(node, JSONWriter::OPTIONS_PRETTY_PRINT,
&json_text);
std::string compressed_json_text;
if (!compression::GzipCompress(json_text, &compressed_json_text)) {
VLOG(1) << "Cannot compress json to gzip.";
return false;
}
if (!base::WriteFile(file_path, compressed_json_text)) {
VLOG(1) << "Could not write json at file: " << file_path;
return false;
}
return true;
}
// Write cache to file in json text format.
bool WriteJSON(const base::FilePath& file_path,
const std::vector<RequestResponsePair>& request_response_pairs,
RequestType request_type = RequestType::kQueryProtoPOST) {
// Make json list node that contains all query requests.
base::Value::Dict urls_dict;
for (const auto& request_response_pair : request_response_pairs) {
std::string serialized_request;
std::string url;
if (!MakeSerializedRequest(request_response_pair.first, request_type,
&serialized_request, &url)) {
return false;
}
Value::Dict request_response_node;
request_response_node.Set("SerializedRequest",
std::move(serialized_request));
request_response_node.Set(
"SerializedResponse",
MakeSerializedResponse(request_response_pair.second));
// Populate json dict node that contains Autofill Server requests per URL.
// This will construct an empty list for `url` if it didn't exist already.
if (!urls_dict.contains(url))
urls_dict.Set(url, base::Value::List());
urls_dict.FindList(url)->Append(std::move(request_response_node));
}
// Make json dict node that contains requests per domain.
base::Value::Dict domains_dict;
domains_dict.Set(kHostname, base::Value(std::move(urls_dict)));
// Make json root dict.
base::Value::Dict root_dict;
root_dict.Set("Requests", std::move(domains_dict));
// Write content to JSON file.
return WriteJSONNode(file_path, Value(std::move(root_dict)));
}
// TODO(crbug.com/40768066): The test flakily times out.
TEST(AutofillCacheReplayerDeathTest,
DISABLED_ServerCacheReplayerConstructor_CrashesWhenNoDomainNode) {
// Make death test threadsafe.
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// JSON structure is not right.
const std::string invalid_json = "{\"NoDomainNode\": \"invalid_field\"}";
// Write json to file.
ASSERT_TRUE(base::WriteFile(file_path, invalid_json))
<< "there was an error when writing content to json file: " << file_path;
// Crash since json content is invalid.
ASSERT_DEATH_IF_SUPPORTED(
ServerCacheReplayer(file_path,
ServerCacheReplayer::kOptionFailOnInvalidJsonRecord),
".*");
}
TEST(AutofillCacheReplayerDeathTest,
ServerCacheReplayerConstructor_CrashesWhenNoQueryNodesAndFailOnEmpty) {
// Make death test threadsafe.
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// Make empty request/response pairs to write in cache.
std::vector<RequestResponsePair> request_response_pairs;
// Write cache to json and create replayer.
ASSERT_TRUE(WriteJSON(file_path, request_response_pairs));
// Crash since there are no Query nodes and set to fail on empty.
ASSERT_DEATH_IF_SUPPORTED(
ServerCacheReplayer(file_path,
ServerCacheReplayer::kOptionFailOnInvalidJsonRecord |
ServerCacheReplayer::kOptionFailOnEmpty),
".*");
}
// Test suite for GET Query death test.
class AutofillCacheReplayerGETQueryDeathTest
: public testing::TestWithParam<std::string> {};
TEST_P(
AutofillCacheReplayerGETQueryDeathTest,
ApiServerCacheReplayerConstructor_CrashesWhenInvalidRequestURLForGETQuery) {
// Parameterized death test for populating cache when keys that are obtained
// from the URL's query parameter are invalid.
// Make death test threadsafe.
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// Make JSON content.
// Make json list node that contains the problematic query request.
Value::Dict request_response_node;
// Put some textual content for HTTP request. Content does not matter because
// the Query content will be parsed from the URL that corresponds to the
// dictionary key.
request_response_node.Set(
"SerializedRequest", base::StrCat({"GET ", CreateQueryUrl("1234").c_str(),
" HTTP/1.1\r\n\r\n"}));
request_response_node.Set("SerializedResponse",
MakeSerializedResponse(AutofillQueryResponse()));
base::Value::List url_list;
url_list.Append(std::move(request_response_node));
// Populate json dict node that contains Autofill Server requests per URL.
base::Value::Dict urls_dict;
// The query parameter in the URL cannot be parsed to a proto because
// parameter value is in invalid format.
urls_dict.Set(CreateQueryUrl(GetParam()), std::move(url_list));
// Make json dict node that contains requests per domain.
base::Value::Dict domains_dict;
domains_dict.Set(kHostname, std::move(urls_dict));
// Make json root dict.
base::Value::Dict root_dict;
root_dict.Set("Requests", std::move(domains_dict));
// Write content to JSON file.
ASSERT_TRUE(WriteJSONNode(file_path, Value(std::move(root_dict))));
// Make death assertion.
// Crash since request cannot be parsed to a proto.
ASSERT_DEATH_IF_SUPPORTED(
ServerCacheReplayer(file_path,
ServerCacheReplayer::kOptionFailOnInvalidJsonRecord),
".*");
}
INSTANTIATE_TEST_SUITE_P(
GetQueryParameterizedDeathTest,
AutofillCacheReplayerGETQueryDeathTest,
testing::Values( // Can be base-64 decoded, but not parsed to proto.
"1234",
// Cannot be base-64 decoded.
"^^^"));
TEST(AutofillCacheReplayerTest,
CanUseReplayerWhenNoCacheContentWithNotFailOnEmpty) {
// Make death test threadsafe.
GTEST_FLAG_SET(death_test_style, "threadsafe");
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// Make empty request/response pairs to write in cache.
std::vector<RequestResponsePair> request_response_pairs;
// Write cache to json and create replayer.
ASSERT_TRUE(WriteJSON(file_path, request_response_pairs));
// Should not crash even if no cache because kOptionFailOnEmpty is not
// flipped.
ServerCacheReplayer cache_replayer(
file_path, ServerCacheReplayer::kOptionFailOnInvalidJsonRecord &
(ServerCacheReplayer::kOptionFailOnEmpty & 0));
// Should be able to read cache, which will give nothing.
std::string http_text;
AutofillPageQueryRequest query_with_no_match;
EXPECT_FALSE(GetServerResponseForQuery(cache_replayer, query_with_no_match,
&http_text));
}
template <typename U, typename V>
bool ProtobufsEqual(const U& u, const V& v) {
// Unfortunately, Chrome uses MessageLite, so we cannot use DebugString or the
// MessageDifferencer.
std::string u_serialized, v_serialized;
u.SerializeToString(&u_serialized);
v.SerializeToString(&v_serialized);
if (u_serialized != v_serialized) {
LOG(ERROR) << "Expected protobufs to be equal:\n" << u << "and:\n" << v;
LOG(ERROR) << "Note that this output is based on custom written string "
"serializers and the protobufs may be different in ways that "
"are not shown here.";
}
return u_serialized == v_serialized;
}
// Test suite for Query response retrieval test.
class AutofillCacheReplayerGetResponseForQueryTest
: public testing::TestWithParam<RequestType> {};
TEST_P(AutofillCacheReplayerGetResponseForQueryTest,
FillsResponseWhenNoErrors) {
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// Make request/response pairs to write in cache.
std::vector<RequestResponsePair> request_response_pairs;
{
LightForm form_to_add;
form_to_add.signature = 1234;
form_to_add.fields = {LightField{1234, 1}};
request_response_pairs.push_back(
MakeQueryRequestResponsePair({form_to_add}));
}
// Write cache to json.
ASSERT_TRUE(WriteJSON(file_path, request_response_pairs, GetParam()));
ServerCacheReplayer cache_replayer(
file_path, ServerCacheReplayer::kOptionFailOnInvalidJsonRecord &
ServerCacheReplayer::kOptionFailOnEmpty);
// Verify if we can get cached response.
std::string http_text_response;
ASSERT_TRUE(GetServerResponseForQuery(
cache_replayer, request_response_pairs[0].first, &http_text_response));
std::string body = SplitHTTP(http_text_response).second;
// The Api Environment expects the response to be base64 encoded.
std::string tmp;
ASSERT_TRUE(base::Base64Decode(body, &tmp));
body = tmp;
AutofillQueryResponse response_from_cache;
ASSERT_TRUE(response_from_cache.ParseFromString(body));
}
INSTANTIATE_TEST_SUITE_P(GetResponseForQueryParameterizeTest,
AutofillCacheReplayerGetResponseForQueryTest,
testing::Values(
// Read Query content from URL "q" param.
RequestType::kQueryProtoGET,
// Read Query content from HTTP body.
RequestType::kQueryProtoPOST));
TEST(AutofillCacheReplayerTest, GetResponseForQueryGivesFalseWhenNullptr) {
ServerCacheReplayer cache_replayer(ServerCache{{}});
EXPECT_FALSE(GetServerResponseForQuery(cache_replayer,
AutofillPageQueryRequest(), nullptr));
}
TEST(AutofillCacheReplayerTest, GetResponseForQueryGivesFalseWhenNoKeyMatch) {
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
// Make request/response pairs to write in cache.
std::vector<RequestResponsePair> request_response_pairs;
{
LightForm form_to_add;
form_to_add.signature = 1234;
form_to_add.fields = {LightField{1234, 1}};
request_response_pairs.push_back(
MakeQueryRequestResponsePair({form_to_add}));
}
// Write cache to json and create replayer.
ASSERT_TRUE(WriteJSON(file_path, request_response_pairs));
ServerCacheReplayer cache_replayer(
file_path, ServerCacheReplayer::kOptionFailOnInvalidJsonRecord &
ServerCacheReplayer::kOptionFailOnEmpty);
// Verify if we get false when there is no cache for the query.
std::string http_text;
AutofillPageQueryRequest query_with_no_match;
EXPECT_FALSE(GetServerResponseForQuery(cache_replayer, query_with_no_match,
&http_text));
}
TEST(AutofillCacheReplayerTest,
GetResponseForQueryGivesFalseWhenDecompressFailsBecauseInvalidHTTP) {
// Make query request and key.
LightForm form_to_add;
form_to_add.signature = 1234;
form_to_add.fields = {LightField{1234, 1}};
const AutofillPageQueryRequest query_request_for_key =
MakeQueryRequestResponsePair({form_to_add}).first;
const std::string key = GetKeyFromQuery(query_request_for_key);
const char invalid_http[] = "Dumb Nonsense That Doesn't Have a HTTP Header";
ServerCacheReplayer cache_replayer(ServerCache{{key, invalid_http}});
// Verify if we get false when invalid HTTP response to decompress.
std::string response_http_text;
EXPECT_FALSE(GetServerResponseForQuery(cache_replayer, query_request_for_key,
&response_http_text));
}
TEST(AutofillCacheReplayerTest,
GetResponseForQueryGivesTrueWhenDecompressSucceededBecauseEmptyBody) {
// Make query request and key.
LightForm form_to_add;
form_to_add.signature = 1234;
form_to_add.fields = {LightField{1234, 1}};
const AutofillPageQueryRequest query_request_for_key =
MakeQueryRequestResponsePair({form_to_add}).first;
const std::string key = GetKeyFromQuery(query_request_for_key);
const char http_without_body[] = "Test HTTP Header\r\n\r\n";
ServerCacheReplayer cache_replayer(ServerCache{{key, http_without_body}});
// Verify if we get true when no HTTP body.
std::string response_http_text;
EXPECT_TRUE(GetServerResponseForQuery(cache_replayer, query_request_for_key,
&response_http_text));
}
// Returns whether the forms in |response| and |forms| match. If both contain
// the same number of forms, a boolean is appended to the output for each form
// indicating whether the expectation and actual form matched. In case of
// gross mismatch, the function may return an empty vector.
std::vector<bool> DoFormsMatch(const AutofillQueryResponse& response,
const std::vector<LightForm>& forms) {
std::vector<bool> found;
for (int i = 0; i < std::min(static_cast<int>(forms.size()),
response.form_suggestions_size());
++i) {
const auto& expected_form = forms[i];
const auto& response_form = response.form_suggestions(i);
if (static_cast<int>(expected_form.fields.size()) !=
response_form.field_suggestions_size()) {
LOG(ERROR) << "Expected form " << i << " to have suggestions for "
<< expected_form.fields.size() << " fields but got "
<< response_form.field_suggestions_size();
found.push_back(false);
continue;
}
bool found_all_fields = true;
for (size_t j = 0; j < expected_form.fields.size(); ++j) {
const auto& expected_field = expected_form.fields[j];
if (expected_field.signature !=
response_form.field_suggestions(j).field_signature()) {
LOG(ERROR) << "Expected field " << j << " of form " << i
<< " to have signature " << expected_field.signature
<< " but got "
<< response_form.field_suggestions(j).field_signature();
found_all_fields = false;
}
if (expected_field.prediction !=
static_cast<unsigned int>(
response_form.field_suggestions(j).predictions(0).type())) {
LOG(ERROR) << "Expected field " << j << " of form " << i
<< " to have primary type prediction "
<< expected_field.prediction << " but got "
<< response_form.field_suggestions(j).predictions(0).type();
found_all_fields = false;
}
}
found.push_back(found_all_fields);
}
return found;
}
std::vector<bool> CheckFormsInCache(const ServerCacheReplayer& cache_replayer,
const std::vector<LightForm>& forms) {
RequestResponsePair request_response_pair =
MakeQueryRequestResponsePair(forms);
std::string http_text;
if (!GetServerResponseForQuery(cache_replayer, request_response_pair.first,
&http_text)) {
VLOG(1) << "Server did not respond to the query.";
return std::vector<bool>();
}
std::string body = SplitHTTP(http_text).second;
// The Api Environment expects the response to be base64 encoded.
std::string tmp;
if (!base::Base64Decode(body, &tmp)) {
LOG(ERROR) << "Unable to base64 decode contents" << body;
return std::vector<bool>();
}
body = tmp;
AutofillQueryResponse response;
CHECK(response.ParseFromString(body)) << body;
return DoFormsMatch(response, forms);
}
TEST(AutofillCacheReplayerTest, CrossEnvironmentIntegrationTest) {
// Make writable file path.
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_path =
temp_dir.GetPath().AppendASCII("test_wpr_capture.json");
LightForm form1;
form1.signature = 1111;
form1.fields = {LightField{1111, 1}, LightField{1112, 31},
LightField{1113, 33}};
LightForm form2;
form2.signature = 2222;
form2.fields = {LightField{2221, 2}};
LightForm form3;
form3.signature = 3333;
form3.fields = {LightField{3331, 3}};
LightForm form4;
form4.signature = 4444;
form4.fields = {LightField{4441, 4}};
LightForm form5;
form5.signature = 5555;
form5.fields = {LightField{5551, 42}};
// Make request/response pairs to write in cache.
std::vector<RequestResponsePair> request_response_pairs;
request_response_pairs.push_back(
MakeQueryRequestResponsePair({form1, form2}));
request_response_pairs.push_back(
MakeQueryRequestResponsePair({form3, form4}));
// Write cache to json and create replayer.
ASSERT_TRUE(WriteJSON(file_path, request_response_pairs));
ServerCacheReplayer cache_replayer(
file_path, ServerCacheReplayer::kOptionFailOnInvalidJsonRecord &
ServerCacheReplayer::kOptionFailOnEmpty);
std::string http_text;
// First, check the exact same key combos we sent properly respond
EXPECT_EQ(std::vector<bool>({true, true}),
CheckFormsInCache(cache_replayer, {form1, form2}));
EXPECT_EQ(std::vector<bool>({true, true}),
CheckFormsInCache(cache_replayer, {form3, form4}));
// Existing keys that were requested in a different combination are not
// processed.
EXPECT_EQ(std::vector<bool>(),
CheckFormsInCache(cache_replayer, {form1, form3}));
EXPECT_EQ(std::vector<bool>(), CheckFormsInCache(cache_replayer, {form1}));
// Not in the cache.
EXPECT_EQ(std::vector<bool>(), CheckFormsInCache(cache_replayer, {form5}));
// Now, load the same thing into the cache replayer with
// ServerCacheReplayer::kOptionSplitRequestsByForm set and expect matches
// for all combos
ServerCacheReplayer form_split_cache_replayer(
file_path, ServerCacheReplayer::kOptionSplitRequestsByForm);
// First, check the exact same key combos we sent properly respond
EXPECT_EQ(std::vector<bool>({true, true}),
CheckFormsInCache(form_split_cache_replayer, {form1, form2}));
EXPECT_EQ(std::vector<bool>({true, true}),
CheckFormsInCache(form_split_cache_replayer, {form3, form4}));
// Existing keys that were requested in a different combination are not
// processed.
EXPECT_EQ(std::vector<bool>({true, true}),
CheckFormsInCache(form_split_cache_replayer, {form1, form3}));
EXPECT_EQ(std::vector<bool>({true}),
CheckFormsInCache(form_split_cache_replayer, {form1}));
// Not in the cache.
EXPECT_EQ(std::vector<bool>(),
CheckFormsInCache(form_split_cache_replayer, {form5}));
}
#endif // if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
} // namespace
} // namespace test
} // namespace autofill
|