| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "chrome/browser/metrics/metrics_log.h" | 10 #include "chrome/browser/metrics/metrics_log.h" |
| 11 #include "chrome/browser/prefs/browser_prefs.h" | 11 #include "chrome/browser/prefs/browser_prefs.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_pref_service.h" | 14 #include "chrome/test/base/testing_pref_service.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 using base::TimeDelta; | 18 using base::TimeDelta; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 class MetricsLogTest : public testing::Test { | |
| 22 }; | |
| 23 }; | |
| 24 | |
| 25 | 21 |
| 26 // Since buildtime is highly variable, this function will scan an output log and | 22 // Since buildtime is highly variable, this function will scan an output log and |
| 27 // replace it with a consistent number. | 23 // replace it with a consistent number. |
| 28 static void NormalizeBuildtime(std::string* xml_encoded) { | 24 void NormalizeBuildtime(std::string* xml_encoded) { |
| 29 std::string prefix = "buildtime=\""; | 25 std::string prefix = "buildtime=\""; |
| 30 const char postfix = '\"'; | 26 const char postfix = '\"'; |
| 31 size_t offset = xml_encoded->find(prefix); | 27 size_t offset = xml_encoded->find(prefix); |
| 32 ASSERT_NE(std::string::npos, offset); | 28 ASSERT_NE(std::string::npos, offset); |
| 33 offset += prefix.size(); | 29 offset += prefix.size(); |
| 34 size_t postfix_position = xml_encoded->find(postfix, offset); | 30 size_t postfix_position = xml_encoded->find(postfix, offset); |
| 35 ASSERT_NE(std::string::npos, postfix_position); | 31 ASSERT_NE(std::string::npos, postfix_position); |
| 36 for (size_t i = offset; i < postfix_position; ++i) { | 32 for (size_t i = offset; i < postfix_position; ++i) { |
| 37 char digit = xml_encoded->at(i); | 33 char digit = xml_encoded->at(i); |
| 38 ASSERT_GE(digit, '0'); | 34 ASSERT_GE(digit, '0'); |
| 39 ASSERT_LE(digit, '9'); | 35 ASSERT_LE(digit, '9'); |
| 40 } | 36 } |
| 41 | 37 |
| 42 // Insert a single fake buildtime. | 38 // Insert a single fake buildtime. |
| 43 xml_encoded->replace(offset, postfix_position - offset, "123246"); | 39 xml_encoded->replace(offset, postfix_position - offset, "123246"); |
| 44 } | 40 } |
| 45 | 41 |
| 46 TEST(MetricsLogTest, EmptyRecord) { | |
| 47 std::string expected_output = base::StringPrintf( | |
| 48 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | |
| 49 "appversion=\"%s\"/>", MetricsLog::GetVersionString().c_str()); | |
| 50 | |
| 51 MetricsLog log("bogus client ID", 0); | |
| 52 log.CloseLog(); | |
| 53 | |
| 54 int size = log.GetEncodedLogSize(); | |
| 55 ASSERT_GT(size, 0); | |
| 56 | |
| 57 std::string encoded; | |
| 58 // Leave room for the NUL terminator. | |
| 59 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
| 60 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | |
| 61 NormalizeBuildtime(&encoded); | |
| 62 NormalizeBuildtime(&expected_output); | |
| 63 | |
| 64 ASSERT_EQ(expected_output, encoded); | |
| 65 } | |
| 66 | |
| 67 #if defined(OS_CHROMEOS) | |
| 68 TEST(MetricsLogTest, ChromeOSEmptyRecord) { | |
| 69 std::string expected_output = base::StringPrintf( | |
| 70 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | |
| 71 "appversion=\"%s\" hardwareclass=\"sample-class\"/>", | |
| 72 MetricsLog::GetVersionString().c_str()); | |
| 73 | |
| 74 MetricsLog log("bogus client ID", 0); | |
| 75 log.set_hardware_class("sample-class"); | |
| 76 log.CloseLog(); | |
| 77 | |
| 78 int size = log.GetEncodedLogSize(); | |
| 79 ASSERT_GT(size, 0); | |
| 80 | |
| 81 std::string encoded; | |
| 82 // Leave room for the NUL terminator. | |
| 83 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
| 84 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | |
| 85 NormalizeBuildtime(&encoded); | |
| 86 NormalizeBuildtime(&expected_output); | |
| 87 | |
| 88 ASSERT_EQ(expected_output, encoded); | |
| 89 } | |
| 90 #endif // OS_CHROMEOS | |
| 91 | |
| 92 namespace { | |
| 93 | |
| 94 class NoTimeMetricsLog : public MetricsLog { | 42 class NoTimeMetricsLog : public MetricsLog { |
| 95 public: | 43 public: |
| 96 NoTimeMetricsLog(std::string client_id, int session_id): | 44 NoTimeMetricsLog(std::string client_id, int session_id): |
| 97 MetricsLog(client_id, session_id) {} | 45 MetricsLog(client_id, session_id) {} |
| 98 virtual ~NoTimeMetricsLog() {} | 46 virtual ~NoTimeMetricsLog() {} |
| 99 | 47 |
| 100 // Override this so that output is testable. | 48 // Override this so that output is testable. |
| 101 virtual std::string GetCurrentTimeString() { | 49 virtual std::string GetCurrentTimeString() { |
| 102 return std::string(); | 50 return std::string(); |
| 103 } | 51 } |
| 104 }; | 52 }; |
| 105 | 53 |
| 106 }; // namespace | 54 } // namespace |
| 107 | 55 |
| 108 TEST(MetricsLogTest, WindowEvent) { | 56 class MetricsLogTest : public testing::Test { |
| 109 std::string expected_output = base::StringPrintf( | 57 }; |
| 110 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | |
| 111 "appversion=\"%s\">\n" | |
| 112 " <window action=\"create\" windowid=\"0\" session=\"0\" time=\"\"/>\n" | |
| 113 " <window action=\"open\" windowid=\"1\" parent=\"0\" " | |
| 114 "session=\"0\" time=\"\"/>\n" | |
| 115 " <window action=\"close\" windowid=\"1\" parent=\"0\" " | |
| 116 "session=\"0\" time=\"\"/>\n" | |
| 117 " <window action=\"destroy\" windowid=\"0\" session=\"0\" time=\"\"/>\n" | |
| 118 "</log>", MetricsLog::GetVersionString().c_str()); | |
| 119 | |
| 120 NoTimeMetricsLog log("bogus client ID", 0); | |
| 121 log.RecordWindowEvent(MetricsLog::WINDOW_CREATE, 0, -1); | |
| 122 log.RecordWindowEvent(MetricsLog::WINDOW_OPEN, 1, 0); | |
| 123 log.RecordWindowEvent(MetricsLog::WINDOW_CLOSE, 1, 0); | |
| 124 log.RecordWindowEvent(MetricsLog::WINDOW_DESTROY, 0, -1); | |
| 125 log.CloseLog(); | |
| 126 | |
| 127 ASSERT_EQ(4, log.num_events()); | |
| 128 | |
| 129 int size = log.GetEncodedLogSize(); | |
| 130 ASSERT_GT(size, 0); | |
| 131 | |
| 132 std::string encoded; | |
| 133 // Leave room for the NUL terminator. | |
| 134 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
| 135 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | |
| 136 NormalizeBuildtime(&encoded); | |
| 137 NormalizeBuildtime(&expected_output); | |
| 138 | |
| 139 ASSERT_EQ(expected_output, encoded); | |
| 140 } | |
| 141 | |
| 142 TEST(MetricsLogTest, LoadEvent) { | |
| 143 std::string expected_output = base::StringPrintf( | |
| 144 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | |
| 145 "appversion=\"%s\">\n" | |
| 146 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " | |
| 147 "origin=\"link\" session=\"0\" time=\"\"/>\n" | |
| 148 "</log>", MetricsLog::GetVersionString().c_str()); | |
| 149 | |
| 150 NoTimeMetricsLog log("bogus client ID", 0); | |
| 151 log.RecordLoadEvent(3, GURL("http://google.com"), | |
| 152 content::PAGE_TRANSITION_LINK, 1, | |
| 153 TimeDelta::FromMilliseconds(7219)); | |
| 154 | |
| 155 log.CloseLog(); | |
| 156 | |
| 157 ASSERT_EQ(1, log.num_events()); | |
| 158 | |
| 159 int size = log.GetEncodedLogSize(); | |
| 160 ASSERT_GT(size, 0); | |
| 161 | |
| 162 std::string encoded; | |
| 163 // Leave room for the NUL terminator. | |
| 164 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
| 165 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | |
| 166 NormalizeBuildtime(&encoded); | |
| 167 NormalizeBuildtime(&expected_output); | |
| 168 | |
| 169 ASSERT_EQ(expected_output, encoded); | |
| 170 } | |
| 171 | 58 |
| 172 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
| 173 TEST(MetricsLogTest, ChromeOSLoadEvent) { | |
| 174 std::string expected_output = base::StringPrintf( | |
| 175 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | |
| 176 "appversion=\"%s\" hardwareclass=\"sample-class\">\n" | |
| 177 " <document action=\"load\" docid=\"1\" window=\"3\" loadtime=\"7219\" " | |
| 178 "origin=\"link\" session=\"0\" time=\"\"/>\n" | |
| 179 "</log>", MetricsLog::GetVersionString().c_str()); | |
| 180 | |
| 181 NoTimeMetricsLog log("bogus client ID", 0); | |
| 182 log.RecordLoadEvent(3, GURL("http://google.com"), | |
| 183 content::PAGE_TRANSITION_LINK, 1, | |
| 184 TimeDelta::FromMilliseconds(7219)); | |
| 185 log.set_hardware_class("sample-class"); | |
| 186 log.CloseLog(); | |
| 187 | |
| 188 ASSERT_EQ(1, log.num_events()); | |
| 189 | |
| 190 int size = log.GetEncodedLogSize(); | |
| 191 ASSERT_GT(size, 0); | |
| 192 | |
| 193 std::string encoded; | |
| 194 // Leave room for the NUL terminator. | |
| 195 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
| 196 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | |
| 197 NormalizeBuildtime(&encoded); | |
| 198 NormalizeBuildtime(&expected_output); | |
| 199 | |
| 200 ASSERT_EQ(expected_output, encoded); | |
| 201 } | |
| 202 | |
| 203 TEST(MetricsLogTest, ChromeOSStabilityData) { | 60 TEST(MetricsLogTest, ChromeOSStabilityData) { |
| 204 NoTimeMetricsLog log("bogus client ID", 0); | 61 NoTimeMetricsLog log("bogus client ID", 0); |
| 205 TestingPrefService prefs; | 62 TestingPrefService prefs; |
| 206 browser::RegisterLocalState(&prefs); | 63 browser::RegisterLocalState(&prefs); |
| 207 | 64 |
| 208 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); | 65 prefs.SetInteger(prefs::kStabilityChildProcessCrashCount, 10); |
| 209 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); | 66 prefs.SetInteger(prefs::kStabilityOtherUserCrashCount, 11); |
| 210 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12); | 67 prefs.SetInteger(prefs::kStabilityKernelCrashCount, 12); |
| 211 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); | 68 prefs.SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); |
| 212 std::string expected_output = base::StringPrintf( | 69 std::string expected_output = base::StringPrintf( |
| 213 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " | 70 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 214 "appversion=\"%s\">\n" | 71 "appversion=\"%s\">\n" |
| 215 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); | 72 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); |
| 216 // Expect 3 warnings about not yet being able to send the | 73 // Expect 3 warnings about not yet being able to send the |
| 217 // Chrome OS stability stats. | 74 // Chrome OS stability stats. |
| 218 log.WriteStabilityElement(&prefs); | 75 log.WriteStabilityElement(&prefs); |
| 219 log.CloseLog(); | 76 log.CloseLog(); |
| 220 | 77 |
| 221 int size = log.GetEncodedLogSize(); | 78 int size = log.GetEncodedLogSizeXml(); |
| 222 ASSERT_GT(size, 0); | 79 ASSERT_GT(size, 0); |
| 223 | 80 |
| 224 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); | 81 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityChildProcessCrashCount)); |
| 225 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); | 82 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityOtherUserCrashCount)); |
| 226 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); | 83 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilityKernelCrashCount)); |
| 227 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); | 84 EXPECT_EQ(0, prefs.GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); |
| 228 | 85 |
| 229 std::string encoded; | 86 std::string encoded; |
| 230 // Leave room for the NUL terminator. | 87 // Leave room for the NUL terminator. |
| 231 bool encoding_result = log.GetEncodedLog( | 88 bool encoding_result = log.GetEncodedLogXml( |
| 232 WriteInto(&encoded, size + 1), size); | 89 WriteInto(&encoded, size + 1), size); |
| 233 ASSERT_TRUE(encoding_result); | 90 ASSERT_TRUE(encoding_result); |
| 234 | 91 |
| 235 // Check that we can find childprocesscrashcount, but not | 92 // Check that we can find childprocesscrashcount, but not |
| 236 // any of the ChromeOS ones that we are not emitting until log | 93 // any of the ChromeOS ones that we are not emitting until log |
| 237 // servers can handle them. | 94 // servers can handle them. |
| 238 EXPECT_NE(std::string::npos, | 95 EXPECT_NE(std::string::npos, |
| 239 encoded.find(" childprocesscrashcount=\"10\"")); | 96 encoded.find(" childprocesscrashcount=\"10\"")); |
| 240 EXPECT_EQ(std::string::npos, | 97 EXPECT_EQ(std::string::npos, |
| 241 encoded.find(" otherusercrashcount=")); | 98 encoded.find(" otherusercrashcount=")); |
| 242 EXPECT_EQ(std::string::npos, | 99 EXPECT_EQ(std::string::npos, |
| 243 encoded.find(" kernelcrashcount=")); | 100 encoded.find(" kernelcrashcount=")); |
| 244 EXPECT_EQ(std::string::npos, | 101 EXPECT_EQ(std::string::npos, |
| 245 encoded.find(" systemuncleanshutdowns=")); | 102 encoded.find(" systemuncleanshutdowns=")); |
| 246 } | 103 } |
| 247 | |
| 248 #endif // OS_CHROMEOS | 104 #endif // OS_CHROMEOS |
| 249 | |
| 250 // Make sure our ID hashes are the same as what we see on the server side. | |
| 251 TEST(MetricsLogTest, CreateHash) { | |
| 252 static const struct { | |
| 253 std::string input; | |
| 254 std::string output; | |
| 255 } cases[] = { | |
| 256 {"Back", "0x0557fa923dcee4d0"}, | |
| 257 {"Forward", "0x67d2f6740a8eaebf"}, | |
| 258 {"NewTab", "0x290eb683f96572f1"}, | |
| 259 }; | |
| 260 | |
| 261 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
| 262 std::string hash_string = MetricsLog::CreateHash(cases[i].input); | |
| 263 | |
| 264 // Convert to hex string | |
| 265 // We're only checking the first 8 bytes, because that's what | |
| 266 // the metrics server uses. | |
| 267 std::string hash_hex = "0x"; | |
| 268 for (size_t j = 0; j < 8; j++) { | |
| 269 base::StringAppendF(&hash_hex, "%02x", | |
| 270 static_cast<uint8>(hash_string.data()[j])); | |
| 271 } | |
| 272 EXPECT_EQ(cases[i].output, hash_hex); | |
| 273 } | |
| 274 }; | |
| OLD | NEW |