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