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