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