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.
5 #include "net/base/sdch_dictionary.h"
10 #include "net/base/sdch_problem_codes.h"
11 #include "testing/gtest/include/gtest/gtest.h"
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
;
21 std::set
<int> single_port
;
22 single_port
.insert(1);
24 std::set
<int> dual_port
;
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")));
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")));
51 TEST(SdchDictionaryTest
, CanUse
) {
52 std::set
<int> dual_port
;
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
64 // Not testing specific error codes; that's implementation, not behavior.
66 test_dictionary_1
.CanUse(GURL("http://www.example.com:2/url")));
68 test_dictionary_1
.CanUse(GURL("http://www.google.com:2/url")));
70 test_dictionary_1
.CanUse(GURL("http://www.example.com:4/url")));
72 test_dictionary_1
.CanUse(GURL("http://www.example.com:2/wurl")));
74 test_dictionary_1
.CanUse(GURL("https://www.example.com:2/url")));
76 test_dictionary_1
.CanUse(GURL("ws://www.example.com:2/url")));
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/"));
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.
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"));
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/"));
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"));
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"));
110 // Make sure adding characters to path will also fail.
111 EXPECT_FALSE(PathMatch("/searching", "/search/"));
112 EXPECT_FALSE(PathMatch("/searching", "/search"));
114 // Make sure we're case sensitive.
115 EXPECT_FALSE(PathMatch("/ABC", "/abc"));
116 EXPECT_FALSE(PathMatch("/abc", "/ABC"));
119 TEST(SdchDictionaryTest
, Expired
) {
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());
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());