[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 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 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 5 | #include "ipc/ipc_message_utils.h" |
| 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 8 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | #include <memory> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 10 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
amistry | c7a7a76 | 2016-04-08 04:21:54 | [diff] [blame] | 12 | #include "base/json/json_reader.h" |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 14 | #include "base/memory/shared_memory.h" |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame^] | 15 | #include "base/test/test_shared_memory_util.h" |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 16 | #include "base/unguessable_token.h" |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 17 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 18 | #include "ipc/ipc_message.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 21 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 22 | namespace { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 23 | |
| 24 | // Tests nesting of messages as parameters to other messages. |
| 25 | TEST(IPCMessageUtilsTest, NestedMessages) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 26 | int32_t nested_routing = 12; |
| 27 | uint32_t nested_type = 78; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 28 | int nested_content = 456789; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 29 | Message::PriorityValue nested_priority = Message::PRIORITY_HIGH; |
| 30 | Message nested_msg(nested_routing, nested_type, nested_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 31 | nested_msg.set_sync(); |
| 32 | ParamTraits<int>::Write(&nested_msg, nested_content); |
| 33 | |
| 34 | // Outer message contains the nested one as its parameter. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 35 | int32_t outer_routing = 91; |
| 36 | uint32_t outer_type = 88; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 37 | Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL; |
| 38 | Message outer_msg(outer_routing, outer_type, outer_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 39 | ParamTraits<Message>::Write(&outer_msg, nested_msg); |
| 40 | |
| 41 | // Read back the nested message. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 42 | base::PickleIterator iter(outer_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 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()); |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 49 | EXPECT_EQ(nested_msg.priority(), result_msg.priority()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 50 | EXPECT_EQ(nested_msg.flags(), result_msg.flags()); |
| 51 | |
| 52 | // Verify nested message content |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 53 | base::PickleIterator nested_iter(nested_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 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 | |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 66 | // Tests that detection of various bad parameters is working correctly. |
| 67 | TEST(IPCMessageUtilsTest, ParameterValidation) { |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 68 | base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5); |
| 69 | base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5); |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 70 | |
| 71 | // Change this if ParamTraits<FilePath>::Write() changes. |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 72 | IPC::Message message; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 73 | ParamTraits<base::FilePath::StringType>::Write(&message, ok_string); |
| 74 | ParamTraits<base::FilePath::StringType>::Write(&message, bad_string); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 75 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 76 | base::PickleIterator iter(message); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 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)); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 81 | } |
| 82 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 83 | |
| 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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 107 | base::PickleIterator iter(message); |
| 108 | IPC::ChannelHandle result_handle; |
| 109 | EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle)); |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 110 | EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle); |
| 111 | } |
| 112 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 113 | TEST(IPCMessageUtilsTest, OptionalUnset) { |
| 114 | base::Optional<int> opt; |
| 115 | base::Pickle pickle; |
| 116 | IPC::WriteParam(&pickle, opt); |
| 117 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 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 | |
| 128 | TEST(IPCMessageUtilsTest, OptionalSet) { |
| 129 | base::Optional<int> opt(10); |
| 130 | base::Pickle pickle; |
| 131 | IPC::WriteParam(&pickle, opt); |
| 132 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 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 | |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 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 | |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame^] | 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 | |
| 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 | |
| 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 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 209 | TEST(IPCMessageUtilsTest, UnguessableTokenTest) { |
| 210 | base::UnguessableToken token = base::UnguessableToken::Create(); |
| 211 | base::Pickle pickle; |
| 212 | IPC::WriteParam(&pickle, token); |
| 213 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 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 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 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 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 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 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 239 | } // namespace |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 240 | } // namespace IPC |