OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "net/base/sdch_dictionary.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "net/base/sdch_problem_codes.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 TEST(SdchDictionaryTest, CanSet) { |
| 16 SdchProblemCode (*CanSet)(const std::string& domain, const std::string& path, |
| 17 const std::set<int>& ports, |
| 18 const GURL& dictionary_url) = |
| 19 SdchDictionary::CanSet; |
| 20 |
| 21 std::set<int> single_port; |
| 22 single_port.insert(1); |
| 23 |
| 24 std::set<int> dual_port; |
| 25 dual_port.insert(2); |
| 26 dual_port.insert(3); |
| 27 |
| 28 // Not testing specific error codes; that's implementation, not behavior. |
| 29 EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", std::set<int>(), |
| 30 GURL("http://www.google.com/dictionary"))); |
| 31 EXPECT_NE(SDCH_OK, CanSet("", "", std::set<int>(), |
| 32 GURL("http://www.google.com/dictionary"))); |
| 33 EXPECT_NE(SDCH_OK, |
| 34 CanSet("com", "", std::set<int>(), GURL("http://com/dictionary"))); |
| 35 EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", std::set<int>(), |
| 36 GURL("http://www.simple.com/dictionary"))); |
| 37 EXPECT_EQ(SDCH_OK, CanSet(".google.com", "", std::set<int>(), |
| 38 GURL("http://www.google.com/dictionary"))); |
| 39 EXPECT_NE(SDCH_OK, CanSet("google.com", "", std::set<int>(), |
| 40 GURL("http://www.google.com/dictionary"))); |
| 41 EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", single_port, |
| 42 GURL("http://www.google.com:1/dictionary"))); |
| 43 EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", dual_port, |
| 44 GURL("http://www.google.com:2/dictionary"))); |
| 45 EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", single_port, |
| 46 GURL("http://www.google.com:10/dictionary"))); |
| 47 EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", dual_port, |
| 48 GURL("http://www.google.com:10/dictionary"))); |
| 49 } |
| 50 |
| 51 TEST(SdchDictionaryTest, CanUse) { |
| 52 std::set<int> dual_port; |
| 53 dual_port.insert(2); |
| 54 dual_port.insert(3); |
| 55 |
| 56 SdchDictionary test_dictionary_1( |
| 57 "xyzzy", 0u, // text, offset |
| 58 "ch", "sh", // client hash, server hash |
| 59 GURL("http://www.example.com"), "www.example.com", |
| 60 "/url", // domain, path |
| 61 base::Time::Now() + base::TimeDelta::FromSeconds(1), // expiration |
| 62 dual_port); // ports |
| 63 |
| 64 // Not testing specific error codes; that's implementation, not behavior. |
| 65 EXPECT_EQ(SDCH_OK, |
| 66 test_dictionary_1.CanUse(GURL("http://www.example.com:2/url"))); |
| 67 EXPECT_NE(SDCH_OK, |
| 68 test_dictionary_1.CanUse(GURL("http://www.google.com:2/url"))); |
| 69 EXPECT_NE(SDCH_OK, |
| 70 test_dictionary_1.CanUse(GURL("http://www.example.com:4/url"))); |
| 71 EXPECT_NE(SDCH_OK, |
| 72 test_dictionary_1.CanUse(GURL("http://www.example.com:2/wurl"))); |
| 73 EXPECT_NE(SDCH_OK, |
| 74 test_dictionary_1.CanUse(GURL("https://www.example.com:2/url"))); |
| 75 EXPECT_NE(SDCH_OK, |
| 76 test_dictionary_1.CanUse(GURL("ws://www.example.com:2/url"))); |
| 77 } |
| 78 |
| 79 TEST(SdchDictionaryTest, PathMatch) { |
| 80 bool (*PathMatch)(const std::string& path, const std::string& restriction) = |
| 81 SdchDictionary::PathMatch; |
| 82 // Perfect match is supported. |
| 83 EXPECT_TRUE(PathMatch("/search", "/search")); |
| 84 EXPECT_TRUE(PathMatch("/search/", "/search/")); |
| 85 |
| 86 // Prefix only works if last character of restriction is a slash, or first |
| 87 // character in path after a match is a slash. Validate each case separately. |
| 88 |
| 89 // Rely on the slash in the path (not at the end of the restriction). |
| 90 EXPECT_TRUE(PathMatch("/search/something", "/search")); |
| 91 EXPECT_TRUE(PathMatch("/search/s", "/search")); |
| 92 EXPECT_TRUE(PathMatch("/search/other", "/search")); |
| 93 EXPECT_TRUE(PathMatch("/search/something", "/search")); |
| 94 |
| 95 // Rely on the slash at the end of the restriction. |
| 96 EXPECT_TRUE(PathMatch("/search/something", "/search/")); |
| 97 EXPECT_TRUE(PathMatch("/search/s", "/search/")); |
| 98 EXPECT_TRUE(PathMatch("/search/other", "/search/")); |
| 99 EXPECT_TRUE(PathMatch("/search/something", "/search/")); |
| 100 |
| 101 // Make sure less that sufficient prefix match is false. |
| 102 EXPECT_FALSE(PathMatch("/sear", "/search")); |
| 103 EXPECT_FALSE(PathMatch("/", "/search")); |
| 104 EXPECT_FALSE(PathMatch(std::string(), "/search")); |
| 105 |
| 106 // Add examples with several levels of direcories in the restriction. |
| 107 EXPECT_FALSE(PathMatch("/search/something", "search/s")); |
| 108 EXPECT_FALSE(PathMatch("/search/", "/search/s")); |
| 109 |
| 110 // Make sure adding characters to path will also fail. |
| 111 EXPECT_FALSE(PathMatch("/searching", "/search/")); |
| 112 EXPECT_FALSE(PathMatch("/searching", "/search")); |
| 113 |
| 114 // Make sure we're case sensitive. |
| 115 EXPECT_FALSE(PathMatch("/ABC", "/abc")); |
| 116 EXPECT_FALSE(PathMatch("/abc", "/ABC")); |
| 117 } |
| 118 |
| 119 TEST(SdchDictionaryTest, Expired) { |
| 120 EXPECT_TRUE( |
| 121 SdchDictionary("xyzzy", 0u, "ch", "sh", GURL("http://www.example.com"), |
| 122 "www.example.com", "/url", |
| 123 base::Time::Now() - base::TimeDelta::FromSeconds(1), |
| 124 std::set<int>()).Expired()); |
| 125 EXPECT_FALSE( |
| 126 SdchDictionary("xyzzy", 0u, "ch", "sh", GURL("http://www.example.com"), |
| 127 "www.example.com", "/url", |
| 128 base::Time::Now() + base::TimeDelta::FromSeconds(1), |
| 129 std::set<int>()).Expired()); |
| 130 } |
| 131 |
| 132 } // namespace net |
OLD | NEW |