1// Copyright (c) 2012 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/registry_controlled_domains/registry_controlled_domain.h"
6#include "testing/gtest/include/gtest/gtest.h"
7#include "url/gurl.h"
8
9namespace {
10namespace test1 {
11#include "net/base/registry_controlled_domains/effective_tld_names_unittest1-inc.cc"
12}
13namespace test2 {
14#include "net/base/registry_controlled_domains/effective_tld_names_unittest2-inc.cc"
15}
16namespace test3 {
17#include "net/base/registry_controlled_domains/effective_tld_names_unittest3-inc.cc"
18}
19namespace test4 {
20#include "net/base/registry_controlled_domains/effective_tld_names_unittest4-inc.cc"
21}
22namespace test5 {
23#include "net/base/registry_controlled_domains/effective_tld_names_unittest5-inc.cc"
24}
25namespace test6 {
26#include "net/base/registry_controlled_domains/effective_tld_names_unittest6-inc.cc"
27}
28}  // namespace
29
30namespace net {
31namespace registry_controlled_domains {
32namespace {
33
34std::string GetDomainFromURL(const std::string& url) {
35  return GetDomainAndRegistry(GURL(url), EXCLUDE_PRIVATE_REGISTRIES);
36}
37
38std::string GetDomainFromHost(const std::string& host) {
39  return GetDomainAndRegistry(host, EXCLUDE_PRIVATE_REGISTRIES);
40}
41
42size_t GetRegistryLengthFromURL(
43    const std::string& url,
44    UnknownRegistryFilter unknown_filter) {
45  return GetRegistryLength(GURL(url),
46                           unknown_filter,
47                           EXCLUDE_PRIVATE_REGISTRIES);
48}
49
50size_t GetRegistryLengthFromURLIncludingPrivate(
51    const std::string& url,
52    UnknownRegistryFilter unknown_filter) {
53  return GetRegistryLength(GURL(url),
54                           unknown_filter,
55                           INCLUDE_PRIVATE_REGISTRIES);
56}
57
58size_t GetRegistryLengthFromHost(
59    const std::string& host,
60    UnknownRegistryFilter unknown_filter) {
61  return GetRegistryLength(host, unknown_filter, EXCLUDE_PRIVATE_REGISTRIES);
62}
63
64size_t GetRegistryLengthFromHostIncludingPrivate(
65    const std::string& host,
66    UnknownRegistryFilter unknown_filter) {
67  return GetRegistryLength(host, unknown_filter, INCLUDE_PRIVATE_REGISTRIES);
68}
69
70bool CompareDomains(const std::string& url1, const std::string& url2) {
71  GURL g1 = GURL(url1);
72  GURL g2 = GURL(url2);
73  return SameDomainOrHost(g1, g2, EXCLUDE_PRIVATE_REGISTRIES);
74}
75
76}  // namespace
77
78class RegistryControlledDomainTest : public testing::Test {
79 protected:
80  template <typename Graph>
81  void UseDomainData(const Graph& graph) {
82    SetFindDomainGraph(graph, sizeof(Graph));
83  }
84
85  virtual void TearDown() { SetFindDomainGraph(); }
86};
87
88TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) {
89  UseDomainData(test1::kDafsa);
90
91  // Test GURL version of GetDomainAndRegistry().
92  EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html"));    // 1
93  EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html"));  // 1
94  EXPECT_EQ("", GetDomainFromURL("http://ac.jp"));                       // 2
95  EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp"));                    // 3
96  EXPECT_EQ("", GetDomainFromURL("http://bar.jp"));                      // 3
97  EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp"));                  // 3 4
98  EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp"));
99                                                                         // 4
100  EXPECT_EQ("pref.bar.jp", GetDomainFromURL("http://baz.pref.bar.jp"));  // 5
101  EXPECT_EQ("b.bar.baz.com.", GetDomainFromURL("http://a.b.bar.baz.com."));
102                                                                         // 6
103  EXPECT_EQ("a.d.c", GetDomainFromURL("http://a.d.c"));                  // 7
104  EXPECT_EQ("a.d.c", GetDomainFromURL("http://.a.d.c"));                 // 7
105  EXPECT_EQ("a.d.c", GetDomainFromURL("http://..a.d.c"));                // 7
106  EXPECT_EQ("b.c", GetDomainFromURL("http://a.b.c"));                    // 7 8
107  EXPECT_EQ("baz.com", GetDomainFromURL("http://baz.com"));              // none
108  EXPECT_EQ("baz.com.", GetDomainFromURL("http://baz.com."));            // none
109
110  EXPECT_EQ("", GetDomainFromURL(std::string()));
111  EXPECT_EQ("", GetDomainFromURL("http://"));
112  EXPECT_EQ("", GetDomainFromURL("file:///C:/file.html"));
113  EXPECT_EQ("", GetDomainFromURL("http://foo.com.."));
114  EXPECT_EQ("", GetDomainFromURL("http://..."));
115  EXPECT_EQ("", GetDomainFromURL("http://192.168.0.1"));
116  EXPECT_EQ("", GetDomainFromURL("http://localhost"));
117  EXPECT_EQ("", GetDomainFromURL("http://localhost."));
118  EXPECT_EQ("", GetDomainFromURL("http:////Comment"));
119
120  // Test std::string version of GetDomainAndRegistry().  Uses the same
121  // underpinnings as the GURL version, so this is really more of a check of
122  // CanonicalizeHost().
123  EXPECT_EQ("baz.jp", GetDomainFromHost("a.baz.jp"));                  // 1
124  EXPECT_EQ("baz.jp.", GetDomainFromHost("a.baz.jp."));                // 1
125  EXPECT_EQ("", GetDomainFromHost("ac.jp"));                           // 2
126  EXPECT_EQ("", GetDomainFromHost("a.bar.jp"));                        // 3
127  EXPECT_EQ("", GetDomainFromHost("bar.jp"));                          // 3
128  EXPECT_EQ("", GetDomainFromHost("baz.bar.jp"));                      // 3 4
129  EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromHost("a.b.baz.bar.jp"));    // 3 4
130  EXPECT_EQ("pref.bar.jp", GetDomainFromHost("baz.pref.bar.jp"));      // 5
131  EXPECT_EQ("b.bar.baz.com.", GetDomainFromHost("a.b.bar.baz.com."));  // 6
132  EXPECT_EQ("a.d.c", GetDomainFromHost("a.d.c"));                      // 7
133  EXPECT_EQ("a.d.c", GetDomainFromHost(".a.d.c"));                     // 7
134  EXPECT_EQ("a.d.c", GetDomainFromHost("..a.d.c"));                    // 7
135  EXPECT_EQ("b.c", GetDomainFromHost("a.b.c"));                        // 7 8
136  EXPECT_EQ("baz.com", GetDomainFromHost("baz.com"));                  // none
137  EXPECT_EQ("baz.com.", GetDomainFromHost("baz.com."));                // none
138
139  EXPECT_EQ("", GetDomainFromHost(std::string()));
140  EXPECT_EQ("", GetDomainFromHost("foo.com.."));
141  EXPECT_EQ("", GetDomainFromHost("..."));
142  EXPECT_EQ("", GetDomainFromHost("192.168.0.1"));
143  EXPECT_EQ("", GetDomainFromHost("localhost."));
144  EXPECT_EQ("", GetDomainFromHost(".localhost."));
145}
146
147TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) {
148  UseDomainData(test1::kDafsa);
149
150  // Test GURL version of GetRegistryLength().
151  EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html",
152                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 1
153  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html",
154                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 1
155  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp",
156                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 2
157  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp",
158                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 3
159  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://bar.jp",
160                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 3
161  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.bar.jp",
162                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 3 4
163  EXPECT_EQ(12U, GetRegistryLengthFromURL("http://a.b.baz.bar.jp",
164                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 4
165  EXPECT_EQ(6U, GetRegistryLengthFromURL("http://baz.pref.bar.jp",
166                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 5
167  EXPECT_EQ(11U, GetRegistryLengthFromURL("http://a.b.bar.baz.com",
168                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 6
169  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.d.c",
170                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 7
171  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://.a.d.c",
172                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 7
173  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://..a.d.c",
174                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 7
175  EXPECT_EQ(1U, GetRegistryLengthFromURL("http://a.b.c",
176                                         EXCLUDE_UNKNOWN_REGISTRIES)); // 7 8
177  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com",
178                                         EXCLUDE_UNKNOWN_REGISTRIES)); // none
179  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com.",
180                                         EXCLUDE_UNKNOWN_REGISTRIES)); // none
181  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://baz.com",
182                                         INCLUDE_UNKNOWN_REGISTRIES)); // none
183  EXPECT_EQ(4U, GetRegistryLengthFromURL("http://baz.com.",
184                                         INCLUDE_UNKNOWN_REGISTRIES)); // none
185
186  EXPECT_EQ(std::string::npos,
187      GetRegistryLengthFromURL(std::string(), EXCLUDE_UNKNOWN_REGISTRIES));
188  EXPECT_EQ(std::string::npos,
189      GetRegistryLengthFromURL("http://", EXCLUDE_UNKNOWN_REGISTRIES));
190  EXPECT_EQ(std::string::npos,
191      GetRegistryLengthFromURL("file:///C:/file.html",
192                               EXCLUDE_UNKNOWN_REGISTRIES));
193  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://foo.com..",
194                                         EXCLUDE_UNKNOWN_REGISTRIES));
195  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://...",
196                                         EXCLUDE_UNKNOWN_REGISTRIES));
197  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://192.168.0.1",
198                                         EXCLUDE_UNKNOWN_REGISTRIES));
199  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost",
200                                         EXCLUDE_UNKNOWN_REGISTRIES));
201  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost",
202                                         INCLUDE_UNKNOWN_REGISTRIES));
203  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.",
204                                         EXCLUDE_UNKNOWN_REGISTRIES));
205  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.",
206                                         INCLUDE_UNKNOWN_REGISTRIES));
207  EXPECT_EQ(0U, GetRegistryLengthFromURL("http:////Comment",
208                                         EXCLUDE_UNKNOWN_REGISTRIES));
209
210  // Test std::string version of GetRegistryLength().  Uses the same
211  // underpinnings as the GURL version, so this is really more of a check of
212  // CanonicalizeHost().
213  EXPECT_EQ(2U, GetRegistryLengthFromHost("a.baz.jp",
214                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 1
215  EXPECT_EQ(3U, GetRegistryLengthFromHost("a.baz.jp.",
216                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 1
217  EXPECT_EQ(0U, GetRegistryLengthFromHost("ac.jp",
218                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 2
219  EXPECT_EQ(0U, GetRegistryLengthFromHost("a.bar.jp",
220                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 3
221  EXPECT_EQ(0U, GetRegistryLengthFromHost("bar.jp",
222                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 3
223  EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.bar.jp",
224                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 3 4
225  EXPECT_EQ(12U, GetRegistryLengthFromHost("a.b.baz.bar.jp",
226                                           EXCLUDE_UNKNOWN_REGISTRIES)); // 4
227  EXPECT_EQ(6U, GetRegistryLengthFromHost("baz.pref.bar.jp",
228                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 5
229  EXPECT_EQ(11U, GetRegistryLengthFromHost("a.b.bar.baz.com",
230                                           EXCLUDE_UNKNOWN_REGISTRIES)); // 6
231  EXPECT_EQ(3U, GetRegistryLengthFromHost("a.d.c",
232                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 7
233  EXPECT_EQ(3U, GetRegistryLengthFromHost(".a.d.c",
234                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 7
235  EXPECT_EQ(3U, GetRegistryLengthFromHost("..a.d.c",
236                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 7
237  EXPECT_EQ(1U, GetRegistryLengthFromHost("a.b.c",
238                                          EXCLUDE_UNKNOWN_REGISTRIES));  // 7 8
239  EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com",
240                                          EXCLUDE_UNKNOWN_REGISTRIES));  // none
241  EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com.",
242                                          EXCLUDE_UNKNOWN_REGISTRIES));  // none
243  EXPECT_EQ(3U, GetRegistryLengthFromHost("baz.com",
244                                          INCLUDE_UNKNOWN_REGISTRIES));  // none
245  EXPECT_EQ(4U, GetRegistryLengthFromHost("baz.com.",
246                                          INCLUDE_UNKNOWN_REGISTRIES));  // none
247
248  EXPECT_EQ(std::string::npos,
249      GetRegistryLengthFromHost(std::string(), EXCLUDE_UNKNOWN_REGISTRIES));
250  EXPECT_EQ(0U, GetRegistryLengthFromHost("foo.com..",
251                                          EXCLUDE_UNKNOWN_REGISTRIES));
252  EXPECT_EQ(0U, GetRegistryLengthFromHost("..",
253                                          EXCLUDE_UNKNOWN_REGISTRIES));
254  EXPECT_EQ(0U, GetRegistryLengthFromHost("192.168.0.1",
255                                          EXCLUDE_UNKNOWN_REGISTRIES));
256  EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost",
257                                          EXCLUDE_UNKNOWN_REGISTRIES));
258  EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost",
259                                          INCLUDE_UNKNOWN_REGISTRIES));
260  EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.",
261                                          EXCLUDE_UNKNOWN_REGISTRIES));
262  EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.",
263                                          INCLUDE_UNKNOWN_REGISTRIES));
264}
265
266TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) {
267  UseDomainData(test2::kDafsa);
268
269  EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
270                             "http://a.b.bar.jp/file.html"));  // b.bar.jp
271  EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
272                             "http://b.b.bar.jp/file.html"));  // b.bar.jp
273  EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html",     // foo.jp
274                              "http://a.not.jp/file.html"));   // not.jp
275  EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html",     // foo.jp
276                              "http://a.foo.jp./file.html"));  // foo.jp.
277  EXPECT_FALSE(CompareDomains("http://a.com/file.html",        // a.com
278                              "http://b.com/file.html"));      // b.com
279  EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
280                             "http://b.x.com/file.html"));     // x.com
281  EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
282                             "http://.x.com/file.html"));      // x.com
283  EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
284                             "http://..b.x.com/file.html"));   // x.com
285  EXPECT_TRUE(CompareDomains("http://intranet/file.html",
286                             "http://intranet/file.html"));    // intranet
287  EXPECT_TRUE(CompareDomains("http://127.0.0.1/file.html",
288                             "http://127.0.0.1/file.html"));   // 127.0.0.1
289  EXPECT_FALSE(CompareDomains("http://192.168.0.1/file.html",  // 192.168.0.1
290                              "http://127.0.0.1/file.html"));  // 127.0.0.1
291  EXPECT_FALSE(CompareDomains("file:///C:/file.html",
292                              "file:///C:/file.html"));        // no host
293}
294
295TEST_F(RegistryControlledDomainTest, TestDefaultData) {
296  // Note that no data is set: we're using the default rules.
297  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com",
298                                         EXCLUDE_UNKNOWN_REGISTRIES));
299  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu",
300                                         EXCLUDE_UNKNOWN_REGISTRIES));
301  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ustreas.gov",
302                                         EXCLUDE_UNKNOWN_REGISTRIES));
303  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://icann.net",
304                                         EXCLUDE_UNKNOWN_REGISTRIES));
305  EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org",
306                                         EXCLUDE_UNKNOWN_REGISTRIES));
307  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.notavaliddomain",
308                                         EXCLUDE_UNKNOWN_REGISTRIES));
309  EXPECT_EQ(15U, GetRegistryLengthFromURL("http://nowhere.notavaliddomain",
310                                         INCLUDE_UNKNOWN_REGISTRIES));
311}
312
313TEST_F(RegistryControlledDomainTest, TestPrivateRegistryHandling) {
314  UseDomainData(test1::kDafsa);
315
316  // Testing the same dataset for INCLUDE_PRIVATE_REGISTRIES and
317  // EXCLUDE_PRIVATE_REGISTRIES arguments.
318  // For the domain data used for this test, the private registries are
319  // 'priv.no' and 'private'.
320
321  // Non-private registries.
322  EXPECT_EQ(2U, GetRegistryLengthFromURL("http://priv.no",
323                                         EXCLUDE_UNKNOWN_REGISTRIES));
324  EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.priv.no",
325                                         EXCLUDE_UNKNOWN_REGISTRIES));
326  EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.jp",
327                                         EXCLUDE_UNKNOWN_REGISTRIES));
328  EXPECT_EQ(2U, GetRegistryLengthFromURL("http://www.foo.jp",
329                                         EXCLUDE_UNKNOWN_REGISTRIES));
330  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://private",
331                                         EXCLUDE_UNKNOWN_REGISTRIES));
332  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://foo.private",
333                                         EXCLUDE_UNKNOWN_REGISTRIES));
334  EXPECT_EQ(0U, GetRegistryLengthFromURL("http://private",
335                                         INCLUDE_UNKNOWN_REGISTRIES));
336  EXPECT_EQ(7U, GetRegistryLengthFromURL("http://foo.private",
337                                         INCLUDE_UNKNOWN_REGISTRIES));
338
339  // Private registries.
340  EXPECT_EQ(0U,
341      GetRegistryLengthFromURLIncludingPrivate("http://priv.no",
342                                               EXCLUDE_UNKNOWN_REGISTRIES));
343  EXPECT_EQ(7U,
344      GetRegistryLengthFromURLIncludingPrivate("http://foo.priv.no",
345                                               EXCLUDE_UNKNOWN_REGISTRIES));
346  EXPECT_EQ(2U,
347      GetRegistryLengthFromURLIncludingPrivate("http://foo.jp",
348                                               EXCLUDE_UNKNOWN_REGISTRIES));
349  EXPECT_EQ(2U,
350      GetRegistryLengthFromURLIncludingPrivate("http://www.foo.jp",
351                                               EXCLUDE_UNKNOWN_REGISTRIES));
352  EXPECT_EQ(0U,
353      GetRegistryLengthFromURLIncludingPrivate("http://private",
354                                               EXCLUDE_UNKNOWN_REGISTRIES));
355  EXPECT_EQ(7U,
356      GetRegistryLengthFromURLIncludingPrivate("http://foo.private",
357                                               EXCLUDE_UNKNOWN_REGISTRIES));
358  EXPECT_EQ(0U,
359      GetRegistryLengthFromURLIncludingPrivate("http://private",
360                                               INCLUDE_UNKNOWN_REGISTRIES));
361  EXPECT_EQ(7U,
362      GetRegistryLengthFromURLIncludingPrivate("http://foo.private",
363                                               INCLUDE_UNKNOWN_REGISTRIES));
364}
365
366TEST_F(RegistryControlledDomainTest, TestDafsaTwoByteOffsets) {
367  UseDomainData(test3::kDafsa);
368
369  // Testing to lookup keys in a DAFSA with two byte offsets.
370  // This DAFSA is constructed so that labels begin and end with unique
371  // characters, which makes it impossible to merge labels. Each inner node
372  // is about 100 bytes and a one byte offset can at most add 64 bytes to
373  // previous offset. Thus the paths must go over two byte offsets.
374
375  const char* key0 =
376      "a.b.6____________________________________________________"
377      "________________________________________________6";
378  const char* key1 =
379      "a.b.7____________________________________________________"
380      "________________________________________________7";
381  const char* key2 =
382      "a.b.a____________________________________________________"
383      "________________________________________________8";
384
385  EXPECT_EQ(102U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
386  EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
387  EXPECT_EQ(102U,
388            GetRegistryLengthFromHostIncludingPrivate(
389                key1, EXCLUDE_UNKNOWN_REGISTRIES));
390  EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
391}
392
393TEST_F(RegistryControlledDomainTest, TestDafsaThreeByteOffsets) {
394  UseDomainData(test4::kDafsa);
395
396  // Testing to lookup keys in a DAFSA with three byte offsets.
397  // This DAFSA is constructed so that labels begin and end with unique
398  // characters, which makes it impossible to merge labels. The byte array
399  // has a size of ~54k. A two byte offset can add at most add 8k to the
400  // previous offset. Since we can skip only forward in memory, the nodes
401  // representing the return values must be located near the end of the byte
402  // array. The probability that we can reach from an arbitrary inner node to
403  // a return value without using a three byte offset is small (but not zero).
404  // The test is repeated with some different keys and with a reasonable
405  // probability at least one of the tested paths has go over a three byte
406  // offset.
407
408  const char* key0 =
409      "a.b.Z6___________________________________________________"
410      "_________________________________________________Z6";
411  const char* key1 =
412      "a.b.Z7___________________________________________________"
413      "_________________________________________________Z7";
414  const char* key2 =
415      "a.b.Za___________________________________________________"
416      "_________________________________________________Z8";
417
418  EXPECT_EQ(104U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
419  EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
420  EXPECT_EQ(104U,
421            GetRegistryLengthFromHostIncludingPrivate(
422                key1, EXCLUDE_UNKNOWN_REGISTRIES));
423  EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
424}
425
426TEST_F(RegistryControlledDomainTest, TestDafsaJoinedPrefixes) {
427  UseDomainData(test5::kDafsa);
428
429  // Testing to lookup keys in a DAFSA with compressed prefixes.
430  // This DAFSA is constructed from words with similar prefixes but distinct
431  // suffixes. The DAFSA will then form a trie with the implicit source node
432  // as root.
433
434  const char* key0 = "a.b.ai";
435  const char* key1 = "a.b.bj";
436  const char* key2 = "a.b.aak";
437  const char* key3 = "a.b.bbl";
438  const char* key4 = "a.b.aaa";
439  const char* key5 = "a.b.bbb";
440  const char* key6 = "a.b.aaaam";
441  const char* key7 = "a.b.bbbbn";
442
443  EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
444  EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
445  EXPECT_EQ(2U,
446            GetRegistryLengthFromHostIncludingPrivate(
447                key1, EXCLUDE_UNKNOWN_REGISTRIES));
448  EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
449  EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES));
450  EXPECT_EQ(3U,
451            GetRegistryLengthFromHostIncludingPrivate(
452                key3, EXCLUDE_UNKNOWN_REGISTRIES));
453  EXPECT_EQ(0U,
454            GetRegistryLengthFromHostIncludingPrivate(
455                key4, EXCLUDE_UNKNOWN_REGISTRIES));
456  EXPECT_EQ(0U,
457            GetRegistryLengthFromHostIncludingPrivate(
458                key5, EXCLUDE_UNKNOWN_REGISTRIES));
459  EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES));
460  EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES));
461}
462
463TEST_F(RegistryControlledDomainTest, TestDafsaJoinedSuffixes) {
464  UseDomainData(test6::kDafsa);
465
466  // Testing to lookup keys in a DAFSA with compressed suffixes.
467  // This DAFSA is constructed from words with similar suffixes but distinct
468  // prefixes. The DAFSA will then form a trie with the implicit sink node as
469  // root.
470
471  const char* key0 = "a.b.ia";
472  const char* key1 = "a.b.jb";
473  const char* key2 = "a.b.kaa";
474  const char* key3 = "a.b.lbb";
475  const char* key4 = "a.b.aaa";
476  const char* key5 = "a.b.bbb";
477  const char* key6 = "a.b.maaaa";
478  const char* key7 = "a.b.nbbbb";
479
480  EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
481  EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
482  EXPECT_EQ(2U,
483            GetRegistryLengthFromHostIncludingPrivate(
484                key1, EXCLUDE_UNKNOWN_REGISTRIES));
485  EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
486  EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES));
487  EXPECT_EQ(3U,
488            GetRegistryLengthFromHostIncludingPrivate(
489                key3, EXCLUDE_UNKNOWN_REGISTRIES));
490  EXPECT_EQ(0U,
491            GetRegistryLengthFromHostIncludingPrivate(
492                key4, EXCLUDE_UNKNOWN_REGISTRIES));
493  EXPECT_EQ(0U,
494            GetRegistryLengthFromHostIncludingPrivate(
495                key5, EXCLUDE_UNKNOWN_REGISTRIES));
496  EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES));
497  EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES));
498}
499}  // namespace registry_controlled_domains
500}  // namespace net
501