blob: 34e279e72d22ea47bf8ea5f56552fa546c2b6e69 [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;
Peter Kasting8db5d142022-01-25 01:46:02159 auto [pre_pickle, pre_mapping] = base::CreateMappedRegion<TypeParam>(size);
Alexandr Ilind497eee2018-04-19 22:50:54160 const size_t pre_size = pre_pickle.GetSize();
161
162 const std::string content = "Hello, world!";
163 memcpy(pre_mapping.memory(), content.data(), content.size());
164
165 IPC::Message message;
166 IPC::WriteParam(&message, pre_pickle);
167 EXPECT_FALSE(pre_pickle.IsValid());
168
169 TypeParam post_pickle;
170 base::PickleIterator iter(message);
171 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
172 EXPECT_EQ(pre_size, post_pickle.GetSize());
173 typename TypeParam::MappingType post_mapping = post_pickle.Map();
174 EXPECT_EQ(pre_mapping.guid(), post_mapping.guid());
175 EXPECT_EQ(0, memcmp(pre_mapping.memory(), post_mapping.memory(),
176 post_pickle.GetSize()));
177}
178
179TYPED_TEST(SharedMemoryRegionTypedTest, InvalidRegion) {
180 TypeParam pre_pickle;
181 EXPECT_FALSE(pre_pickle.IsValid());
182
183 IPC::Message message;
184 IPC::WriteParam(&message, pre_pickle);
185
186 TypeParam post_pickle;
187 base::PickleIterator iter(message);
188 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
189 EXPECT_FALSE(post_pickle.IsValid());
190}
191
tguilbert4a5ac602016-09-19 21:11:25192TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
193 base::UnguessableToken token = base::UnguessableToken::Create();
194 base::Pickle pickle;
195 IPC::WriteParam(&pickle, token);
196
tguilbert4a5ac602016-09-19 21:11:25197 std::string log;
198 IPC::LogParam(token, &log);
199 EXPECT_EQ(token.ToString(), log);
200
201 base::UnguessableToken deserialized_token;
202 base::PickleIterator iter(pickle);
203 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token));
204 EXPECT_EQ(token, deserialized_token);
205}
206
brettwca2ec7632017-04-20 06:10:20207TEST(IPCMessageUtilsTest, FlatMap) {
208 base::flat_map<std::string, int> input;
209 input["foo"] = 42;
210 input["bar"] = 96;
211
212 base::Pickle pickle;
213 IPC::WriteParam(&pickle, input);
214
brettwca2ec7632017-04-20 06:10:20215 base::PickleIterator iter(pickle);
216 base::flat_map<std::string, int> output;
217 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
218
219 EXPECT_EQ(input, output);
220}
221
Istiaque Ahmed4832b03b2020-01-06 23:58:18222TEST(IPCMessageUtilsTest, StrongAlias) {
Peter Kasting796cde22020-11-18 21:06:53223 using TestType = base::StrongAlias<class Tag, int>;
Istiaque Ahmed4832b03b2020-01-06 23:58:18224 TestType input(42);
225
226 base::Pickle pickle;
227 IPC::WriteParam(&pickle, input);
228
229 base::PickleIterator iter(pickle);
230 TestType output;
231 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
232
233 EXPECT_EQ(input, output);
234}
235
Xiaohan Wangab909b32022-01-12 17:57:39236#if BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51237TEST(IPCMessageUtilsTest, ScopedHandle) {
238 HANDLE raw_dupe_handle;
239 ASSERT_TRUE(::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentProcess(),
240 ::GetCurrentProcess(), &raw_dupe_handle, 0,
241 FALSE, DUPLICATE_SAME_ACCESS));
242 base::win::ScopedHandle dupe_handle(raw_dupe_handle);
243
244 Message message(0, 0, Message::PRIORITY_LOW);
245 WriteParam(&message, dupe_handle);
246
247 base::PickleIterator iter(message);
248 base::win::ScopedHandle read_handle;
249 EXPECT_TRUE(ReadParam(&message, &iter, &read_handle));
250 EXPECT_TRUE(read_handle.IsValid());
251}
Xiaohan Wangab909b32022-01-12 17:57:39252#endif // BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51253
[email protected]2a3aa7b52013-01-11 20:56:22254} // namespace
[email protected]14062da2013-02-15 20:05:17255} // namespace IPC