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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/proxy_resolution/win/dhcp_pac_file_adapter_fetcher_win.h"
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/timer/timer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/proxy_resolution/mock_pac_file_fetcher.h"
#include "net/proxy_resolution/pac_file_fetcher_impl.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/gtest_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::test::IsError;
using net::test::IsOk;
namespace net {
namespace {
const char kPacUrl[] = "http://pacserver/script.pac";
// In dhcp_pac_file_fetcher_win_unittest.cc there are
// a few tests that exercise DhcpPacFileAdapterFetcher end-to-end along with
// DhcpPacFileFetcherWin, i.e. it tests the end-to-end usage of Win32 APIs
// and the network. In this file we test only by stubbing out functionality.
// Version of DhcpPacFileAdapterFetcher that mocks out dependencies
// to allow unit testing.
class MockDhcpPacFileAdapterFetcher : public DhcpPacFileAdapterFetcher {
public:
explicit MockDhcpPacFileAdapterFetcher(
URLRequestContext* context,
scoped_refptr<base::TaskRunner> task_runner)
: DhcpPacFileAdapterFetcher(context, task_runner),
timeout_(TestTimeouts::action_timeout()),
pac_script_("bingo") {}
void Cancel() override {
DhcpPacFileAdapterFetcher::Cancel();
fetcher_ = nullptr;
}
std::unique_ptr<PacFileFetcher> ImplCreateScriptFetcher() override {
// We don't maintain ownership of the fetcher, it is transferred to
// the caller.
auto fetcher = std::make_unique<MockPacFileFetcher>();
fetcher_ = fetcher.get();
if (fetcher_delay_ms_ != -1) {
fetcher_timer_.Start(FROM_HERE, base::Milliseconds(fetcher_delay_ms_),
this,
&MockDhcpPacFileAdapterFetcher::OnFetcherTimer);
}
return fetcher;
}
class DelayingDhcpQuery : public DhcpQuery {
public:
explicit DelayingDhcpQuery()
: DhcpQuery(),
test_finished_event_(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
std::string ImplGetPacURLFromDhcp(
const std::string& adapter_name) override {
base::ElapsedTimer timer;
{
base::ScopedAllowBaseSyncPrimitivesForTesting
scoped_allow_base_sync_primitives;
test_finished_event_.TimedWait(dhcp_delay_);
}
return configured_url_;
}
base::WaitableEvent test_finished_event_;
base::TimeDelta dhcp_delay_;
std::string configured_url_;
private:
~DelayingDhcpQuery() override {}
};
scoped_refptr<DhcpQuery> ImplCreateDhcpQuery() override {
dhcp_query_ = base::MakeRefCounted<DelayingDhcpQuery>();
dhcp_query_->dhcp_delay_ = dhcp_delay_;
dhcp_query_->configured_url_ = configured_url_;
return dhcp_query_;
}
// Use a shorter timeout so tests can finish more quickly.
base::TimeDelta ImplGetTimeout() const override { return timeout_; }
void OnFetcherTimer() {
// Note that there is an assumption by this mock implementation that
// DhcpPacFileAdapterFetcher::Fetch will call ImplCreateScriptFetcher
// and call Fetch on the fetcher before the message loop is re-entered.
// This holds true today, but if you hit this DCHECK the problem can
// possibly be resolved by having a separate subclass of
// MockPacFileFetcher that adds the delay internally (instead of
// the simple approach currently used in ImplCreateScriptFetcher above).
DCHECK(fetcher_ && fetcher_->has_pending_request());
fetcher_->NotifyFetchCompletion(fetcher_result_, pac_script_);
fetcher_ = nullptr;
}
bool IsWaitingForFetcher() const {
return state() == STATE_WAIT_URL;
}
bool WasCancelled() const {
return state() == STATE_CANCEL;
}
void FinishTest() {
DCHECK(dhcp_query_.get());
dhcp_query_->test_finished_event_.Signal();
}
base::TimeDelta dhcp_delay_ = base::Milliseconds(1);
base::TimeDelta timeout_;
std::string configured_url_{kPacUrl};
int fetcher_delay_ms_ = 1;
int fetcher_result_ = OK;
std::string pac_script_;
raw_ptr<MockPacFileFetcher, DanglingUntriaged> fetcher_;
base::OneShotTimer fetcher_timer_;
scoped_refptr<DelayingDhcpQuery> dhcp_query_;
};
class FetcherClient {
public:
FetcherClient()
: url_request_context_(CreateTestURLRequestContextBuilder()->Build()),
fetcher_(std::make_unique<MockDhcpPacFileAdapterFetcher>(
url_request_context_.get(),
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}))) {}
void WaitForResult(int expected_error) {
EXPECT_EQ(expected_error, callback_.WaitForResult());
}
void RunTest() {
fetcher_->Fetch("adapter name", callback_.callback(),
TRAFFIC_ANNOTATION_FOR_TESTS);
}
void FinishTestAllowCleanup() {
fetcher_->FinishTest();
base::RunLoop().RunUntilIdle();
}
URLRequestContext* url_request_context() {
return url_request_context_.get();
}
TestCompletionCallback callback_;
std::unique_ptr<URLRequestContext> url_request_context_;
std::unique_ptr<MockDhcpPacFileAdapterFetcher> fetcher_;
std::u16string pac_text_;
};
TEST(DhcpPacFileAdapterFetcher, NormalCaseURLNotInDhcp) {
base::test::TaskEnvironment task_environment;
FetcherClient client;
client.fetcher_->configured_url_ = "";
client.RunTest();
client.WaitForResult(ERR_PAC_NOT_IN_DHCP);
ASSERT_TRUE(client.fetcher_->DidFinish());
EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_PAC_NOT_IN_DHCP));
EXPECT_EQ(std::u16string(), client.fetcher_->GetPacScript());
}
TEST(DhcpPacFileAdapterFetcher, NormalCaseURLInDhcp) {
base::test::TaskEnvironment task_environment;
FetcherClient client;
client.RunTest();
client.WaitForResult(OK);
ASSERT_TRUE(client.fetcher_->DidFinish());
EXPECT_THAT(client.fetcher_->GetResult(), IsOk());
EXPECT_EQ(std::u16string(u"bingo"), client.fetcher_->GetPacScript());
EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
}
TEST(DhcpPacFileAdapterFetcher, TimeoutDuringDhcp) {
base::test::TaskEnvironment task_environment;
// Does a Fetch() with a long enough delay on accessing DHCP that the
// fetcher should time out. This is to test a case manual testing found,
// where under certain circumstances (e.g. adapter enabled for DHCP and
// needs to retrieve its configuration from DHCP, but no DHCP server
// present on the network) accessing DHCP can take on the order of tens
// of seconds.
FetcherClient client;
client.fetcher_->dhcp_delay_ = TestTimeouts::action_max_timeout();
client.fetcher_->timeout_ = base::Milliseconds(25);
base::ElapsedTimer timer;
client.RunTest();
// An error different from this would be received if the timeout didn't
// kick in.
client.WaitForResult(ERR_TIMED_OUT);
ASSERT_TRUE(client.fetcher_->DidFinish());
EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_TIMED_OUT));
EXPECT_EQ(std::u16string(), client.fetcher_->GetPacScript());
EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
client.FinishTestAllowCleanup();
}
TEST(DhcpPacFileAdapterFetcher, CancelWhileDhcp) {
base::test::TaskEnvironment task_environment;
FetcherClient client;
client.RunTest();
client.fetcher_->Cancel();
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(client.fetcher_->DidFinish());
ASSERT_TRUE(client.fetcher_->WasCancelled());
EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_ABORTED));
EXPECT_EQ(std::u16string(), client.fetcher_->GetPacScript());
EXPECT_EQ(GURL(), client.fetcher_->GetPacURL());
client.FinishTestAllowCleanup();
}
TEST(DhcpPacFileAdapterFetcher, CancelWhileFetcher) {
base::test::TaskEnvironment task_environment;
FetcherClient client;
// This causes the mock fetcher not to pretend the
// fetcher finishes after a timeout.
client.fetcher_->fetcher_delay_ms_ = -1;
client.RunTest();
int max_loops = 4;
while (!client.fetcher_->IsWaitingForFetcher() && max_loops--) {
base::PlatformThread::Sleep(base::Milliseconds(10));
base::RunLoop().RunUntilIdle();
}
client.fetcher_->Cancel();
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(client.fetcher_->DidFinish());
ASSERT_TRUE(client.fetcher_->WasCancelled());
EXPECT_THAT(client.fetcher_->GetResult(), IsError(ERR_ABORTED));
EXPECT_EQ(std::u16string(), client.fetcher_->GetPacScript());
// GetPacURL() still returns the URL fetched in this case.
EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
client.FinishTestAllowCleanup();
}
TEST(DhcpPacFileAdapterFetcher, CancelAtCompletion) {
base::test::TaskEnvironment task_environment;
FetcherClient client;
client.RunTest();
client.WaitForResult(OK);
client.fetcher_->Cancel();
// Canceling after you're done should have no effect, so these
// are identical expectations to the NormalCaseURLInDhcp test.
ASSERT_TRUE(client.fetcher_->DidFinish());
EXPECT_THAT(client.fetcher_->GetResult(), IsOk());
EXPECT_EQ(std::u16string(u"bingo"), client.fetcher_->GetPacScript());
EXPECT_EQ(GURL(kPacUrl), client.fetcher_->GetPacURL());
client.FinishTestAllowCleanup();
}
// Does a real fetch on a mock DHCP configuration.
class MockDhcpRealFetchPacFileAdapterFetcher
: public MockDhcpPacFileAdapterFetcher {
public:
explicit MockDhcpRealFetchPacFileAdapterFetcher(
URLRequestContext* context,
scoped_refptr<base::TaskRunner> task_runner)
: MockDhcpPacFileAdapterFetcher(context, task_runner),
url_request_context_(context) {}
// Returns a real PAC file fetcher.
std::unique_ptr<PacFileFetcher> ImplCreateScriptFetcher() override {
return PacFileFetcherImpl::Create(url_request_context_);
}
raw_ptr<URLRequestContext> url_request_context_;
};
TEST(DhcpPacFileAdapterFetcher, MockDhcpRealFetch) {
base::test::TaskEnvironment task_environment;
EmbeddedTestServer test_server;
test_server.ServeFilesFromSourceDirectory(
"net/data/pac_file_fetcher_unittest");
ASSERT_TRUE(test_server.Start());
GURL configured_url = test_server.GetURL("/downloadable.pac");
FetcherClient client;
client.fetcher_ = std::make_unique<MockDhcpRealFetchPacFileAdapterFetcher>(
client.url_request_context(),
base::ThreadPool::CreateTaskRunner(
{base::MayBlock(),
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}));
client.fetcher_->configured_url_ = configured_url.spec();
client.RunTest();
client.WaitForResult(OK);
ASSERT_TRUE(client.fetcher_->DidFinish());
EXPECT_THAT(client.fetcher_->GetResult(), IsOk());
EXPECT_EQ(std::u16string(u"-downloadable.pac-\n"),
client.fetcher_->GetPacScript());
EXPECT_EQ(configured_url,
client.fetcher_->GetPacURL());
}
#define BASE_URL "http://corpserver/proxy.pac"
TEST(DhcpPacFileAdapterFetcher, SanitizeDhcpApiString) {
base::test::TaskEnvironment task_environment;
const size_t kBaseUrlLen = strlen(BASE_URL);
// Default case.
EXPECT_EQ(BASE_URL, DhcpPacFileAdapterFetcher::SanitizeDhcpApiString(
BASE_URL, kBaseUrlLen));
// Trailing \n and no null-termination.
EXPECT_EQ(BASE_URL, DhcpPacFileAdapterFetcher::SanitizeDhcpApiString(
BASE_URL "\nblablabla", kBaseUrlLen + 1));
// Embedded NULLs.
EXPECT_EQ(BASE_URL, DhcpPacFileAdapterFetcher::SanitizeDhcpApiString(
BASE_URL "\0foo\0blat", kBaseUrlLen + 9));
}
#undef BASE_URL
} // namespace
} // namespace net
|