1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <ostream>
6
7
8#include "base/memory/scoped_ptr.h"
9#include "net/http/http_auth_filter.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "url/gurl.h"
12
13namespace net {
14
15namespace {
16
17static const char* const server_whitelist_array[] = {
18  "google.com",
19  "linkedin.com",
20  "book.com",
21  ".chromium.org",
22  ".gag",
23  "gog"
24};
25
26enum {
27  ALL_SERVERS_MATCH = (1 << arraysize(server_whitelist_array)) - 1
28};
29
30struct UrlData {
31  GURL url;
32  HttpAuth::Target target;
33  bool matches;
34  int match_bits;
35};
36
37static const UrlData urls[] = {
38  { GURL(std::string()), HttpAuth::AUTH_NONE, false, 0 },
39  { GURL("http://foo.cn"), HttpAuth::AUTH_PROXY, true, ALL_SERVERS_MATCH },
40  { GURL("http://foo.cn"), HttpAuth::AUTH_SERVER, false, 0 },
41  { GURL("http://slashdot.org"), HttpAuth::AUTH_NONE, false, 0 },
42  { GURL("http://www.google.com"), HttpAuth::AUTH_SERVER, true, 1 << 0 },
43  { GURL("http://www.google.com"), HttpAuth::AUTH_PROXY, true,
44    ALL_SERVERS_MATCH },
45  { GURL("https://login.facebook.com/login.php?login_attempt=1"),
46    HttpAuth::AUTH_NONE, false, 0 },
47  { GURL("http://codereview.chromium.org/634002/show"), HttpAuth::AUTH_SERVER,
48    true, 1 << 3 },
49  { GURL("http://code.google.com/p/chromium/issues/detail?id=34505"),
50    HttpAuth::AUTH_SERVER, true, 1 << 0 },
51  { GURL("http://code.google.com/p/chromium/issues/list?can=2&q=label:"
52         "spdy&sort=owner&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20"
53         "Summary%20Modified%20Owner%20Mstone%20OS"),
54    HttpAuth::AUTH_SERVER, true, 1 << 3 },
55  { GURL("https://www.linkedin.com/secure/login?trk=hb_signin"),
56    HttpAuth::AUTH_SERVER, true, 1 << 1 },
57  { GURL("http://www.linkedin.com/mbox?displayMBoxItem=&"
58         "itemID=I1717980652_2&trk=COMM_HP_MSGVW_MEBC_MEBC&goback=.hom"),
59    HttpAuth::AUTH_SERVER, true, 1 << 1 },
60  { GURL("http://news.slashdot.org/story/10/02/18/190236/"
61         "New-Plan-Lets-Top-HS-Students-Graduate-2-Years-Early"),
62    HttpAuth::AUTH_PROXY, true, ALL_SERVERS_MATCH },
63  { GURL("http://codereview.chromium.org/646068/diff/4001/5003"),
64    HttpAuth::AUTH_SERVER, true, 1 << 3 },
65  { GURL("http://codereview.chromium.gag/646068/diff/4001/5003"),
66    HttpAuth::AUTH_SERVER, true, 1 << 4 },
67  { GURL("http://codereview.chromium.gog/646068/diff/4001/5003"),
68    HttpAuth::AUTH_SERVER, true, 1 << 5 },
69};
70
71}   // namespace
72
73TEST(HttpAuthFilterTest, EmptyFilter) {
74  // Create an empty filter
75  HttpAuthFilterWhitelist filter((std::string()));
76  for (size_t i = 0; i < arraysize(urls); i++) {
77    EXPECT_EQ(urls[i].target == HttpAuth::AUTH_PROXY,
78              filter.IsValid(urls[i].url, urls[i].target))
79        << " " << i << ": " << urls[i].url;
80  }
81}
82
83TEST(HttpAuthFilterTest, NonEmptyFilter) {
84  // Create an non-empty filter
85  std::string server_whitelist_filter_string;
86  for (size_t i = 0; i < arraysize(server_whitelist_array); ++i) {
87    if (!server_whitelist_filter_string.empty())
88      server_whitelist_filter_string += ",";
89    server_whitelist_filter_string += "*";
90    server_whitelist_filter_string += server_whitelist_array[i];
91  }
92  HttpAuthFilterWhitelist filter(server_whitelist_filter_string);
93  for (size_t i = 0; i < arraysize(urls); i++) {
94    EXPECT_EQ(urls[i].matches, filter.IsValid(urls[i].url, urls[i].target))
95        << " " << i << ": " << urls[i].url;
96  }
97}
98
99}   // namespace net
100