OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/crash/app/crash_keys_win.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "components/crash/app/crash_reporter_client.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace breakpad { | |
15 | |
16 using testing::_; | |
17 using testing::DoAll; | |
18 using testing::Return; | |
19 using testing::SetArgPointee; | |
20 | |
21 class MockCrashReporterClient : public crash_reporter::CrashReporterClient { | |
22 public: | |
23 MOCK_METHOD1(GetAlternativeCrashDumpLocation, | |
24 bool(base::FilePath* crash_dir)); | |
25 MOCK_METHOD5(GetProductNameAndVersion, void(const base::FilePath& exe_path, | |
26 base::string16* product_name, | |
27 base::string16* version, | |
28 base::string16* special_build, | |
29 base::string16* channel_name)); | |
30 MOCK_METHOD3(ShouldShowRestartDialog, bool(base::string16* title, | |
31 base::string16* message, | |
32 bool* is_rtl_locale)); | |
33 MOCK_METHOD0(AboutToRestart, bool()); | |
34 MOCK_METHOD1(GetDeferredUploadsSupported, bool(bool is_per_user_install)); | |
35 MOCK_METHOD1(GetIsPerUserInstall, bool(const base::FilePath& exe_path)); | |
36 MOCK_METHOD1(GetShouldDumpLargerDumps, bool(bool is_per_user_install)); | |
37 MOCK_METHOD0(GetResultCodeRespawnFailed, int()); | |
38 MOCK_METHOD0(InitBrowserCrashDumpsRegKey, void()); | |
39 MOCK_METHOD1(RecordCrashDumpAttempt, void(bool is_real_crash)); | |
40 | |
41 MOCK_METHOD2(GetProductNameAndVersion, void(std::string* product_name, | |
42 std::string* version)); | |
43 MOCK_METHOD0(GetReporterLogFilename, base::FilePath()); | |
44 MOCK_METHOD1(GetCrashDumpLocation, bool(base::FilePath* crash_dir)); | |
45 MOCK_METHOD0(RegisterCrashKeys, size_t()); | |
46 MOCK_METHOD0(IsRunningUnattended, bool()); | |
47 MOCK_METHOD0(GetCollectStatsConsent, bool()); | |
48 MOCK_METHOD1(ReportingIsEnforcedByPolicy, bool(bool* breakpad_enabled)); | |
49 MOCK_METHOD0(GetAndroidMinidumpDescriptor, int()); | |
50 MOCK_METHOD1(EnableBreakpadForProcess, bool(const std::string& process_type)); | |
51 }; | |
52 | |
53 class CrashKeysWinTest : public testing::Test { | |
54 public: | |
55 | |
56 size_t CountKeyValueOccurences( | |
57 const google_breakpad::CustomClientInfo* client_info, | |
58 const wchar_t* key, const wchar_t* value); | |
59 | |
60 protected: | |
61 testing::StrictMock<MockCrashReporterClient> crash_client_; | |
62 }; | |
63 | |
64 size_t CrashKeysWinTest::CountKeyValueOccurences( | |
65 const google_breakpad::CustomClientInfo* client_info, | |
66 const wchar_t* key, const wchar_t* value) { | |
67 size_t occurrences = 0; | |
68 for (size_t i = 0; i < client_info->count; ++i) { | |
69 if (wcscmp(client_info->entries[i].name, key) == 0 && | |
70 wcscmp(client_info->entries[i].value, value) == 0) { | |
71 ++occurrences; | |
72 } | |
73 } | |
74 | |
75 return occurrences; | |
76 } | |
77 | |
78 TEST_F(CrashKeysWinTest, RecordsSelf) { | |
79 ASSERT_EQ(static_cast<CrashKeysWin*>(NULL), CrashKeysWin::keeper()); | |
80 | |
81 { | |
82 CrashKeysWin crash_keys; | |
83 | |
84 ASSERT_EQ(&crash_keys, CrashKeysWin::keeper()); | |
85 } | |
86 | |
87 ASSERT_EQ(static_cast<CrashKeysWin*>(NULL), CrashKeysWin::keeper()); | |
88 } | |
89 | |
90 // Tests the crash keys set up for the most common official build consumer | |
91 // scenario. No policy controls, not running unattended and no explicit | |
92 // switches. | |
93 TEST_F(CrashKeysWinTest, OfficialLikeKeys) { | |
94 CrashKeysWin crash_keys; | |
95 | |
96 const base::FilePath kExePath(L"C:\\temp\\exe_path.exe"); | |
97 // The exe path ought to get passed through to the breakpad client. | |
98 EXPECT_CALL(crash_client_, GetProductNameAndVersion(kExePath, _, _, _, _)) | |
99 .WillRepeatedly(DoAll( | |
100 SetArgPointee<1>(L"SomeProdName"), | |
101 SetArgPointee<2>(L"1.2.3.4"), | |
102 SetArgPointee<3>(L""), | |
103 SetArgPointee<4>(L"-devm"))); | |
104 | |
105 EXPECT_CALL(crash_client_, GetAlternativeCrashDumpLocation(_)) | |
106 .WillRepeatedly(DoAll( | |
107 SetArgPointee<0>(base::FilePath(L"C:\\temp")), | |
108 Return(false))); | |
109 | |
110 EXPECT_CALL(crash_client_, ReportingIsEnforcedByPolicy(_)) | |
111 .WillRepeatedly(Return(false)); | |
112 | |
113 EXPECT_CALL(crash_client_, IsRunningUnattended()) | |
114 .WillRepeatedly(Return(false)); | |
115 | |
116 // Provide an empty command line. | |
117 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); | |
118 google_breakpad::CustomClientInfo* info = | |
119 crash_keys.GetCustomInfo(kExePath.value(), | |
120 L"made_up_type", | |
121 L"temporary", | |
122 &cmd_line, | |
123 &crash_client_); | |
124 | |
125 ASSERT_TRUE(info != NULL); | |
126 ASSERT_TRUE(info->entries != NULL); | |
127 | |
128 // We expect 7 fixed keys and a "freeboard" of 256 keys for dynamic entries. | |
129 EXPECT_EQ(256U + 7U, info->count); | |
130 | |
131 EXPECT_EQ(1, CountKeyValueOccurences(info, L"ver", L"1.2.3.4")); | |
132 EXPECT_EQ(1, CountKeyValueOccurences(info, L"prod", L"SomeProdName")); | |
133 EXPECT_EQ(1, CountKeyValueOccurences(info, L"plat", L"Win32")); | |
134 EXPECT_EQ(1, CountKeyValueOccurences(info, L"ptype", L"made_up_type")); | |
135 std::wstring pid_str(base::StringPrintf(L"%d", ::GetCurrentProcessId())); | |
136 EXPECT_EQ(1, CountKeyValueOccurences(info, L"pid", pid_str.c_str())); | |
137 EXPECT_EQ(1, CountKeyValueOccurences(info, L"channel", L"-devm")); | |
138 EXPECT_EQ(1, CountKeyValueOccurences(info, L"profile-type", L"temporary")); | |
139 EXPECT_EQ(256, CountKeyValueOccurences(info, L"unspecified-crash-key", L"")); | |
140 } | |
141 | |
142 } // namespace breakpad | |
OLD | NEW |