OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "chrome/browser/metrics/metrics_log.h" | 9 #include "chrome/browser/metrics/metrics_log.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/testing_profile.h" |
10 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
12 | 15 |
13 using base::TimeDelta; | 16 using base::TimeDelta; |
14 | 17 |
15 namespace { | 18 namespace { |
16 class MetricsLogTest : public testing::Test { | 19 class MetricsLogTest : public testing::Test { |
17 }; | 20 }; |
18 }; | 21 }; |
19 | 22 |
20 | 23 |
21 // Since buildtime is highly variable, this function will scan an output log and | 24 // Since buildtime is highly variable, this function will scan an output log and |
22 // replace it with a consistent number. | 25 // replace it with a consistent number. |
23 static void NormalizeBuildtime(std::string* xml_encoded) { | 26 static void NormalizeBuildtime(std::string* xml_encoded) { |
24 std::string prefix = "buildtime=\""; | 27 std::string prefix = "buildtime=\""; |
25 const char postfix = '\"'; | 28 const char postfix = '\"'; |
26 size_t offset = xml_encoded->find(prefix); | 29 size_t offset = xml_encoded->find(prefix); |
27 ASSERT_GT(offset, 0u); | 30 ASSERT_NE(std::string::npos, offset); |
28 offset += prefix.size(); | 31 offset += prefix.size(); |
29 size_t postfix_position = xml_encoded->find(postfix, offset); | 32 size_t postfix_position = xml_encoded->find(postfix, offset); |
30 ASSERT_GT(postfix_position, offset); | 33 ASSERT_NE(std::string::npos, postfix_position); |
31 for (size_t i = offset; i < postfix_position; ++i) { | 34 for (size_t i = offset; i < postfix_position; ++i) { |
32 char digit = xml_encoded->at(i); | 35 char digit = xml_encoded->at(i); |
33 ASSERT_GE(digit, '0'); | 36 ASSERT_GE(digit, '0'); |
34 ASSERT_LE(digit, '9'); | 37 ASSERT_LE(digit, '9'); |
35 } | 38 } |
36 | 39 |
37 // Insert a single fake buildtime. | 40 // Insert a single fake buildtime. |
38 xml_encoded->replace(offset, postfix_position - offset, "123246"); | 41 xml_encoded->replace(offset, postfix_position - offset, "123246"); |
39 } | 42 } |
40 | 43 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 188 |
186 std::string encoded; | 189 std::string encoded; |
187 // Leave room for the NUL terminator. | 190 // Leave room for the NUL terminator. |
188 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | 191 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); |
189 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); | 192 TrimWhitespaceASCII(encoded, TRIM_ALL, &encoded); |
190 NormalizeBuildtime(&encoded); | 193 NormalizeBuildtime(&encoded); |
191 NormalizeBuildtime(&expected_output); | 194 NormalizeBuildtime(&expected_output); |
192 | 195 |
193 ASSERT_EQ(expected_output, encoded); | 196 ASSERT_EQ(expected_output, encoded); |
194 } | 197 } |
| 198 |
| 199 TEST(MetricsLogTest, ChromeOSStabilityData) { |
| 200 NoTimeMetricsLog log("bogus client ID", 0); |
| 201 TestingProfile profile; |
| 202 PrefService* pref = profile.GetPrefs(); |
| 203 |
| 204 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 10); |
| 205 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 11); |
| 206 pref->SetInteger(prefs::kStabilityKernelCrashCount, 12); |
| 207 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13); |
| 208 std::string expected_output = StringPrintf( |
| 209 "<log clientid=\"bogus client ID\" buildtime=\"123456789\" " |
| 210 "appversion=\"%s\">\n" |
| 211 "<stability stuff>\n", MetricsLog::GetVersionString().c_str()); |
| 212 // Expect 3 warnings about not yet being able to send the |
| 213 // Chrome OS stability stats. |
| 214 log.WriteStabilityElement(profile.GetPrefs()); |
| 215 log.CloseLog(); |
| 216 |
| 217 int size = log.GetEncodedLogSize(); |
| 218 ASSERT_GT(size, 0); |
| 219 |
| 220 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityChildProcessCrashCount)); |
| 221 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityOtherUserCrashCount)); |
| 222 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilityKernelCrashCount)); |
| 223 EXPECT_EQ(0, pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount)); |
| 224 |
| 225 std::string encoded; |
| 226 // Leave room for the NUL terminator. |
| 227 bool encoding_result = log.GetEncodedLog( |
| 228 WriteInto(&encoded, size + 1), size); |
| 229 ASSERT_TRUE(encoding_result); |
| 230 |
| 231 // Check that we can find childprocesscrashcount, but not |
| 232 // any of the ChromeOS ones that we are not emitting until log |
| 233 // servers can handle them. |
| 234 EXPECT_NE(std::string::npos, |
| 235 encoded.find(" childprocesscrashcount=\"10\"")); |
| 236 EXPECT_EQ(std::string::npos, |
| 237 encoded.find(" otherusercrashcount=")); |
| 238 EXPECT_EQ(std::string::npos, |
| 239 encoded.find(" kernelcrashcount=")); |
| 240 EXPECT_EQ(std::string::npos, |
| 241 encoded.find(" systemuncleanshutdowns=")); |
| 242 } |
| 243 |
195 #endif // OS_CHROMEOS | 244 #endif // OS_CHROMEOS |
196 | 245 |
197 // Make sure our ID hashes are the same as what we see on the server side. | 246 // Make sure our ID hashes are the same as what we see on the server side. |
198 TEST(MetricsLogTest, CreateHash) { | 247 TEST(MetricsLogTest, CreateHash) { |
199 static const struct { | 248 static const struct { |
200 std::string input; | 249 std::string input; |
201 std::string output; | 250 std::string output; |
202 } cases[] = { | 251 } cases[] = { |
203 {"Back", "0x0557fa923dcee4d0"}, | 252 {"Back", "0x0557fa923dcee4d0"}, |
204 {"Forward", "0x67d2f6740a8eaebf"}, | 253 {"Forward", "0x67d2f6740a8eaebf"}, |
205 {"NewTab", "0x290eb683f96572f1"}, | 254 {"NewTab", "0x290eb683f96572f1"}, |
206 }; | 255 }; |
207 | 256 |
208 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | 257 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
209 std::string hash_string = MetricsLog::CreateHash(cases[i].input); | 258 std::string hash_string = MetricsLog::CreateHash(cases[i].input); |
210 | 259 |
211 // Convert to hex string | 260 // Convert to hex string |
212 // We're only checking the first 8 bytes, because that's what | 261 // We're only checking the first 8 bytes, because that's what |
213 // the metrics server uses. | 262 // the metrics server uses. |
214 std::string hash_hex = "0x"; | 263 std::string hash_hex = "0x"; |
215 for (size_t j = 0; j < 8; j++) { | 264 for (size_t j = 0; j < 8; j++) { |
216 base::StringAppendF(&hash_hex, "%02x", | 265 base::StringAppendF(&hash_hex, "%02x", |
217 static_cast<uint8>(hash_string.data()[j])); | 266 static_cast<uint8>(hash_string.data()[j])); |
218 } | 267 } |
219 EXPECT_EQ(cases[i].output, hash_hex); | 268 EXPECT_EQ(cases[i].output, hash_hex); |
220 } | 269 } |
221 }; | 270 }; |
OLD | NEW |