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