summaryrefslogtreecommitdiffstats
path: root/net/base/sdch_dictionary_unittest.cc
blob: d9fd92e7db41ba8e77791f8ed1287607cd219702 (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
// Copyright 2015 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 "net/base/sdch_dictionary.h"

#include <set>
#include <string>

#include "net/base/sdch_problem_codes.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

TEST(SdchDictionaryTest, CanSet) {
  SdchProblemCode (*CanSet)(const std::string& domain, const std::string& path,
                            const std::set<int>& ports,
                            const GURL& dictionary_url) =
      SdchDictionary::CanSet;

  std::set<int> single_port;
  single_port.insert(1);

  std::set<int> dual_port;
  dual_port.insert(2);
  dual_port.insert(3);

  // Not testing specific error codes; that's implementation, not behavior.
  EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", std::set<int>(),
                            GURL("http://www.google.com/dictionary")));
  EXPECT_NE(SDCH_OK, CanSet("", "", std::set<int>(),
                            GURL("http://www.google.com/dictionary")));
  EXPECT_NE(SDCH_OK,
            CanSet("com", "", std::set<int>(), GURL("http://com/dictionary")));
  EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", std::set<int>(),
                            GURL("http://www.simple.com/dictionary")));
  EXPECT_EQ(SDCH_OK, CanSet(".google.com", "", std::set<int>(),
                            GURL("http://www.google.com/dictionary")));
  EXPECT_NE(SDCH_OK, CanSet("google.com", "", std::set<int>(),
                            GURL("http://www.google.com/dictionary")));
  EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", single_port,
                            GURL("http://www.google.com:1/dictionary")));
  EXPECT_EQ(SDCH_OK, CanSet("www.google.com", "", dual_port,
                            GURL("http://www.google.com:2/dictionary")));
  EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", single_port,
                            GURL("http://www.google.com:10/dictionary")));
  EXPECT_NE(SDCH_OK, CanSet("www.google.com", "", dual_port,
                            GURL("http://www.google.com:10/dictionary")));
}

TEST(SdchDictionaryTest, CanUse) {
  std::set<int> dual_port;
  dual_port.insert(2);
  dual_port.insert(3);

  SdchDictionary test_dictionary_1(
      "xyzzy", 0u,  // text, offset
      "ch", "sh",   // client hash, server hash
      GURL("http://www.example.com"), "www.example.com",
      "/url",                                               // domain, path
      base::Time::Now() + base::TimeDelta::FromSeconds(1),  // expiration
      dual_port);                                           // ports

  // Not testing specific error codes; that's implementation, not behavior.
  EXPECT_EQ(SDCH_OK,
            test_dictionary_1.CanUse(GURL("http://www.example.com:2/url")));
  EXPECT_NE(SDCH_OK,
            test_dictionary_1.CanUse(GURL("http://www.google.com:2/url")));
  EXPECT_NE(SDCH_OK,
            test_dictionary_1.CanUse(GURL("http://www.example.com:4/url")));
  EXPECT_NE(SDCH_OK,
            test_dictionary_1.CanUse(GURL("http://www.example.com:2/wurl")));
  EXPECT_NE(SDCH_OK,
            test_dictionary_1.CanUse(GURL("https://www.example.com:2/url")));
  EXPECT_NE(SDCH_OK,
            test_dictionary_1.CanUse(GURL("ws://www.example.com:2/url")));
}

TEST(SdchDictionaryTest, PathMatch) {
  bool (*PathMatch)(const std::string& path, const std::string& restriction) =
      SdchDictionary::PathMatch;
  // Perfect match is supported.
  EXPECT_TRUE(PathMatch("/search", "/search"));
  EXPECT_TRUE(PathMatch("/search/", "/search/"));

  // Prefix only works if last character of restriction is a slash, or first
  // character in path after a match is a slash. Validate each case separately.

  // Rely on the slash in the path (not at the end of the restriction).
  EXPECT_TRUE(PathMatch("/search/something", "/search"));
  EXPECT_TRUE(PathMatch("/search/s", "/search"));
  EXPECT_TRUE(PathMatch("/search/other", "/search"));
  EXPECT_TRUE(PathMatch("/search/something", "/search"));

  // Rely on the slash at the end of the restriction.
  EXPECT_TRUE(PathMatch("/search/something", "/search/"));
  EXPECT_TRUE(PathMatch("/search/s", "/search/"));
  EXPECT_TRUE(PathMatch("/search/other", "/search/"));
  EXPECT_TRUE(PathMatch("/search/something", "/search/"));

  // Make sure less that sufficient prefix match is false.
  EXPECT_FALSE(PathMatch("/sear", "/search"));
  EXPECT_FALSE(PathMatch("/", "/search"));
  EXPECT_FALSE(PathMatch(std::string(), "/search"));

  // Add examples with several levels of direcories in the restriction.
  EXPECT_FALSE(PathMatch("/search/something", "search/s"));
  EXPECT_FALSE(PathMatch("/search/", "/search/s"));

  // Make sure adding characters to path will also fail.
  EXPECT_FALSE(PathMatch("/searching", "/search/"));
  EXPECT_FALSE(PathMatch("/searching", "/search"));

  // Make sure we're case sensitive.
  EXPECT_FALSE(PathMatch("/ABC", "/abc"));
  EXPECT_FALSE(PathMatch("/abc", "/ABC"));
}

TEST(SdchDictionaryTest, Expired) {
  EXPECT_TRUE(
      SdchDictionary("xyzzy", 0u, "ch", "sh", GURL("http://www.example.com"),
                     "www.example.com", "/url",
                     base::Time::Now() - base::TimeDelta::FromSeconds(1),
                     std::set<int>()).Expired());
  EXPECT_FALSE(
      SdchDictionary("xyzzy", 0u, "ch", "sh", GURL("http://www.example.com"),
                     "www.example.com", "/url",
                     base::Time::Now() + base::TimeDelta::FromSeconds(1),
                     std::set<int>()).Expired());
}

}  // namespace net