blob: 2b1c2c285afa3db3c6549aecf714bd1623d2ffe1 [file] [log] [blame]
[email protected]34d48612012-06-29 00:05:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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]14062da2013-02-15 20:05:175#include "ipc/ipc_message_utils.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
danakj03de39b22016-04-23 04:21:099#include <memory>
tfarina10a5c062015-09-04 18:47:5710
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
amistryc7a7a762016-04-08 04:21:5412#include "base/json/json_reader.h"
jdoerriee067999a2017-04-07 06:39:0013#include "base/memory/ptr_util.h"
erikchen9d6afd712017-05-18 17:49:0614#include "base/memory/shared_memory.h"
Alexandr Ilind497eee2018-04-19 22:50:5415#include "base/test/test_shared_memory_util.h"
tguilbert4a5ac602016-09-19 21:11:2516#include "base/unguessable_token.h"
amistry36182522016-06-27 06:34:4217#include "ipc/ipc_channel_handle.h"
[email protected]34d48612012-06-29 00:05:0418#include "ipc/ipc_message.h"
[email protected]34d48612012-06-29 00:05:0419#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]14062da2013-02-15 20:05:1721namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2222namespace {
[email protected]34d48612012-06-29 00:05:0423
24// Tests nesting of messages as parameters to other messages.
25TEST(IPCMessageUtilsTest, NestedMessages) {
tfarina10a5c062015-09-04 18:47:5726 int32_t nested_routing = 12;
27 uint32_t nested_type = 78;
[email protected]34d48612012-06-29 00:05:0428 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1229 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
30 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0431 nested_msg.set_sync();
32 ParamTraits<int>::Write(&nested_msg, nested_content);
33
34 // Outer message contains the nested one as its parameter.
tfarina10a5c062015-09-04 18:47:5735 int32_t outer_routing = 91;
36 uint32_t outer_type = 88;
[email protected]753bb252013-11-04 22:28:1237 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
38 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0439 ParamTraits<Message>::Write(&outer_msg, nested_msg);
40
41 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2542 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0443 IPC::Message result_msg;
44 ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg));
45
46 // Verify nested message headers.
47 EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id());
48 EXPECT_EQ(nested_msg.type(), result_msg.type());
[email protected]753bb252013-11-04 22:28:1249 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0450 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
51
52 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2553 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0454 int result_content = 0;
55 ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
56 &result_content));
57 EXPECT_EQ(nested_content, result_content);
58
59 // Try reading past the ends for both messages and make sure it fails.
60 IPC::Message dummy;
61 ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy));
62 ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
63 &result_content));
64}
65
[email protected]10cbaf02013-01-03 04:11:5466// Tests that detection of various bad parameters is working correctly.
67TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3068 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
69 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5570
71 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5472 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3073 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
74 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5475
brettwbd4d7112015-06-03 04:29:2576 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3077 base::FilePath ok_path;
78 base::FilePath bad_path;
79 ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path));
80 ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path));
[email protected]10cbaf02013-01-03 04:11:5481}
82
miletus1edf97f92015-07-23 19:42:3683
84TEST(IPCMessageUtilsTest, StackVector) {
85 static const size_t stack_capacity = 5;
86 base::StackVector<double, stack_capacity> stack_vector;
87 for (size_t i = 0; i < 2 * stack_capacity; i++)
88 stack_vector->push_back(i * 2.0);
89
90 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
91 IPC::WriteParam(&msg, stack_vector);
92
93 base::StackVector<double, stack_capacity> output;
94 base::PickleIterator iter(msg);
95 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
96 for (size_t i = 0; i < 2 * stack_capacity; i++)
97 EXPECT_EQ(stack_vector[i], output[i]);
98}
99
amistry36182522016-06-27 06:34:42100TEST(IPCMessageUtilsTest, MojoChannelHandle) {
101 mojo::MessagePipe message_pipe;
102 IPC::ChannelHandle channel_handle(message_pipe.handle0.release());
103
104 IPC::Message message;
105 IPC::WriteParam(&message, channel_handle);
106
amistry36182522016-06-27 06:34:42107 base::PickleIterator iter(message);
108 IPC::ChannelHandle result_handle;
109 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle));
amistry36182522016-06-27 06:34:42110 EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle);
111}
112
bmcquade18573e12016-07-01 13:13:50113TEST(IPCMessageUtilsTest, OptionalUnset) {
114 base::Optional<int> opt;
115 base::Pickle pickle;
116 IPC::WriteParam(&pickle, opt);
117
bmcquade18573e12016-07-01 13:13:50118 std::string log;
119 IPC::LogParam(opt, &log);
120 EXPECT_EQ("(unset)", log);
121
122 base::Optional<int> unserialized_opt;
123 base::PickleIterator iter(pickle);
124 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
125 EXPECT_FALSE(unserialized_opt);
126}
127
128TEST(IPCMessageUtilsTest, OptionalSet) {
129 base::Optional<int> opt(10);
130 base::Pickle pickle;
131 IPC::WriteParam(&pickle, opt);
132
bmcquade18573e12016-07-01 13:13:50133 std::string log;
134 IPC::LogParam(opt, &log);
135 EXPECT_EQ("10", log);
136
137 base::Optional<int> unserialized_opt;
138 base::PickleIterator iter(pickle);
139 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
140 EXPECT_TRUE(unserialized_opt);
141 EXPECT_EQ(opt.value(), unserialized_opt.value());
142}
143
erikchen9d6afd712017-05-18 17:49:06144TEST(IPCMessageUtilsTest, SharedMemoryHandle) {
145 base::SharedMemoryCreateOptions options;
146 options.size = 1004;
147 base::SharedMemory shmem;
148 ASSERT_TRUE(shmem.Create(options));
149
150 base::SharedMemoryHandle pre_pickle = shmem.handle().Duplicate();
151 ASSERT_TRUE(pre_pickle.IsValid());
152
153 IPC::Message message;
154 IPC::WriteParam(&message, pre_pickle);
155
156 base::SharedMemoryHandle post_pickle;
157 base::PickleIterator iter(message);
158 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
159 EXPECT_EQ(pre_pickle.GetGUID(), post_pickle.GetGUID());
160 EXPECT_EQ(pre_pickle.GetSize(), post_pickle.GetSize());
161}
162
Alexandr Ilind497eee2018-04-19 22:50:54163template <typename SharedMemoryRegionType>
164class SharedMemoryRegionTypedTest : public ::testing::Test {};
165
166typedef ::testing::Types<base::WritableSharedMemoryRegion,
167 base::UnsafeSharedMemoryRegion,
168 base::ReadOnlySharedMemoryRegion>
169 AllSharedMemoryRegionTypes;
170TYPED_TEST_CASE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes);
171
172TYPED_TEST(SharedMemoryRegionTypedTest, WriteAndRead) {
173 const size_t size = 2314;
174 TypeParam pre_pickle;
175 base::WritableSharedMemoryMapping pre_mapping;
176 std::tie(pre_pickle, pre_mapping) = base::CreateMappedRegion<TypeParam>(size);
177 const size_t pre_size = pre_pickle.GetSize();
178
179 const std::string content = "Hello, world!";
180 memcpy(pre_mapping.memory(), content.data(), content.size());
181
182 IPC::Message message;
183 IPC::WriteParam(&message, pre_pickle);
184 EXPECT_FALSE(pre_pickle.IsValid());
185
186 TypeParam post_pickle;
187 base::PickleIterator iter(message);
188 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
189 EXPECT_EQ(pre_size, post_pickle.GetSize());
190 typename TypeParam::MappingType post_mapping = post_pickle.Map();
191 EXPECT_EQ(pre_mapping.guid(), post_mapping.guid());
192 EXPECT_EQ(0, memcmp(pre_mapping.memory(), post_mapping.memory(),
193 post_pickle.GetSize()));
194}
195
196TYPED_TEST(SharedMemoryRegionTypedTest, InvalidRegion) {
197 TypeParam pre_pickle;
198 EXPECT_FALSE(pre_pickle.IsValid());
199
200 IPC::Message message;
201 IPC::WriteParam(&message, pre_pickle);
202
203 TypeParam post_pickle;
204 base::PickleIterator iter(message);
205 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
206 EXPECT_FALSE(post_pickle.IsValid());
207}
208
tguilbert4a5ac602016-09-19 21:11:25209TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
210 base::UnguessableToken token = base::UnguessableToken::Create();
211 base::Pickle pickle;
212 IPC::WriteParam(&pickle, token);
213
tguilbert4a5ac602016-09-19 21:11:25214 std::string log;
215 IPC::LogParam(token, &log);
216 EXPECT_EQ(token.ToString(), log);
217
218 base::UnguessableToken deserialized_token;
219 base::PickleIterator iter(pickle);
220 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token));
221 EXPECT_EQ(token, deserialized_token);
222}
223
brettwca2ec7632017-04-20 06:10:20224TEST(IPCMessageUtilsTest, FlatMap) {
225 base::flat_map<std::string, int> input;
226 input["foo"] = 42;
227 input["bar"] = 96;
228
229 base::Pickle pickle;
230 IPC::WriteParam(&pickle, input);
231
brettwca2ec7632017-04-20 06:10:20232 base::PickleIterator iter(pickle);
233 base::flat_map<std::string, int> output;
234 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
235
236 EXPECT_EQ(input, output);
237}
238
[email protected]2a3aa7b52013-01-11 20:56:22239} // namespace
[email protected]14062da2013-02-15 20:05:17240} // namespace IPC