Commit 403d6fcc authored by Mila Green's avatar Mila Green Committed by Commit Bot

Implement NetworkFetcher interface for Mac.

Bug: 1020650

Change-Id: I8da0968e81d86646f62fda300bb783142a4d6236
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1898613
Commit-Queue: Mila Green <milagreen@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714251}
parent 23fc7bdf
......@@ -101,6 +101,8 @@ if (is_win || is_mac) {
}
if (is_mac) {
deps += [ "//chrome/updater/mac:updater_tests" ]
data_deps = [
"//chrome/updater/mac:updater",
]
......
......@@ -14,6 +14,37 @@ executable("updater") {
]
deps = [
":network_fetcher_sources",
"//chrome/updater:common",
]
}
source_set("network_fetcher_sources") {
sources = [
"net/network.h",
"net/network_fetcher.h",
"net/network_fetcher.mm",
]
deps = [
"//base",
"//components/update_client",
"//net",
]
}
source_set("updater_tests") {
testonly = true
sources = [
"net/network_unittest.mm",
]
deps = [
":network_fetcher_sources",
"//base/test:test_support",
"//net:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_UPDATER_MAC_NET_NETWORK_H_
#define CHROME_UPDATER_MAC_NET_NETWORK_H_
#include <memory>
#include "base/macros.h"
#include "base/threading/thread_checker.h"
#include "components/update_client/network.h"
namespace updater {
class NetworkFetcherFactory : public update_client::NetworkFetcherFactory {
public:
NetworkFetcherFactory();
std::unique_ptr<update_client::NetworkFetcher> Create() const override;
protected:
~NetworkFetcherFactory() override;
private:
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(NetworkFetcherFactory);
};
} // namespace updater
#endif // CHROME_UPDATER_MAC_NET_NETWORK_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_UPDATER_MAC_NET_NETWORK_FETCHER_H_
#define CHROME_UPDATER_MAC_NET_NETWORK_FETCHER_H_
#include <stdint.h>
#include <string>
#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "components/update_client/network.h"
class GURL;
namespace base {
class FilePath;
} // namespace base
namespace updater {
class NetworkFetcher : public update_client::NetworkFetcher {
public:
NetworkFetcher();
NetworkFetcher& operator=(const NetworkFetcher&) = delete;
NetworkFetcher(const NetworkFetcher&) = delete;
~NetworkFetcher() override;
// NetworkFetcher overrides.
void PostRequest(
const GURL& url,
const std::string& post_data,
const base::flat_map<std::string, std::string>& post_additional_headers,
update_client::NetworkFetcher::ResponseStartedCallback
response_started_callback,
update_client::NetworkFetcher::ProgressCallback progress_callback,
update_client::NetworkFetcher::PostRequestCompleteCallback
post_request_complete_callback) override;
void DownloadToFile(
const GURL& url,
const base::FilePath& file_path,
update_client::NetworkFetcher::ResponseStartedCallback
response_started_callback,
update_client::NetworkFetcher::ProgressCallback progress_callback,
update_client::NetworkFetcher::DownloadToFileCompleteCallback
download_to_file_complete_callback) override;
private:
THREAD_CHECKER(thread_checker_);
};
} // namespace updater
#endif // CHROME_UPDATER_MAC_NET_NETWORK_FETCHER_H_
This diff is collapsed.
// Copyright 2019 The Chromium 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 "chrome/updater/mac/net/network.h"
#include "chrome/updater/mac/net/network_fetcher.h"
#include <stdint.h>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ResponseStartedCallback =
update_client::NetworkFetcher::ResponseStartedCallback;
using ProgressCallback = update_client::NetworkFetcher::ProgressCallback;
using PostRequestCompleteCallback =
update_client::NetworkFetcher::PostRequestCompleteCallback;
using DownloadToFileCompleteCallback =
update_client::NetworkFetcher::DownloadToFileCompleteCallback;
namespace updater {
ACTION_P(RunClosure, closure) {
closure.Run();
}
static base::FilePath testFilePath;
class ChromeUpdaterNetworkMacTest : public ::testing::Test {
public:
~ChromeUpdaterNetworkMacTest() override = default;
#pragma mark - Callback Methods
void StartedCallback(int response_code, int64_t content_length) {
EXPECT_EQ(response_code, 200);
}
void ProgressCallback(int64_t current) {
EXPECT_GE(current, 0);
EXPECT_LE(current, 100);
}
void PostRequestCompleteCallback(std::unique_ptr<std::string> response_body,
int net_error,
const std::string& header_etag,
int64_t xheader_retry_after_sec) {
EXPECT_EQ(net_error, 200);
EXPECT_GT(header_etag.length(), 0u);
EXPECT_EQ(xheader_retry_after_sec, 67);
PostRequestCompleted();
}
void DownloadCallback(int net_error, int64_t content_size) {
EXPECT_EQ(net_error, 200);
EXPECT_GT(content_size, 0);
ASSERT_FALSE(testFilePath.empty());
ASSERT_TRUE(base::PathExists(testFilePath));
ASSERT_TRUE(base::DeleteFile(testFilePath, false));
DownloadToFileCompleted();
}
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request) {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->set_content("hello");
http_response->set_content_type("text/plain");
http_response->AddCustomHeader("X-Retry-After", "67");
http_response->AddCustomHeader("ETag", "Wfhw789h");
return http_response;
}
MOCK_METHOD0(DownloadToFileCompleted, void(void));
MOCK_METHOD0(PostRequestCompleted, void(void));
base::test::SingleThreadTaskEnvironment task_environment_;
};
#pragma mark - Test Methods
TEST_F(ChromeUpdaterNetworkMacTest, NetworkFetcherMacHTTPFactory) {
base::RunLoop run_loop;
base::RepeatingClosure quit_closure = run_loop.QuitClosure();
auto fetcher = base::MakeRefCounted<NetworkFetcherFactory>()->Create();
quit_closure.Run();
run_loop.Run();
EXPECT_NE(nullptr, fetcher.get());
}
TEST_F(ChromeUpdaterNetworkMacTest, NetworkFetcherMacPostRequest) {
base::RunLoop run_loop;
base::RepeatingClosure quit_closure = run_loop.QuitClosure();
EXPECT_CALL(*this, PostRequestCompleted()).WillOnce(RunClosure(quit_closure));
auto fetcher = base::MakeRefCounted<NetworkFetcherFactory>()->Create();
net::EmbeddedTestServer test_server;
test_server.RegisterRequestHandler(base::Bind(
&ChromeUpdaterNetworkMacTest::HandleRequest, base::Unretained(this)));
ASSERT_TRUE(test_server.Start());
const GURL url = test_server.GetURL("/echo");
fetcher->PostRequest(
url, "", {},
base::BindOnce(&ChromeUpdaterNetworkMacTest::StartedCallback,
base::Unretained(this)),
base::BindRepeating(&ChromeUpdaterNetworkMacTest::ProgressCallback,
base::Unretained(this)),
base::BindOnce(&ChromeUpdaterNetworkMacTest::PostRequestCompleteCallback,
base::Unretained(this)));
run_loop.Run();
}
TEST_F(ChromeUpdaterNetworkMacTest, NetworkFetcherMacDownloadToFile) {
base::RunLoop run_loop;
base::RepeatingClosure quit_closure = run_loop.QuitClosure();
EXPECT_CALL(*this, DownloadToFileCompleted())
.WillOnce(RunClosure(quit_closure));
auto fetcher = base::MakeRefCounted<NetworkFetcherFactory>()->Create();
net::EmbeddedTestServer test_server;
test_server.RegisterRequestHandler(base::Bind(
&ChromeUpdaterNetworkMacTest::HandleRequest, base::Unretained(this)));
ASSERT_TRUE(test_server.Start());
const GURL url = test_server.GetURL("/echo");
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
testFilePath = temp_dir.GetPath().Append(FILE_PATH_LITERAL("from_file"));
fetcher->DownloadToFile(
url, testFilePath,
base::BindOnce(&ChromeUpdaterNetworkMacTest::StartedCallback,
base::Unretained(this)),
base::BindRepeating(&ChromeUpdaterNetworkMacTest::ProgressCallback,
base::Unretained(this)),
base::BindOnce(&ChromeUpdaterNetworkMacTest::DownloadCallback,
base::Unretained(this)));
run_loop.Run();
}
} // namespace updater
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