[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] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 10 | #include "base/strings/string16.h" |
| 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 12 | #include "base/threading/platform_thread.h" |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 13 | #include "ipc/ipc_test_base.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 16 | // IPC messages for testing ---------------------------------------------------- |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 17 | |
| 18 | #define IPC_MESSAGE_IMPL |
| 19 | #include "ipc/ipc_message_macros.h" |
| 20 | |
| 21 | #define IPC_MESSAGE_START TestMsgStart |
| 22 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 23 | // Generic message class that is an int followed by a string16. |
| 24 | IPC_MESSAGE_CONTROL2(MsgClassIS, int, base::string16) |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 25 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 26 | // Generic message class that is a string16 followed by an int. |
| 27 | IPC_MESSAGE_CONTROL2(MsgClassSI, base::string16, int) |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 28 | |
| 29 | // Message to create a mutex in the IPC server, using the received name. |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 30 | IPC_MESSAGE_CONTROL2(MsgDoMutex, base::string16, int) |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 31 | |
| 32 | // Used to generate an ID for a message that should not exist. |
| 33 | IPC_MESSAGE_CONTROL0(MsgUnhandled) |
| 34 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 35 | // ----------------------------------------------------------------------------- |
| 36 | |
| 37 | namespace { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | TEST(IPCMessageIntegrity, ReadBeyondBufferStr) { |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 40 | // This was BUG 984408. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | uint32 v1 = kuint32max - 1; |
| 42 | int v2 = 666; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 43 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | EXPECT_TRUE(m.WriteInt(v1)); |
| 45 | EXPECT_TRUE(m.WriteInt(v2)); |
| 46 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 47 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | std::string vs; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 49 | EXPECT_FALSE(iter.ReadString(&vs)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 52 | TEST(IPCMessageIntegrity, ReadBeyondBufferStr16) { |
| 53 | // This was BUG 984408. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | uint32 v1 = kuint32max - 1; |
| 55 | int v2 = 777; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 56 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | EXPECT_TRUE(m.WriteInt(v1)); |
| 58 | EXPECT_TRUE(m.WriteInt(v2)); |
| 59 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 60 | base::PickleIterator iter(m); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 61 | base::string16 vs; |
| 62 | EXPECT_FALSE(iter.ReadString16(&vs)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST(IPCMessageIntegrity, ReadBytesBadIterator) { |
| 66 | // This was BUG 1035467. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 67 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | EXPECT_TRUE(m.WriteInt(1)); |
| 69 | EXPECT_TRUE(m.WriteInt(2)); |
| 70 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 71 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | const char* data = NULL; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 73 | EXPECT_TRUE(iter.ReadBytes(&data, sizeof(int))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(IPCMessageIntegrity, ReadVectorNegativeSize) { |
| 77 | // A slight variation of BUG 984408. Note that the pickling of vector<char> |
| 78 | // has a specialized template which is not vulnerable to this bug. So here |
| 79 | // try to hit the non-specialized case vector<P>. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 80 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | EXPECT_TRUE(m.WriteInt(-1)); // This is the count of elements. |
| 82 | EXPECT_TRUE(m.WriteInt(1)); |
| 83 | EXPECT_TRUE(m.WriteInt(2)); |
| 84 | EXPECT_TRUE(m.WriteInt(3)); |
| 85 | |
| 86 | std::vector<double> vec; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 87 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 89 | } |
| 90 | |
| 91 | TEST(IPCMessageIntegrity, ReadVectorTooLarge1) { |
| 92 | // This was BUG 1006367. This is the large but positive length case. Again |
| 93 | // we try to hit the non-specialized case vector<P>. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 94 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements. |
| 96 | EXPECT_TRUE(m.WriteInt64(1)); |
| 97 | EXPECT_TRUE(m.WriteInt64(2)); |
| 98 | |
| 99 | std::vector<int64> vec; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 100 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 102 | } |
| 103 | |
| 104 | TEST(IPCMessageIntegrity, ReadVectorTooLarge2) { |
| 105 | // This was BUG 1006367. This is the large but positive with an additional |
| 106 | // integer overflow when computing the actual byte size. Again we try to hit |
| 107 | // the non-specialized case vector<P>. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 108 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements. |
| 110 | EXPECT_TRUE(m.WriteInt64(1)); |
| 111 | EXPECT_TRUE(m.WriteInt64(2)); |
| 112 | |
| 113 | std::vector<int64> vec; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 114 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 116 | } |
| 117 | |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 118 | class SimpleListener : public IPC::Listener { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | public: |
| 120 | SimpleListener() : other_(NULL) { |
| 121 | } |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 122 | void Init(IPC::Sender* s) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | other_ = s; |
| 124 | } |
| 125 | protected: |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 126 | IPC::Sender* other_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | enum { |
| 130 | FUZZER_ROUTING_ID = 5 |
| 131 | }; |
| 132 | |
| 133 | // The fuzzer server class. It runs in a child process and expects |
| 134 | // only two IPC calls; after that it exits the message loop which |
| 135 | // terminates the child process. |
| 136 | class FuzzerServerListener : public SimpleListener { |
| 137 | public: |
| 138 | FuzzerServerListener() : message_count_(2), pending_messages_(0) { |
| 139 | } |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 140 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 142 | ++pending_messages_; |
| 143 | IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg) |
| 144 | IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage) |
| 145 | IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage) |
| 146 | IPC_END_MESSAGE_MAP() |
| 147 | if (pending_messages_) { |
| 148 | // Probably a problem de-serializing the message. |
| 149 | ReplyMsgNotHandled(msg.type()); |
| 150 | } |
| 151 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 152 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 156 | void OnMsgClassISMessage(int value, const base::string16& text) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | UseData(MsgClassIS::ID, value, text); |
| 158 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value); |
| 159 | Cleanup(); |
| 160 | } |
| 161 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 162 | void OnMsgClassSIMessage(const base::string16& text, int value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | UseData(MsgClassSI::ID, value, text); |
| 164 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value); |
| 165 | Cleanup(); |
| 166 | } |
| 167 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 168 | bool RoundtripAckReply(int routing, uint32 type_id, int reply) { |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 169 | IPC::Message* message = new IPC::Message(routing, type_id, |
| 170 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | message->WriteInt(reply + 1); |
| 172 | message->WriteInt(reply); |
| 173 | return other_->Send(message); |
| 174 | } |
| 175 | |
| 176 | void Cleanup() { |
| 177 | --message_count_; |
| 178 | --pending_messages_; |
| 179 | if (0 == message_count_) |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 180 | base::MessageLoop::current()->Quit(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 183 | void ReplyMsgNotHandled(uint32 type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 184 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | Cleanup(); |
| 186 | } |
| 187 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 188 | void UseData(int caller, int value, const base::string16& text) { |
| 189 | std::ostringstream os; |
| 190 | os << "IPC fuzzer:" << caller << " [" << value << " " |
| 191 | << base::UTF16ToUTF8(text) << "]\n"; |
| 192 | std::string output = os.str(); |
| 193 | LOG(WARNING) << output; |
| 194 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 205 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | last_msg_ = new IPC::Message(msg); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 207 | base::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; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 216 | base::PickleIterator iter(*last_msg_); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 217 | if (!iter.ReadInt(&msg_value1)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | return false; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 219 | if (!iter.ReadInt(&msg_value2)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 237 | base::MessageLoop::current()->Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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()); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 243 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | |
| 245 | IPC::Message* last_msg_; |
| 246 | }; |
| 247 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 248 | // Runs the fuzzing server child mode. Returns when the preset number of |
| 249 | // messages have been received. |
| 250 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 251 | base::MessageLoopForIO main_message_loop; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | FuzzerServerListener listener; |
[email protected] | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 253 | scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 254 | IPCTestBase::GetChannelName("FuzzServerClient"), &listener, nullptr)); |
[email protected] | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 255 | CHECK(channel->Connect()); |
| 256 | listener.Init(channel.get()); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 257 | base::MessageLoop::current()->Run(); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 258 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 261 | class IPCFuzzingTest : public IPCTestBase { |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 262 | }; |
| 263 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | // This test makes sure that the FuzzerClientListener and FuzzerServerListener |
| 265 | // are working properly by generating two well formed IPC calls. |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 266 | TEST_F(IPCFuzzingTest, SanityTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 267 | Init("FuzzServerClient"); |
| 268 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 269 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 270 | CreateChannel(&listener); |
| 271 | listener.Init(channel()); |
| 272 | ASSERT_TRUE(ConnectChannel()); |
| 273 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | |
| 275 | IPC::Message* msg = NULL; |
| 276 | int value = 43; |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 277 | msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43")); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 278 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID)); |
| 280 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 281 | msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 282 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); |
| 284 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 285 | EXPECT_TRUE(WaitForClientShutdown()); |
| 286 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 289 | // This test uses a payload that is smaller than expected. This generates an |
| 290 | // error while unpacking the IPC buffer which in debug trigger an assertion and |
| 291 | // in release is ignored (!). Right after we generate another valid IPC to make |
| 292 | // sure framing is working properly. |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 293 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 294 | TEST_F(IPCFuzzingTest, MsgBadPayloadShort) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 295 | Init("FuzzServerClient"); |
| 296 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 297 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 298 | CreateChannel(&listener); |
| 299 | listener.Init(channel()); |
| 300 | ASSERT_TRUE(ConnectChannel()); |
| 301 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 302 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 303 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 304 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | msg->WriteInt(666); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 306 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 308 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 309 | msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 310 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); |
| 312 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 313 | EXPECT_TRUE(WaitForClientShutdown()); |
| 314 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | } |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 316 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 318 | // This test uses a payload that has too many arguments, but so the payload size |
| 319 | // is big enough so the unpacking routine does not generate an error as in the |
| 320 | // case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se) |
| 321 | // as by design we don't carry type information on the IPC message. |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 322 | TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 323 | Init("FuzzServerClient"); |
| 324 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 325 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 326 | CreateChannel(&listener); |
| 327 | listener.Init(channel()); |
| 328 | ASSERT_TRUE(ConnectChannel()); |
| 329 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 331 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 332 | IPC::Message::PRIORITY_NORMAL); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 333 | msg->WriteString16(base::ASCIIToUTF16("d")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | msg->WriteInt(0); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 335 | msg->WriteInt(0x65); // Extra argument. |
| 336 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 337 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 339 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 340 | // Now send a well formed message to make sure the receiver wasn't |
| 341 | // thrown out of sync by the extra argument. |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 342 | msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three")); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 343 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 345 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 346 | EXPECT_TRUE(WaitForClientShutdown()); |
| 347 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 350 | } // namespace |