summaryrefslogtreecommitdiffstats
path: root/net/proxy/init_proxy_resolver_unittest.cc
blob: e9d05a2966bf6857c9e1b85dfd4109616625b5ba (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright (c) 2009 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 <vector>

#include "net/base/net_errors.h"
#include "net/base/load_log.h"
#include "net/base/load_log_unittest.h"
#include "net/base/test_completion_callback.h"
#include "net/proxy/init_proxy_resolver.h"
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_resolver.h"
#include "net/proxy/proxy_script_fetcher.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace {

enum Error {
  kFailedDownloading = -100,
  kFailedParsing = -200,
};

class Rules {
 public:
  struct Rule {
    Rule(const GURL& url,
         int fetch_error,
         int set_pac_error)
        : url(url),
          fetch_error(fetch_error),
          set_pac_error(set_pac_error) {
    }

    std::string bytes() const {
      if (set_pac_error == OK)
        return url.spec() + "!valid-script";
      if (fetch_error == OK)
        return url.spec() + "!invalid-script";
      return std::string();
    }

    GURL url;
    int fetch_error;
    int set_pac_error;
  };

  Rule AddSuccessRule(const char* url) {
    Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/);
    rules_.push_back(rule);
    return rule;
  }

  void AddFailDownloadRule(const char* url) {
    rules_.push_back(Rule(GURL(url), kFailedDownloading /*fetch_error*/,
        ERR_UNEXPECTED /*set_pac_error*/));
  }

  void AddFailParsingRule(const char* url) {
    rules_.push_back(Rule(GURL(url), OK /*fetch_error*/,
        kFailedParsing /*set_pac_error*/));
  }

  const Rule& GetRuleByUrl(const GURL& url) const {
    for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
         ++it) {
      if (it->url == url)
        return *it;
    }
    CHECK(false) << "Rule not found for " << url;
    return rules_[0];
  }

  const Rule& GetRuleByBytes(const std::string& bytes) const {
    for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
         ++it) {
      if (it->bytes() == bytes)
        return *it;
    }
    CHECK(false) << "Rule not found for " << bytes;
    return rules_[0];
  }

 private:
  typedef std::vector<Rule> RuleList;
  RuleList rules_;
};

class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
 public:
  explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {}

  // ProxyScriptFetcher implementation.
  virtual int Fetch(const GURL& url,
                    std::string* bytes,
                    CompletionCallback* callback) {
    const Rules::Rule& rule = rules_->GetRuleByUrl(url);
    int rv = rule.fetch_error;
    EXPECT_NE(ERR_UNEXPECTED, rv);
    if (rv == OK)
      *bytes = rule.bytes();
    return rv;
  }

  virtual void Cancel() {}

 private:
  const Rules* rules_;
};

class RuleBasedProxyResolver : public ProxyResolver {
 public:
  RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes)
      : ProxyResolver(expects_pac_bytes), rules_(rules) {}

  // ProxyResolver implementation:
  virtual int GetProxyForURL(const GURL& /*url*/,
                             ProxyInfo* /*results*/,
                             CompletionCallback* /*callback*/,
                             RequestHandle* /*request_handle*/,
                             LoadLog* /*load_log*/) {
    NOTREACHED();
    return ERR_UNEXPECTED;
  }

  virtual void CancelRequest(RequestHandle request_handle) {
    NOTREACHED();
  }

  virtual int SetPacScript(const GURL& pac_url,
                           const std::string& pac_bytes,
                           CompletionCallback* callback) {
    const Rules::Rule& rule = expects_pac_bytes() ?
        rules_->GetRuleByBytes(pac_bytes) :
        rules_->GetRuleByUrl(pac_url);

    int rv = rule.set_pac_error;
    EXPECT_NE(ERR_UNEXPECTED, rv);

    if (expects_pac_bytes())
      EXPECT_EQ(rule.bytes(), pac_bytes);
    else
      EXPECT_EQ(rule.url, pac_url);

    if (rv == OK) {
      pac_bytes_ = pac_bytes;
      pac_url_ = pac_url;
    }
    return rv;
  }

  const std::string& pac_bytes() const { return pac_bytes_; }
  const GURL& pac_url() const { return pac_url_; }

 private:
  const Rules* rules_;
  std::string pac_bytes_;
  GURL pac_url_;
};

// Succeed using custom PAC script.
TEST(InitProxyResolverTest, CustomPacSucceeds) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.pac_url = GURL("http://custom/proxy.pac");

  Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  scoped_refptr<LoadLog> log(new LoadLog);
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(OK, init.Init(config, &callback, log));
  EXPECT_EQ(rule.bytes(), resolver.pac_bytes());

  // Check the LoadLog was filled correctly.
  EXPECT_EQ(6u, log->events().size());
  ExpectLogContains(log, 0, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 1, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 2, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 3, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 4, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 5, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_END);
}

// Fail downloading the custom PAC script.
TEST(InitProxyResolverTest, CustomPacFails1) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailDownloadRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  scoped_refptr<LoadLog> log(new LoadLog);
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(kFailedDownloading, init.Init(config, &callback, log));
  EXPECT_EQ("", resolver.pac_bytes());

  // Check the LoadLog was filled correctly.
  EXPECT_EQ(4u, log->events().size());
  ExpectLogContains(log, 0, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 1, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 2, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 3, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_END);
}

// Fail parsing the custom PAC script.
TEST(InitProxyResolverTest, CustomPacFails2) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailParsingRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(kFailedParsing, init.Init(config, &callback, NULL));
  EXPECT_EQ("", resolver.pac_bytes());
}

// Succeeds in choosing autodetect (wpad).
TEST(InitProxyResolverTest, AutodetectSuccess) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;

  Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(OK, init.Init(config, &callback, NULL));
  EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
}

// Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailDownloadRule("http://wpad/wpad.dat");
  Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(OK, init.Init(config, &callback, NULL));
  EXPECT_EQ(rule.bytes(), resolver.pac_bytes());
}

// Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailParsingRule("http://wpad/wpad.dat");
  Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  scoped_refptr<LoadLog> log(new LoadLog);
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(OK, init.Init(config, &callback, log));
  EXPECT_EQ(rule.bytes(), resolver.pac_bytes());

  // Check the LoadLog was filled correctly.
  // (Note that the Fetch and Set states are repeated since both WPAD and custom
  // PAC scripts are tried).
  EXPECT_EQ(10u, log->events().size());
  ExpectLogContains(log, 0, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 1, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 2, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 3, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 4, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 5, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 6, LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 7, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_BEGIN);
  ExpectLogContains(log, 8, LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT,
                    LoadLog::PHASE_END);
  ExpectLogContains(log, 9, LoadLog::TYPE_INIT_PROXY_RESOLVER,
                    LoadLog::PHASE_END);
}

// Fails at WPAD (downloading), and fails at custom PAC (downloading).
TEST(InitProxyResolverTest, AutodetectFailCustomFails1) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailDownloadRule("http://wpad/wpad.dat");
  rules.AddFailDownloadRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(kFailedDownloading, init.Init(config, &callback, NULL));
  EXPECT_EQ("", resolver.pac_bytes());
}

// Fails at WPAD (downloading), and fails at custom PAC (parsing).
TEST(InitProxyResolverTest, AutodetectFailCustomFails2) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailDownloadRule("http://wpad/wpad.dat");
  rules.AddFailParsingRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(kFailedParsing, init.Init(config, &callback, NULL));
  EXPECT_EQ("", resolver.pac_bytes());
}

// Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
// This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver
// that doesn't |expects_pac_bytes|.
TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) {
  Rules rules;
  RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/);
  RuleBasedProxyScriptFetcher fetcher(&rules);

  ProxyConfig config;
  config.auto_detect = true;
  config.pac_url = GURL("http://custom/proxy.pac");

  rules.AddFailParsingRule("");  // Autodetect.
  Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");

  TestCompletionCallback callback;
  InitProxyResolver init(&resolver, &fetcher);
  EXPECT_EQ(OK, init.Init(config, &callback, NULL));
  EXPECT_EQ(rule.url, resolver.pac_url());
}

}  // namespace
}  // namespace net