OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/lookup_string_in_fixed_set.h" | 5 #include "net/base/lookup_string_in_fixed_set.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
| 8 |
| 9 #include <algorithm> |
| 10 #include <limits> |
8 #include <ostream> | 11 #include <ostream> |
| 12 #include <utility> |
| 13 #include <vector> |
9 | 14 |
| 15 #include "base/base_paths.h" |
| 16 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" |
| 18 #include "base/path_service.h" |
| 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
11 | 22 |
12 namespace net { | 23 namespace net { |
13 namespace { | 24 namespace { |
14 namespace test1 { | 25 namespace test1 { |
15 #include "net/base/registry_controlled_domains/effective_tld_names_unittest1-inc
.cc" | 26 #include "net/base/registry_controlled_domains/effective_tld_names_unittest1-inc
.cc" |
16 } | 27 } |
17 namespace test3 { | 28 namespace test3 { |
18 #include "net/base/registry_controlled_domains/effective_tld_names_unittest3-inc
.cc" | 29 #include "net/base/registry_controlled_domains/effective_tld_names_unittest3-inc
.cc" |
19 } | 30 } |
(...skipping 29 matching lines...) Expand all Loading... |
49 TEST_P(Dafsa1Test, BasicTest) { | 60 TEST_P(Dafsa1Test, BasicTest) { |
50 const Expectation& param = GetParam(); | 61 const Expectation& param = GetParam(); |
51 EXPECT_EQ(param.value, LookupInGraph(test1::kDafsa, param.key)); | 62 EXPECT_EQ(param.value, LookupInGraph(test1::kDafsa, param.key)); |
52 } | 63 } |
53 | 64 |
54 const Expectation kBasicTestCases[] = { | 65 const Expectation kBasicTestCases[] = { |
55 {"", -1}, {"j", -1}, {"jp", 0}, {"jjp", -1}, {"jpp", -1}, | 66 {"", -1}, {"j", -1}, {"jp", 0}, {"jjp", -1}, {"jpp", -1}, |
56 {"bar.jp", 2}, {"pref.bar.jp", 1}, {"c", 2}, {"b.c", 1}, {"priv.no", 4}, | 67 {"bar.jp", 2}, {"pref.bar.jp", 1}, {"c", 2}, {"b.c", 1}, {"priv.no", 4}, |
57 }; | 68 }; |
58 | 69 |
| 70 // Helper function for EnumerateDafsaLanaguage. |
| 71 void RecursivelyEnumerateDafsaLanguage(const FixedSetIncrementalLookup& lookup, |
| 72 std::vector<char>* sequence, |
| 73 std::vector<std::string>* language) { |
| 74 int result = lookup.GetResultForCurrentSequence(); |
| 75 if (result != kDafsaNotFound) { |
| 76 std::string line(sequence->begin(), sequence->end()); |
| 77 line += base::StringPrintf(", %d", result); |
| 78 language->emplace_back(std::move(line)); |
| 79 } |
| 80 // Try appending each char value. |
| 81 for (char c = std::numeric_limits<char>::min();; ++c) { |
| 82 FixedSetIncrementalLookup continued_lookup = lookup; |
| 83 if (continued_lookup.Advance(c)) { |
| 84 sequence->push_back(c); |
| 85 size_t saved_language_size = language->size(); |
| 86 RecursivelyEnumerateDafsaLanguage(continued_lookup, sequence, language); |
| 87 CHECK_LT(saved_language_size, language->size()) |
| 88 << "DAFSA includes a branch to nowhere at node: " |
| 89 << std::string(sequence->begin(), sequence->end()); |
| 90 sequence->pop_back(); |
| 91 } |
| 92 if (c == std::numeric_limits<char>::max()) |
| 93 break; |
| 94 } |
| 95 } |
| 96 |
| 97 // Uses FixedSetIncrementalLookup to build a vector of every string in the |
| 98 // language of the DAFSA. |
| 99 template <typename Graph> |
| 100 std::vector<std::string> EnumerateDafsaLanguage(const Graph& graph) { |
| 101 FixedSetIncrementalLookup query(graph, sizeof(Graph)); |
| 102 std::vector<char> sequence; |
| 103 std::vector<std::string> language; |
| 104 RecursivelyEnumerateDafsaLanguage(query, &sequence, &language); |
| 105 return language; |
| 106 } |
| 107 |
59 INSTANTIATE_TEST_CASE_P(LookupStringInFixedSetTest, | 108 INSTANTIATE_TEST_CASE_P(LookupStringInFixedSetTest, |
60 Dafsa1Test, | 109 Dafsa1Test, |
61 ::testing::ValuesIn(kBasicTestCases)); | 110 ::testing::ValuesIn(kBasicTestCases)); |
62 | 111 |
63 class Dafsa3Test : public LookupStringInFixedSetTest {}; | 112 class Dafsa3Test : public LookupStringInFixedSetTest {}; |
64 | 113 |
65 // This DAFSA is constructed so that labels begin and end with unique | 114 // This DAFSA is constructed so that labels begin and end with unique |
66 // characters, which makes it impossible to merge labels. Each inner node | 115 // characters, which makes it impossible to merge labels. Each inner node |
67 // is about 100 bytes and a one byte offset can at most add 64 bytes to | 116 // is about 100 bytes and a one byte offset can at most add 64 bytes to |
68 // previous offset. Thus the paths must go over two byte offsets. | 117 // previous offset. Thus the paths must go over two byte offsets. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 200 |
152 const Expectation kJoinedSuffixesTestCases[] = { | 201 const Expectation kJoinedSuffixesTestCases[] = { |
153 {"ia", 0}, {"jb", 4}, {"kaa", 0}, {"lbb", 4}, | 202 {"ia", 0}, {"jb", 4}, {"kaa", 0}, {"lbb", 4}, |
154 {"aaa", -1}, {"bbb", -1}, {"maaaa", 0}, {"nbbbb", 0}, | 203 {"aaa", -1}, {"bbb", -1}, {"maaaa", 0}, {"nbbbb", 0}, |
155 }; | 204 }; |
156 | 205 |
157 INSTANTIATE_TEST_CASE_P(LookupStringInFixedSetTest, | 206 INSTANTIATE_TEST_CASE_P(LookupStringInFixedSetTest, |
158 Dafsa6Test, | 207 Dafsa6Test, |
159 ::testing::ValuesIn(kJoinedSuffixesTestCases)); | 208 ::testing::ValuesIn(kJoinedSuffixesTestCases)); |
160 | 209 |
| 210 // Validates that the generated DAFSA contains exactly the same information as |
| 211 // effective_tld_names_unittest1.gperf. |
| 212 TEST(LookupStringInFixedSetTest, Dafsa1EnumerateLanguage) { |
| 213 auto language = EnumerateDafsaLanguage(test1::kDafsa); |
| 214 |
| 215 // These are the lines of effective_tld_names_unittest1.gperf, in sorted |
| 216 // order. |
| 217 std::vector<std::string> expected_language = { |
| 218 "ac.jp, 0", "b.c, 1", "bar.baz.com, 0", "bar.jp, 2", |
| 219 "baz.bar.jp, 2", "c, 2", "jp, 0", "no, 0", |
| 220 "pref.bar.jp, 1", "priv.no, 4", "private, 4", "xn--fiqs8s, 0", |
| 221 }; |
| 222 |
| 223 EXPECT_EQ(expected_language, language); |
| 224 } |
| 225 |
| 226 // Validates that the generated DAFSA contains exactly the same information as |
| 227 // effective_tld_names_unittest5.gperf. |
| 228 TEST(LookupStringInFixedSetTest, Dafsa5EnumerateLanguage) { |
| 229 auto language = EnumerateDafsaLanguage(test5::kDafsa); |
| 230 |
| 231 std::vector<std::string> expected_language = { |
| 232 "aaaam, 0", "aak, 0", "ai, 0", "bbbbn, 0", "bbl, 4", "bj, 4", |
| 233 }; |
| 234 |
| 235 EXPECT_EQ(expected_language, language); |
| 236 } |
| 237 |
| 238 // Validates that the generated DAFSA contains exactly the same information as |
| 239 // effective_tld_names_unittest6.gperf. |
| 240 TEST(LookupStringInFixedSetTest, Dafsa6EnumerateLanguage) { |
| 241 auto language = EnumerateDafsaLanguage(test6::kDafsa); |
| 242 |
| 243 std::vector<std::string> expected_language = { |
| 244 "ia, 0", "jb, 4", "kaa, 0", "lbb, 4", "maaaa, 0", "nbbbb, 0", |
| 245 }; |
| 246 |
| 247 EXPECT_EQ(expected_language, language); |
| 248 } |
| 249 |
161 } // namespace | 250 } // namespace |
162 } // namespace net | 251 } // namespace net |
OLD | NEW |