[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 5 | #include "ipc/ipc_message.h" |
| 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 11 | #include <limits> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 12 | #include <memory> |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 13 | #include <utility> |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 14 | |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
tfarina | 477fc5da7 | 2015-07-27 17:30:18 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 17 | #include "base/values.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 18 | #include "build/build_config.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 19 | #include "ipc/ipc_message_utils.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 22 | // IPC messages for testing ---------------------------------------------------- |
| 23 | |
| 24 | #define IPC_MESSAGE_IMPL |
| 25 | #include "ipc/ipc_message_macros.h" |
| 26 | |
| 27 | #define IPC_MESSAGE_START TestMsgStart |
| 28 | |
| 29 | IPC_MESSAGE_CONTROL0(TestMsgClassEmpty) |
| 30 | |
| 31 | IPC_MESSAGE_CONTROL1(TestMsgClassI, int) |
| 32 | |
| 33 | IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string) |
| 34 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 35 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 36 | |
tfarina | 477fc5da7 | 2015-07-27 17:30:18 | [diff] [blame] | 37 | TEST(IPCMessageTest, BasicMessageTest) { |
| 38 | int v1 = 10; |
| 39 | std::string v2("foobar"); |
| 40 | base::string16 v3(base::ASCIIToUTF16("hello world")); |
| 41 | |
| 42 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 43 | m.WriteInt(v1); |
| 44 | m.WriteString(v2); |
| 45 | m.WriteString16(v3); |
tfarina | 477fc5da7 | 2015-07-27 17:30:18 | [diff] [blame] | 46 | |
| 47 | base::PickleIterator iter(m); |
| 48 | |
| 49 | int vi; |
| 50 | std::string vs; |
| 51 | base::string16 vs16; |
| 52 | |
| 53 | EXPECT_TRUE(iter.ReadInt(&vi)); |
| 54 | EXPECT_EQ(v1, vi); |
| 55 | |
| 56 | EXPECT_TRUE(iter.ReadString(&vs)); |
| 57 | EXPECT_EQ(v2, vs); |
| 58 | |
| 59 | EXPECT_TRUE(iter.ReadString16(&vs16)); |
| 60 | EXPECT_EQ(v3, vs16); |
| 61 | |
| 62 | // should fail |
| 63 | EXPECT_FALSE(iter.ReadInt(&vi)); |
| 64 | EXPECT_FALSE(iter.ReadString(&vs)); |
| 65 | EXPECT_FALSE(iter.ReadString16(&vs16)); |
| 66 | } |
| 67 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 68 | TEST(IPCMessageTest, ListValue) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 69 | base::ListValue input; |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 70 | input.AppendDouble(42.42); |
| 71 | input.AppendString("forty"); |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 72 | input.Append(std::make_unique<base::Value>()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 73 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 74 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 75 | IPC::WriteParam(&msg, input); |
| 76 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 77 | base::ListValue output; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 78 | base::PickleIterator iter(msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 79 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 80 | |
| 81 | EXPECT_TRUE(input.Equals(&output)); |
| 82 | |
| 83 | // Also test the corrupt case. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 84 | IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 85 | bad_msg.WriteInt(99); |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 86 | iter = base::PickleIterator(bad_msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 87 | EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 88 | } |
| 89 | |
| 90 | TEST(IPCMessageTest, DictionaryValue) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 91 | base::DictionaryValue input; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 92 | input.Set("null", std::make_unique<base::Value>()); |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 93 | input.SetBoolean("bool", true); |
| 94 | input.SetInteger("int", 42); |
jdoerrie | 19cdc03 | 2017-08-05 02:21:55 | [diff] [blame] | 95 | input.SetKey("int.with.dot", base::Value(43)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 96 | |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 97 | auto subdict = std::make_unique<base::DictionaryValue>(); |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 98 | subdict->SetString("str", "forty two"); |
| 99 | subdict->SetBoolean("bool", false); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 100 | |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 101 | auto sublist = std::make_unique<base::ListValue>(); |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 102 | sublist->AppendDouble(42.42); |
| 103 | sublist->AppendString("forty"); |
| 104 | sublist->AppendString("two"); |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 105 | subdict->Set("list", std::move(sublist)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 106 | |
jdoerrie | cb205a5 | 2017-06-08 16:16:44 | [diff] [blame] | 107 | input.Set("dict", std::move(subdict)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 108 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 109 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 110 | IPC::WriteParam(&msg, input); |
| 111 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 112 | base::DictionaryValue output; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 113 | base::PickleIterator iter(msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 114 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 115 | |
| 116 | EXPECT_TRUE(input.Equals(&output)); |
| 117 | |
| 118 | // Also test the corrupt case. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 119 | IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 120 | bad_msg.WriteInt(99); |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 121 | iter = base::PickleIterator(bad_msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 122 | EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 123 | } |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 124 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 125 | TEST(IPCMessageTest, FindNext) { |
| 126 | IPC::Message message; |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 127 | message.WriteString("Goooooooogle"); |
| 128 | message.WriteInt(111); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 129 | |
| 130 | std::vector<char> message_data(message.size() + 7); |
| 131 | memcpy(message_data.data(), message.data(), message.size()); |
| 132 | |
| 133 | const char* data_start = message_data.data(); |
| 134 | const char* data_end = data_start + message.size(); |
| 135 | |
| 136 | IPC::Message::NextMessageInfo next; |
| 137 | |
| 138 | // Data range contains the entire message plus some extra bytes |
| 139 | IPC::Message::FindNext(data_start, data_end + 1, &next); |
| 140 | EXPECT_TRUE(next.message_found); |
| 141 | EXPECT_EQ(next.message_size, message.size()); |
| 142 | EXPECT_EQ(next.pickle_end, data_end); |
| 143 | EXPECT_EQ(next.message_end, data_end); |
| 144 | |
| 145 | // Data range exactly contains the entire message |
| 146 | IPC::Message::FindNext(data_start, data_end, &next); |
| 147 | EXPECT_TRUE(next.message_found); |
| 148 | EXPECT_EQ(next.message_size, message.size()); |
| 149 | EXPECT_EQ(next.pickle_end, data_end); |
| 150 | EXPECT_EQ(next.message_end, data_end); |
| 151 | |
| 152 | // Data range doesn't contain the entire message |
| 153 | // (but contains the message header) |
| 154 | IPC::Message::FindNext(data_start, data_end - 1, &next); |
| 155 | EXPECT_FALSE(next.message_found); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 156 | EXPECT_EQ(next.message_size, message.size()); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 157 | |
| 158 | // Data range doesn't contain the message header |
| 159 | // (but contains the pickle header) |
| 160 | IPC::Message::FindNext(data_start, |
| 161 | data_start + sizeof(IPC::Message::Header) - 1, |
| 162 | &next); |
| 163 | EXPECT_FALSE(next.message_found); |
| 164 | EXPECT_EQ(next.message_size, 0u); |
| 165 | |
| 166 | // Data range doesn't contain the pickle header |
| 167 | IPC::Message::FindNext(data_start, |
| 168 | data_start + sizeof(base::Pickle::Header) - 1, |
| 169 | &next); |
| 170 | EXPECT_FALSE(next.message_found); |
| 171 | EXPECT_EQ(next.message_size, 0u); |
| 172 | } |
| 173 | |
| 174 | TEST(IPCMessageTest, FindNextOverflow) { |
| 175 | IPC::Message message; |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 176 | message.WriteString("Data"); |
| 177 | message.WriteInt(777); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 178 | |
| 179 | const char* data_start = reinterpret_cast<const char*>(message.data()); |
| 180 | const char* data_end = data_start + message.size(); |
| 181 | |
| 182 | IPC::Message::NextMessageInfo next; |
| 183 | |
| 184 | // Payload size is negative (defeats 'start + size > end' check) |
| 185 | message.header()->payload_size = static_cast<uint32_t>(-1); |
| 186 | IPC::Message::FindNext(data_start, data_end, &next); |
| 187 | EXPECT_FALSE(next.message_found); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 188 | if (sizeof(size_t) > sizeof(uint32_t)) { |
| 189 | // No overflow, just insane message size |
| 190 | EXPECT_EQ(next.message_size, |
| 191 | message.header()->payload_size + sizeof(IPC::Message::Header)); |
| 192 | } else { |
| 193 | // Actual overflow, reported as max size_t |
| 194 | EXPECT_EQ(next.message_size, std::numeric_limits<size_t>::max()); |
| 195 | } |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 196 | |
| 197 | // Payload size is max positive integer (defeats size < 0 check, while |
| 198 | // still potentially causing overflow down the road). |
| 199 | message.header()->payload_size = std::numeric_limits<int32_t>::max(); |
| 200 | IPC::Message::FindNext(data_start, data_end, &next); |
| 201 | EXPECT_FALSE(next.message_found); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 202 | EXPECT_EQ(next.message_size, |
| 203 | message.header()->payload_size + sizeof(IPC::Message::Header)); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | namespace { |
| 207 | |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 208 | class IPCMessageParameterTest : public testing::Test { |
| 209 | public: |
| 210 | IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {} |
| 211 | |
| 212 | bool OnMessageReceived(const IPC::Message& message) { |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 213 | bool handled = true; |
| 214 | IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message, |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 215 | &extra_param_) |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 216 | IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty) |
| 217 | IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt) |
| 218 | //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync) |
| 219 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 220 | IPC_END_MESSAGE_MAP() |
| 221 | |
| 222 | return handled; |
| 223 | } |
| 224 | |
| 225 | void OnEmpty(std::string* extra_param) { |
| 226 | EXPECT_EQ(extra_param, &extra_param_); |
| 227 | called_ = true; |
| 228 | } |
| 229 | |
| 230 | void OnInt(std::string* extra_param, int foo) { |
| 231 | EXPECT_EQ(extra_param, &extra_param_); |
| 232 | EXPECT_EQ(foo, 42); |
| 233 | called_ = true; |
| 234 | } |
| 235 | |
| 236 | /* TODO: handle sync IPCs |
| 237 | void OnSync(std::string* extra_param, int foo, std::string* out) { |
| 238 | EXPECT_EQ(extra_param, &extra_param_); |
| 239 | EXPECT_EQ(foo, 42); |
| 240 | called_ = true; |
| 241 | *out = std::string("out"); |
| 242 | } |
| 243 | |
| 244 | bool Send(IPC::Message* reply) { |
| 245 | delete reply; |
| 246 | return true; |
| 247 | }*/ |
| 248 | |
| 249 | std::string extra_param_; |
| 250 | bool called_; |
| 251 | }; |
| 252 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 253 | } // namespace |
| 254 | |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 255 | TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) { |
| 256 | TestMsgClassEmpty message; |
| 257 | EXPECT_TRUE(OnMessageReceived(message)); |
| 258 | EXPECT_TRUE(called_); |
| 259 | } |
| 260 | |
tfarina | 8514f0d | 2015-07-28 14:41:47 | [diff] [blame] | 261 | #if defined(OS_ANDROID) |
| 262 | #define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam |
| 263 | #else |
| 264 | #define MAYBE_OneIntegerWithParam OneIntegerWithParam |
| 265 | #endif |
| 266 | TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) { |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 267 | TestMsgClassI message(42); |
| 268 | EXPECT_TRUE(OnMessageReceived(message)); |
| 269 | EXPECT_TRUE(called_); |
| 270 | } |
| 271 | |
| 272 | /* TODO: handle sync IPCs |
| 273 | TEST_F(IPCMessageParameterTest, Sync) { |
| 274 | std::string output; |
| 275 | TestMsgClassIS message(42, &output); |
| 276 | EXPECT_TRUE(OnMessageReceived(message)); |
| 277 | EXPECT_TRUE(called_); |
| 278 | EXPECT_EQ(output, std::string("out")); |
| 279 | }*/ |
| 280 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 281 | } // namespace IPC |