[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 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 91 | #if defined(OS_ANDROID) |
| 92 | #define MAYBE_ReadVectorTooLarge1 DISABLED_ReadVectorTooLarge1 |
| 93 | #else |
| 94 | #define MAYBE_ReadVectorTooLarge1 ReadVectorTooLarge1 |
| 95 | #endif |
| 96 | TEST(IPCMessageIntegrity, MAYBE_ReadVectorTooLarge1) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | // This was BUG 1006367. This is the large but positive length case. Again |
| 98 | // we try to hit the non-specialized case vector<P>. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 99 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | EXPECT_TRUE(m.WriteInt(0x21000003)); // This is the count of elements. |
| 101 | EXPECT_TRUE(m.WriteInt64(1)); |
| 102 | EXPECT_TRUE(m.WriteInt64(2)); |
| 103 | |
| 104 | std::vector<int64> vec; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 105 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 107 | } |
| 108 | |
| 109 | TEST(IPCMessageIntegrity, ReadVectorTooLarge2) { |
| 110 | // This was BUG 1006367. This is the large but positive with an additional |
| 111 | // integer overflow when computing the actual byte size. Again we try to hit |
| 112 | // the non-specialized case vector<P>. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 113 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | EXPECT_TRUE(m.WriteInt(0x71000000)); // This is the count of elements. |
| 115 | EXPECT_TRUE(m.WriteInt64(1)); |
| 116 | EXPECT_TRUE(m.WriteInt64(2)); |
| 117 | |
| 118 | std::vector<int64> vec; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 119 | base::PickleIterator iter(m); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | EXPECT_FALSE(ReadParam(&m, &iter, &vec)); |
| 121 | } |
| 122 | |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 123 | class SimpleListener : public IPC::Listener { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | public: |
| 125 | SimpleListener() : other_(NULL) { |
| 126 | } |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 127 | void Init(IPC::Sender* s) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | other_ = s; |
| 129 | } |
| 130 | protected: |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 131 | IPC::Sender* other_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | enum { |
| 135 | FUZZER_ROUTING_ID = 5 |
| 136 | }; |
| 137 | |
| 138 | // The fuzzer server class. It runs in a child process and expects |
| 139 | // only two IPC calls; after that it exits the message loop which |
| 140 | // terminates the child process. |
| 141 | class FuzzerServerListener : public SimpleListener { |
| 142 | public: |
| 143 | FuzzerServerListener() : message_count_(2), pending_messages_(0) { |
| 144 | } |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 145 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 147 | ++pending_messages_; |
| 148 | IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg) |
| 149 | IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage) |
| 150 | IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage) |
| 151 | IPC_END_MESSAGE_MAP() |
| 152 | if (pending_messages_) { |
| 153 | // Probably a problem de-serializing the message. |
| 154 | ReplyMsgNotHandled(msg.type()); |
| 155 | } |
| 156 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 157 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 161 | void OnMsgClassISMessage(int value, const base::string16& text) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | UseData(MsgClassIS::ID, value, text); |
| 163 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value); |
| 164 | Cleanup(); |
| 165 | } |
| 166 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 167 | void OnMsgClassSIMessage(const base::string16& text, int value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | UseData(MsgClassSI::ID, value, text); |
| 169 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value); |
| 170 | Cleanup(); |
| 171 | } |
| 172 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 173 | bool RoundtripAckReply(int routing, uint32 type_id, int reply) { |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 174 | IPC::Message* message = new IPC::Message(routing, type_id, |
| 175 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | message->WriteInt(reply + 1); |
| 177 | message->WriteInt(reply); |
| 178 | return other_->Send(message); |
| 179 | } |
| 180 | |
| 181 | void Cleanup() { |
| 182 | --message_count_; |
| 183 | --pending_messages_; |
| 184 | if (0 == message_count_) |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 185 | base::MessageLoop::current()->Quit(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 188 | void ReplyMsgNotHandled(uint32 type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 189 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | Cleanup(); |
| 191 | } |
| 192 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 193 | void UseData(int caller, int value, const base::string16& text) { |
| 194 | std::ostringstream os; |
| 195 | os << "IPC fuzzer:" << caller << " [" << value << " " |
| 196 | << base::UTF16ToUTF8(text) << "]\n"; |
| 197 | std::string output = os.str(); |
| 198 | LOG(WARNING) << output; |
| 199 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | |
| 201 | int message_count_; |
| 202 | int pending_messages_; |
| 203 | }; |
| 204 | |
| 205 | class FuzzerClientListener : public SimpleListener { |
| 206 | public: |
| 207 | FuzzerClientListener() : last_msg_(NULL) { |
| 208 | } |
| 209 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 210 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | last_msg_ = new IPC::Message(msg); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 212 | base::MessageLoop::current()->Quit(); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 213 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 216 | bool ExpectMessage(int value, uint32 type_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | if (!MsgHandlerInternal(type_id)) |
| 218 | return false; |
| 219 | int msg_value1 = 0; |
| 220 | int msg_value2 = 0; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 221 | base::PickleIterator iter(*last_msg_); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 222 | if (!iter.ReadInt(&msg_value1)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | return false; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 224 | if (!iter.ReadInt(&msg_value2)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | return false; |
| 226 | if ((msg_value2 + 1) != msg_value1) |
| 227 | return false; |
| 228 | if (msg_value2 != value) |
| 229 | return false; |
| 230 | |
| 231 | delete last_msg_; |
| 232 | last_msg_ = NULL; |
| 233 | return true; |
| 234 | } |
| 235 | |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 236 | bool ExpectMsgNotHandled(uint32 type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 237 | return ExpectMessage(type_id, MsgUnhandled::ID); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private: |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 241 | bool MsgHandlerInternal(uint32 type_id) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 242 | base::MessageLoop::current()->Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | if (NULL == last_msg_) |
| 244 | return false; |
| 245 | if (FUZZER_ROUTING_ID != last_msg_->routing_id()) |
| 246 | return false; |
| 247 | return (type_id == last_msg_->type()); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 248 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 249 | |
| 250 | IPC::Message* last_msg_; |
| 251 | }; |
| 252 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 253 | // Runs the fuzzing server child mode. Returns when the preset number of |
| 254 | // messages have been received. |
| 255 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(FuzzServerClient) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 256 | base::MessageLoopForIO main_message_loop; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | FuzzerServerListener listener; |
[email protected] | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 258 | scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 259 | IPCTestBase::GetChannelName("FuzzServerClient"), &listener, nullptr)); |
[email protected] | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 260 | CHECK(channel->Connect()); |
| 261 | listener.Init(channel.get()); |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 262 | base::MessageLoop::current()->Run(); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 263 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | 0cb7d8c8 | 2013-01-11 15:13:37 | [diff] [blame] | 266 | class IPCFuzzingTest : public IPCTestBase { |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 267 | }; |
| 268 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 269 | #if defined(OS_ANDROID) |
| 270 | #define MAYBE_SanityTest DISABLED_SanityTest |
| 271 | #else |
| 272 | #define MAYBE_SanityTest SanityTest |
| 273 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | // This test makes sure that the FuzzerClientListener and FuzzerServerListener |
| 275 | // are working properly by generating two well formed IPC calls. |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 276 | TEST_F(IPCFuzzingTest, MAYBE_SanityTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 277 | Init("FuzzServerClient"); |
| 278 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 279 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 280 | CreateChannel(&listener); |
| 281 | listener.Init(channel()); |
| 282 | ASSERT_TRUE(ConnectChannel()); |
| 283 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | |
| 285 | IPC::Message* msg = NULL; |
| 286 | int value = 43; |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 287 | msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43")); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 288 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassIS::ID)); |
| 290 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 291 | msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 292 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); |
| 294 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 295 | EXPECT_TRUE(WaitForClientShutdown()); |
| 296 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | } |
| 298 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 299 | #if defined(OS_ANDROID) |
| 300 | #define MAYBE_MsgBadPayloadShort DISABLED_MsgBadPayloadShort |
| 301 | #else |
| 302 | #define MAYBE_MsgBadPayloadShort MsgBadPayloadShort |
| 303 | #endif |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 304 | // This test uses a payload that is smaller than expected. This generates an |
| 305 | // error while unpacking the IPC buffer which in debug trigger an assertion and |
| 306 | // in release is ignored (!). Right after we generate another valid IPC to make |
| 307 | // sure framing is working properly. |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 308 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 309 | TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadShort) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 310 | Init("FuzzServerClient"); |
| 311 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 312 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 313 | CreateChannel(&listener); |
| 314 | listener.Init(channel()); |
| 315 | ASSERT_TRUE(ConnectChannel()); |
| 316 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 318 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 319 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | msg->WriteInt(666); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 321 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 323 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 324 | msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 325 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | EXPECT_TRUE(listener.ExpectMessage(1, MsgClassSI::ID)); |
| 327 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 328 | EXPECT_TRUE(WaitForClientShutdown()); |
| 329 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 331 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 333 | #if defined(OS_ANDROID) |
| 334 | #define MAYBE_MsgBadPayloadArgs DISABLED_MsgBadPayloadArgs |
| 335 | #else |
| 336 | #define MAYBE_MsgBadPayloadArgs MsgBadPayloadArgs |
| 337 | #endif |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 338 | // This test uses a payload that has too many arguments, but so the payload size |
| 339 | // is big enough so the unpacking routine does not generate an error as in the |
| 340 | // case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se) |
| 341 | // as by design we don't carry type information on the IPC message. |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame^] | 342 | TEST_F(IPCFuzzingTest, MAYBE_MsgBadPayloadArgs) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 343 | Init("FuzzServerClient"); |
| 344 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 345 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 346 | CreateChannel(&listener); |
| 347 | listener.Init(channel()); |
| 348 | ASSERT_TRUE(ConnectChannel()); |
| 349 | ASSERT_TRUE(StartClient()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 351 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 352 | IPC::Message::PRIORITY_NORMAL); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 353 | msg->WriteString16(base::ASCIIToUTF16("d")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | msg->WriteInt(0); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 355 | msg->WriteInt(0x65); // Extra argument. |
| 356 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 357 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 359 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 360 | // Now send a well formed message to make sure the receiver wasn't |
| 361 | // thrown out of sync by the extra argument. |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 362 | msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three")); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 363 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 365 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 366 | EXPECT_TRUE(WaitForClientShutdown()); |
| 367 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 370 | } // namespace |