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
|
// Copyright (c) 2008 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 "net/proxy/proxy_script_fetcher.h"
#include "base/file_path.h"
#include "base/compiler_specific.h"
#include "base/path_service.h"
#include "net/base/net_util.h"
#include "net/url_request/url_request_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
// TODO(eroman):
// - Test canceling an outstanding request.
// - Test deleting ProxyScriptFetcher while a request is in progress.
const wchar_t kDocRoot[] = L"net/data/proxy_script_fetcher_unittest";
struct FetchResult {
int code;
std::string bytes;
};
// A non-mock URL request which can access http:// and file:// urls.
class RequestContext : public URLRequestContext {
public:
RequestContext() {
net::ProxyInfo no_proxy;
proxy_service_ = net::ProxyService::Create(&no_proxy);
http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory(
proxy_service_);
}
~RequestContext() {
delete http_transaction_factory_;
delete proxy_service_;
}
};
// Helper for doing synch fetches. This object lives in SynchFetcher's
// |io_thread_| and communicates with SynchFetcher though (|result|, |event|).
class SynchFetcherThreadHelper {
public:
SynchFetcherThreadHelper(base::WaitableEvent* event, FetchResult* result)
: event_(event),
fetch_result_(result),
url_request_context_(NULL),
fetcher_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(
callback_(this, &SynchFetcherThreadHelper::OnFetchCompletion)) {
url_request_context_ = new RequestContext;
fetcher_.reset(net::ProxyScriptFetcher::Create(url_request_context_.get()));
}
// Starts fetching the script at |url|. Upon completion |event_| will be
// signalled, and the bytes read will have been written to |fetch_result_|.
void Start(const GURL& url) {
fetcher_->Fetch(url, &fetch_result_->bytes, &callback_);
}
void OnFetchCompletion(int result) {
fetch_result_->code = result;
event_->Signal();
}
private:
base::WaitableEvent* event_;
FetchResult* fetch_result_;
scoped_refptr<URLRequestContext> url_request_context_;
scoped_ptr<net::ProxyScriptFetcher> fetcher_;
net::CompletionCallbackImpl<SynchFetcherThreadHelper> callback_;
};
// Helper that wraps ProxyScriptFetcher::Fetch() with a synchronous interface.
// It executes Fetch() on a helper thread (IO_Thread).
class SynchFetcher {
public:
SynchFetcher()
: event_(false, false),
io_thread_("IO_Thread"),
thread_helper_(NULL) {
// Start an IO thread.
base::Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_IO;
io_thread_.StartWithOptions(options);
// Initialize the state in |io_thread_|.
io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &SynchFetcher::Init));
Wait();
}
~SynchFetcher() {
// Tear down the state in |io_thread_|.
io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &SynchFetcher::Cleanup));
Wait();
}
// Synchronously fetch the url.
FetchResult Fetch(const GURL& url) {
io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
this, &SynchFetcher::AsynchFetch, url));
Wait();
return fetch_result_;
}
private:
// [Runs on |io_thread_|] Allocates the URLRequestContext and the
// ProxyScriptFetcher, which live inside |thread_helper_|.
void Init() {
thread_helper_ = new SynchFetcherThreadHelper(&event_, &fetch_result_);
event_.Signal();
}
// [Runs on |io_thread_|] Signals |event_| on completion.
void AsynchFetch(const GURL& url) {
thread_helper_->Start(url);
}
// [Runs on |io_thread_|] Signals |event_| on cleanup completion.
void Cleanup() {
delete thread_helper_;
thread_helper_ = NULL;
MessageLoop::current()->RunAllPending();
event_.Signal();
}
void Wait() {
event_.Wait();
event_.Reset();
}
base::WaitableEvent event_;
base::Thread io_thread_;
FetchResult fetch_result_;
// Holds all the state that lives on the IO thread, for easy cleanup.
SynchFetcherThreadHelper* thread_helper_;
};
// Template specialization so SynchFetcher does not have to be refcounted.
template<>
void RunnableMethodTraits<SynchFetcher>::RetainCallee(SynchFetcher* remover) {}
template<>
void RunnableMethodTraits<SynchFetcher>::ReleaseCallee(SynchFetcher* remover) {}
// Required to be in net namespace by FRIEND_TEST.
namespace net {
// Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
GURL GetTestFileUrl(const std::string& relpath) {
FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
path = path.AppendASCII("net");
path = path.AppendASCII("data");
path = path.AppendASCII("proxy_script_fetcher_unittest");
GURL base_url = net::FilePathToFileURL(path);
return GURL(base_url.spec() + "/" + relpath);
}
typedef PlatformTest ProxyScriptFetcherTest;
TEST_F(ProxyScriptFetcherTest, FileUrl) {
SynchFetcher pac_fetcher;
{ // Fetch a non-existent file.
FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"));
EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result.code);
EXPECT_TRUE(result.bytes.empty());
}
{ // Fetch a file that exists.
FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"));
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.txt-\n", result.bytes);
}
}
// Note that all mime types are allowed for PAC file, to be consistent
// with other browsers.
TEST_F(ProxyScriptFetcherTest, HttpMimeType) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateServer(kDocRoot, NULL);
ASSERT_TRUE(NULL != server.get());
SynchFetcher pac_fetcher;
{ // Fetch a PAC with mime type "text/plain"
GURL url = server->TestServerPage("files/pac.txt");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.txt-\n", result.bytes);
}
{ // Fetch a PAC with mime type "text/html"
GURL url = server->TestServerPage("files/pac.html");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.html-\n", result.bytes);
}
{ // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
GURL url = server->TestServerPage("files/pac.nsproxy");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.nsproxy-\n", result.bytes);
}
}
TEST_F(ProxyScriptFetcherTest, HttpStatusCode) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateServer(kDocRoot, NULL);
ASSERT_TRUE(NULL != server.get());
SynchFetcher pac_fetcher;
{ // Fetch a PAC which gives a 500 -- FAIL
GURL url = server->TestServerPage("files/500.pac");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code);
EXPECT_TRUE(result.bytes.empty());
}
{ // Fetch a PAC which gives a 404 -- FAIL
GURL url = server->TestServerPage("files/404.pac");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code);
EXPECT_TRUE(result.bytes.empty());
}
}
TEST_F(ProxyScriptFetcherTest, ContentDisposition) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateServer(kDocRoot, NULL);
ASSERT_TRUE(NULL != server.get());
SynchFetcher pac_fetcher;
// Fetch PAC scripts via HTTP with a Content-Disposition header -- should
// have no effect.
GURL url = server->TestServerPage("files/downloadable.pac");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-downloadable.pac-\n", result.bytes);
}
TEST_F(ProxyScriptFetcherTest, TooLarge) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateServer(kDocRoot, NULL);
ASSERT_TRUE(NULL != server.get());
SynchFetcher pac_fetcher;
// Set the maximum response size to 50 bytes.
int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50);
// These two URLs are the same file, but are http:// vs file://
GURL urls[] = {
server->TestServerPage("files/large-pac.nsproxy"),
GetTestFileUrl("large-pac.nsproxy")
};
// Try fetching URLs that are 101 bytes large. We should abort the request
// after 50 bytes have been read, and fail with a too large error.
for (size_t i = 0; i < arraysize(urls); ++i) {
const GURL& url = urls[i];
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::ERR_FILE_TOO_BIG, result.code);
EXPECT_TRUE(result.bytes.empty());
}
// Restore the original size bound.
net::ProxyScriptFetcher::SetSizeConstraintForUnittest(prev_size);
{ // Make sure we can still fetch regular URLs.
GURL url = server->TestServerPage("files/pac.nsproxy");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.nsproxy-\n", result.bytes);
}
}
TEST_F(ProxyScriptFetcherTest, Hang) {
scoped_refptr<HTTPTestServer> server =
HTTPTestServer::CreateServer(kDocRoot, NULL);
ASSERT_TRUE(NULL != server.get());
SynchFetcher pac_fetcher;
// Set the timeout period to 0.5 seconds.
int prev_timeout =
net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500);
// Try fetching a URL which takes 1.2 seconds. We should abort the request
// after 500 ms, and fail with a timeout error.
{ GURL url = server->TestServerPage("slow/proxy.pac?1.2");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::ERR_TIMED_OUT, result.code);
EXPECT_TRUE(result.bytes.empty());
}
// Restore the original timeout period.
net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(prev_timeout);
{ // Make sure we can still fetch regular URLs.
GURL url = server->TestServerPage("files/pac.nsproxy");
FetchResult result = pac_fetcher.Fetch(url);
EXPECT_EQ(net::OK, result.code);
EXPECT_EQ("-pac.nsproxy-\n", result.bytes);
}
}
} // namespace net
|