blob: 92f31029276c50ab9b7f87489b7f529ad546d3b9 [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"
amistry36182522016-06-27 06:34:4216#include "ipc/ipc_channel_handle.h"
[email protected]34d48612012-06-29 00:05:0417#include "ipc/ipc_message.h"
[email protected]34d48612012-06-29 00:05:0418#include "testing/gtest/include/gtest/gtest.h"
19
[email protected]14062da2013-02-15 20:05:1720namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2221namespace {
[email protected]34d48612012-06-29 00:05:0422
23// Tests nesting of messages as parameters to other messages.
24TEST(IPCMessageUtilsTest, NestedMessages) {
tfarina10a5c062015-09-04 18:47:5725 int32_t nested_routing = 12;
26 uint32_t nested_type = 78;
[email protected]34d48612012-06-29 00:05:0427 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1228 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
29 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0430 nested_msg.set_sync();
31 ParamTraits<int>::Write(&nested_msg, nested_content);
32
33 // Outer message contains the nested one as its parameter.
tfarina10a5c062015-09-04 18:47:5734 int32_t outer_routing = 91;
35 uint32_t outer_type = 88;
[email protected]753bb252013-11-04 22:28:1236 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
37 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0438 ParamTraits<Message>::Write(&outer_msg, nested_msg);
39
40 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2541 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0442 IPC::Message result_msg;
43 ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg));
44
45 // Verify nested message headers.
46 EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id());
47 EXPECT_EQ(nested_msg.type(), result_msg.type());
[email protected]753bb252013-11-04 22:28:1248 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0449 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
50
51 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2552 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0453 int result_content = 0;
54 ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
55 &result_content));
56 EXPECT_EQ(nested_content, result_content);
57
58 // Try reading past the ends for both messages and make sure it fails.
59 IPC::Message dummy;
60 ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy));
61 ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
62 &result_content));
63}
64
[email protected]10cbaf02013-01-03 04:11:5465// Tests that detection of various bad parameters is working correctly.
66TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3067 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
68 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5569
70 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5471 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3072 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
73 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5474
brettwbd4d7112015-06-03 04:29:2575 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3076 base::FilePath ok_path;
77 base::FilePath bad_path;
78 ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path));
79 ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path));
[email protected]10cbaf02013-01-03 04:11:5480}
81
miletus1edf97f92015-07-23 19:42:3682
83TEST(IPCMessageUtilsTest, StackVector) {
84 static const size_t stack_capacity = 5;
85 base::StackVector<double, stack_capacity> stack_vector;
86 for (size_t i = 0; i < 2 * stack_capacity; i++)
87 stack_vector->push_back(i * 2.0);
88
89 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
90 IPC::WriteParam(&msg, stack_vector);
91
92 base::StackVector<double, stack_capacity> output;
93 base::PickleIterator iter(msg);
94 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
95 for (size_t i = 0; i < 2 * stack_capacity; i++)
96 EXPECT_EQ(stack_vector[i], output[i]);
97}
98
amistry36182522016-06-27 06:34:4299TEST(IPCMessageUtilsTest, MojoChannelHandle) {
100 mojo::MessagePipe message_pipe;
101 IPC::ChannelHandle channel_handle(message_pipe.handle0.release());
102
103 IPC::Message message;
104 IPC::WriteParam(&message, channel_handle);
105
amistry36182522016-06-27 06:34:42106 base::PickleIterator iter(message);
107 IPC::ChannelHandle result_handle;
108 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle));
amistry36182522016-06-27 06:34:42109 EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle);
110}
111
bmcquade18573e12016-07-01 13:13:50112TEST(IPCMessageUtilsTest, OptionalUnset) {
113 base::Optional<int> opt;
114 base::Pickle pickle;
115 IPC::WriteParam(&pickle, opt);
116
bmcquade18573e12016-07-01 13:13:50117 std::string log;
118 IPC::LogParam(opt, &log);
119 EXPECT_EQ("(unset)", log);
120
121 base::Optional<int> unserialized_opt;
122 base::PickleIterator iter(pickle);
123 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
124 EXPECT_FALSE(unserialized_opt);
125}
126
127TEST(IPCMessageUtilsTest, OptionalSet) {
128 base::Optional<int> opt(10);
129 base::Pickle pickle;
130 IPC::WriteParam(&pickle, opt);
131
bmcquade18573e12016-07-01 13:13:50132 std::string log;
133 IPC::LogParam(opt, &log);
134 EXPECT_EQ("10", log);
135
136 base::Optional<int> unserialized_opt;
137 base::PickleIterator iter(pickle);
138 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt));
139 EXPECT_TRUE(unserialized_opt);
140 EXPECT_EQ(opt.value(), unserialized_opt.value());
141}
142
Alexandr Ilind497eee2018-04-19 22:50:54143template <typename SharedMemoryRegionType>
144class SharedMemoryRegionTypedTest : public ::testing::Test {};
145
146typedef ::testing::Types<base::WritableSharedMemoryRegion,
147 base::UnsafeSharedMemoryRegion,
148 base::ReadOnlySharedMemoryRegion>
149 AllSharedMemoryRegionTypes;
Victor Costanebc52732019-02-15 02:39:47150TYPED_TEST_SUITE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes);
Alexandr Ilind497eee2018-04-19 22:50:54151
152TYPED_TEST(SharedMemoryRegionTypedTest, WriteAndRead) {
153 const size_t size = 2314;
154 TypeParam pre_pickle;
155 base::WritableSharedMemoryMapping pre_mapping;
156 std::tie(pre_pickle, pre_mapping) = base::CreateMappedRegion<TypeParam>(size);
157 const size_t pre_size = pre_pickle.GetSize();
158
159 const std::string content = "Hello, world!";
160 memcpy(pre_mapping.memory(), content.data(), content.size());
161
162 IPC::Message message;
163 IPC::WriteParam(&message, pre_pickle);
164 EXPECT_FALSE(pre_pickle.IsValid());
165
166 TypeParam post_pickle;
167 base::PickleIterator iter(message);
168 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
169 EXPECT_EQ(pre_size, post_pickle.GetSize());
170 typename TypeParam::MappingType post_mapping = post_pickle.Map();
171 EXPECT_EQ(pre_mapping.guid(), post_mapping.guid());
172 EXPECT_EQ(0, memcmp(pre_mapping.memory(), post_mapping.memory(),
173 post_pickle.GetSize()));
174}
175
176TYPED_TEST(SharedMemoryRegionTypedTest, InvalidRegion) {
177 TypeParam pre_pickle;
178 EXPECT_FALSE(pre_pickle.IsValid());
179
180 IPC::Message message;
181 IPC::WriteParam(&message, pre_pickle);
182
183 TypeParam post_pickle;
184 base::PickleIterator iter(message);
185 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
186 EXPECT_FALSE(post_pickle.IsValid());
187}
188
tguilbert4a5ac602016-09-19 21:11:25189TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
190 base::UnguessableToken token = base::UnguessableToken::Create();
191 base::Pickle pickle;
192 IPC::WriteParam(&pickle, token);
193
tguilbert4a5ac602016-09-19 21:11:25194 std::string log;
195 IPC::LogParam(token, &log);
196 EXPECT_EQ(token.ToString(), log);
197
198 base::UnguessableToken deserialized_token;
199 base::PickleIterator iter(pickle);
200 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token));
201 EXPECT_EQ(token, deserialized_token);
202}
203
brettwca2ec7632017-04-20 06:10:20204TEST(IPCMessageUtilsTest, FlatMap) {
205 base::flat_map<std::string, int> input;
206 input["foo"] = 42;
207 input["bar"] = 96;
208
209 base::Pickle pickle;
210 IPC::WriteParam(&pickle, input);
211
brettwca2ec7632017-04-20 06:10:20212 base::PickleIterator iter(pickle);
213 base::flat_map<std::string, int> output;
214 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
215
216 EXPECT_EQ(input, output);
217}
218
Istiaque Ahmed4832b03b2020-01-06 23:58:18219TEST(IPCMessageUtilsTest, StrongAlias) {
220 using TestType = util::StrongAlias<class Tag, int>;
221 TestType input(42);
222
223 base::Pickle pickle;
224 IPC::WriteParam(&pickle, input);
225
226 base::PickleIterator iter(pickle);
227 TestType output;
228 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
229
230 EXPECT_EQ(input, output);
231}
232
[email protected]2a3aa7b52013-01-11 20:56:22233} // namespace
[email protected]14062da2013-02-15 20:05:17234} // namespace IPC