Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: components/feedback/anonymizer_tool_unittest.cc

Issue 1543633003: Added anonymization patterns for URLs and email addresses (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bug-567870-introduce-anonymizer
Patch Set: Some more tests Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 static 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 AnonymizerTool::AnonymizeCustomPatternWithContext(input, pattern,
28 space);
29 }
30
31 static std::string AnonymizeCustomPatternWithoutContext(
32 const std::string& input,
33 const CustomPatternWithoutContext& pattern,
34 std::map<std::string, std::string>* space) {
35 return AnonymizerTool::AnonymizeCustomPatternWithoutContext(input, pattern,
36 space);
26 } 37 }
27 38
28 AnonymizerTool anonymizer_; 39 AnonymizerTool anonymizer_;
29 }; 40 };
30 41
31 TEST_F(AnonymizerToolTest, Anonymize) { 42 TEST_F(AnonymizerToolTest, Anonymize) {
32 EXPECT_EQ("", anonymizer_.Anonymize("")); 43 EXPECT_EQ("", anonymizer_.Anonymize(""));
33 EXPECT_EQ("foo\nbar\n", anonymizer_.Anonymize("foo\nbar\n")); 44 EXPECT_EQ("foo\nbar\n", anonymizer_.Anonymize("foo\nbar\n"));
34 45
35 // Make sure MAC address anonymization is invoked. 46 // Make sure MAC address anonymization is invoked.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'")); 88 EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'"));
78 EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'")); 89 EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'"));
79 90
80 EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo", 91 EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo",
81 AnonymizeCustomPatterns( 92 AnonymizeCustomPatterns(
82 "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo")); 93 "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo"));
83 94
84 EXPECT_EQ( 95 EXPECT_EQ(
85 "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b", 96 "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b",
86 AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b")); 97 AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b"));
98
99 EXPECT_EQ("<email: 1>",
100 AnonymizeCustomPatterns("foo@bar.com"));
101 EXPECT_EQ("Email: <email: 1>.",
102 AnonymizeCustomPatterns("Email: foo@bar.com."));
103 EXPECT_EQ("Email:\n<email: 2>\n",
104 AnonymizeCustomPatterns("Email:\nfooooo@bar.com\n"));
105
106 EXPECT_EQ("[<IPv6: 1>]", AnonymizeCustomPatterns(
107 "[2001:0db8:0000:0000:0000:ff00:0042:8329]"));
108 EXPECT_EQ("[<IPv6: 2>]",
109 AnonymizeCustomPatterns("[2001:db8:0:0:0:ff00:42:8329]"));
110 EXPECT_EQ("[<IPv6: 3>]", AnonymizeCustomPatterns("[2001:db8::ff00:42:8329]"));
111 EXPECT_EQ("[<IPv6: 4>]", AnonymizeCustomPatterns("[::1]"));
112 EXPECT_EQ("<IPv4: 1>", AnonymizeCustomPatterns("192.168.0.1"));
113
114 EXPECT_EQ("<URL: 1>",
115 AnonymizeCustomPatterns("http://example.com/foo?test=1"));
116 EXPECT_EQ("Foo <URL: 2> Bar",
117 AnonymizeCustomPatterns("Foo http://192.168.0.1/foo?test=1#123 Bar") );
118 const char* kURLs[] = {
119 "http://example.com/foo?test=1",
120 "http://userid:password@example.com:8080",
121 "http://userid:password@example.com:8080/",
122 "http://@example.com",
123 "http://192.168.0.1",
124 "http://192.168.0.1/",
125 "http://اختبار.com",
126 "http://test.com/foo(bar)baz.html",
127 "http://test.com/foo%20bar",
128 "ftp://test:tester@test.com",
129 "chrome://extensions/",
130 "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/options.html",
131 "http://example.com/foo?email=foo@bar.com",
132 };
133 for (size_t i = 0; i < arraysize(kURLs); ++i) {
134 SCOPED_TRACE(kURLs[i]);
135 std::string got = AnonymizeCustomPatterns(kURLs[i]);
136 EXPECT_TRUE(
137 base::StartsWith(got, "<URL: ", base::CompareCase::INSENSITIVE_ASCII));
138 EXPECT_TRUE(base::EndsWith(got, ">", base::CompareCase::INSENSITIVE_ASCII));
139 }
140 // Test that "Android:" is not considered a schema with empty hier part.
141 EXPECT_EQ("The following applies to Android:",
142 AnonymizeCustomPatterns("The following applies to Android:"));
87 } 143 }
88 144
89 TEST_F(AnonymizerToolTest, AnonymizeCustomPattern) { 145 TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithContext) {
90 const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')"; 146 const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')";
91 std::map<std::string, std::string> space; 147 std::map<std::string, std::string> space;
92 EXPECT_EQ("", AnonymizeCustomPattern("", kPattern, &space)); 148 EXPECT_EQ("", AnonymizeCustomPatternWithContext("", kPattern, &space));
93 EXPECT_EQ("foo\nbar\n", 149 EXPECT_EQ("foo\nbar\n",
94 AnonymizeCustomPattern("foo\nbar\n", kPattern, &space)); 150 AnonymizeCustomPatternWithContext("foo\nbar\n", kPattern, &space));
95 EXPECT_EQ("id '1'", AnonymizeCustomPattern("id '2345'", kPattern, &space)); 151 EXPECT_EQ("id '1'",
96 EXPECT_EQ("id '2'", AnonymizeCustomPattern("id '1234'", kPattern, &space)); 152 AnonymizeCustomPatternWithContext("id '2345'", kPattern, &space));
97 EXPECT_EQ("id: '2'", AnonymizeCustomPattern("id: '1234'", kPattern, &space)); 153 EXPECT_EQ("id '2'",
98 EXPECT_EQ("ID: '1'", AnonymizeCustomPattern("ID: '2345'", kPattern, &space)); 154 AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space));
155 EXPECT_EQ("id: '2'",
156 AnonymizeCustomPatternWithContext("id: '1234'", kPattern, &space));
157 EXPECT_EQ("ID: '1'",
158 AnonymizeCustomPatternWithContext("ID: '2345'", kPattern, &space));
99 EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n", 159 EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n",
100 AnonymizeCustomPattern("x1 id '2345' 1x id '1234'\nid '2345'\n", 160 AnonymizeCustomPatternWithContext(
101 kPattern, &space)); 161 "x1 id '2345' 1x id '1234'\nid '2345'\n", kPattern, &space));
102 space.clear(); 162 space.clear();
103 EXPECT_EQ("id '1'", AnonymizeCustomPattern("id '1234'", kPattern, &space)); 163 EXPECT_EQ("id '1'",
164 AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space));
104 165
105 space.clear(); 166 space.clear();
106 EXPECT_EQ("x1z", AnonymizeCustomPattern("xyz", "()(y+)()", &space)); 167 EXPECT_EQ("x1z",
168 AnonymizeCustomPatternWithContext("xyz", "()(y+)()", &space));
169 }
170
171 TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithoutContext) {
172 CustomPatternWithoutContext kPattern = { "pattern", "(o+)" };
173 std::map<std::string, std::string> space;
174 EXPECT_EQ("", AnonymizeCustomPatternWithoutContext("", kPattern, &space));
175 EXPECT_EQ("f<pattern: 1>\nf<pattern: 2>z\nf<pattern: 1>l\n",
176 AnonymizeCustomPatternWithoutContext("fo\nfooz\nfol\n", kPattern,
177 &space));
107 } 178 }
108 179
109 } // namespace feedback 180 } // namespace feedback
OLDNEW
« components/feedback/anonymizer_tool.cc ('K') | « components/feedback/anonymizer_tool.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698