| Index: net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc
|
| diff --git a/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc b/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c355edcedbb55c9312e76275dc3c4ba8497fdb58
|
| --- /dev/null
|
| +++ b/net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc
|
| @@ -0,0 +1,506 @@
|
| +// Copyright (c) 2011 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 "base/message_loop.h"
|
| +#include "base/perftimer.h"
|
| +#include "base/rand_util.h"
|
| +#include "base/threading/platform_thread.h"
|
| +#include "net/base/completion_callback.h"
|
| +#include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
|
| +#include "net/proxy/dhcp_proxy_script_fetcher_win.h"
|
| +#include "net/url_request/url_request_test_util.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +#include <vector>
|
| +
|
| +namespace net {
|
| +namespace {
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, AdapterNamesAndPacURLFromDhcp) {
|
| + // This tests our core Win32 implementation without any of the wrappers
|
| + // we layer on top to achieve asynchronous and parallel operations.
|
| + //
|
| + // We don't make assumptions about the environment this unit test is
|
| + // running in, so it just exercises the code to make sure there
|
| + // is no crash and no error returned, but does not assert on the number
|
| + // of interfaces or the information returned via DHCP.
|
| + std::set<std::string> adapter_names;
|
| + WindowsDhcpProxyScriptFetcher::GetCandidateAdapterNames(&adapter_names);
|
| + for (std::set<std::string>::iterator it = adapter_names.begin();
|
| + it != adapter_names.end();
|
| + ++it) {
|
| + const std::string& adapter_name = *it;
|
| + std::string pac_url =
|
| + DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name);
|
| + printf("Adapter '%s' has PAC URL '%s' configured in DHCP.\n",
|
| + adapter_name.c_str(),
|
| + pac_url.c_str());
|
| + }
|
| +}
|
| +
|
| +// Helper for RealFetch* tests below.
|
| +class RealFetchTester {
|
| + public:
|
| + RealFetchTester()
|
| + : context_((new TestURLRequestContext())),
|
| + fetcher_(new WindowsDhcpProxyScriptFetcher(context_.get())),
|
| + finished_(false),
|
| + ALLOW_THIS_IN_INITIALIZER_LIST(
|
| + completion_callback_(this, &RealFetchTester::OnCompletion)),
|
| + on_completion_is_error_(false) {
|
| + // Make sure the test ends.
|
| + timeout_.Start(
|
| + base::TimeDelta::FromSeconds(5), this, &RealFetchTester::OnTimeout);
|
| + }
|
| +
|
| + void RunTest() {
|
| + fetcher_->Fetch(&pac_text_, &completion_callback_);
|
| + }
|
| +
|
| + void RunTestWithCancel() {
|
| + RunTest();
|
| + fetcher_->Cancel();
|
| + }
|
| +
|
| + void RunTestWithDeferredCancel() {
|
| + RunTest();
|
| + cancel_timer_.Start(base::TimeDelta::FromMilliseconds(1),
|
| + this, &RealFetchTester::OnCancelTimer);
|
| + }
|
| +
|
| + void OnCompletion(int result) {
|
| + if (on_completion_is_error_) {
|
| + FAIL() << "Received completion for test in which this is error.";
|
| + }
|
| + finished_ = true;
|
| + printf("Result code %d PAC data length %d\n", result, pac_text_.size());
|
| + }
|
| +
|
| + void OnTimeout() {
|
| + printf("Timeout!");
|
| + OnCompletion(0);
|
| + }
|
| +
|
| + void OnCancelTimer() {
|
| + fetcher_->Cancel();
|
| + finished_ = true;
|
| + }
|
| +
|
| + scoped_refptr<URLRequestContext> context_;
|
| + scoped_ptr<WindowsDhcpProxyScriptFetcher> fetcher_;
|
| + bool finished_;
|
| + string16 pac_text_;
|
| + CompletionCallbackImpl<RealFetchTester> completion_callback_;
|
| + base::OneShotTimer<RealFetchTester> timeout_;
|
| + base::OneShotTimer<RealFetchTester> cancel_timer_;
|
| + bool on_completion_is_error_;
|
| +};
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, RealFetch) {
|
| + // This tests a call to Fetch() with no stubbing out of dependencies.
|
| + //
|
| + // We don't make assumptions about the environment this unit test is
|
| + // running in, so it just exercises the code to make sure there
|
| + // is no crash and no unexpected error returned, but does not assert on
|
| + // results beyond that.
|
| + RealFetchTester fetcher;
|
| + fetcher.RunTest();
|
| +
|
| + while (!fetcher.finished_) {
|
| + MessageLoop::current()->RunAllPending();
|
| + }
|
| + printf("PAC URL was %s\n",
|
| + fetcher.fetcher_->GetPacURL().possibly_invalid_spec().c_str());
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, RealFetchWithCancel) {
|
| + // Does a Fetch() with an immediate cancel. As before, just
|
| + // exercises the code without stubbing out dependencies.
|
| + RealFetchTester fetcher;
|
| + fetcher.RunTestWithCancel();
|
| + MessageLoop::current()->RunAllPending();
|
| +}
|
| +
|
| +// For RealFetchWithDeferredCancel, below.
|
| +class DelayingDhcpProxyScriptAdapterFetcher
|
| + : public DhcpProxyScriptAdapterFetcher {
|
| + public:
|
| + explicit DelayingDhcpProxyScriptAdapterFetcher(
|
| + URLRequestContext* url_request_context)
|
| + : DhcpProxyScriptAdapterFetcher(url_request_context) {
|
| + }
|
| +
|
| + std::string ImplGetPacURLFromDhcp(const std::string& adapter_name) OVERRIDE {
|
| + base::PlatformThread::Sleep(20);
|
| + return DhcpProxyScriptAdapterFetcher::ImplGetPacURLFromDhcp(adapter_name);
|
| + }
|
| +};
|
| +
|
| +// For RealFetchWithDeferredCancel, below.
|
| +class DelayingWindowsDhcpProxyScriptFetcher
|
| + : public WindowsDhcpProxyScriptFetcher {
|
| + public:
|
| + explicit DelayingWindowsDhcpProxyScriptFetcher(
|
| + URLRequestContext* context)
|
| + : WindowsDhcpProxyScriptFetcher(context) {
|
| + }
|
| +
|
| + DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE {
|
| + return new DelayingDhcpProxyScriptAdapterFetcher(url_request_context_);
|
| + }
|
| +};
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, RealFetchWithDeferredCancel) {
|
| + // Does a Fetch() with a slightly delayed cancel. As before, just
|
| + // exercises the code without stubbing out dependencies, but
|
| + // introduces a guaranteed 20 ms delay on the worker threads so that
|
| + // the cancel is called before they complete.
|
| + RealFetchTester fetcher;
|
| + fetcher.fetcher_.reset(
|
| + new DelayingWindowsDhcpProxyScriptFetcher(fetcher.context_));
|
| + fetcher.on_completion_is_error_ = true;
|
| + fetcher.RunTestWithDeferredCancel();
|
| + while (!fetcher.finished_) {
|
| + MessageLoop::current()->RunAllPending();
|
| + }
|
| + MessageLoop::current()->RunAllPending();
|
| +}
|
| +
|
| +// The remaining tests are to exercise our state machine in various
|
| +// situations, with actual network access fully stubbed out.
|
| +
|
| +class DummyDhcpProxyScriptAdapterFetcher
|
| + : public DhcpProxyScriptAdapterFetcher {
|
| + public:
|
| + DummyDhcpProxyScriptAdapterFetcher()
|
| + : DhcpProxyScriptAdapterFetcher(new TestURLRequestContext()),
|
| + did_finish_(false),
|
| + result_(OK),
|
| + pac_script_(L"bingo"),
|
| + fetch_delay_ms_(1),
|
| + client_callback_(NULL) {
|
| + }
|
| +
|
| + void Fetch(const std::string& adapter_name,
|
| + CompletionCallback* callback) OVERRIDE {
|
| + client_callback_ = callback;
|
| + timer_.Start(base::TimeDelta::FromMilliseconds(fetch_delay_ms_),
|
| + this, &DummyDhcpProxyScriptAdapterFetcher::OnTimer);
|
| + }
|
| +
|
| + void Cancel() OVERRIDE {
|
| + timer_.Stop();
|
| + }
|
| +
|
| + bool DidFinish() const OVERRIDE {
|
| + return did_finish_;
|
| + }
|
| +
|
| + int result() const OVERRIDE {
|
| + return result_;
|
| + }
|
| +
|
| + string16 pac_script() const OVERRIDE {
|
| + return pac_script_;
|
| + }
|
| +
|
| + void OnTimer() {
|
| + client_callback_->Run(result_);
|
| + }
|
| +
|
| + void Configure(
|
| + bool did_finish, int result, string16 pac_script, int fetch_delay_ms) {
|
| + did_finish_ = did_finish;
|
| + result_ = result;
|
| + pac_script_ = pac_script;
|
| + fetch_delay_ms_ = fetch_delay_ms;
|
| + }
|
| +
|
| + private:
|
| + bool did_finish_;
|
| + int result_;
|
| + string16 pac_script_;
|
| + int fetch_delay_ms_;
|
| + CompletionCallback* client_callback_;
|
| + base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_;
|
| +};
|
| +
|
| +class MockWindowsDhcpProxyScriptFetcher : public WindowsDhcpProxyScriptFetcher {
|
| + public:
|
| + MockWindowsDhcpProxyScriptFetcher()
|
| + : WindowsDhcpProxyScriptFetcher(new TestURLRequestContext()),
|
| + next_adapter_fetcher_index_(0) {
|
| + }
|
| +
|
| + // Adds a fetcher object to the queue of fetchers used by
|
| + // |ImplCreateAdapterFetcher()|, and its name to the list of adapters
|
| + // returned by ImplGetCandidateAdapterNames.
|
| + void PushBackAdapter(const std::string& adapter_name,
|
| + DhcpProxyScriptAdapterFetcher* fetcher) {
|
| + adapter_names_.push_back(adapter_name);
|
| + adapter_fetchers_.push_back(fetcher);
|
| + }
|
| +
|
| + void ConfigureAndPushBackAdapter(const std::string& adapter_name,
|
| + bool did_finish,
|
| + int result,
|
| + string16 pac_script,
|
| + int fetch_delay_ms) {
|
| + scoped_refptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher =
|
| + new DummyDhcpProxyScriptAdapterFetcher();
|
| + adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms);
|
| + PushBackAdapter(adapter_name, adapter_fetcher.get());
|
| + }
|
| +
|
| + DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE {
|
| + return adapter_fetchers_[next_adapter_fetcher_index_++].get();
|
| + }
|
| +
|
| + bool ImplGetCandidateAdapterNames(
|
| + std::set<std::string>* adapter_names) OVERRIDE {
|
| + adapter_names->insert(adapter_names_.begin(), adapter_names_.end());
|
| + return true;
|
| + }
|
| +
|
| + int ImplGetMaxWaitMs() OVERRIDE {
|
| + return 25;
|
| + }
|
| +
|
| + void ResetTestState() {
|
| + next_adapter_fetcher_index_ = 0;
|
| + adapter_fetchers_.clear();
|
| + // String pointers contained herein will have been freed during test.
|
| + adapter_names_.clear();
|
| + }
|
| +
|
| + int next_adapter_fetcher_index_;
|
| + std::vector<scoped_refptr<DhcpProxyScriptAdapterFetcher>> adapter_fetchers_;
|
| + std::vector<std::string> adapter_names_;
|
| +};
|
| +
|
| +class FetcherClient {
|
| +public:
|
| + FetcherClient()
|
| + : finished_(false),
|
| + result_(ERR_UNEXPECTED),
|
| + ALLOW_THIS_IN_INITIALIZER_LIST(
|
| + completion_callback_(this, &FetcherClient::OnCompletion)) {
|
| + }
|
| +
|
| + void RunTest() {
|
| + int result = fetcher_.Fetch(&pac_text_, &completion_callback_);
|
| + ASSERT_EQ(ERR_IO_PENDING, result);
|
| + }
|
| +
|
| + void RunImmediateReturnTest() {
|
| + int result = fetcher_.Fetch(&pac_text_, &completion_callback_);
|
| + ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, result);
|
| + }
|
| +
|
| + void RunMessageLoopUntilComplete() {
|
| + while (!finished_) {
|
| + MessageLoop::current()->RunAllPending();
|
| + }
|
| + MessageLoop::current()->RunAllPending();
|
| + }
|
| +
|
| + void OnCompletion(int result) {
|
| + finished_ = true;
|
| + result_ = result;
|
| + }
|
| +
|
| + void ResetTestState() {
|
| + finished_ = false;
|
| + result_ = ERR_UNEXPECTED;
|
| + pac_text_ = L"";
|
| + fetcher_.ResetTestState();
|
| + }
|
| +
|
| + MockWindowsDhcpProxyScriptFetcher fetcher_;
|
| + bool finished_;
|
| + int result_;
|
| + string16 pac_text_;
|
| + CompletionCallbackImpl<FetcherClient> completion_callback_;
|
| +};
|
| +
|
| +// We separate out each test's logic so that we can easily implement
|
| +// the ReuseFetcher test at the bottom.
|
| +void TestNormalCaseURLConfiguredOneAdapter(FetcherClient* client) {
|
| + scoped_refptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher =
|
| + new DummyDhcpProxyScriptAdapterFetcher();
|
| + adapter_fetcher->Configure(true, OK, L"bingo", 1);
|
| + client->fetcher_.PushBackAdapter("a", adapter_fetcher.get());
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + ASSERT_EQ(OK, client->result_);
|
| + ASSERT_EQ(L"bingo", client->pac_text_);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, NormalCaseURLConfiguredOneAdapter) {
|
| + FetcherClient client;
|
| + TestNormalCaseURLConfiguredOneAdapter(&client);
|
| +}
|
| +
|
| +void TestNormalCaseURLConfiguredMultipleAdapters(FetcherClient* client) {
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "second", true, OK, L"bingo", 50);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "third", true, OK, L"rocko", 1);
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + ASSERT_EQ(OK, client->result_);
|
| + ASSERT_EQ(L"bingo", client->pac_text_);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, NormalCaseURLConfiguredMultipleAdapters) {
|
| + FetcherClient client;
|
| + TestNormalCaseURLConfiguredMultipleAdapters(&client);
|
| +}
|
| +
|
| +void TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout(
|
| + FetcherClient* client) {
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + // This will time out.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "second", false, ERR_IO_PENDING, L"bingo", 1000);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "third", true, OK, L"rocko", 1);
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + ASSERT_EQ(OK, client->result_);
|
| + ASSERT_EQ(L"rocko", client->pac_text_);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher,
|
| + NormalCaseURLConfiguredMultipleAdaptersWithTimeout) {
|
| + FetcherClient client;
|
| + TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout(&client);
|
| +}
|
| +
|
| +void TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout(
|
| + FetcherClient* client) {
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + // This will time out.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "second", false, ERR_IO_PENDING, L"bingo", 1000);
|
| + // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such
|
| + // should be chosen.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "third", true, ERR_PAC_STATUS_NOT_OK, L"", 1);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "fourth", true, ERR_NOT_IMPLEMENTED, L"", 1);
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + ASSERT_EQ(ERR_PAC_STATUS_NOT_OK, client->result_);
|
| + ASSERT_EQ(L"", client->pac_text_);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher,
|
| + FailureCaseURLConfiguredMultipleAdaptersWithTimeout) {
|
| + FetcherClient client;
|
| + TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout(&client);
|
| +}
|
| +
|
| +void TestFailureCaseNoURLConfigured(FetcherClient* client) {
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + // This will time out.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "second", false, ERR_IO_PENDING, L"bingo", 1000);
|
| + // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such
|
| + // should be chosen.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "third", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_);
|
| + ASSERT_EQ(L"", client->pac_text_);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, FailureCaseNoURLConfigured) {
|
| + FetcherClient client;
|
| + TestFailureCaseNoURLConfigured(&client);
|
| +}
|
| +
|
| +void TestFailureCaseNoDhcpAdapters(FetcherClient* client) {
|
| + client->RunImmediateReturnTest();
|
| + // In case there are any pending messages that get us in a bad state
|
| + // (there shouldn't be).
|
| + MessageLoop::current()->RunAllPending();
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, FailureCaseNoDhcpAdapters) {
|
| + FetcherClient client;
|
| + TestFailureCaseNoDhcpAdapters(&client);
|
| +}
|
| +
|
| +void TestShortCircuitLessPreferredAdapters(FetcherClient* client) {
|
| + // Here we have a bunch of adapters; the first reports no PAC in DHCP,
|
| + // the second responds quickly with a PAC file, the rest take a long
|
| + // time. Verify that we complete quickly and do not wait for the slow
|
| + // adapters, i.e. we finish before timeout.
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "1", true, ERR_PAC_NOT_IN_DHCP, L"", 1);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "2", true, OK, L"bingo", 1);
|
| + client->fetcher_.ConfigureAndPushBackAdapter(
|
| + "3", true, OK, L"wrongo", 1000);
|
| +
|
| + PerfTimer timer;
|
| + client->RunTest();
|
| + client->RunMessageLoopUntilComplete();
|
| + // Assert that the time passed is just less than the wait timer
|
| + // timeout (which we have mocked out above to be 25 ms), to avoid
|
| + // flakiness but still get a strong signal that it was the shortcut
|
| + // mechanism (in OnFetcherDone) that kicked in.
|
| + ASSERT_GT(TimeDelta::FromMilliseconds(23), timer.Elapsed());
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, ShortCircuitLessPreferredAdapters) {
|
| + FetcherClient client;
|
| + TestShortCircuitLessPreferredAdapters(&client);
|
| +}
|
| +
|
| +TEST(WindowsDhcpProxyScriptFetcher, ReuseFetcher) {
|
| + FetcherClient client;
|
| +
|
| + // The ProxyScriptFetcher interface stipulates that only a single
|
| + // |Fetch()| may be in flight at once, but allows reuse, so test
|
| + // that the state transitions correctly from done to start in all
|
| + // cases we're testing.
|
| +
|
| + typedef void (*FetcherClientTestFunction)(FetcherClient*);
|
| + typedef std::vector<FetcherClientTestFunction> TestVector;
|
| + TestVector test_functions;
|
| + test_functions.push_back(TestNormalCaseURLConfiguredOneAdapter);
|
| + test_functions.push_back(TestNormalCaseURLConfiguredMultipleAdapters);
|
| + test_functions.push_back(
|
| + TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout);
|
| + test_functions.push_back(
|
| + TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout);
|
| + test_functions.push_back(TestFailureCaseNoURLConfigured);
|
| + test_functions.push_back(TestFailureCaseNoDhcpAdapters);
|
| + test_functions.push_back(TestShortCircuitLessPreferredAdapters);
|
| +
|
| + std::random_shuffle(test_functions.begin(),
|
| + test_functions.end(),
|
| + base::RandGenerator);
|
| + for (TestVector::iterator it = test_functions.begin();
|
| + it != test_functions.end();
|
| + ++it) {
|
| + (*it)(&client);
|
| + client.ResetTestState();
|
| + }
|
| +
|
| + // Re-do the first test to make sure the last test that was run did
|
| + // not leave things in a bad state.
|
| + (*test_functions.begin())(&client);
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace net
|
|
|