[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 | |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 13 | #include "base/run_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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 49 | m.WriteInt(v1); |
| 50 | m.WriteInt(v2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 62 | m.WriteInt(v1); |
| 63 | m.WriteInt(v2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 73 | m.WriteInt(1); |
| 74 | m.WriteInt(2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 86 | m.WriteInt(-1); // This is the count of elements. |
| 87 | m.WriteInt(1); |
| 88 | m.WriteInt(2); |
| 89 | m.WriteInt(3); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 105 | m.WriteInt(0x21000003); // This is the count of elements. |
| 106 | m.WriteInt64(1); |
| 107 | m.WriteInt64(2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 119 | m.WriteInt(0x71000000); // This is the count of elements. |
| 120 | m.WriteInt64(1); |
| 121 | m.WriteInt64(2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
zijiehe | 84eef4fa | 2017-07-18 20:39:50 | [diff] [blame] | 128 | // This test needs ~20 seconds in Debug mode, or ~4 seconds in Release mode. |
| 129 | // See http://crbug.com/741866 for details. |
| 130 | TEST(IPCMessageIntegrity, DISABLED_ReadVectorTooLarge3) { |
| 131 | base::Pickle pickle; |
| 132 | IPC::WriteParam(&pickle, 256 * 1024 * 1024); |
| 133 | IPC::WriteParam(&pickle, 0); |
| 134 | IPC::WriteParam(&pickle, 1); |
| 135 | IPC::WriteParam(&pickle, 2); |
| 136 | |
| 137 | base::PickleIterator iter(pickle); |
| 138 | std::vector<int> vec; |
| 139 | EXPECT_FALSE(IPC::ReadParam(&pickle, &iter, &vec)); |
| 140 | } |
| 141 | |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 142 | class SimpleListener : public IPC::Listener { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | public: |
| 144 | SimpleListener() : other_(NULL) { |
| 145 | } |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 146 | void Init(IPC::Sender* s) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | other_ = s; |
| 148 | } |
| 149 | protected: |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 150 | IPC::Sender* other_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | enum { |
| 154 | FUZZER_ROUTING_ID = 5 |
| 155 | }; |
| 156 | |
| 157 | // The fuzzer server class. It runs in a child process and expects |
| 158 | // only two IPC calls; after that it exits the message loop which |
| 159 | // terminates the child process. |
| 160 | class FuzzerServerListener : public SimpleListener { |
| 161 | public: |
| 162 | FuzzerServerListener() : message_count_(2), pending_messages_(0) { |
| 163 | } |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 164 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 166 | ++pending_messages_; |
| 167 | IPC_BEGIN_MESSAGE_MAP(FuzzerServerListener, msg) |
| 168 | IPC_MESSAGE_HANDLER(MsgClassIS, OnMsgClassISMessage) |
| 169 | IPC_MESSAGE_HANDLER(MsgClassSI, OnMsgClassSIMessage) |
| 170 | IPC_END_MESSAGE_MAP() |
| 171 | if (pending_messages_) { |
| 172 | // Probably a problem de-serializing the message. |
| 173 | ReplyMsgNotHandled(msg.type()); |
| 174 | } |
| 175 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 176 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private: |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 180 | void OnMsgClassISMessage(int value, const base::string16& text) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | UseData(MsgClassIS::ID, value, text); |
| 182 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassIS::ID, value); |
| 183 | Cleanup(); |
| 184 | } |
| 185 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 186 | void OnMsgClassSIMessage(const base::string16& text, int value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | UseData(MsgClassSI::ID, value, text); |
| 188 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgClassSI::ID, value); |
| 189 | Cleanup(); |
| 190 | } |
| 191 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 192 | bool RoundtripAckReply(int routing, uint32_t type_id, int reply) { |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 193 | IPC::Message* message = new IPC::Message(routing, type_id, |
| 194 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | message->WriteInt(reply + 1); |
| 196 | message->WriteInt(reply); |
| 197 | return other_->Send(message); |
| 198 | } |
| 199 | |
| 200 | void Cleanup() { |
| 201 | --message_count_; |
| 202 | --pending_messages_; |
| 203 | if (0 == message_count_) |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 204 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | } |
| 206 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 207 | void ReplyMsgNotHandled(uint32_t type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 208 | RoundtripAckReply(FUZZER_ROUTING_ID, MsgUnhandled::ID, type_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | Cleanup(); |
| 210 | } |
| 211 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 212 | void UseData(int caller, int value, const base::string16& text) { |
| 213 | std::ostringstream os; |
| 214 | os << "IPC fuzzer:" << caller << " [" << value << " " |
| 215 | << base::UTF16ToUTF8(text) << "]\n"; |
| 216 | std::string output = os.str(); |
| 217 | LOG(WARNING) << output; |
| 218 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | |
| 220 | int message_count_; |
| 221 | int pending_messages_; |
| 222 | }; |
| 223 | |
| 224 | class FuzzerClientListener : public SimpleListener { |
| 225 | public: |
| 226 | FuzzerClientListener() : last_msg_(NULL) { |
| 227 | } |
| 228 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 229 | bool OnMessageReceived(const IPC::Message& msg) override { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | last_msg_ = new IPC::Message(msg); |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 231 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 232 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | } |
| 234 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 235 | bool ExpectMessage(int value, uint32_t type_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | if (!MsgHandlerInternal(type_id)) |
| 237 | return false; |
| 238 | int msg_value1 = 0; |
| 239 | int msg_value2 = 0; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 240 | base::PickleIterator iter(*last_msg_); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 241 | if (!iter.ReadInt(&msg_value1)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | return false; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 243 | if (!iter.ReadInt(&msg_value2)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | return false; |
| 245 | if ((msg_value2 + 1) != msg_value1) |
| 246 | return false; |
| 247 | if (msg_value2 != value) |
| 248 | return false; |
| 249 | |
| 250 | delete last_msg_; |
| 251 | last_msg_ = NULL; |
| 252 | return true; |
| 253 | } |
| 254 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 255 | bool ExpectMsgNotHandled(uint32_t type_id) { |
[email protected] | 1d4ecf4 | 2011-08-26 21:27:30 | [diff] [blame] | 256 | return ExpectMessage(type_id, MsgUnhandled::ID); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private: |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 260 | bool MsgHandlerInternal(uint32_t type_id) { |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 261 | base::RunLoop().Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | if (NULL == last_msg_) |
| 263 | return false; |
| 264 | if (FUZZER_ROUTING_ID != last_msg_->routing_id()) |
| 265 | return false; |
| 266 | return (type_id == last_msg_->type()); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 267 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | |
| 269 | IPC::Message* last_msg_; |
| 270 | }; |
| 271 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 272 | // Runs the fuzzing server child mode. Returns when the preset number of |
| 273 | // messages have been received. |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 274 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(FuzzServerClient) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | FuzzerServerListener listener; |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 276 | Connect(&listener); |
| 277 | listener.Init(channel()); |
fdoray | 8e3258685 | 2016-06-22 19:56:16 | [diff] [blame] | 278 | base::RunLoop().Run(); |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 279 | Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 280 | } |
| 281 | |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 282 | using IPCFuzzingTest = IPCChannelMojoTestBase; |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 283 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | // This test makes sure that the FuzzerClientListener and FuzzerServerListener |
| 285 | // are working properly by generating two well formed IPC calls. |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 286 | TEST_F(IPCFuzzingTest, SanityTest) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 287 | Init("FuzzServerClient"); |
| 288 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 289 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 290 | CreateChannel(&listener); |
| 291 | listener.Init(channel()); |
| 292 | ASSERT_TRUE(ConnectChannel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | |
| 294 | IPC::Message* msg = NULL; |
| 295 | int value = 43; |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 296 | msg = new MsgClassIS(value, base::ASCIIToUTF16("expect 43")); |
[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, MsgClassIS::ID)); |
| 299 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 300 | msg = new MsgClassSI(base::ASCIIToUTF16("expect 44"), ++value); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 301 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 302 | EXPECT_TRUE(listener.ExpectMessage(value, MsgClassSI::ID)); |
| 303 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 304 | EXPECT_TRUE(WaitForClientShutdown()); |
| 305 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | } |
| 307 | |
[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 |
sammc | 4bcc4ed6 | 2016-10-27 10:13:59 | [diff] [blame] | 309 | // error while unpacking the IPC buffer. Right after we generate another valid |
| 310 | // IPC to make sure framing is working properly. |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 311 | TEST_F(IPCFuzzingTest, 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()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 319 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassIS::ID, |
| 320 | IPC::Message::PRIORITY_NORMAL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | msg->WriteInt(666); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 322 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | EXPECT_TRUE(listener.ExpectMsgNotHandled(MsgClassIS::ID)); |
| 324 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 325 | msg = new MsgClassSI(base::ASCIIToUTF16("expect one"), 1); |
[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.ExpectMessage(1, MsgClassSI::ID)); |
| 328 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 329 | EXPECT_TRUE(WaitForClientShutdown()); |
| 330 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 333 | // This test uses a payload that has too many arguments, but so the payload size |
| 334 | // is big enough so the unpacking routine does not generate an error as in the |
| 335 | // case of MsgBadPayloadShort test. This test does not pinpoint a flaw (per se) |
| 336 | // as by design we don't carry type information on the IPC message. |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 337 | TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) { |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 338 | Init("FuzzServerClient"); |
| 339 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 340 | FuzzerClientListener listener; |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 341 | CreateChannel(&listener); |
| 342 | listener.Init(channel()); |
| 343 | ASSERT_TRUE(ConnectChannel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 345 | IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID, |
| 346 | IPC::Message::PRIORITY_NORMAL); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 347 | msg->WriteString16(base::ASCIIToUTF16("d")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | msg->WriteInt(0); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 349 | msg->WriteInt(0x65); // Extra argument. |
| 350 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 351 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 352 | EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID)); |
| 353 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 354 | // Now send a well formed message to make sure the receiver wasn't |
| 355 | // thrown out of sync by the extra argument. |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame] | 356 | msg = new MsgClassIS(3, base::ASCIIToUTF16("expect three")); |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 357 | sender()->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID)); |
| 359 | |
[email protected] | 3c78858 | 2013-01-25 21:51:35 | [diff] [blame] | 360 | EXPECT_TRUE(WaitForClientShutdown()); |
| 361 | DestroyChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 364 | } // namespace |