blob: 5d9da31715a36115e5f1ea055c583bc05f97400c [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]946d1b22009-07-22 23:57:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]22b42c52010-12-20 06:59:235#include "ipc/ipc_message.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
8#include <stdint.h>
[email protected]946d1b22009-07-22 23:57:219#include <string.h>
10
dskiba6f3790a2015-09-30 17:24:3011#include <limits>
danakj03de39b22016-04-23 04:21:0912#include <memory>
jdoerriecb205a52017-06-08 16:16:4413#include <utility>
dskiba6f3790a2015-09-30 17:24:3014
jdoerriee067999a2017-04-07 06:39:0015#include "base/memory/ptr_util.h"
tfarina477fc5da72015-07-27 17:30:1816#include "base/strings/utf_string_conversions.h"
[email protected]946d1b22009-07-22 23:57:2117#include "base/values.h"
avi246998d82015-12-22 02:39:0418#include "build/build_config.h"
[email protected]946d1b22009-07-22 23:57:2119#include "ipc/ipc_message_utils.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]5636d902014-05-13 23:19:1022// 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
29IPC_MESSAGE_CONTROL0(TestMsgClassEmpty)
30
31IPC_MESSAGE_CONTROL1(TestMsgClassI, int)
32
33IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string)
34
dskiba6f3790a2015-09-30 17:24:3035namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2236
tfarina477fc5da72015-07-27 17:30:1837TEST(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 Cheng0d89f9222017-09-22 05:05:0743 m.WriteInt(v1);
44 m.WriteString(v2);
45 m.WriteString16(v3);
tfarina477fc5da72015-07-27 17:30:1846
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]946d1b22009-07-22 23:57:2168TEST(IPCMessageTest, ListValue) {
[email protected]ea5ef4c2013-06-13 22:50:2769 base::ListValue input;
jdoerrief1e72e32017-04-26 16:23:5570 input.AppendDouble(42.42);
71 input.AppendString("forty");
Jeremy Roman160eb922017-08-29 17:43:4372 input.Append(std::make_unique<base::Value>());
[email protected]946d1b22009-07-22 23:57:2173
[email protected]753bb252013-11-04 22:28:1274 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2175 IPC::WriteParam(&msg, input);
76
[email protected]ea5ef4c2013-06-13 22:50:2777 base::ListValue output;
brettwbd4d7112015-06-03 04:29:2578 base::PickleIterator iter(msg);
[email protected]946d1b22009-07-22 23:57:2179 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
80
81 EXPECT_TRUE(input.Equals(&output));
82
83 // Also test the corrupt case.
[email protected]753bb252013-11-04 22:28:1284 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2185 bad_msg.WriteInt(99);
brettwbd4d7112015-06-03 04:29:2586 iter = base::PickleIterator(bad_msg);
[email protected]946d1b22009-07-22 23:57:2187 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
88}
89
90TEST(IPCMessageTest, DictionaryValue) {
[email protected]ea5ef4c2013-06-13 22:50:2791 base::DictionaryValue input;
Jeremy Roman160eb922017-08-29 17:43:4392 input.Set("null", std::make_unique<base::Value>());
jdoerriecb205a52017-06-08 16:16:4493 input.SetBoolean("bool", true);
94 input.SetInteger("int", 42);
jdoerrie19cdc032017-08-05 02:21:5595 input.SetKey("int.with.dot", base::Value(43));
[email protected]946d1b22009-07-22 23:57:2196
Jeremy Roman160eb922017-08-29 17:43:4397 auto subdict = std::make_unique<base::DictionaryValue>();
jdoerriecb205a52017-06-08 16:16:4498 subdict->SetString("str", "forty two");
99 subdict->SetBoolean("bool", false);
[email protected]946d1b22009-07-22 23:57:21100
Jeremy Roman160eb922017-08-29 17:43:43101 auto sublist = std::make_unique<base::ListValue>();
jdoerrief1e72e32017-04-26 16:23:55102 sublist->AppendDouble(42.42);
103 sublist->AppendString("forty");
104 sublist->AppendString("two");
jdoerriecb205a52017-06-08 16:16:44105 subdict->Set("list", std::move(sublist));
[email protected]946d1b22009-07-22 23:57:21106
jdoerriecb205a52017-06-08 16:16:44107 input.Set("dict", std::move(subdict));
[email protected]946d1b22009-07-22 23:57:21108
[email protected]753bb252013-11-04 22:28:12109 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:21110 IPC::WriteParam(&msg, input);
111
[email protected]ea5ef4c2013-06-13 22:50:27112 base::DictionaryValue output;
brettwbd4d7112015-06-03 04:29:25113 base::PickleIterator iter(msg);
[email protected]946d1b22009-07-22 23:57:21114 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
115
116 EXPECT_TRUE(input.Equals(&output));
117
118 // Also test the corrupt case.
[email protected]753bb252013-11-04 22:28:12119 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:21120 bad_msg.WriteInt(99);
brettwbd4d7112015-06-03 04:29:25121 iter = base::PickleIterator(bad_msg);
[email protected]946d1b22009-07-22 23:57:21122 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
123}
[email protected]2a3aa7b52013-01-11 20:56:22124
dskiba6f3790a2015-09-30 17:24:30125TEST(IPCMessageTest, FindNext) {
126 IPC::Message message;
Daniel Cheng0d89f9222017-09-22 05:05:07127 message.WriteString("Goooooooogle");
128 message.WriteInt(111);
dskiba6f3790a2015-09-30 17:24:30129
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);
dskiba6f3790a2015-09-30 17:24:30156 EXPECT_EQ(next.message_size, message.size());
dskiba6f3790a2015-09-30 17:24:30157
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
174TEST(IPCMessageTest, FindNextOverflow) {
175 IPC::Message message;
Daniel Cheng0d89f9222017-09-22 05:05:07176 message.WriteString("Data");
177 message.WriteInt(777);
dskiba6f3790a2015-09-30 17:24:30178
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);
dskiba6f3790a2015-09-30 17:24:30188 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 }
dskiba6f3790a2015-09-30 17:24:30196
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);
dskiba6f3790a2015-09-30 17:24:30202 EXPECT_EQ(next.message_size,
203 message.header()->payload_size + sizeof(IPC::Message::Header));
dskiba6f3790a2015-09-30 17:24:30204}
205
206namespace {
207
[email protected]5636d902014-05-13 23:19:10208class IPCMessageParameterTest : public testing::Test {
209 public:
210 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {}
211
212 bool OnMessageReceived(const IPC::Message& message) {
[email protected]5636d902014-05-13 23:19:10213 bool handled = true;
214 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message,
[email protected]e44d1342014-05-16 21:29:33215 &extra_param_)
[email protected]5636d902014-05-13 23:19:10216 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
dskiba6f3790a2015-09-30 17:24:30253} // namespace
254
[email protected]5636d902014-05-13 23:19:10255TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) {
256 TestMsgClassEmpty message;
257 EXPECT_TRUE(OnMessageReceived(message));
258 EXPECT_TRUE(called_);
259}
260
tfarina8514f0d2015-07-28 14:41:47261#if defined(OS_ANDROID)
262#define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam
263#else
264#define MAYBE_OneIntegerWithParam OneIntegerWithParam
265#endif
266TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) {
[email protected]5636d902014-05-13 23:19:10267 TestMsgClassI message(42);
268 EXPECT_TRUE(OnMessageReceived(message));
269 EXPECT_TRUE(called_);
270}
271
272/* TODO: handle sync IPCs
273TEST_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
dskiba6f3790a2015-09-30 17:24:30281} // namespace IPC