Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: snapshot/mac/crashpad_info_client_options_test.cc

Issue 997713002: Allow exception forwarding to the system’s native crash reporter to be disabled (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review feedback Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "snapshot/mac/crashpad_info_client_options.h"
16
17 #include <dlfcn.h>
18
19 #include "base/files/file_path.h"
20 #include "client/crashpad_info.h"
21 #include "gtest/gtest.h"
22 #include "snapshot/mac/process_snapshot_mac.h"
23 #include "util/test/paths.h"
24
25 namespace crashpad {
26 namespace test {
27 namespace {
28
29 class ScopedSetCrashpadInfoOptionsToDefault {
30 public:
31 explicit ScopedSetCrashpadInfoOptionsToDefault(CrashpadInfo* crashpad_info)
32 : crashpad_info_(crashpad_info) {
33 }
34
35 ~ScopedSetCrashpadInfoOptionsToDefault() {
36 crashpad_info_->set_crashpad_handler_behavior(
37 CrashpadInfo::TriState::kDefault);
38 crashpad_info_->set_system_crash_reporter_forwarding(
39 CrashpadInfo::TriState::kDefault);
40 }
41
42 private:
43 CrashpadInfo* crashpad_info_;
44
45 DISALLOW_COPY_AND_ASSIGN(ScopedSetCrashpadInfoOptionsToDefault);
46 };
47
48 TEST(CrashpadInfoClientOptions, OneModule) {
49 // Make sure that the initial state has all values at their defaults.
50 ProcessSnapshotMac process_snapshot;
51 ASSERT_TRUE(process_snapshot.Initialize(mach_task_self()));
52
53 CrashpadInfoClientOptions options;
54 process_snapshot.GetCrashpadOptions(&options);
55
56 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
57 options.crashpad_handler_behavior);
58 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
59 options.system_crash_reporter_forwarding);
60
61 CrashpadInfo* crashpad_info = CrashpadInfo::GetCrashpadInfo();
62 ASSERT_TRUE(crashpad_info);
63
64 {
65 ScopedSetCrashpadInfoOptionsToDefault restore_defaults(crashpad_info);
66
67 crashpad_info->set_crashpad_handler_behavior(
68 CrashpadInfo::TriState::kEnabled);
69
70 process_snapshot.GetCrashpadOptions(&options);
71 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kEnabled,
72 options.crashpad_handler_behavior);
73 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
74 options.system_crash_reporter_forwarding);
75 }
76
77 {
78 ScopedSetCrashpadInfoOptionsToDefault restore_defaults(crashpad_info);
79
80 crashpad_info->set_system_crash_reporter_forwarding(
81 CrashpadInfo::TriState::kDisabled);
82
83 process_snapshot.GetCrashpadOptions(&options);
84 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
85 options.crashpad_handler_behavior);
86 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDisabled,
87 options.system_crash_reporter_forwarding);
88 }
89 }
90
91 class ScopedDlHandle {
92 public:
93 explicit ScopedDlHandle(void* dl_handle)
94 : dl_handle_(dl_handle) {
95 }
96
97 ~ScopedDlHandle() {
98 if (dl_handle_) {
99 if (dlclose(dl_handle_) != 0) {
100 LOG(ERROR) << "dlclose: " << dlerror();
101 }
102 }
103 }
104
105 bool valid() const { return dl_handle_ != nullptr; }
106
107 template <typename T>
108 T LookUpSymbol(const char* symbol_name) {
109 return reinterpret_cast<T>(dlsym(dl_handle_, symbol_name));
110 }
111
112 private:
113 void* dl_handle_;
114
115 DISALLOW_COPY_AND_ASSIGN(ScopedDlHandle);
116 };
117
118 TEST(CrashpadInfoClientOptions, TwoModules) {
119 // Open the module, which has its own CrashpadInfo structure.
120 base::FilePath module_path =
121 Paths::Executable().DirName().Append("crashpad_snapshot_test_module.so");
122 ScopedDlHandle dl_handle(
123 dlopen(module_path.value().c_str(), RTLD_LAZY | RTLD_LOCAL));
124 ASSERT_TRUE(dl_handle.valid()) << "dlopen " << module_path.value() << ": "
125 << dlerror();
126
127 // Get the function pointer from the module. This wraps GetCrashpadInfo(), but
128 // because it runs in the module, it returns the remote module’s CrashpadInfo
129 // structure.
130 CrashpadInfo* (*TestModule_GetCrashpadInfo)() =
131 dl_handle.LookUpSymbol<CrashpadInfo* (*)()>("TestModule_GetCrashpadInfo");
132 ASSERT_TRUE(TestModule_GetCrashpadInfo);
133
134 // Make sure that the initial state has all values at their defaults.
135 ProcessSnapshotMac process_snapshot;
136 ASSERT_TRUE(process_snapshot.Initialize(mach_task_self()));
137
138 CrashpadInfoClientOptions options;
139 process_snapshot.GetCrashpadOptions(&options);
140
141 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
142 options.crashpad_handler_behavior);
143 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
144 options.system_crash_reporter_forwarding);
145
146 // Get both CrashpadInfo structures.
147 CrashpadInfo* local_crashpad_info = CrashpadInfo::GetCrashpadInfo();
148 ASSERT_TRUE(local_crashpad_info);
149
150 CrashpadInfo* remote_crashpad_info = TestModule_GetCrashpadInfo();
151 ASSERT_TRUE(remote_crashpad_info);
152
153 {
154 ScopedSetCrashpadInfoOptionsToDefault restore_local(local_crashpad_info);
155 ScopedSetCrashpadInfoOptionsToDefault restore_remote(remote_crashpad_info);
156
157 // When only one module sets a non-default value, it applies to the entire
158 // process.
159 remote_crashpad_info->set_crashpad_handler_behavior(
160 CrashpadInfo::TriState::kEnabled);
161
162 process_snapshot.GetCrashpadOptions(&options);
163 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kEnabled,
164 options.crashpad_handler_behavior);
165 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
166 options.system_crash_reporter_forwarding);
167
168 // When more than one module sets a non-default value, the first one in the
169 // module list applies to the process. The local module should appear before
170 // the remote module, because the local module loaded the remote module.
171 local_crashpad_info->set_crashpad_handler_behavior(
172 CrashpadInfo::TriState::kDisabled);
173
174 process_snapshot.GetCrashpadOptions(&options);
175 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDisabled,
176 options.crashpad_handler_behavior);
177 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
178 options.system_crash_reporter_forwarding);
179 }
180
181 {
182 ScopedSetCrashpadInfoOptionsToDefault restore_local(local_crashpad_info);
183 ScopedSetCrashpadInfoOptionsToDefault restore_remote(remote_crashpad_info);
184
185 // When only one module sets a non-default value, it applies to the entire
186 // process.
187 remote_crashpad_info->set_system_crash_reporter_forwarding(
188 CrashpadInfo::TriState::kDisabled);
189
190 process_snapshot.GetCrashpadOptions(&options);
191 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
192 options.crashpad_handler_behavior);
193 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDisabled,
194 options.system_crash_reporter_forwarding);
195
196 // When more than one module sets a non-default value, the first one in the
197 // module list applies to the process. The local module should appear before
198 // the remote module, because the local module loaded the remote module.
199 local_crashpad_info->set_system_crash_reporter_forwarding(
200 CrashpadInfo::TriState::kEnabled);
201
202 process_snapshot.GetCrashpadOptions(&options);
203 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kDefault,
204 options.crashpad_handler_behavior);
205 EXPECT_EQ(CrashpadInfoClientOptions::TriState::kEnabled,
206 options.system_crash_reporter_forwarding);
207 }
208 }
209
210 } // namespace
211 } // namespace test
212 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698