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