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, 11); | |
205 pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 10); | |
206 pref->SetInteger(prefs::kStabilityKernelCrashCount, 11); | |
jar (doing other things)
2011/01/14 02:31:47
nit: four unique integers would probably be better
kmixter1
2011/01/15 22:21:07
I didn't even notice that - 4 different was my int
| |
207 pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 12); | |
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 ASSERT_TRUE(log.GetEncodedLog(WriteInto(&encoded, size + 1), size)); | |
jar (doing other things)
2011/01/14 02:31:47
nit: I'd rather not see an ASSERT with side effect
kmixter1
2011/01/15 22:21:07
Done.
| |
228 | |
229 // Check that we can find childprocesscrashcount, but not | |
230 // any of the ChromeOS ones that we are not emitting until log | |
231 // servers can handle them. | |
jar (doing other things)
2011/01/14 02:31:47
:-) Nice!
kmixter1
2011/01/15 22:21:07
Done.
| |
232 EXPECT_NE(std::string::npos, | |
233 encoded.find(" childprocesscrashcount=\"11\"")); | |
234 EXPECT_EQ(std::string::npos, | |
235 encoded.find(" otherusercrashcount=")); | |
236 EXPECT_EQ(std::string::npos, | |
237 encoded.find(" kernelcrashcount=")); | |
238 EXPECT_EQ(std::string::npos, | |
239 encoded.find(" systemuncleanshutdowns=")); | |
240 } | |
241 | |
195 #endif // OS_CHROMEOS | 242 #endif // OS_CHROMEOS |
196 | 243 |
197 // Make sure our ID hashes are the same as what we see on the server side. | 244 // Make sure our ID hashes are the same as what we see on the server side. |
198 TEST(MetricsLogTest, CreateHash) { | 245 TEST(MetricsLogTest, CreateHash) { |
199 static const struct { | 246 static const struct { |
200 std::string input; | 247 std::string input; |
201 std::string output; | 248 std::string output; |
202 } cases[] = { | 249 } cases[] = { |
203 {"Back", "0x0557fa923dcee4d0"}, | 250 {"Back", "0x0557fa923dcee4d0"}, |
204 {"Forward", "0x67d2f6740a8eaebf"}, | 251 {"Forward", "0x67d2f6740a8eaebf"}, |
205 {"NewTab", "0x290eb683f96572f1"}, | 252 {"NewTab", "0x290eb683f96572f1"}, |
206 }; | 253 }; |
207 | 254 |
208 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | 255 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
209 std::string hash_string = MetricsLog::CreateHash(cases[i].input); | 256 std::string hash_string = MetricsLog::CreateHash(cases[i].input); |
210 | 257 |
211 // Convert to hex string | 258 // Convert to hex string |
212 // We're only checking the first 8 bytes, because that's what | 259 // We're only checking the first 8 bytes, because that's what |
213 // the metrics server uses. | 260 // the metrics server uses. |
214 std::string hash_hex = "0x"; | 261 std::string hash_hex = "0x"; |
215 for (size_t j = 0; j < 8; j++) { | 262 for (size_t j = 0; j < 8; j++) { |
216 base::StringAppendF(&hash_hex, "%02x", | 263 base::StringAppendF(&hash_hex, "%02x", |
217 static_cast<uint8>(hash_string.data()[j])); | 264 static_cast<uint8>(hash_string.data()[j])); |
218 } | 265 } |
219 EXPECT_EQ(cases[i].output, hash_hex); | 266 EXPECT_EQ(cases[i].output, hash_hex); |
220 } | 267 } |
221 }; | 268 }; |
OLD | NEW |