Commit 4e636d5e authored by Vincent Boisselle's avatar Vincent Boisselle Committed by Commit Bot

Changed the way the Query URL is matched in the JSON file and re-added unittests

(only for Linux)

Bug: 954379
Change-Id: I0fa6d80dd56cc239ed2eaa554c89b0700950acf6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1574899
Commit-Queue: Vincent Boisselle <vincb@google.com>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656495}
parent 9a0bfb19
......@@ -153,6 +153,24 @@ bool PopulateCacheFromJsonRequestsNode(const base::Value& requests_node,
return true;
}
// TODO(crbug/958125): Add the possibility to retrieve nodes with different
// Query URLs.
// Finds the Autofill server Query node in dictionary node. Gives nullptr if
// cannot find the node or |domain_dict| is invalid.
const base::Value* FindAutofillQueryNodeInDomainDict(
const base::Value& domain_dict) {
if (!domain_dict.is_dict()) {
return nullptr;
}
for (const auto& pair : domain_dict.DictItems()) {
if (pair.first.find("https://clients1.google.com/tbproxy/af/query") !=
std::string::npos) {
return &pair.second;
}
}
return nullptr;
}
// Populates the cache mapping request keys to their corresponding compressed
// response.
bool PopulateCacheFromJSONFile(const base::FilePath& json_file_path,
......@@ -194,11 +212,11 @@ bool PopulateCacheFromJSONFile(const base::FilePath& json_file_path,
root_node = std::move(value_with_error.value.value());
}
// TODO(crbug/958136): It should tolerate a cache with no Queries.
// Get requests node and populate the cache.
{
const base::Value* requests_node =
root_node.FindPath({"Requests", "clients1.google.com",
"https://clients1.google.com/tbproxy/af/query?"});
const base::Value* requests_node = FindAutofillQueryNodeInDomainDict(
*root_node.FindPath({"Requests", "clients1.google.com"}));
if (!CheckNodeValidity(requests_node,
"Requests->clients1.google.com->https://"
"clients1.google.com/tbproxy/af/query?",
......@@ -220,13 +238,13 @@ bool DecompressHTTPResponse(const std::string& http_text,
auto header_and_body = SplitHTTP(http_text);
if (header_and_body.first == "") {
*decompressed_http = "";
VLOG(1) << "Cannot decompress response of invalid HTTP text: " << http_text;
VLOG(1) << "Cannot decompress response of invalid HTTP text";
return false;
}
// Look if there is a body to decompress, if not just return HTTP text as is.
if (header_and_body.second == "") {
*decompressed_http = http_text;
VLOG(1) << "There is no HTTP body to decompress: " << http_text;
VLOG(1) << "There is no HTTP body to decompress";
return true;
}
// TODO(crbug.com/945925): Add compression format detection, return an
......@@ -235,8 +253,7 @@ bool DecompressHTTPResponse(const std::string& http_text,
std::string decompressed_body;
if (!compression::GzipUncompress(header_and_body.second,
&decompressed_body)) {
VLOG(1) << "Could not gzip decompress HTTP response: "
<< header_and_body.second;
VLOG(1) << "Could not gzip decompress HTTP response";
return false;
}
// Rebuild the response HTTP text by using the new decompressed body.
......@@ -317,7 +334,7 @@ bool ServerCacheReplayer::GetResponseForQuery(
// mutation done when there is concurrency.
const std::string& http_response = const_cache_.at(key);
if (!DecompressHTTPResponse(http_response, &decompressed_http_response)) {
VLOG(1) << "Could not decompress " << http_response;
VLOG(1) << "Could not decompress http response";
return false;
}
*http_text = decompressed_http_response;
......@@ -351,14 +368,27 @@ bool ServerUrlLoader::InterceptAutofillRequest(
// Parse HTTP request body to proto.
VLOG(1) << "Intercepted in-flight request to Autofill Server: "
<< resource_request.url.spec();
// TODO(crbug/958158): Extract URL content for GET Query requests.
// Look if the body has data.
if (resource_request.request_body == nullptr) {
constexpr char kNoBodyHTTPErrorHeaders[] = "HTTP/2.0 400 Bad Request";
constexpr char kNoBodyHTTPErrorBody[] =
"there is no body data in the request";
VLOG(1) << "Served Autofill error response: " << kNoBodyHTTPErrorBody;
content::URLLoaderInterceptor::WriteResponse(
std::string(kNoBodyHTTPErrorHeaders), std::string(kNoBodyHTTPErrorBody),
params->client.get());
return true;
}
std::string http_body =
GetStringFromDataElements(resource_request.request_body->elements());
AutofillQueryContents query_request;
query_request.ParseFromString(http_body);
DCHECK(query_request.ParseFromString(http_body))
<< "could not parse HTTP request body to AutofillQueryContents "
"proto: "
<< http_body;
"proto.";
// Get response from cache using query request proto as key.
std::string http_response;
......@@ -367,7 +397,7 @@ bool ServerUrlLoader::InterceptAutofillRequest(
constexpr char kNoKeyMatchHTTPErrorHeaders[] = "HTTP/2.0 404 Not Found";
constexpr char kNoKeyMatchHTTPErrorBody[] =
"could not find response matching request";
VLOG(1) << kNoKeyMatchHTTPErrorBody << ": " << http_body;
VLOG(1) << "Served Autofill error response: " << kNoKeyMatchHTTPErrorBody;
content::URLLoaderInterceptor::WriteResponse(
std::string(kNoKeyMatchHTTPErrorHeaders),
std::string(kNoKeyMatchHTTPErrorBody), params->client.get());
......
......@@ -2561,6 +2561,9 @@ test("unit_tests") {
"../browser/android/history_report/usage_reports_buffer_backend_unittest.cc",
"../browser/android/locale/locale_template_url_loader_unittest.cc",
"../browser/android/tab_web_contents_delegate_android_unittest.cc",
"../browser/autofill/automated_tests/cache_replayer.cc",
"../browser/autofill/automated_tests/cache_replayer.h",
"../browser/autofill/automated_tests/cache_replayer_unittest.cc",
"../browser/security_events/security_event_recorder_impl_unittest.cc",
"../browser/ui/webui/version_handler_win_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment