OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 13 matching lines...) Expand all Loading... |
24 namespace crashpad { | 24 namespace crashpad { |
25 namespace test { | 25 namespace test { |
26 namespace { | 26 namespace { |
27 | 27 |
28 TEST(CompositeMachMessageServer, Empty) { | 28 TEST(CompositeMachMessageServer, Empty) { |
29 CompositeMachMessageServer server; | 29 CompositeMachMessageServer server; |
30 | 30 |
31 EXPECT_TRUE(server.MachMessageServerRequestIDs().empty()); | 31 EXPECT_TRUE(server.MachMessageServerRequestIDs().empty()); |
32 | 32 |
33 mach_msg_empty_rcv_t request = {}; | 33 mach_msg_empty_rcv_t request = {}; |
34 EXPECT_EQ(sizeof(request.header), server.MachMessageServerRequestSize()); | 34 EXPECT_EQ(server.MachMessageServerRequestSize(), sizeof(request.header)); |
35 | 35 |
36 mig_reply_error_t reply = {}; | 36 mig_reply_error_t reply = {}; |
37 EXPECT_EQ(sizeof(reply), server.MachMessageServerReplySize()); | 37 EXPECT_EQ(server.MachMessageServerReplySize(), sizeof(reply)); |
38 | 38 |
39 bool destroy_complex_request = false; | 39 bool destroy_complex_request = false; |
40 EXPECT_FALSE(server.MachMessageServerFunction( | 40 EXPECT_FALSE(server.MachMessageServerFunction( |
41 &request.header, &reply.Head, &destroy_complex_request)); | 41 &request.header, &reply.Head, &destroy_complex_request)); |
42 EXPECT_EQ(MIG_BAD_ID, reply.RetCode); | 42 EXPECT_EQ(reply.RetCode, MIG_BAD_ID); |
43 } | 43 } |
44 | 44 |
45 class TestMachMessageHandler : public MachMessageServer::Interface { | 45 class TestMachMessageHandler : public MachMessageServer::Interface { |
46 public: | 46 public: |
47 TestMachMessageHandler() | 47 TestMachMessageHandler() |
48 : MachMessageServer::Interface(), | 48 : MachMessageServer::Interface(), |
49 request_ids_(), | 49 request_ids_(), |
50 request_size_(0), | 50 request_size_(0), |
51 reply_size_(0), | 51 reply_size_(0), |
52 return_code_(KERN_FAILURE), | 52 return_code_(KERN_FAILURE), |
(...skipping 22 matching lines...) Expand all Loading... |
75 | 75 |
76 void SetReplySize(mach_msg_size_t reply_size) { | 76 void SetReplySize(mach_msg_size_t reply_size) { |
77 reply_size_ = reply_size; | 77 reply_size_ = reply_size; |
78 } | 78 } |
79 | 79 |
80 // MachMessageServer::Interface: | 80 // MachMessageServer::Interface: |
81 | 81 |
82 bool MachMessageServerFunction(const mach_msg_header_t* in, | 82 bool MachMessageServerFunction(const mach_msg_header_t* in, |
83 mach_msg_header_t* out, | 83 mach_msg_header_t* out, |
84 bool* destroy_complex_request) override { | 84 bool* destroy_complex_request) override { |
85 EXPECT_NE(request_ids_.end(), request_ids_.find(in->msgh_id)); | 85 EXPECT_NE(request_ids_.find(in->msgh_id), request_ids_.end()); |
86 | 86 |
87 *destroy_complex_request = destroy_complex_request_; | 87 *destroy_complex_request = destroy_complex_request_; |
88 PrepareMIGReplyFromRequest(in, out); | 88 PrepareMIGReplyFromRequest(in, out); |
89 SetMIGReplyError(out, return_code_); | 89 SetMIGReplyError(out, return_code_); |
90 return return_value_; | 90 return return_value_; |
91 } | 91 } |
92 | 92 |
93 std::set<mach_msg_id_t> MachMessageServerRequestIDs() override { | 93 std::set<mach_msg_id_t> MachMessageServerRequestIDs() override { |
94 return request_ids_; | 94 return request_ids_; |
95 } | 95 } |
(...skipping 19 matching lines...) Expand all Loading... |
115 | 115 |
116 TEST(CompositeMachMessageServer, HandlerDoesNotHandle) { | 116 TEST(CompositeMachMessageServer, HandlerDoesNotHandle) { |
117 TestMachMessageHandler handler; | 117 TestMachMessageHandler handler; |
118 | 118 |
119 CompositeMachMessageServer server; | 119 CompositeMachMessageServer server; |
120 server.AddHandler(&handler); | 120 server.AddHandler(&handler); |
121 | 121 |
122 EXPECT_TRUE(server.MachMessageServerRequestIDs().empty()); | 122 EXPECT_TRUE(server.MachMessageServerRequestIDs().empty()); |
123 | 123 |
124 mach_msg_empty_rcv_t request = {}; | 124 mach_msg_empty_rcv_t request = {}; |
125 EXPECT_EQ(sizeof(request.header), server.MachMessageServerRequestSize()); | 125 EXPECT_EQ(server.MachMessageServerRequestSize(), sizeof(request.header)); |
126 | 126 |
127 mig_reply_error_t reply = {}; | 127 mig_reply_error_t reply = {}; |
128 EXPECT_EQ(sizeof(reply), server.MachMessageServerReplySize()); | 128 EXPECT_EQ(server.MachMessageServerReplySize(), sizeof(reply)); |
129 | 129 |
130 bool destroy_complex_request = false; | 130 bool destroy_complex_request = false; |
131 EXPECT_FALSE(server.MachMessageServerFunction( | 131 EXPECT_FALSE(server.MachMessageServerFunction( |
132 &request.header, &reply.Head, &destroy_complex_request)); | 132 &request.header, &reply.Head, &destroy_complex_request)); |
133 EXPECT_EQ(MIG_BAD_ID, reply.RetCode); | 133 EXPECT_EQ(reply.RetCode, MIG_BAD_ID); |
134 EXPECT_FALSE(destroy_complex_request); | 134 EXPECT_FALSE(destroy_complex_request); |
135 } | 135 } |
136 | 136 |
137 TEST(CompositeMachMessageServer, OneHandler) { | 137 TEST(CompositeMachMessageServer, OneHandler) { |
138 const mach_msg_id_t kRequestID = 100; | 138 const mach_msg_id_t kRequestID = 100; |
139 const mach_msg_size_t kRequestSize = 256; | 139 const mach_msg_size_t kRequestSize = 256; |
140 const mach_msg_size_t kReplySize = 128; | 140 const mach_msg_size_t kReplySize = 128; |
141 const kern_return_t kReturnCode = KERN_SUCCESS; | 141 const kern_return_t kReturnCode = KERN_SUCCESS; |
142 | 142 |
143 TestMachMessageHandler handler; | 143 TestMachMessageHandler handler; |
144 handler.AddRequestID(kRequestID); | 144 handler.AddRequestID(kRequestID); |
145 handler.SetRequestSize(kRequestSize); | 145 handler.SetRequestSize(kRequestSize); |
146 handler.SetReplySize(kReplySize); | 146 handler.SetReplySize(kReplySize); |
147 handler.SetReturnCodes(true, kReturnCode, true); | 147 handler.SetReturnCodes(true, kReturnCode, true); |
148 | 148 |
149 CompositeMachMessageServer server; | 149 CompositeMachMessageServer server; |
150 | 150 |
151 // The chosen request and reply sizes must be larger than the defaults for | 151 // The chosen request and reply sizes must be larger than the defaults for |
152 // that portion fo the test to be valid. | 152 // that portion fo the test to be valid. |
153 EXPECT_GT(kRequestSize, server.MachMessageServerRequestSize()); | 153 EXPECT_GT(kRequestSize, server.MachMessageServerRequestSize()); |
154 EXPECT_GT(kReplySize, server.MachMessageServerReplySize()); | 154 EXPECT_GT(kReplySize, server.MachMessageServerReplySize()); |
155 | 155 |
156 server.AddHandler(&handler); | 156 server.AddHandler(&handler); |
157 | 157 |
158 std::set<mach_msg_id_t> expect_request_ids; | 158 std::set<mach_msg_id_t> expect_request_ids; |
159 expect_request_ids.insert(kRequestID); | 159 expect_request_ids.insert(kRequestID); |
160 EXPECT_EQ(expect_request_ids, server.MachMessageServerRequestIDs()); | 160 EXPECT_EQ(server.MachMessageServerRequestIDs(), expect_request_ids); |
161 | 161 |
162 EXPECT_EQ(kRequestSize, server.MachMessageServerRequestSize()); | 162 EXPECT_EQ(server.MachMessageServerRequestSize(), kRequestSize); |
163 EXPECT_EQ(kReplySize, server.MachMessageServerReplySize()); | 163 EXPECT_EQ(server.MachMessageServerReplySize(), kReplySize); |
164 | 164 |
165 mach_msg_empty_rcv_t request = {}; | 165 mach_msg_empty_rcv_t request = {}; |
166 mig_reply_error_t reply = {}; | 166 mig_reply_error_t reply = {}; |
167 | 167 |
168 // Send a message with an unknown request ID. | 168 // Send a message with an unknown request ID. |
169 request.header.msgh_id = 0; | 169 request.header.msgh_id = 0; |
170 bool destroy_complex_request = false; | 170 bool destroy_complex_request = false; |
171 EXPECT_FALSE(server.MachMessageServerFunction( | 171 EXPECT_FALSE(server.MachMessageServerFunction( |
172 &request.header, &reply.Head, &destroy_complex_request)); | 172 &request.header, &reply.Head, &destroy_complex_request)); |
173 EXPECT_EQ(MIG_BAD_ID, reply.RetCode); | 173 EXPECT_EQ(reply.RetCode, MIG_BAD_ID); |
174 EXPECT_FALSE(destroy_complex_request); | 174 EXPECT_FALSE(destroy_complex_request); |
175 | 175 |
176 // Send a message with a known request ID. | 176 // Send a message with a known request ID. |
177 request.header.msgh_id = kRequestID; | 177 request.header.msgh_id = kRequestID; |
178 EXPECT_TRUE(server.MachMessageServerFunction( | 178 EXPECT_TRUE(server.MachMessageServerFunction( |
179 &request.header, &reply.Head, &destroy_complex_request)); | 179 &request.header, &reply.Head, &destroy_complex_request)); |
180 EXPECT_EQ(kReturnCode, reply.RetCode); | 180 EXPECT_EQ(reply.RetCode, kReturnCode); |
181 EXPECT_TRUE(destroy_complex_request); | 181 EXPECT_TRUE(destroy_complex_request); |
182 } | 182 } |
183 | 183 |
184 TEST(CompositeMachMessageServer, ThreeHandlers) { | 184 TEST(CompositeMachMessageServer, ThreeHandlers) { |
185 const mach_msg_id_t kRequestIDs0[] = {5}; | 185 const mach_msg_id_t kRequestIDs0[] = {5}; |
186 const kern_return_t kReturnCode0 = KERN_SUCCESS; | 186 const kern_return_t kReturnCode0 = KERN_SUCCESS; |
187 | 187 |
188 const mach_msg_id_t kRequestIDs1[] = {4, 7}; | 188 const mach_msg_id_t kRequestIDs1[] = {4, 7}; |
189 const kern_return_t kReturnCode1 = KERN_PROTECTION_FAILURE; | 189 const kern_return_t kReturnCode1 = KERN_PROTECTION_FAILURE; |
190 | 190 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 227 |
228 // The chosen request and reply sizes must be larger than the defaults for | 228 // The chosen request and reply sizes must be larger than the defaults for |
229 // that portion fo the test to be valid. | 229 // that portion fo the test to be valid. |
230 EXPECT_GT(kRequestSize2, server.MachMessageServerRequestSize()); | 230 EXPECT_GT(kRequestSize2, server.MachMessageServerRequestSize()); |
231 EXPECT_GT(kReplySize2, server.MachMessageServerReplySize()); | 231 EXPECT_GT(kReplySize2, server.MachMessageServerReplySize()); |
232 | 232 |
233 server.AddHandler(&handlers[0]); | 233 server.AddHandler(&handlers[0]); |
234 server.AddHandler(&handlers[1]); | 234 server.AddHandler(&handlers[1]); |
235 server.AddHandler(&handlers[2]); | 235 server.AddHandler(&handlers[2]); |
236 | 236 |
237 EXPECT_EQ(expect_request_ids, server.MachMessageServerRequestIDs()); | 237 EXPECT_EQ(server.MachMessageServerRequestIDs(), expect_request_ids); |
238 | 238 |
239 EXPECT_EQ(kRequestSize2, server.MachMessageServerRequestSize()); | 239 EXPECT_EQ(server.MachMessageServerRequestSize(), kRequestSize2); |
240 EXPECT_EQ(kReplySize2, server.MachMessageServerReplySize()); | 240 EXPECT_EQ(server.MachMessageServerReplySize(), kReplySize2); |
241 | 241 |
242 mach_msg_empty_rcv_t request = {}; | 242 mach_msg_empty_rcv_t request = {}; |
243 mig_reply_error_t reply = {}; | 243 mig_reply_error_t reply = {}; |
244 | 244 |
245 // Send a message with an unknown request ID. | 245 // Send a message with an unknown request ID. |
246 request.header.msgh_id = 100; | 246 request.header.msgh_id = 100; |
247 bool destroy_complex_request = false; | 247 bool destroy_complex_request = false; |
248 EXPECT_FALSE(server.MachMessageServerFunction( | 248 EXPECT_FALSE(server.MachMessageServerFunction( |
249 &request.header, &reply.Head, &destroy_complex_request)); | 249 &request.header, &reply.Head, &destroy_complex_request)); |
250 EXPECT_EQ(MIG_BAD_ID, reply.RetCode); | 250 EXPECT_EQ(reply.RetCode, MIG_BAD_ID); |
251 EXPECT_FALSE(destroy_complex_request); | 251 EXPECT_FALSE(destroy_complex_request); |
252 | 252 |
253 // Send messages with known request IDs. | 253 // Send messages with known request IDs. |
254 | 254 |
255 for (size_t index = 0; index < arraysize(kRequestIDs0); ++index) { | 255 for (size_t index = 0; index < arraysize(kRequestIDs0); ++index) { |
256 request.header.msgh_id = kRequestIDs0[index]; | 256 request.header.msgh_id = kRequestIDs0[index]; |
257 SCOPED_TRACE(base::StringPrintf( | 257 SCOPED_TRACE(base::StringPrintf( |
258 "handler 0, index %zu, id %d", index, request.header.msgh_id)); | 258 "handler 0, index %zu, id %d", index, request.header.msgh_id)); |
259 | 259 |
260 EXPECT_TRUE(server.MachMessageServerFunction( | 260 EXPECT_TRUE(server.MachMessageServerFunction( |
261 &request.header, &reply.Head, &destroy_complex_request)); | 261 &request.header, &reply.Head, &destroy_complex_request)); |
262 EXPECT_EQ(kReturnCode0, reply.RetCode); | 262 EXPECT_EQ(reply.RetCode, kReturnCode0); |
263 EXPECT_FALSE(destroy_complex_request); | 263 EXPECT_FALSE(destroy_complex_request); |
264 } | 264 } |
265 | 265 |
266 for (size_t index = 0; index < arraysize(kRequestIDs1); ++index) { | 266 for (size_t index = 0; index < arraysize(kRequestIDs1); ++index) { |
267 request.header.msgh_id = kRequestIDs1[index]; | 267 request.header.msgh_id = kRequestIDs1[index]; |
268 SCOPED_TRACE(base::StringPrintf( | 268 SCOPED_TRACE(base::StringPrintf( |
269 "handler 1, index %zu, id %d", index, request.header.msgh_id)); | 269 "handler 1, index %zu, id %d", index, request.header.msgh_id)); |
270 | 270 |
271 EXPECT_FALSE(server.MachMessageServerFunction( | 271 EXPECT_FALSE(server.MachMessageServerFunction( |
272 &request.header, &reply.Head, &destroy_complex_request)); | 272 &request.header, &reply.Head, &destroy_complex_request)); |
273 EXPECT_EQ(kReturnCode1, reply.RetCode); | 273 EXPECT_EQ(reply.RetCode, kReturnCode1); |
274 EXPECT_TRUE(destroy_complex_request); | 274 EXPECT_TRUE(destroy_complex_request); |
275 } | 275 } |
276 | 276 |
277 for (size_t index = 0; index < arraysize(kRequestIDs2); ++index) { | 277 for (size_t index = 0; index < arraysize(kRequestIDs2); ++index) { |
278 request.header.msgh_id = kRequestIDs2[index]; | 278 request.header.msgh_id = kRequestIDs2[index]; |
279 SCOPED_TRACE(base::StringPrintf( | 279 SCOPED_TRACE(base::StringPrintf( |
280 "handler 2, index %zu, id %d", index, request.header.msgh_id)); | 280 "handler 2, index %zu, id %d", index, request.header.msgh_id)); |
281 | 281 |
282 EXPECT_TRUE(server.MachMessageServerFunction( | 282 EXPECT_TRUE(server.MachMessageServerFunction( |
283 &request.header, &reply.Head, &destroy_complex_request)); | 283 &request.header, &reply.Head, &destroy_complex_request)); |
284 EXPECT_EQ(kReturnCode2, reply.RetCode); | 284 EXPECT_EQ(reply.RetCode, kReturnCode2); |
285 EXPECT_TRUE(destroy_complex_request); | 285 EXPECT_TRUE(destroy_complex_request); |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
289 // CompositeMachMessageServer can’t deal with two handlers that want to handle | 289 // CompositeMachMessageServer can’t deal with two handlers that want to handle |
290 // the same request ID. | 290 // the same request ID. |
291 TEST(CompositeMachMessageServerDeathTest, DuplicateRequestID) { | 291 TEST(CompositeMachMessageServerDeathTest, DuplicateRequestID) { |
292 const mach_msg_id_t kRequestID = 400; | 292 const mach_msg_id_t kRequestID = 400; |
293 | 293 |
294 TestMachMessageHandler handlers[2]; | 294 TestMachMessageHandler handlers[2]; |
295 handlers[0].AddRequestID(kRequestID); | 295 handlers[0].AddRequestID(kRequestID); |
296 handlers[1].AddRequestID(kRequestID); | 296 handlers[1].AddRequestID(kRequestID); |
297 | 297 |
298 CompositeMachMessageServer server; | 298 CompositeMachMessageServer server; |
299 | 299 |
300 server.AddHandler(&handlers[0]); | 300 server.AddHandler(&handlers[0]); |
301 EXPECT_DEATH_CHECK(server.AddHandler(&handlers[1]), "duplicate request ID"); | 301 EXPECT_DEATH_CHECK(server.AddHandler(&handlers[1]), "duplicate request ID"); |
302 } | 302 } |
303 | 303 |
304 } // namespace | 304 } // namespace |
305 } // namespace test | 305 } // namespace test |
306 } // namespace crashpad | 306 } // namespace crashpad |
OLD | NEW |