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 "components/feedback/anonymizer_tool.h" | 5 #include "components/feedback/anonymizer_tool.h" |
6 | 6 |
7 #include <gtest/gtest.h> | 7 #include <gtest/gtest.h> |
8 | 8 |
| 9 #include "base/strings/string_util.h" |
| 10 |
9 namespace feedback { | 11 namespace feedback { |
10 | 12 |
11 class AnonymizerToolTest : public testing::Test { | 13 class AnonymizerToolTest : public testing::Test { |
12 protected: | 14 protected: |
13 std::string AnonymizeMACAddresses(const std::string& input) { | 15 std::string AnonymizeMACAddresses(const std::string& input) { |
14 return anonymizer_.AnonymizeMACAddresses(input); | 16 return anonymizer_.AnonymizeMACAddresses(input); |
15 } | 17 } |
16 | 18 |
17 std::string AnonymizeCustomPatterns(const std::string& input) { | 19 std::string AnonymizeCustomPatterns(const std::string& input) { |
18 return anonymizer_.AnonymizeCustomPatterns(input); | 20 return anonymizer_.AnonymizeCustomPatterns(input); |
19 } | 21 } |
20 | 22 |
21 static std::string AnonymizeCustomPattern( | 23 std::string AnonymizeCustomPatternWithContext( |
22 const std::string& input, | 24 const std::string& input, |
23 const std::string& pattern, | 25 const std::string& pattern, |
24 std::map<std::string, std::string>* space) { | 26 std::map<std::string, std::string>* space) { |
25 return AnonymizerTool::AnonymizeCustomPattern(input, pattern, space); | 27 return anonymizer_.AnonymizeCustomPatternWithContext(input, pattern, space); |
| 28 } |
| 29 |
| 30 std::string AnonymizeCustomPatternWithoutContext( |
| 31 const std::string& input, |
| 32 const CustomPatternWithoutContext& pattern, |
| 33 std::map<std::string, std::string>* space) { |
| 34 return anonymizer_.AnonymizeCustomPatternWithoutContext(input, pattern, |
| 35 space); |
26 } | 36 } |
27 | 37 |
28 AnonymizerTool anonymizer_; | 38 AnonymizerTool anonymizer_; |
29 }; | 39 }; |
30 | 40 |
31 TEST_F(AnonymizerToolTest, Anonymize) { | 41 TEST_F(AnonymizerToolTest, Anonymize) { |
32 EXPECT_EQ("", anonymizer_.Anonymize("")); | 42 EXPECT_EQ("", anonymizer_.Anonymize("")); |
33 EXPECT_EQ("foo\nbar\n", anonymizer_.Anonymize("foo\nbar\n")); | 43 EXPECT_EQ("foo\nbar\n", anonymizer_.Anonymize("foo\nbar\n")); |
34 | 44 |
35 // Make sure MAC address anonymization is invoked. | 45 // Make sure MAC address anonymization is invoked. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'")); | 87 EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'")); |
78 EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'")); | 88 EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'")); |
79 | 89 |
80 EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo", | 90 EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo", |
81 AnonymizeCustomPatterns( | 91 AnonymizeCustomPatterns( |
82 "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo")); | 92 "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo")); |
83 | 93 |
84 EXPECT_EQ( | 94 EXPECT_EQ( |
85 "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b", | 95 "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b", |
86 AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b")); | 96 AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b")); |
| 97 |
| 98 EXPECT_EQ("<email: 1>", |
| 99 AnonymizeCustomPatterns("foo@bar.com")); |
| 100 EXPECT_EQ("Email: <email: 1>.", |
| 101 AnonymizeCustomPatterns("Email: foo@bar.com.")); |
| 102 EXPECT_EQ("Email:\n<email: 2>\n", |
| 103 AnonymizeCustomPatterns("Email:\nfooooo@bar.com\n")); |
| 104 |
| 105 EXPECT_EQ("[<IPv6: 1>]", AnonymizeCustomPatterns( |
| 106 "[2001:0db8:0000:0000:0000:ff00:0042:8329]")); |
| 107 EXPECT_EQ("[<IPv6: 2>]", |
| 108 AnonymizeCustomPatterns("[2001:db8:0:0:0:ff00:42:8329]")); |
| 109 EXPECT_EQ("[<IPv6: 3>]", AnonymizeCustomPatterns("[2001:db8::ff00:42:8329]")); |
| 110 EXPECT_EQ("[<IPv6: 4>]", AnonymizeCustomPatterns("[::1]")); |
| 111 EXPECT_EQ("<IPv4: 1>", AnonymizeCustomPatterns("192.168.0.1")); |
| 112 |
| 113 EXPECT_EQ("<URL: 1>", |
| 114 AnonymizeCustomPatterns("http://example.com/foo?test=1")); |
| 115 EXPECT_EQ("Foo <URL: 2> Bar", |
| 116 AnonymizeCustomPatterns("Foo http://192.168.0.1/foo?test=1#123 Bar")
); |
| 117 const char* kURLs[] = { |
| 118 "http://example.com/foo?test=1", |
| 119 "http://userid:password@example.com:8080", |
| 120 "http://userid:password@example.com:8080/", |
| 121 "http://@example.com", |
| 122 "http://192.168.0.1", |
| 123 "http://192.168.0.1/", |
| 124 "http://اختبار.com", |
| 125 "http://test.com/foo(bar)baz.html", |
| 126 "http://test.com/foo%20bar", |
| 127 "ftp://test:tester@test.com", |
| 128 "chrome://extensions/", |
| 129 "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/options.html", |
| 130 "http://example.com/foo?email=foo@bar.com", |
| 131 }; |
| 132 for (size_t i = 0; i < arraysize(kURLs); ++i) { |
| 133 SCOPED_TRACE(kURLs[i]); |
| 134 std::string got = AnonymizeCustomPatterns(kURLs[i]); |
| 135 EXPECT_TRUE( |
| 136 base::StartsWith(got, "<URL: ", base::CompareCase::INSENSITIVE_ASCII)); |
| 137 EXPECT_TRUE(base::EndsWith(got, ">", base::CompareCase::INSENSITIVE_ASCII)); |
| 138 } |
| 139 // Test that "Android:" is not considered a schema with empty hier part. |
| 140 EXPECT_EQ("The following applies to Android:", |
| 141 AnonymizeCustomPatterns("The following applies to Android:")); |
87 } | 142 } |
88 | 143 |
89 TEST_F(AnonymizerToolTest, AnonymizeCustomPattern) { | 144 TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithContext) { |
90 const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')"; | 145 const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')"; |
91 std::map<std::string, std::string> space; | 146 std::map<std::string, std::string> space; |
92 EXPECT_EQ("", AnonymizeCustomPattern("", kPattern, &space)); | 147 EXPECT_EQ("", AnonymizeCustomPatternWithContext("", kPattern, &space)); |
93 EXPECT_EQ("foo\nbar\n", | 148 EXPECT_EQ("foo\nbar\n", |
94 AnonymizeCustomPattern("foo\nbar\n", kPattern, &space)); | 149 AnonymizeCustomPatternWithContext("foo\nbar\n", kPattern, &space)); |
95 EXPECT_EQ("id '1'", AnonymizeCustomPattern("id '2345'", kPattern, &space)); | 150 EXPECT_EQ("id '1'", |
96 EXPECT_EQ("id '2'", AnonymizeCustomPattern("id '1234'", kPattern, &space)); | 151 AnonymizeCustomPatternWithContext("id '2345'", kPattern, &space)); |
97 EXPECT_EQ("id: '2'", AnonymizeCustomPattern("id: '1234'", kPattern, &space)); | 152 EXPECT_EQ("id '2'", |
98 EXPECT_EQ("ID: '1'", AnonymizeCustomPattern("ID: '2345'", kPattern, &space)); | 153 AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space)); |
| 154 EXPECT_EQ("id: '2'", |
| 155 AnonymizeCustomPatternWithContext("id: '1234'", kPattern, &space)); |
| 156 EXPECT_EQ("ID: '1'", |
| 157 AnonymizeCustomPatternWithContext("ID: '2345'", kPattern, &space)); |
99 EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n", | 158 EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n", |
100 AnonymizeCustomPattern("x1 id '2345' 1x id '1234'\nid '2345'\n", | 159 AnonymizeCustomPatternWithContext( |
101 kPattern, &space)); | 160 "x1 id '2345' 1x id '1234'\nid '2345'\n", kPattern, &space)); |
102 space.clear(); | 161 space.clear(); |
103 EXPECT_EQ("id '1'", AnonymizeCustomPattern("id '1234'", kPattern, &space)); | 162 EXPECT_EQ("id '1'", |
| 163 AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space)); |
104 | 164 |
105 space.clear(); | 165 space.clear(); |
106 EXPECT_EQ("x1z", AnonymizeCustomPattern("xyz", "()(y+)()", &space)); | 166 EXPECT_EQ("x1z", |
| 167 AnonymizeCustomPatternWithContext("xyz", "()(y+)()", &space)); |
| 168 } |
| 169 |
| 170 TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithoutContext) { |
| 171 CustomPatternWithoutContext kPattern = {"pattern", "(o+)"}; |
| 172 std::map<std::string, std::string> space; |
| 173 EXPECT_EQ("", AnonymizeCustomPatternWithoutContext("", kPattern, &space)); |
| 174 EXPECT_EQ("f<pattern: 1>\nf<pattern: 2>z\nf<pattern: 1>l\n", |
| 175 AnonymizeCustomPatternWithoutContext("fo\nfooz\nfol\n", kPattern, |
| 176 &space)); |
107 } | 177 } |
108 | 178 |
109 } // namespace feedback | 179 } // namespace feedback |
OLD | NEW |