[email protected] | b539333 | 2012-01-13 00:11:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | #include <stdio.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <sstream> |
| 8 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 10 | #include "base/process_util.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 11 | #include "base/threading/platform_thread.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 12 | #include "ipc/ipc_channel.h" |
| 13 | #include "ipc/ipc_channel_proxy.h" |
[email protected] | 97c652b | 2012-06-27 01:12:24 | [diff] [blame] | 14 | #include "ipc/ipc_multiprocess_test.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 15 | #include "ipc/ipc_tests.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 17 | #include "testing/multiprocess_func_list.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 19 | // IPC messages for testing --------------------------------------------------- |
| 20 | |
| 21 | #define IPC_MESSAGE_IMPL |
| 22 | #include "ipc/ipc_message_macros.h" |
| 23 | |
| 24 | #define IPC_MESSAGE_START TestMsgStart |
| 25 | |
| 26 | // Generic message class that is an int followed by a wstring. |
| 27 | IPC_MESSAGE_CONTROL2(MsgClassIS, int, std::wstring) |
| 28 | |
| 29 | // Generic message class that is a wstring followed by an int. |
| 30 | IPC_MESSAGE_CONTROL2(MsgClassSI, std::wstring, int) |
| 31 | |
| 32 | // Message to create a mutex in the IPC server, using the received name. |
| 33 | IPC_MESSAGE_CONTROL2(MsgDoMutex, std::wstring, int) |
| 34 | |
| 35 | // Used to generate an ID for a message that should not exist. |
| 36 | IPC_MESSAGE_CONTROL0(MsgUnhandled) |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | TEST(IPCMessageIntegrity, ReadBeyondBufferStr) { |
| 41 | //This was BUG 984408. |
| 42 | uint32 v1 = kuint32max - 1; |
| 43 | int v2 = 666; |
| 44 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 45 | EXPECT_TRUE(m.WriteInt(v1)); |
| 46 | EXPECT_TRUE(m.WriteInt(v2)); |
| 47 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 48 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | std::string vs; |
| 50 | EXPECT_FALSE(m.ReadString(&iter, &vs)); |
| 51 | } |
| 52 | |
| 53 | TEST(IPCMessageIntegrity, ReadBeyondBufferWStr) { |
| 54 | //This was BUG 984408. |
| 55 | uint32 v1 = kuint32max - 1; |
| 56 | int v2 = 777; |
| 57 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 58 | EXPECT_TRUE(m.WriteInt(v1)); |
| 59 | EXPECT_TRUE(m.WriteInt(v2)); |
| 60 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 61 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | std::wstring vs; |
| 63 | EXPECT_FALSE(m.ReadWString(&iter, &vs)); |
| 64 | } |
| 65 | |
| 66 | TEST(IPCMessageIntegrity, ReadBytesBadIterator) { |
| 67 | // This was BUG 1035467. |
| 68 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 69 | EXPECT_TRUE(m.WriteInt(1)); |
| 70 | EXPECT_TRUE(m.WriteInt(2)); |
| 71 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 72 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | const char* data = NULL; |
[email protected] | 26d2f47 | 2010-03-30 23:52:24 | [diff] [blame] | 74 | EXPECT_TRUE(m.ReadBytes(&iter, &data, sizeof(int))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(IPCMessageIntegrity, ReadVectorNegativeSize) { |
| 78 | // A slight variation of BUG 984408. Note that the pickling of vector<char> |
| 79 | // has a specialized template which is not vulnerable to this bug. So here |
| 80 | // try to hit the non-specialized case vector<P>. |
| 81 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 82 | EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements. |
| 83 | EXPECT_TRUE(m.WriteInt(1)); |
| 84 | EXPECT_TRUE(m.WriteInt(2)); |
| 85 | EXPECT_TRUE(m.WriteInt(3)); |
| 86 | |
| 87 | std::vector<double> vec; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 88 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 90 | } |
| 91 | |
| 92 | TEST(IPCMessageIntegrity, ReadVectorTooLarge1) { |
| 93 | // This was BUG 1006367. This is the large but positive length case. Again |
| 94 | // we try to hit the non-specialized case vector<P>. |
| 95 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 96 | EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements. |
| 97 | EXPECT_TRUE(m.WriteInt64(1)); |
| 98 | EXPECT_TRUE(m.WriteInt64(2)); |
| 99 | |
| 100 | std::vector<int64> vec; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 101 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 103 | } |
| 104 | |
| 105 | TEST(IPCMessageIntegrity, ReadVectorTooLarge2) { |
| 106 | // This was BUG 1006367. This is the large but positive with an additional |
| 107 | // integer overflow when computing the actual byte size. Again we try to hit |
| 108 | // the non-specialized case vector<P>. |
| 109 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 110 | EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements. |
| 111 | EXPECT_TRUE(m.WriteInt64(1)); |
| 112 | EXPECT_TRUE(m.WriteInt64(2)); |
| 113 | |
| 114 | std::vector<int64> vec; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 115 | PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 117 | } |
| 118 | |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 119 | class SimpleListener : public IPC::Listener { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | public: |
| 121 | SimpleListener() : other_(NULL) { |
| 122 | } |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 123 | void Init(IPC::Sender* s) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | other_ = s; |
| 125 | } |
| 126 | protected: |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 127 | IPC::Sender* other_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | enum { |
| 131 | FUZZER_ROUTING_ID = 5 |
| 132 | }; |
| 133 | |
| 134 | // The fuzzer server class. It runs in a child process and expects |
| 135 | // only two IPC calls; after that it exits the message loop which |
| 136 | // terminates the child process. |
| 137 | class FuzzerServerListener : public SimpleListener { |
| 138 | public: |
| 139 | FuzzerServerListener() : message_count_(2), pending_messages_(0) { |
| 140 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 141 | virtual bool OnMessageReceived(const IPC::Message& msg) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 143 | ++pending_messages_; |
| 144 | IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg) |
| 145 | IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage) |
| 146 | IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage) |
| 147 | IPC_END_MESSAGE_MAP() |
| 148 | if (pending_messages_) { |
| 149 | // Probably a problem de-serializing the message. |
| 150 | ReplyMsgNotHandled(msg.type()); |
| 151 | } |
| 152 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 153 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | void OnMsgClassISMessage(int value, const std::wstring& text) { |
| 158 | UseData(MsgClassIS::ID, value, text); |
| 159 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value); |
| 160 | Cleanup(); |
| 161 | } |
| 162 | |
| 163 | void OnMsgClassSIMessage(const std::wstring& text, int value) { |
| 164 | UseData(MsgClassSI::ID, value, text); |
| 165 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value); |
| 166 | Cleanup(); |
| 167 | } |
| 168 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 169 | bool RoundtripAckReply(int routing, uint32 type_id, int reply) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | IPC::Message* message = new IPC::Message(routing, type_id, |
| 171 | IPC::Message::PRIORITY_NORMAL); |
| 172 | message->WriteInt(reply + 1); |
| 173 | message->WriteInt(reply); |
| 174 | return other_->Send(message); |
| 175 | } |
| 176 | |
| 177 | void Cleanup() { |
| 178 | --message_count_; |
| 179 | --pending_messages_; |
| 180 | if (0 == message_count_) |
| 181 | MessageLoop::current()->Quit(); |
| 182 | } |
| 183 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 184 | void ReplyMsgNotHandled(uint32 type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 185 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | Cleanup(); |
| 187 | } |
| 188 | |
| 189 | void UseData(int caller, int value, const std::wstring& text) { |
| 190 | std::wostringstream wos; |
| 191 | wos << L"IPC fuzzer:" << caller << " [" << value << L" " << text << L"]\n"; |
| 192 | std::wstring output = wos.str(); |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 193 | LOG(WARNING) << output.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | int message_count_; |
| 197 | int pending_messages_; |
| 198 | }; |
| 199 | |
| 200 | class FuzzerClientListener : public SimpleListener { |
| 201 | public: |
| 202 | FuzzerClientListener() : last_msg_(NULL) { |
| 203 | } |
| 204 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 205 | virtual bool OnMessageReceived(const IPC::Message& msg) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | last_msg_ = new IPC::Message(msg); |
| 207 | MessageLoop::current()->Quit(); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 208 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 211 | bool ExpectMessage(int value, uint32 type_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | if (!MsgHandlerInternal(type_id)) |
| 213 | return false; |
| 214 | int msg_value1 = 0; |
| 215 | int msg_value2 = 0; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 216 | PickleIterator iter(*last_msg_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | if (!last_msg_->ReadInt(&iter, &msg_value1)) |
| 218 | return false; |
| 219 | if (!last_msg_->ReadInt(&iter, &msg_value2)) |
| 220 | return false; |
| 221 | if ((msg_value2 + 1) != msg_value1) |
| 222 | return false; |
| 223 | if (msg_value2 != value) |
| 224 | return false; |
| 225 | |
| 226 | delete last_msg_; |
| 227 | last_msg_ = NULL; |
| 228 | return true; |
| 229 | } |
| 230 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 231 | bool ExpectMsgNotHandled(uint32 type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 232 | return ExpectMessage(type_id, MsgUnhandled::ID); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | private: |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 236 | bool MsgHandlerInternal(uint32 type_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | MessageLoop::current()->Run(); |
| 238 | if (NULL == last_msg_) |
| 239 | return false; |
| 240 | if (FUZZER_ROUTING_ID != last_msg_->routing_id()) |
| 241 | return false; |
| 242 | return (type_id == last_msg_->type()); |
| 243 | }; |
| 244 | |
| 245 | IPC::Message* last_msg_; |
| 246 | }; |
| 247 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 248 | // Runs the fuzzing server child mode. Returns when the preset number |
| 249 | // of messages have been received. |
[email protected] | 97c652b | 2012-06-27 01:12:24 | [diff] [blame] | 250 | MULTIPROCESS_IPC_TEST_MAIN(RunFuzzServer) { |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 251 | MessageLoopForIO main_message_loop; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | FuzzerServerListener listener; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 253 | IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame] | 254 | CHECK(chan.Connect()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | listener.Init(&chan); |
| 256 | MessageLoop::current()->Run(); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 257 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 260 | class IPCFuzzingTest : public IPCChannelTest { |
| 261 | }; |
| 262 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | // This test makes sure that the FuzzerClientListener and FuzzerServerListener |
| 264 | // are working properly by generating two well formed IPC calls. |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 265 | TEST_F(IPCFuzzingTest, SanityTest) { |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 266 | FuzzerClientListener listener; |
| 267 | IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, |
| 268 | &listener); |
| 269 | base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | ASSERT_TRUE(server_process); |
[email protected] | b539333 | 2012-01-13 00:11:01 | [diff] [blame] | 271 | base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | ASSERT_TRUE(chan.Connect()); |
| 273 | listener.Init(&chan); |
| 274 | |
| 275 | IPC::Message* msg = NULL; |
| 276 | int value = 43; |
| 277 | msg = new MsgClassIS(value, L"expect 43"); |
| 278 | chan.Send(msg); |
| 279 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID)); |
| 280 | |
| 281 | msg = new MsgClassSI(L"expect 44", ++value); |
| 282 | chan.Send(msg); |
| 283 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); |
| 284 | |
[email protected] | 0f2e45ec | 2012-07-11 15:41:43 | [diff] [blame^] | 285 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 286 | server_process, base::TimeDelta::FromSeconds(5))); |
[email protected] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 287 | base::CloseProcessHandle(server_process); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // This test uses a payload that is smaller than expected. |
| 291 | // This generates an error while unpacking the IPC buffer which in |
| 292 | // In debug this triggers an assertion and in release it is ignored(!!). Right |
| 293 | // after we generate another valid IPC to make sure framing is working |
| 294 | // properly. |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 295 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 296 | TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 297 | FuzzerClientListener listener; |
| 298 | IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, |
| 299 | &listener); |
| 300 | base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | ASSERT_TRUE(server_process); |
[email protected] | b539333 | 2012-01-13 00:11:01 | [diff] [blame] | 302 | base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | ASSERT_TRUE(chan.Connect()); |
| 304 | listener.Init(&chan); |
| 305 | |
| 306 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 307 | IPC::Message::PRIORITY_NORMAL); |
| 308 | msg->WriteInt(666); |
| 309 | chan.Send(msg); |
| 310 | EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 311 | |
| 312 | msg = new MsgClassSI(L"expect one", 1); |
| 313 | chan.Send(msg); |
| 314 | EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); |
| 315 | |
[email protected] | 0f2e45ec | 2012-07-11 15:41:43 | [diff] [blame^] | 316 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 317 | server_process, base::TimeDelta::FromSeconds(5))); |
[email protected] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 318 | base::CloseProcessHandle(server_process); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | } |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 320 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 322 | // This test uses a payload that has too many arguments, but so the payload |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | // size is big enough so the unpacking routine does not generate an error as |
| 324 | // in the case of MsgBadPayloadShort test. |
| 325 | // This test does not pinpoint a flaw (per se) as by design we don't carry |
| 326 | // type information on the IPC message. |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 327 | TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 328 | FuzzerClientListener listener; |
| 329 | IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, |
| 330 | &listener); |
| 331 | base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER, &chan); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | ASSERT_TRUE(server_process); |
[email protected] | b539333 | 2012-01-13 00:11:01 | [diff] [blame] | 333 | base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | ASSERT_TRUE(chan.Connect()); |
| 335 | listener.Init(&chan); |
| 336 | |
| 337 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 338 | IPC::Message::PRIORITY_NORMAL); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 339 | msg->WriteWString(L"d"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | msg->WriteInt(0); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 341 | msg->WriteInt(0x65); // Extra argument. |
| 342 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | chan.Send(msg); |
| 344 | EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 345 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 346 | // Now send a well formed message to make sure the receiver wasn't |
| 347 | // thrown out of sync by the extra argument. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | msg = new MsgClassIS(3, L"expect three"); |
| 349 | chan.Send(msg); |
| 350 | EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 351 | |
[email protected] | 0f2e45ec | 2012-07-11 15:41:43 | [diff] [blame^] | 352 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 353 | server_process, base::TimeDelta::FromSeconds(5))); |
[email protected] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 354 | base::CloseProcessHandle(server_process); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // This class is for testing the IPC_BEGIN_MESSAGE_MAP_EX macros. |
| 358 | class ServerMacroExTest { |
| 359 | public: |
| 360 | ServerMacroExTest() : unhandled_msgs_(0) { |
| 361 | } |
[email protected] | 6fa21d0 | 2011-11-04 00:14:16 | [diff] [blame] | 362 | |
[email protected] | 695092f | 2010-08-02 16:34:16 | [diff] [blame] | 363 | virtual ~ServerMacroExTest() { |
| 364 | } |
[email protected] | 6fa21d0 | 2011-11-04 00:14:16 | [diff] [blame] | 365 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | virtual bool OnMessageReceived(const IPC::Message& msg) { |
| 367 | bool msg_is_ok = false; |
| 368 | IPC_BEGIN_MESSAGE_MAP_EX(ServerMacroExTest, msg, msg_is_ok) |
| 369 | IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage) |
| 370 | IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage) |
| 371 | IPC_MESSAGE_UNHANDLED(++unhandled_msgs_) |
| 372 | IPC_END_MESSAGE_MAP_EX() |
| 373 | return msg_is_ok; |
| 374 | } |
| 375 | |
| 376 | int unhandled_msgs() const { |
| 377 | return unhandled_msgs_; |
| 378 | } |
| 379 | |
| 380 | private: |
| 381 | void OnMsgClassISMessage(int value, const std::wstring& text) { |
| 382 | } |
| 383 | void OnMsgClassSIMessage(const std::wstring& text, int value) { |
| 384 | } |
| 385 | |
| 386 | int unhandled_msgs_; |
[email protected] | 6fa21d0 | 2011-11-04 00:14:16 | [diff] [blame] | 387 | |
| 388 | DISALLOW_COPY_AND_ASSIGN(ServerMacroExTest); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | }; |
| 390 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 391 | TEST_F(IPCFuzzingTest, MsgMapExMacro) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | IPC::Message* msg = NULL; |
| 393 | ServerMacroExTest server; |
| 394 | |
| 395 | // Test the regular messages. |
| 396 | msg = new MsgClassIS(3, L"text3"); |
| 397 | EXPECT_TRUE(server.OnMessageReceived(*msg)); |
| 398 | delete msg; |
| 399 | msg = new MsgClassSI(L"text2", 2); |
| 400 | EXPECT_TRUE(server.OnMessageReceived(*msg)); |
| 401 | delete msg; |
| 402 | |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 403 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | // Test a bad message. |
| 405 | msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 406 | IPC::Message::PRIORITY_NORMAL); |
| 407 | msg->WriteInt(2); |
| 408 | EXPECT_FALSE(server.OnMessageReceived(*msg)); |
| 409 | delete msg; |
| 410 | |
| 411 | msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 412 | IPC::Message::PRIORITY_NORMAL); |
| 413 | msg->WriteInt(0x64); |
| 414 | msg->WriteInt(0x32); |
| 415 | EXPECT_FALSE(server.OnMessageReceived(*msg)); |
| 416 | delete msg; |
| 417 | |
| 418 | EXPECT_EQ(0, server.unhandled_msgs()); |
| 419 | #endif |
| 420 | } |