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

Side by Side Diff: ipc/ipc_fuzzing_tests.cc

Issue 14383024: ipc: Use base::MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stdio.h> 5 #include <stdio.h>
6 #include <string> 6 #include <string>
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 IPC::Message::PRIORITY_NORMAL); 168 IPC::Message::PRIORITY_NORMAL);
169 message->WriteInt(reply + 1); 169 message->WriteInt(reply + 1);
170 message->WriteInt(reply); 170 message->WriteInt(reply);
171 return other_->Send(message); 171 return other_->Send(message);
172 } 172 }
173 173
174 void Cleanup() { 174 void Cleanup() {
175 --message_count_; 175 --message_count_;
176 --pending_messages_; 176 --pending_messages_;
177 if (0 == message_count_) 177 if (0 == message_count_)
178 MessageLoop::current()->Quit(); 178 base::MessageLoop::current()->Quit();
179 } 179 }
180 180
181 void ReplyMsgNotHandled(uint32 type_id) { 181 void ReplyMsgNotHandled(uint32 type_id) {
182 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id); 182 RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id);
183 Cleanup(); 183 Cleanup();
184 } 184 }
185 185
186 void UseData(int caller, int value, const std::wstring& text) { 186 void UseData(int caller, int value, const std::wstring& text) {
187 std::wostringstream wos; 187 std::wostringstream wos;
188 wos << L"IPC fuzzer:" << caller << " [" << value << L" " << text << L"]\n"; 188 wos << L"IPC fuzzer:" << caller << " [" << value << L" " << text << L"]\n";
189 std::wstring output = wos.str(); 189 std::wstring output = wos.str();
190 LOG(WARNING) << output.c_str(); 190 LOG(WARNING) << output.c_str();
191 }; 191 };
192 192
193 int message_count_; 193 int message_count_;
194 int pending_messages_; 194 int pending_messages_;
195 }; 195 };
196 196
197 class FuzzerClientListener : public SimpleListener { 197 class FuzzerClientListener : public SimpleListener {
198 public: 198 public:
199 FuzzerClientListener() : last_msg_(NULL) { 199 FuzzerClientListener() : last_msg_(NULL) {
200 } 200 }
201 201
202 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE { 202 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
203 last_msg_ = new IPC::Message(msg); 203 last_msg_ = new IPC::Message(msg);
204 MessageLoop::current()->Quit(); 204 base::MessageLoop::current()->Quit();
205 return true; 205 return true;
206 } 206 }
207 207
208 bool ExpectMessage(int value, uint32 type_id) { 208 bool ExpectMessage(int value, uint32 type_id) {
209 if (!MsgHandlerInternal(type_id)) 209 if (!MsgHandlerInternal(type_id))
210 return false; 210 return false;
211 int msg_value1 = 0; 211 int msg_value1 = 0;
212 int msg_value2 = 0; 212 int msg_value2 = 0;
213 PickleIterator iter(*last_msg_); 213 PickleIterator iter(*last_msg_);
214 if (!last_msg_->ReadInt(&iter, &msg_value1)) 214 if (!last_msg_->ReadInt(&iter, &msg_value1))
215 return false; 215 return false;
216 if (!last_msg_->ReadInt(&iter, &msg_value2)) 216 if (!last_msg_->ReadInt(&iter, &msg_value2))
217 return false; 217 return false;
218 if ((msg_value2 + 1) != msg_value1) 218 if ((msg_value2 + 1) != msg_value1)
219 return false; 219 return false;
220 if (msg_value2 != value) 220 if (msg_value2 != value)
221 return false; 221 return false;
222 222
223 delete last_msg_; 223 delete last_msg_;
224 last_msg_ = NULL; 224 last_msg_ = NULL;
225 return true; 225 return true;
226 } 226 }
227 227
228 bool ExpectMsgNotHandled(uint32 type_id) { 228 bool ExpectMsgNotHandled(uint32 type_id) {
229 return ExpectMessage(type_id, MsgUnhandled::ID); 229 return ExpectMessage(type_id, MsgUnhandled::ID);
230 } 230 }
231 231
232 private: 232 private:
233 bool MsgHandlerInternal(uint32 type_id) { 233 bool MsgHandlerInternal(uint32 type_id) {
234 MessageLoop::current()->Run(); 234 base::MessageLoop::current()->Run();
235 if (NULL == last_msg_) 235 if (NULL == last_msg_)
236 return false; 236 return false;
237 if (FUZZER_ROUTING_ID != last_msg_->routing_id()) 237 if (FUZZER_ROUTING_ID != last_msg_->routing_id())
238 return false; 238 return false;
239 return (type_id == last_msg_->type()); 239 return (type_id == last_msg_->type());
240 }; 240 };
241 241
242 IPC::Message* last_msg_; 242 IPC::Message* last_msg_;
243 }; 243 };
244 244
245 // Runs the fuzzing server child mode. Returns when the preset number of 245 // Runs the fuzzing server child mode. Returns when the preset number of
246 // messages have been received. 246 // messages have been received.
247 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) { 247 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) {
248 MessageLoopForIO main_message_loop; 248 base::MessageLoopForIO main_message_loop;
249 FuzzerServerListener listener; 249 FuzzerServerListener listener;
250 IPC::Channel channel(IPCTestBase::GetChannelName("FuzzServerClient"), 250 IPC::Channel channel(IPCTestBase::GetChannelName("FuzzServerClient"),
251 IPC::Channel::MODE_CLIENT, 251 IPC::Channel::MODE_CLIENT,
252 &listener); 252 &listener);
253 CHECK(channel.Connect()); 253 CHECK(channel.Connect());
254 listener.Init(&channel); 254 listener.Init(&channel);
255 MessageLoop::current()->Run(); 255 base::MessageLoop::current()->Run();
256 return 0; 256 return 0;
257 } 257 }
258 258
259 class IPCFuzzingTest : public IPCTestBase { 259 class IPCFuzzingTest : public IPCTestBase {
260 }; 260 };
261 261
262 // This test makes sure that the FuzzerClientListener and FuzzerServerListener 262 // This test makes sure that the FuzzerClientListener and FuzzerServerListener
263 // are working properly by generating two well formed IPC calls. 263 // are working properly by generating two well formed IPC calls.
264 TEST_F(IPCFuzzingTest, SanityTest) { 264 TEST_F(IPCFuzzingTest, SanityTest) {
265 Init("FuzzServerClient"); 265 Init("FuzzServerClient");
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 msg->WriteInt(0x64); 404 msg->WriteInt(0x64);
405 msg->WriteInt(0x32); 405 msg->WriteInt(0x32);
406 EXPECT_FALSE(server.OnMessageReceived(*msg)); 406 EXPECT_FALSE(server.OnMessageReceived(*msg));
407 delete msg; 407 delete msg;
408 408
409 EXPECT_EQ(0, server.unhandled_msgs()); 409 EXPECT_EQ(0, server.unhandled_msgs());
410 #endif 410 #endif
411 } 411 }
412 412
413 } // namespace 413 } // namespace
OLDNEW
« no previous file with comments | « ipc/ipc_channel_win.cc ('k') | ipc/ipc_logging.h » ('j') | ipc/ipc_sync_channel_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698