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'")); |
afakhry | 5a59f95 | 2017-05-24 21:04:26 | [diff] [blame] | 50 | |
| 51 | // Make sure UUIDs are anonymized. |
| 52 | EXPECT_EQ( |
| 53 | "REQUEST localhost - - \"POST /printers/<UUID: 1> HTTP/1.1\" 200 291 " |
| 54 | "Create-Job successful-ok", |
| 55 | anonymizer_.Anonymize( |
| 56 | "REQUEST localhost - - \"POST /printers/" |
| 57 | "cb738a9f-6433-4d95-a81e-94e4ae0ed30b HTTP/1.1\" 200 291 Create-Job " |
| 58 | "successful-ok")); |
| 59 | EXPECT_EQ( |
| 60 | "REQUEST localhost - - \"POST /printers/<UUID: 2> HTTP/1.1\" 200 286 " |
| 61 | "Create-Job successful-ok", |
| 62 | anonymizer_.Anonymize( |
| 63 | "REQUEST localhost - - \"POST /printers/" |
| 64 | "d17188da-9cd3-44f4-b148-3e1d748a3b0f HTTP/1.1\" 200 286 Create-Job " |
| 65 | "successful-ok")); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST_F(AnonymizerToolTest, AnonymizeMACAddresses) { |
| 69 | EXPECT_EQ("", AnonymizeMACAddresses("")); |
| 70 | EXPECT_EQ("foo\nbar\n", AnonymizeMACAddresses("foo\nbar\n")); |
| 71 | EXPECT_EQ("11:22:33:44:55", AnonymizeMACAddresses("11:22:33:44:55")); |
| 72 | EXPECT_EQ("aa:bb:cc:00:00:01", AnonymizeMACAddresses("aa:bb:cc:dd:ee:ff")); |
| 73 | EXPECT_EQ( |
| 74 | "BSSID: aa:bb:cc:00:00:01 in the middle\n" |
| 75 | "bb:cc:dd:00:00:02 start of line\n" |
| 76 | "end of line aa:bb:cc:00:00:01\n" |
| 77 | "no match across lines aa:bb:cc:\n" |
| 78 | "dd:ee:ff two on the same line:\n" |
| 79 | "x bb:cc:dd:00:00:02 cc:dd:ee:00:00:03 x\n", |
| 80 | AnonymizeMACAddresses("BSSID: aa:bb:cc:dd:ee:ff in the middle\n" |
| 81 | "bb:cc:dd:ee:ff:00 start of line\n" |
| 82 | "end of line aa:bb:cc:dd:ee:ff\n" |
| 83 | "no match across lines aa:bb:cc:\n" |
| 84 | "dd:ee:ff two on the same line:\n" |
| 85 | "x bb:cc:dd:ee:ff:00 cc:dd:ee:ff:00:11 x\n")); |
| 86 | EXPECT_EQ("Remember bb:cc:dd:00:00:02?", |
| 87 | AnonymizeMACAddresses("Remember bB:Cc:DD:ee:ff:00?")); |
| 88 | } |
| 89 | |
| 90 | TEST_F(AnonymizerToolTest, AnonymizeCustomPatterns) { |
| 91 | EXPECT_EQ("", AnonymizeCustomPatterns("")); |
| 92 | |
| 93 | EXPECT_EQ("Cell ID: '1'", AnonymizeCustomPatterns("Cell ID: 'A1B2'")); |
| 94 | EXPECT_EQ("Cell ID: '2'", AnonymizeCustomPatterns("Cell ID: 'C1D2'")); |
| 95 | EXPECT_EQ("foo Cell ID: '1' bar", |
| 96 | AnonymizeCustomPatterns("foo Cell ID: 'A1B2' bar")); |
| 97 | |
| 98 | EXPECT_EQ("foo Location area code: '1' bar", |
| 99 | AnonymizeCustomPatterns("foo Location area code: 'A1B2' bar")); |
| 100 | |
| 101 | EXPECT_EQ("foo\na SSID='1' b\n'", |
| 102 | AnonymizeCustomPatterns("foo\na SSID='Joe's' b\n'")); |
| 103 | EXPECT_EQ("ssid '2'", AnonymizeCustomPatterns("ssid 'My AP'")); |
| 104 | EXPECT_EQ("bssid 'aa:bb'", AnonymizeCustomPatterns("bssid 'aa:bb'")); |
| 105 | |
| 106 | EXPECT_EQ("Scan SSID - hexdump(len=6): 1\nfoo", |
| 107 | AnonymizeCustomPatterns( |
| 108 | "Scan SSID - hexdump(len=6): 47 6f 6f 67 6c 65\nfoo")); |
| 109 | |
| 110 | EXPECT_EQ( |
| 111 | "a\nb [SSID=1] [SSID=2] [SSID=foo\nbar] b", |
| 112 | AnonymizeCustomPatterns("a\nb [SSID=foo] [SSID=bar] [SSID=foo\nbar] b")); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 113 | |
afakhry | 85eea80 | 2017-05-01 17:04:10 | [diff] [blame] | 114 | EXPECT_EQ("SerialNumber: 1", |
| 115 | AnonymizeCustomPatterns("SerialNumber: 1217D7EF")); |
| 116 | EXPECT_EQ("serial number: 2", |
| 117 | AnonymizeCustomPatterns("serial number: 50C971FEE7F3x010900")); |
| 118 | EXPECT_EQ("SerialNumber: 3", |
| 119 | AnonymizeCustomPatterns("SerialNumber: EVT23-17BA01-004")); |
| 120 | |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 121 | EXPECT_EQ("<email: 1>", |
| 122 | AnonymizeCustomPatterns("[email protected]")); |
| 123 | EXPECT_EQ("Email: <email: 1>.", |
| 124 | AnonymizeCustomPatterns("Email: [email protected].")); |
| 125 | EXPECT_EQ("Email:\n<email: 2>\n", |
| 126 | AnonymizeCustomPatterns("Email:\[email protected]\n")); |
| 127 | |
| 128 | EXPECT_EQ("[<IPv6: 1>]", AnonymizeCustomPatterns( |
| 129 | "[2001:0db8:0000:0000:0000:ff00:0042:8329]")); |
| 130 | EXPECT_EQ("[<IPv6: 2>]", |
| 131 | AnonymizeCustomPatterns("[2001:db8:0:0:0:ff00:42:8329]")); |
| 132 | EXPECT_EQ("[<IPv6: 3>]", AnonymizeCustomPatterns("[2001:db8::ff00:42:8329]")); |
| 133 | EXPECT_EQ("[<IPv6: 4>]", AnonymizeCustomPatterns("[::1]")); |
| 134 | EXPECT_EQ("<IPv4: 1>", AnonymizeCustomPatterns("192.168.0.1")); |
| 135 | |
| 136 | EXPECT_EQ("<URL: 1>", |
| 137 | AnonymizeCustomPatterns("http://example.com/foo?test=1")); |
| 138 | EXPECT_EQ("Foo <URL: 2> Bar", |
| 139 | AnonymizeCustomPatterns("Foo http://192.168.0.1/foo?test=1#123 Bar")); |
| 140 | const char* kURLs[] = { |
| 141 | "http://example.com/foo?test=1", |
| 142 | "http://userid:[email protected]:8080", |
| 143 | "http://userid:[email protected]:8080/", |
| 144 | "http://@example.com", |
| 145 | "http://192.168.0.1", |
| 146 | "http://192.168.0.1/", |
| 147 | "http://اختبار.com", |
| 148 | "http://test.com/foo(bar)baz.html", |
| 149 | "http://test.com/foo%20bar", |
| 150 | "ftp://test:[email protected]", |
| 151 | "chrome://extensions/", |
| 152 | "chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/options.html", |
| 153 | "http://example.com/[email protected]", |
pbond | 4104d69 | 2016-02-10 09:41:07 | [diff] [blame] | 154 | "rtsp://[email protected]/", |
| 155 | "https://aaaaaaaaaaaaaaaa.com", |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 156 | }; |
| 157 | for (size_t i = 0; i < arraysize(kURLs); ++i) { |
| 158 | SCOPED_TRACE(kURLs[i]); |
| 159 | std::string got = AnonymizeCustomPatterns(kURLs[i]); |
| 160 | EXPECT_TRUE( |
| 161 | base::StartsWith(got, "<URL: ", base::CompareCase::INSENSITIVE_ASCII)); |
| 162 | EXPECT_TRUE(base::EndsWith(got, ">", base::CompareCase::INSENSITIVE_ASCII)); |
| 163 | } |
| 164 | // Test that "Android:" is not considered a schema with empty hier part. |
| 165 | EXPECT_EQ("The following applies to Android:", |
| 166 | AnonymizeCustomPatterns("The following applies to Android:")); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 167 | } |
| 168 | |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 169 | TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithContext) { |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 170 | const char kPattern[] = "(\\b(?i)id:? ')(\\d+)(')"; |
| 171 | std::map<std::string, std::string> space; |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 172 | EXPECT_EQ("", AnonymizeCustomPatternWithContext("", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 173 | EXPECT_EQ("foo\nbar\n", |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 174 | AnonymizeCustomPatternWithContext("foo\nbar\n", kPattern, &space)); |
| 175 | EXPECT_EQ("id '1'", |
| 176 | AnonymizeCustomPatternWithContext("id '2345'", kPattern, &space)); |
| 177 | EXPECT_EQ("id '2'", |
| 178 | AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space)); |
| 179 | EXPECT_EQ("id: '2'", |
| 180 | AnonymizeCustomPatternWithContext("id: '1234'", kPattern, &space)); |
| 181 | EXPECT_EQ("ID: '1'", |
| 182 | AnonymizeCustomPatternWithContext("ID: '2345'", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 183 | EXPECT_EQ("x1 id '1' 1x id '2'\nid '1'\n", |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 184 | AnonymizeCustomPatternWithContext( |
| 185 | "x1 id '2345' 1x id '1234'\nid '2345'\n", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 186 | space.clear(); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 187 | EXPECT_EQ("id '1'", |
| 188 | AnonymizeCustomPatternWithContext("id '1234'", kPattern, &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 189 | |
| 190 | space.clear(); |
battre | 03910b4 | 2016-01-11 13:42:34 | [diff] [blame] | 191 | EXPECT_EQ("x1z", |
| 192 | AnonymizeCustomPatternWithContext("xyz", "()(y+)()", &space)); |
| 193 | } |
| 194 | |
| 195 | TEST_F(AnonymizerToolTest, AnonymizeCustomPatternWithoutContext) { |
| 196 | CustomPatternWithoutContext kPattern = {"pattern", "(o+)"}; |
| 197 | std::map<std::string, std::string> space; |
| 198 | EXPECT_EQ("", AnonymizeCustomPatternWithoutContext("", kPattern, &space)); |
| 199 | EXPECT_EQ("f<pattern: 1>\nf<pattern: 2>z\nf<pattern: 1>l\n", |
| 200 | AnonymizeCustomPatternWithoutContext("fo\nfooz\nfol\n", kPattern, |
| 201 | &space)); |
battre | 4cdaa7c | 2016-01-07 11:30:27 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace feedback |