blob: b69da7e6bed8764f67915b02ed309c7b00224562 [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"
Alexandr Ilind497eee2018-04-19 22:50:5414#include "base/test/test_shared_memory_util.h"
tguilbert4a5ac602016-09-19 21:11:2515#include "base/unguessable_token.h"
Robert Sesek02910662020-02-28 20:15:5116#include "build/build_config.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
Xiaohan Wangab909b32022-01-12 17:57:3921#if BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:5122#include <windows.h>
23#endif
24
[email protected]14062da2013-02-15 20:05:1725namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2226namespace {
[email protected]34d48612012-06-29 00:05:0427
28// Tests nesting of messages as parameters to other messages.
29TEST(IPCMessageUtilsTest, NestedMessages) {
tfarina10a5c062015-09-04 18:47:5730 int32_t nested_routing = 12;
31 uint32_t nested_type = 78;
[email protected]34d48612012-06-29 00:05:0432 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1233 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
34 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0435 nested_msg.set_sync();
36 ParamTraits<int>::Write(&nested_msg, nested_content);
37
38 // Outer message contains the nested one as its parameter.
tfarina10a5c062015-09-04 18:47:5739 int32_t outer_routing = 91;
40 uint32_t outer_type = 88;
[email protected]753bb252013-11-04 22:28:1241 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
42 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0443 ParamTraits<Message>::Write(&outer_msg, nested_msg);
44
45 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2546 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0447 IPC::Message result_msg;
48 ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg));
49
50 // Verify nested message headers.
51 EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id());
52 EXPECT_EQ(nested_msg.type(), result_msg.type());
[email protected]753bb252013-11-04 22:28:1253 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0454 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
55
56 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2557 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0458 int result_content = 0;
59 ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
60 &result_content));
61 EXPECT_EQ(nested_content, result_content);
62
63 // Try reading past the ends for both messages and make sure it fails.
64 IPC::Message dummy;
65 ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy));
66 ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
67 &result_content));
68}
69
[email protected]10cbaf02013-01-03 04:11:5470// Tests that detection of various bad parameters is working correctly.
71TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3072 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
73 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5574
75 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5476 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3077 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
78 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5479
brettwbd4d7112015-06-03 04:29:2580 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3081 base::FilePath ok_path;
82 base::FilePath bad_path;
83 ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path));
84 ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path));
[email protected]10cbaf02013-01-03 04:11:5485}
86
miletus1edf97f92015-07-23 19:42:3687
88TEST(IPCMessageUtilsTest, StackVector) {
89 static const size_t stack_capacity = 5;
90 base::StackVector<double, stack_capacity> stack_vector;
91 for (size_t i = 0; i < 2 * stack_capacity; i++)
92 stack_vector->push_back(i * 2.0);
93
94 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
95 IPC::WriteParam(&msg, stack_vector);
96
97 base::StackVector<double, stack_capacity> output;
98 base::PickleIterator iter(msg);
99 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
100 for (size_t i = 0; i < 2 * stack_capacity; i++)
101 EXPECT_EQ(stack_vector[i], output[i]);
102}
103
amistry36182522016-06-27 06:34:42104TEST(IPCMessageUtilsTest, MojoChannelHandle) {
105 mojo::MessagePipe message_pipe;
106 IPC::ChannelHandle channel_handle(message_pipe.handle0.release());
107
108 IPC::Message message;
109 IPC::WriteParam(&message, channel_handle);
110
amistry36182522016-06-27 06:34:42111 base::PickleIterator iter(message);
112 IPC::ChannelHandle result_handle;
113 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle));
amistry36182522016-06-27 06:34:42114 EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle);
115}
116
bmcquade18573e12016-07-01 13:13:50117TEST(IPCMessageUtilsTest, OptionalUnset) {
Anton Bikineev1f42a452021-05-15 18:02:50118 absl::optional<int> opt;
bmcquade18573e12016-07-01 13:13:50119 base::Pickle pickle;
120 IPC::WriteParam(&pickle, opt);
121
bmcquade18573e12016-07-01 13:13:50122 std::string log;
123 IPC::LogParam(opt, &log);
124 EXPECT_EQ("(unset)", log);
125
Anton Bikineev1f42a452021-05-15 18:02:50126 absl::optional<int> unserialized_opt;
bmcquade18573e12016-07-01 13:13:50127 base::PickleIterator iter(pickle);
128 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
129 EXPECT_FALSE(unserialized_opt);
130}
131
132TEST(IPCMessageUtilsTest, OptionalSet) {
Anton Bikineev1f42a452021-05-15 18:02:50133 absl::optional<int> opt(10);
bmcquade18573e12016-07-01 13:13:50134 base::Pickle pickle;
135 IPC::WriteParam(&pickle, opt);
136
bmcquade18573e12016-07-01 13:13:50137 std::string log;
138 IPC::LogParam(opt, &log);
139 EXPECT_EQ("10", log);
140
Anton Bikineev1f42a452021-05-15 18:02:50141 absl::optional<int> unserialized_opt;
bmcquade18573e12016-07-01 13:13:50142 base::PickleIterator iter(pickle);
143 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
144 EXPECT_TRUE(unserialized_opt);
145 EXPECT_EQ(opt.value(), unserialized_opt.value());
146}
147
Alexandr Ilind497eee2018-04-19 22:50:54148template <typename SharedMemoryRegionType>
149class SharedMemoryRegionTypedTest : public ::testing::Test {};
150
151typedef ::testing::Types<base::WritableSharedMemoryRegion,
152 base::UnsafeSharedMemoryRegion,
153 base::ReadOnlySharedMemoryRegion>
154 AllSharedMemoryRegionTypes;
Victor Costanebc52732019-02-15 02:39:47155TYPED_TEST_SUITE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes);
Alexandr Ilind497eee2018-04-19 22:50:54156
157TYPED_TEST(SharedMemoryRegionTypedTest, WriteAndRead) {
158 const size_t size = 2314;
159 TypeParam pre_pickle;
160 base::WritableSharedMemoryMapping pre_mapping;
161 std::tie(pre_pickle, pre_mapping) = base::CreateMappedRegion<TypeParam>(size);
162 const size_t pre_size = pre_pickle.GetSize();
163
164 const std::string content = "Hello, world!";
165 memcpy(pre_mapping.memory(), content.data(), content.size());
166
167 IPC::Message message;
168 IPC::WriteParam(&message, pre_pickle);
169 EXPECT_FALSE(pre_pickle.IsValid());
170
171 TypeParam post_pickle;
172 base::PickleIterator iter(message);
173 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
174 EXPECT_EQ(pre_size, post_pickle.GetSize());
175 typename TypeParam::MappingType post_mapping = post_pickle.Map();
176 EXPECT_EQ(pre_mapping.guid(), post_mapping.guid());
177 EXPECT_EQ(0, memcmp(pre_mapping.memory(), post_mapping.memory(),
178 post_pickle.GetSize()));
179}
180
181TYPED_TEST(SharedMemoryRegionTypedTest, InvalidRegion) {
182 TypeParam pre_pickle;
183 EXPECT_FALSE(pre_pickle.IsValid());
184
185 IPC::Message message;
186 IPC::WriteParam(&message, pre_pickle);
187
188 TypeParam post_pickle;
189 base::PickleIterator iter(message);
190 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
191 EXPECT_FALSE(post_pickle.IsValid());
192}
193
tguilbert4a5ac602016-09-19 21:11:25194TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
195 base::UnguessableToken token = base::UnguessableToken::Create();
196 base::Pickle pickle;
197 IPC::WriteParam(&pickle, token);
198
tguilbert4a5ac602016-09-19 21:11:25199 std::string log;
200 IPC::LogParam(token, &log);
201 EXPECT_EQ(token.ToString(), log);
202
203 base::UnguessableToken deserialized_token;
204 base::PickleIterator iter(pickle);
205 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token));
206 EXPECT_EQ(token, deserialized_token);
207}
208
brettwca2ec7632017-04-20 06:10:20209TEST(IPCMessageUtilsTest, FlatMap) {
210 base::flat_map<std::string, int> input;
211 input["foo"] = 42;
212 input["bar"] = 96;
213
214 base::Pickle pickle;
215 IPC::WriteParam(&pickle, input);
216
brettwca2ec7632017-04-20 06:10:20217 base::PickleIterator iter(pickle);
218 base::flat_map<std::string, int> output;
219 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
220
221 EXPECT_EQ(input, output);
222}
223
Istiaque Ahmed4832b03b2020-01-06 23:58:18224TEST(IPCMessageUtilsTest, StrongAlias) {
Peter Kasting796cde22020-11-18 21:06:53225 using TestType = base::StrongAlias<class Tag, int>;
Istiaque Ahmed4832b03b2020-01-06 23:58:18226 TestType input(42);
227
228 base::Pickle pickle;
229 IPC::WriteParam(&pickle, input);
230
231 base::PickleIterator iter(pickle);
232 TestType output;
233 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
234
235 EXPECT_EQ(input, output);
236}
237
Xiaohan Wangab909b32022-01-12 17:57:39238#if BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51239TEST(IPCMessageUtilsTest, ScopedHandle) {
240 HANDLE raw_dupe_handle;
241 ASSERT_TRUE(::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentProcess(),
242 ::GetCurrentProcess(), &raw_dupe_handle, 0,
243 FALSE, DUPLICATE_SAME_ACCESS));
244 base::win::ScopedHandle dupe_handle(raw_dupe_handle);
245
246 Message message(0, 0, Message::PRIORITY_LOW);
247 WriteParam(&message, dupe_handle);
248
249 base::PickleIterator iter(message);
250 base::win::ScopedHandle read_handle;
251 EXPECT_TRUE(ReadParam(&message, &iter, &read_handle));
252 EXPECT_TRUE(read_handle.IsValid());
253}
Xiaohan Wangab909b32022-01-12 17:57:39254#endif // BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51255
[email protected]2a3aa7b52013-01-11 20:56:22256} // namespace
[email protected]14062da2013-02-15 20:05:17257} // namespace IPC