blob: 084809c25a6c99639d9086556776515672176299 [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"
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
erikchen9d6afd712017-05-18 17:49:06143TEST(IPCMessageUtilsTest, SharedMemoryHandle) {
144 base::SharedMemoryCreateOptions options;
145 options.size = 1004;
146 base::SharedMemory shmem;
147 ASSERT_TRUE(shmem.Create(options));
148
149 base::SharedMemoryHandle pre_pickle = shmem.handle().Duplicate();
150 ASSERT_TRUE(pre_pickle.IsValid());
151
152 IPC::Message message;
153 IPC::WriteParam(&message, pre_pickle);
154
155 base::SharedMemoryHandle post_pickle;
156 base::PickleIterator iter(message);
157 EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle));
158 EXPECT_EQ(pre_pickle.GetGUID(), post_pickle.GetGUID());
159 EXPECT_EQ(pre_pickle.GetSize(), post_pickle.GetSize());
160}
161
tguilbert4a5ac602016-09-19 21:11:25162TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
163 base::UnguessableToken token = base::UnguessableToken::Create();
164 base::Pickle pickle;
165 IPC::WriteParam(&pickle, token);
166
tguilbert4a5ac602016-09-19 21:11:25167 std::string log;
168 IPC::LogParam(token, &log);
169 EXPECT_EQ(token.ToString(), log);
170
171 base::UnguessableToken deserialized_token;
172 base::PickleIterator iter(pickle);
173 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token));
174 EXPECT_EQ(token, deserialized_token);
175}
176
brettwca2ec7632017-04-20 06:10:20177TEST(IPCMessageUtilsTest, FlatMap) {
178 base::flat_map<std::string, int> input;
179 input["foo"] = 42;
180 input["bar"] = 96;
181
182 base::Pickle pickle;
183 IPC::WriteParam(&pickle, input);
184
brettwca2ec7632017-04-20 06:10:20185 base::PickleIterator iter(pickle);
186 base::flat_map<std::string, int> output;
187 EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output));
188
189 EXPECT_EQ(input, output);
190}
191
[email protected]2a3aa7b52013-01-11 20:56:22192} // namespace
[email protected]14062da2013-02-15 20:05:17193} // namespace IPC