Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6109)

Unified Diff: chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc

Issue 2772573002: chromeos: Improve ProxyResolutionServiceProvider testing. (Closed)
Patch Set: add a DCHECK Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/dbus/services/proxy_resolution_service_provider.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc
diff --git a/chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc b/chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc
index 26337652f40056ec76d01395aa4793bb1f39a583..4a70a328453fa87c8ec43f64e35ecab3c8be94a9 100644
--- a/chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc
+++ b/chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc
@@ -12,6 +12,7 @@
#include "base/threading/thread_task_runner_handle.h"
#include "chromeos/dbus/services/service_provider_test_helper.h"
#include "dbus/message.h"
+#include "net/base/net_errors.h"
#include "net/url_request/url_request_test_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
@@ -26,20 +27,52 @@ const char kReturnSignalName[] = "TestSignal";
// Test ProxyResolverDelegate implementation.
class TestProxyResolverDelegate : public ProxyResolverDelegate {
-public:
+ public:
explicit TestProxyResolverDelegate(
const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
: request_context_getter_(
- new net::TestURLRequestContextGetter(network_task_runner)) {}
-
+ new net::TestURLRequestContextGetter(network_task_runner)) {
+ // Use direct connections by default.
+ proxy_info_.UseDirect();
+ }
~TestProxyResolverDelegate() override {}
- // ProxyResolverDelegate override.
+ void set_async(bool async) { async_ = async; }
+ void set_result(net::Error result) { result_ = result; }
+
+ const net::ProxyInfo& proxy_info() const { return proxy_info_; }
+ net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; }
+
+ // ProxyResolverDelegate:
scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override {
return request_context_getter_;
}
+ int ResolveProxy(net::ProxyService* proxy_service,
+ const GURL& url,
+ net::ProxyInfo* results,
+ const net::CompletionCallback& callback) override {
+ EXPECT_EQ(proxy_service,
+ request_context_getter_->GetURLRequestContext()->proxy_service());
+ *results = proxy_info_;
+
+ if (!async_)
+ return result_;
+
+ request_context_getter_->GetNetworkTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(callback, result_));
+ return net::ERR_IO_PENDING;
+ }
private:
+ // Should ResolveProxy() run asynchronously (rather than synchronously)?
+ bool async_ = false;
+
+ // Final result for ResolveProxy() to return.
+ net::Error result_ = net::OK;
+
+ // Proxy info for ResolveProxy() to return.
+ net::ProxyInfo proxy_info_;
+
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate);
@@ -52,9 +85,10 @@ class ProxyResolutionServiceProviderTest : public testing::Test {
void SetUp() override {
// Create the proxy resolution service with the mock bus and the mock
// resolver injected.
+ delegate_ =
+ new TestProxyResolverDelegate(base::ThreadTaskRunnerHandle::Get());
service_provider_.reset(ProxyResolutionServiceProvider::Create(
- base::MakeUnique<TestProxyResolverDelegate>(
- base::ThreadTaskRunnerHandle::Get())));
+ std::unique_ptr<TestProxyResolverDelegate>(delegate_)));
test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get());
@@ -76,16 +110,26 @@ class ProxyResolutionServiceProviderTest : public testing::Test {
}
protected:
+ // Arguments extracted from a D-Bus signal.
+ struct SignalInfo {
+ std::string source_url;
+ std::string proxy_info;
+ std::string error_message;
+ };
+
// Called when a signal is received.
void OnSignalReceived(dbus::Signal* signal) {
EXPECT_EQ(kReturnSignalInterface, signal->GetInterface());
EXPECT_EQ(kReturnSignalName, signal->GetMember());
+ ASSERT_FALSE(signal_);
+ signal_ = base::MakeUnique<SignalInfo>();
+
// The signal should contain three strings.
dbus::MessageReader reader(signal);
- EXPECT_TRUE(reader.PopString(&source_url_));
- EXPECT_TRUE(reader.PopString(&proxy_info_));
- EXPECT_TRUE(reader.PopString(&error_message_));
+ EXPECT_TRUE(reader.PopString(&signal_->source_url));
+ EXPECT_TRUE(reader.PopString(&signal_->proxy_info));
+ EXPECT_TRUE(reader.PopString(&signal_->error_message));
}
// Called when connected to a signal.
@@ -94,41 +138,94 @@ class ProxyResolutionServiceProviderTest : public testing::Test {
bool success){
EXPECT_EQ(kReturnSignalInterface, signal_interface);
EXPECT_EQ(kReturnSignalName, signal_name);
-
EXPECT_TRUE(success);
}
- std::string source_url_;
- std::string proxy_info_;
- std::string error_message_;
+ // Makes a D-Bus call to |service_provider_|'s ResolveNetworkProxy method.
+ // |response_out| is updated to hold the response, and |signal_out| is updated
+ // to hold information about the emitted signal, if any.
+ void CallMethod(const std::string& source_url,
+ std::unique_ptr<dbus::Response>* response_out,
+ std::unique_ptr<SignalInfo>* signal_out) {
+ dbus::MethodCall method_call(kLibCrosServiceInterface,
+ kResolveNetworkProxy);
+ dbus::MessageWriter writer(&method_call);
+ writer.AppendString(source_url);
+ writer.AppendString(kReturnSignalInterface);
+ writer.AppendString(kReturnSignalName);
+
+ *response_out = test_helper_.CallMethod(&method_call);
+ base::RunLoop().RunUntilIdle();
+ *signal_out = std::move(signal_);
+ }
+
+ // Information about the last D-Bus signal received by OnSignalReceived().
+ std::unique_ptr<SignalInfo> signal_;
+
ServiceProviderTestHelper test_helper_;
std::unique_ptr<CrosDBusService::ServiceProviderInterface> service_provider_;
+ TestProxyResolverDelegate* delegate_; // Owned by service_provider_.
};
-TEST_F(ProxyResolutionServiceProviderTest, ResolveProxy) {
+// Tests that synchronously-resolved proxy information is returned.
+TEST_F(ProxyResolutionServiceProviderTest, ResolveProxySync) {
const char kSourceURL[] = "http://www.gmail.com/";
- // Create a method call to resolve proxy config for kSourceURL.
- dbus::MethodCall method_call(kLibCrosServiceInterface, kResolveNetworkProxy);
- dbus::MessageWriter writer(&method_call);
- writer.AppendString(kSourceURL);
- writer.AppendString(kReturnSignalInterface);
- writer.AppendString(kReturnSignalName);
+ std::unique_ptr<dbus::Response> response;
+ std::unique_ptr<SignalInfo> signal;
+ CallMethod(kSourceURL, &response, &signal);
+
+ // An empty response should be returned.
+ ASSERT_TRUE(response);
+ EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
+
+ // Confirm that the signal is received successfully.
+ ASSERT_TRUE(signal);
+ EXPECT_EQ(kSourceURL, signal->source_url);
+ EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info);
+ EXPECT_EQ("", signal->error_message);
+}
+
+// Tests that asynchronously-resolved proxy information is returned.
+TEST_F(ProxyResolutionServiceProviderTest, ResolveProxyAsync) {
+ const char kSourceURL[] = "http://www.gmail.com/";
+ delegate_->set_async(true);
+ delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080");
- // Call the ResolveNetworkProxy method.
- std::unique_ptr<dbus::Response> response(
- test_helper_.CallMethod(&method_call));
- base::RunLoop().RunUntilIdle();
+ std::unique_ptr<dbus::Response> response;
+ std::unique_ptr<SignalInfo> signal;
+ CallMethod(kSourceURL, &response, &signal);
// An empty response should be returned.
- ASSERT_TRUE(response.get());
- dbus::MessageReader reader(response.get());
- EXPECT_FALSE(reader.HasMoreData());
+ ASSERT_TRUE(response);
+ EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
// Confirm that the signal is received successfully.
- EXPECT_EQ(kSourceURL, source_url_);
- EXPECT_EQ("DIRECT", proxy_info_);
- EXPECT_EQ("", error_message_);
+ ASSERT_TRUE(signal);
+ EXPECT_EQ(kSourceURL, signal->source_url);
+ EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info);
+ EXPECT_EQ("", signal->error_message);
+}
+
+// Tests that an error received during proxy resolution is returned.
+TEST_F(ProxyResolutionServiceProviderTest, ResolveProxyError) {
+ const char kSourceURL[] = "http://www.gmail.com/";
+ const net::Error kError = net::ERR_FAILED;
+ delegate_->set_result(kError);
+
+ std::unique_ptr<dbus::Response> response;
+ std::unique_ptr<SignalInfo> signal;
+ CallMethod(kSourceURL, &response, &signal);
+
+ // An empty response should be returned.
+ ASSERT_TRUE(response);
+ EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
+
+ // The signal should contain an error.
+ ASSERT_TRUE(signal);
+ EXPECT_EQ(kSourceURL, signal->source_url);
+ EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info);
+ EXPECT_EQ(net::ErrorToString(kError), signal->error_message);
}
} // namespace chromeos
« no previous file with comments | « chromeos/dbus/services/proxy_resolution_service_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698