battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/feedback/anonymizer_tool.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 9 | #include "base/strings/string_util.h" |
| 10 | |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 11 | namespace feedback { |
| 12 | |
| 13 | class AnonymizerToolTest : public testing::Test { |
| 14 | protected: |
| 15 | std::string AnonymizeMACAddresses(const std::string& input) { |
| 16 | return anonymizer_.AnonymizeMACAddresses(input); |
| 17 | } |
| 18 | |
| 19 | std::string AnonymizeCustomPatterns(const std::string& input) { |
| 20 | return anonymizer_.AnonymizeCustomPatterns(input); |
| 21 | } |
| 22 | |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 23 | std::string AnonymizeCustomPatternWithContext( |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 24 | const std::string& input, |
| 25 | const std::string& pattern, |
| 26 | std::map<std::string, std::string>* space) { |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 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); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | AnonymizerTool anonymizer_; |
| 39 | }; |
| 40 | |
| 41 | TEST_F(AnonymizerToolTest, Anonymize) { |
| 42 | EXPECT_EQ("", anonymizer_.Anonymize("")); |
| 43 | EXPECT_EQ("foo\nbar\n", anonymizer_.Anonymize("foo\nbar\n")); |
| 44 | |
| 45 | // Make sure MAC address anonymization is invoked. |
| 46 | EXPECT_EQ("02:46:8a:00:00:01", anonymizer_.Anonymize("02:46:8a:ce:13:57")); |
| 47 | |
| 48 | // Make sure custom pattern anonymization is invoked. |
| 49 | EXPECT_EQ("Cell ID: '1'", AnonymizeCustomPatterns("Cell ID: 'A1B2'")); |
| 50 | } |
| 51 | |
| 52 | TEST_F(AnonymizerToolTest, AnonymizeMACAddresses) { |
| 53 | EXPECT_EQ("", AnonymizeMACAddresses("")); |
| 54 | EXPECT_EQ("foo\nbar\n", AnonymizeMACAddresses("foo\nbar\n")); |
| 55 | EXPECT_EQ("11:22:33:44:55", AnonymizeMACAddresses("11:22:33:44:55")); |
| 56 | EXPECT_EQ("aa:bb:cc:00:00:01", AnonymizeMACAddresses("aa:bb:cc:dd:ee:ff")); |
| 57 | EXPECT_EQ( |
| 58 | "BSSID: aa:bb:cc:00:00:01 in the middle\n" |
| 59 | "bb:cc:dd:00:00:02 start of line\n" |
| 60 | "end of line aa:bb:cc:00:00:01\n" |
| 61 | "no match across lines aa:bb:cc:\n" |
| 62 | "dd:ee:ff two on the same line:\n" |
| 63 | "x bb:cc:dd:00:00:02 cc:dd:ee:00:00:03 x\n", |
| 64 | AnonymizeMACAddresses("BSSID: aa:bb:cc:dd:ee:ff in the middle\n" |
| 65 | "bb:cc:dd:ee:ff:00 start of line\n" |
| 66 | "end of line aa:bb:cc:dd:ee:ff\n" |
| 67 | "no match across lines aa:bb:cc:\n" |
| 68 | "dd:ee:ff two on the same line:\n" |
| 69 | "x bb:cc:dd:ee:ff:00 cc:dd:ee:ff:00:11 x\n")); |
| 70 | EXPECT_EQ("Remember bb:cc:dd:00:00:02?", |
| 71 | AnonymizeMACAddresses("Remember bB:Cc:DD:ee:ff:00?")); |
| 72 | } |
| 73 | |
| 74 | TEST_F(AnonymizerToolTest, AnonymizeCustomPatterns) { |
| 75 | EXPECT_EQ("", AnonymizeCustomPatterns("")); |
| 76 | |
| 77 | EXPECT_EQ("Cell ID: '1'", AnonymizeCustomPatterns("Cell ID: 'A1B2'")); |
| 78 | EXPECT_EQ("Cell ID: '2'", AnonymizeCustomPatterns("Cell ID: 'C1D2'")); |
| 79 | EXPECT_EQ("foo Cell ID: '1' bar", |
| 80 | AnonymizeCustomPatterns("foo Cell ID: 'A1B2' bar")); |
| 81 | |
| 82 | EXPECT_EQ("foo Location area code: '1' bar", |
| 83 | AnonymizeCustomPatterns("foo Location area code: 'A1B2' bar")); |
| 84 | |
| 85 | EXPECT_EQ("foo\na SSID='1' b\n'", |
| 86 | AnonymizeCustomPatterns("foo\na SSID='Joe's' b\n'")); |
| 87 | EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'")); |
| 88 | EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'")); |
| 89 | |
| 90 | EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo", |
| 91 | AnonymizeCustomPatterns( |
| 92 | "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo")); |
| 93 | |
| 94 | EXPECT_EQ( |
| 95 | "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b", |
| 96 | AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b")); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 97 | |
| 98 | EXPECT_EQ("<email: 1>", |
| 99 | AnonymizeCustomPatterns("[email protected]")); |
| 100 | EXPECT_EQ("Email: <email: 1>.", |
| 101 | AnonymizeCustomPatterns("Email: [email protected].")); |
| 102 | EXPECT_EQ("Email:\n<email: 2>\n", |
| 103 | AnonymizeCustomPatterns("Email:\[email protected]\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:[email protected]:8080", |
| 120 | "http://userid:[email protected]: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:[email protected]", |
| 128 | "chrome://extensions/", |
| 129 | "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/options.html", |
| 130 | "http://example.com/[email protected]", |
| 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:")); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 142 | } |
| 143 | |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 144 | TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithContext) { |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 145 | const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')"; |
| 146 | std::map<std::string, std::string> space; |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 147 | EXPECT_EQ("", AnonymizeCustomPatternWithContext("", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 148 | EXPECT_EQ("foo\nbar\n", |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 149 | AnonymizeCustomPatternWithContext("foo\nbar\n", kPattern, &space)); |
| 150 | EXPECT_EQ("id '1'", |
| 151 | AnonymizeCustomPatternWithContext("id '2345'", kPattern, &space)); |
| 152 | EXPECT_EQ("id '2'", |
| 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)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 158 | EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n", |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 159 | AnonymizeCustomPatternWithContext( |
| 160 | "x1 id '2345' 1x id '1234'\nid '2345'\n", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 161 | space.clear(); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 162 | EXPECT_EQ("id '1'", |
| 163 | AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 164 | |
| 165 | space.clear(); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame^] | 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)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } // namespace feedback |