• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
5 #include "ipc/ipc_message_utils.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <memory>
10 
11 #include "base/files/file_path.h"
12 #include "base/json/json_reader.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/test/test_shared_memory_util.h"
16 #include "base/unguessable_token.h"
17 #include "ipc/ipc_channel_handle.h"
18 #include "ipc/ipc_message.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 namespace IPC {
22 namespace {
23 
24 // Tests nesting of messages as parameters to other messages.
TEST(IPCMessageUtilsTest,NestedMessages)25 TEST(IPCMessageUtilsTest, NestedMessages) {
26   int32_t nested_routing = 12;
27   uint32_t nested_type = 78;
28   int nested_content = 456789;
29   Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
30   Message nested_msg(nested_routing, nested_type, nested_priority);
31   nested_msg.set_sync();
32   ParamTraits<int>::Write(&nested_msg, nested_content);
33 
34   // Outer message contains the nested one as its parameter.
35   int32_t outer_routing = 91;
36   uint32_t outer_type = 88;
37   Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
38   Message outer_msg(outer_routing, outer_type, outer_priority);
39   ParamTraits<Message>::Write(&outer_msg, nested_msg);
40 
41   // Read back the nested message.
42   base::PickleIterator iter(outer_msg);
43   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());
49   EXPECT_EQ(nested_msg.priority(), result_msg.priority());
50   EXPECT_EQ(nested_msg.flags(), result_msg.flags());
51 
52   // Verify nested message content
53   base::PickleIterator nested_iter(nested_msg);
54   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 
66 // Tests that detection of various bad parameters is working correctly.
TEST(IPCMessageUtilsTest,ParameterValidation)67 TEST(IPCMessageUtilsTest, ParameterValidation) {
68   base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
69   base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
70 
71   // Change this if ParamTraits<FilePath>::Write() changes.
72   IPC::Message message;
73   ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
74   ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
75 
76   base::PickleIterator iter(message);
77   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));
81 }
82 
83 
TEST(IPCMessageUtilsTest,StackVector)84 TEST(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 
TEST(IPCMessageUtilsTest,MojoChannelHandle)100 TEST(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 
107   base::PickleIterator iter(message);
108   IPC::ChannelHandle result_handle;
109   EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle));
110   EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle);
111 }
112 
TEST(IPCMessageUtilsTest,OptionalUnset)113 TEST(IPCMessageUtilsTest, OptionalUnset) {
114   base::Optional<int> opt;
115   base::Pickle pickle;
116   IPC::WriteParam(&pickle, opt);
117 
118   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 
TEST(IPCMessageUtilsTest,OptionalSet)128 TEST(IPCMessageUtilsTest, OptionalSet) {
129   base::Optional<int> opt(10);
130   base::Pickle pickle;
131   IPC::WriteParam(&pickle, opt);
132 
133   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 
TEST(IPCMessageUtilsTest,SharedMemoryHandle)144 TEST(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 
163 template <typename SharedMemoryRegionType>
164 class SharedMemoryRegionTypedTest : public ::testing::Test {};
165 
166 typedef ::testing::Types<base::WritableSharedMemoryRegion,
167                          base::UnsafeSharedMemoryRegion,
168                          base::ReadOnlySharedMemoryRegion>
169     AllSharedMemoryRegionTypes;
170 TYPED_TEST_CASE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes);
171 
TYPED_TEST(SharedMemoryRegionTypedTest,WriteAndRead)172 TYPED_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 
TYPED_TEST(SharedMemoryRegionTypedTest,InvalidRegion)196 TYPED_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 
TEST(IPCMessageUtilsTest,UnguessableTokenTest)209 TEST(IPCMessageUtilsTest, UnguessableTokenTest) {
210   base::UnguessableToken token = base::UnguessableToken::Create();
211   base::Pickle pickle;
212   IPC::WriteParam(&pickle, token);
213 
214   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 
TEST(IPCMessageUtilsTest,FlatMap)224 TEST(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 
232   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 
239 }  // namespace
240 }  // namespace IPC
241