OLD | NEW |
| (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 "test/paths.h" | |
24 | |
25 namespace crashpad { | |
26 namespace test { | |
27 namespace { | |
28 | |
29 TEST(CrashpadInfoClientOptions, TriStateFromCrashpadInfo) { | |
30 EXPECT_EQ(TriState::kUnset, | |
31 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(0)); | |
32 EXPECT_EQ(TriState::kEnabled, | |
33 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(1)); | |
34 EXPECT_EQ(TriState::kDisabled, | |
35 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(2)); | |
36 | |
37 // These will produce log messages but should result in kUnset being returned. | |
38 EXPECT_EQ(TriState::kUnset, | |
39 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(3)); | |
40 EXPECT_EQ(TriState::kUnset, | |
41 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(4)); | |
42 EXPECT_EQ(TriState::kUnset, | |
43 CrashpadInfoClientOptions::TriStateFromCrashpadInfo(0xff)); | |
44 } | |
45 | |
46 class ScopedUnsetCrashpadInfoOptions { | |
47 public: | |
48 explicit ScopedUnsetCrashpadInfoOptions(CrashpadInfo* crashpad_info) | |
49 : crashpad_info_(crashpad_info) { | |
50 } | |
51 | |
52 ~ScopedUnsetCrashpadInfoOptions() { | |
53 crashpad_info_->set_crashpad_handler_behavior(TriState::kUnset); | |
54 crashpad_info_->set_system_crash_reporter_forwarding(TriState::kUnset); | |
55 } | |
56 | |
57 private: | |
58 CrashpadInfo* crashpad_info_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(ScopedUnsetCrashpadInfoOptions); | |
61 }; | |
62 | |
63 TEST(CrashpadInfoClientOptions, OneModule) { | |
64 // Make sure that the initial state has all values unset. | |
65 ProcessSnapshotMac process_snapshot; | |
66 ASSERT_TRUE(process_snapshot.Initialize(mach_task_self())); | |
67 | |
68 CrashpadInfoClientOptions options; | |
69 process_snapshot.GetCrashpadOptions(&options); | |
70 | |
71 EXPECT_EQ(TriState::kUnset, options.crashpad_handler_behavior); | |
72 EXPECT_EQ(TriState::kUnset, options.system_crash_reporter_forwarding); | |
73 | |
74 CrashpadInfo* crashpad_info = CrashpadInfo::GetCrashpadInfo(); | |
75 ASSERT_TRUE(crashpad_info); | |
76 | |
77 { | |
78 ScopedUnsetCrashpadInfoOptions unset(crashpad_info); | |
79 | |
80 crashpad_info->set_crashpad_handler_behavior(TriState::kEnabled); | |
81 | |
82 process_snapshot.GetCrashpadOptions(&options); | |
83 EXPECT_EQ(TriState::kEnabled, options.crashpad_handler_behavior); | |
84 EXPECT_EQ(TriState::kUnset, options.system_crash_reporter_forwarding); | |
85 } | |
86 | |
87 { | |
88 ScopedUnsetCrashpadInfoOptions unset(crashpad_info); | |
89 | |
90 crashpad_info->set_system_crash_reporter_forwarding(TriState::kDisabled); | |
91 | |
92 process_snapshot.GetCrashpadOptions(&options); | |
93 EXPECT_EQ(TriState::kUnset, options.crashpad_handler_behavior); | |
94 EXPECT_EQ(TriState::kDisabled, options.system_crash_reporter_forwarding); | |
95 } | |
96 } | |
97 | |
98 class ScopedDlHandle { | |
99 public: | |
100 explicit ScopedDlHandle(void* dl_handle) | |
101 : dl_handle_(dl_handle) { | |
102 } | |
103 | |
104 ~ScopedDlHandle() { | |
105 if (dl_handle_) { | |
106 if (dlclose(dl_handle_) != 0) { | |
107 LOG(ERROR) << "dlclose: " << dlerror(); | |
108 } | |
109 } | |
110 } | |
111 | |
112 bool valid() const { return dl_handle_ != nullptr; } | |
113 | |
114 template <typename T> | |
115 T LookUpSymbol(const char* symbol_name) { | |
116 return reinterpret_cast<T>(dlsym(dl_handle_, symbol_name)); | |
117 } | |
118 | |
119 private: | |
120 void* dl_handle_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(ScopedDlHandle); | |
123 }; | |
124 | |
125 TEST(CrashpadInfoClientOptions, TwoModules) { | |
126 // Open the module, which has its own CrashpadInfo structure. | |
127 base::FilePath module_path = | |
128 Paths::Executable().DirName().Append("crashpad_snapshot_test_module.so"); | |
129 ScopedDlHandle dl_handle( | |
130 dlopen(module_path.value().c_str(), RTLD_LAZY | RTLD_LOCAL)); | |
131 ASSERT_TRUE(dl_handle.valid()) << "dlopen " << module_path.value() << ": " | |
132 << dlerror(); | |
133 | |
134 // Get the function pointer from the module. This wraps GetCrashpadInfo(), but | |
135 // because it runs in the module, it returns the remote module’s CrashpadInfo | |
136 // structure. | |
137 CrashpadInfo* (*TestModule_GetCrashpadInfo)() = | |
138 dl_handle.LookUpSymbol<CrashpadInfo* (*)()>("TestModule_GetCrashpadInfo"); | |
139 ASSERT_TRUE(TestModule_GetCrashpadInfo); | |
140 | |
141 // Make sure that the initial state has all values unset. | |
142 ProcessSnapshotMac process_snapshot; | |
143 ASSERT_TRUE(process_snapshot.Initialize(mach_task_self())); | |
144 | |
145 CrashpadInfoClientOptions options; | |
146 process_snapshot.GetCrashpadOptions(&options); | |
147 | |
148 EXPECT_EQ(TriState::kUnset, options.crashpad_handler_behavior); | |
149 EXPECT_EQ(TriState::kUnset, options.system_crash_reporter_forwarding); | |
150 | |
151 // Get both CrashpadInfo structures. | |
152 CrashpadInfo* local_crashpad_info = CrashpadInfo::GetCrashpadInfo(); | |
153 ASSERT_TRUE(local_crashpad_info); | |
154 | |
155 CrashpadInfo* remote_crashpad_info = TestModule_GetCrashpadInfo(); | |
156 ASSERT_TRUE(remote_crashpad_info); | |
157 | |
158 { | |
159 ScopedUnsetCrashpadInfoOptions unset_local(local_crashpad_info); | |
160 ScopedUnsetCrashpadInfoOptions unset_remote(remote_crashpad_info); | |
161 | |
162 // When only one module sets a value, it applies to the entire process. | |
163 remote_crashpad_info->set_crashpad_handler_behavior(TriState::kEnabled); | |
164 | |
165 process_snapshot.GetCrashpadOptions(&options); | |
166 EXPECT_EQ(TriState::kEnabled, options.crashpad_handler_behavior); | |
167 EXPECT_EQ(TriState::kUnset, options.system_crash_reporter_forwarding); | |
168 | |
169 // When more than one module sets a value, the first one in the module list | |
170 // applies to the process. The local module should appear before the remote | |
171 // module, because the local module loaded the remote module. | |
172 local_crashpad_info->set_crashpad_handler_behavior(TriState::kDisabled); | |
173 | |
174 process_snapshot.GetCrashpadOptions(&options); | |
175 EXPECT_EQ(TriState::kDisabled, options.crashpad_handler_behavior); | |
176 EXPECT_EQ(TriState::kUnset, options.system_crash_reporter_forwarding); | |
177 } | |
178 | |
179 { | |
180 ScopedUnsetCrashpadInfoOptions unset_local(local_crashpad_info); | |
181 ScopedUnsetCrashpadInfoOptions unset_remote(remote_crashpad_info); | |
182 | |
183 // When only one module sets a value, it applies to the entire process. | |
184 remote_crashpad_info->set_system_crash_reporter_forwarding( | |
185 TriState::kDisabled); | |
186 | |
187 process_snapshot.GetCrashpadOptions(&options); | |
188 EXPECT_EQ(TriState::kUnset, options.crashpad_handler_behavior); | |
189 EXPECT_EQ(TriState::kDisabled, options.system_crash_reporter_forwarding); | |
190 | |
191 // When more than one module sets a value, the first one in the module list | |
192 // applies to the process. The local module should appear before the remote | |
193 // module, because the local module loaded the remote module. | |
194 local_crashpad_info->set_system_crash_reporter_forwarding( | |
195 TriState::kEnabled); | |
196 | |
197 process_snapshot.GetCrashpadOptions(&options); | |
198 EXPECT_EQ(TriState::kUnset, options.crashpad_handler_behavior); | |
199 EXPECT_EQ(TriState::kEnabled, options.system_crash_reporter_forwarding); | |
200 } | |
201 } | |
202 | |
203 } // namespace | |
204 } // namespace test | |
205 } // namespace crashpad | |
OLD | NEW |