[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" |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 14 | #include "base/test/test_shared_memory_util.h" |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 15 | #include "base/unguessable_token.h" |
Robert Sesek | 0291066 | 2020-02-28 20:15:51 | [diff] [blame] | 16 | #include "build/build_config.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 | |
Robert Sesek | 0291066 | 2020-02-28 20:15:51 | [diff] [blame] | 21 | #if defined(OS_WIN) |
| 22 | #include <windows.h> |
| 23 | #endif |
| 24 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 25 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 26 | namespace { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 27 | |
| 28 | // Tests nesting of messages as parameters to other messages. |
| 29 | TEST(IPCMessageUtilsTest, NestedMessages) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 30 | int32_t nested_routing = 12; |
| 31 | uint32_t nested_type = 78; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 32 | int nested_content = 456789; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 33 | Message::PriorityValue nested_priority = Message::PRIORITY_HIGH; |
| 34 | Message nested_msg(nested_routing, nested_type, nested_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 35 | nested_msg.set_sync(); |
| 36 | ParamTraits<int>::Write(&nested_msg, nested_content); |
| 37 | |
| 38 | // Outer message contains the nested one as its parameter. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 39 | int32_t outer_routing = 91; |
| 40 | uint32_t outer_type = 88; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 41 | Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL; |
| 42 | Message outer_msg(outer_routing, outer_type, outer_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 43 | ParamTraits<Message>::Write(&outer_msg, nested_msg); |
| 44 | |
| 45 | // Read back the nested message. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 46 | base::PickleIterator iter(outer_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 47 | 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] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 53 | EXPECT_EQ(nested_msg.priority(), result_msg.priority()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 54 | EXPECT_EQ(nested_msg.flags(), result_msg.flags()); |
| 55 | |
| 56 | // Verify nested message content |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 57 | base::PickleIterator nested_iter(nested_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 58 | 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] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 70 | // Tests that detection of various bad parameters is working correctly. |
| 71 | TEST(IPCMessageUtilsTest, ParameterValidation) { |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 72 | base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5); |
| 73 | base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5); |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 74 | |
| 75 | // Change this if ParamTraits<FilePath>::Write() changes. |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 76 | IPC::Message message; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 77 | ParamTraits<base::FilePath::StringType>::Write(&message, ok_string); |
| 78 | ParamTraits<base::FilePath::StringType>::Write(&message, bad_string); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 79 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 80 | base::PickleIterator iter(message); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 81 | 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] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 85 | } |
| 86 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 87 | |
| 88 | TEST(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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 104 | TEST(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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 111 | base::PickleIterator iter(message); |
| 112 | IPC::ChannelHandle result_handle; |
| 113 | EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle)); |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 114 | EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle); |
| 115 | } |
| 116 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 117 | TEST(IPCMessageUtilsTest, OptionalUnset) { |
Anton Bikineev | 1f42a45 | 2021-05-15 18:02:50 | [diff] [blame^] | 118 | absl::optional<int> opt; |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 119 | base::Pickle pickle; |
| 120 | IPC::WriteParam(&pickle, opt); |
| 121 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 122 | std::string log; |
| 123 | IPC::LogParam(opt, &log); |
| 124 | EXPECT_EQ("(unset)", log); |
| 125 | |
Anton Bikineev | 1f42a45 | 2021-05-15 18:02:50 | [diff] [blame^] | 126 | absl::optional<int> unserialized_opt; |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 127 | base::PickleIterator iter(pickle); |
| 128 | EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &unserialized_opt)); |
| 129 | EXPECT_FALSE(unserialized_opt); |
| 130 | } |
| 131 | |
| 132 | TEST(IPCMessageUtilsTest, OptionalSet) { |
Anton Bikineev | 1f42a45 | 2021-05-15 18:02:50 | [diff] [blame^] | 133 | absl::optional<int> opt(10); |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 134 | base::Pickle pickle; |
| 135 | IPC::WriteParam(&pickle, opt); |
| 136 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 137 | std::string log; |
| 138 | IPC::LogParam(opt, &log); |
| 139 | EXPECT_EQ("10", log); |
| 140 | |
Anton Bikineev | 1f42a45 | 2021-05-15 18:02:50 | [diff] [blame^] | 141 | absl::optional<int> unserialized_opt; |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 142 | 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 Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 148 | template <typename SharedMemoryRegionType> |
| 149 | class SharedMemoryRegionTypedTest : public ::testing::Test {}; |
| 150 | |
| 151 | typedef ::testing::Types<base::WritableSharedMemoryRegion, |
| 152 | base::UnsafeSharedMemoryRegion, |
| 153 | base::ReadOnlySharedMemoryRegion> |
| 154 | AllSharedMemoryRegionTypes; |
Victor Costan | ebc5273 | 2019-02-15 02:39:47 | [diff] [blame] | 155 | TYPED_TEST_SUITE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 156 | |
| 157 | TYPED_TEST(SharedMemoryRegionTypedTest, WriteAndRead) { |
| 158 | const size_t size = 2314; |
| 159 | TypeParam pre_pickle; |
| 160 | base::WritableSharedMemoryMapping pre_mapping; |
| 161 | std::tie(pre_pickle, pre_mapping) = base::CreateMappedRegion<TypeParam>(size); |
| 162 | const size_t pre_size = pre_pickle.GetSize(); |
| 163 | |
| 164 | const std::string content = "Hello, world!"; |
| 165 | memcpy(pre_mapping.memory(), content.data(), content.size()); |
| 166 | |
| 167 | IPC::Message message; |
| 168 | IPC::WriteParam(&message, pre_pickle); |
| 169 | EXPECT_FALSE(pre_pickle.IsValid()); |
| 170 | |
| 171 | TypeParam post_pickle; |
| 172 | base::PickleIterator iter(message); |
| 173 | EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle)); |
| 174 | EXPECT_EQ(pre_size, post_pickle.GetSize()); |
| 175 | typename TypeParam::MappingType post_mapping = post_pickle.Map(); |
| 176 | EXPECT_EQ(pre_mapping.guid(), post_mapping.guid()); |
| 177 | EXPECT_EQ(0, memcmp(pre_mapping.memory(), post_mapping.memory(), |
| 178 | post_pickle.GetSize())); |
| 179 | } |
| 180 | |
| 181 | TYPED_TEST(SharedMemoryRegionTypedTest, InvalidRegion) { |
| 182 | TypeParam pre_pickle; |
| 183 | EXPECT_FALSE(pre_pickle.IsValid()); |
| 184 | |
| 185 | IPC::Message message; |
| 186 | IPC::WriteParam(&message, pre_pickle); |
| 187 | |
| 188 | TypeParam post_pickle; |
| 189 | base::PickleIterator iter(message); |
| 190 | EXPECT_TRUE(IPC::ReadParam(&message, &iter, &post_pickle)); |
| 191 | EXPECT_FALSE(post_pickle.IsValid()); |
| 192 | } |
| 193 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 194 | TEST(IPCMessageUtilsTest, UnguessableTokenTest) { |
| 195 | base::UnguessableToken token = base::UnguessableToken::Create(); |
| 196 | base::Pickle pickle; |
| 197 | IPC::WriteParam(&pickle, token); |
| 198 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 199 | std::string log; |
| 200 | IPC::LogParam(token, &log); |
| 201 | EXPECT_EQ(token.ToString(), log); |
| 202 | |
| 203 | base::UnguessableToken deserialized_token; |
| 204 | base::PickleIterator iter(pickle); |
| 205 | EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &deserialized_token)); |
| 206 | EXPECT_EQ(token, deserialized_token); |
| 207 | } |
| 208 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 209 | TEST(IPCMessageUtilsTest, FlatMap) { |
| 210 | base::flat_map<std::string, int> input; |
| 211 | input["foo"] = 42; |
| 212 | input["bar"] = 96; |
| 213 | |
| 214 | base::Pickle pickle; |
| 215 | IPC::WriteParam(&pickle, input); |
| 216 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 217 | base::PickleIterator iter(pickle); |
| 218 | base::flat_map<std::string, int> output; |
| 219 | EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output)); |
| 220 | |
| 221 | EXPECT_EQ(input, output); |
| 222 | } |
| 223 | |
Istiaque Ahmed | 4832b03b | 2020-01-06 23:58:18 | [diff] [blame] | 224 | TEST(IPCMessageUtilsTest, StrongAlias) { |
Peter Kasting | 796cde2 | 2020-11-18 21:06:53 | [diff] [blame] | 225 | using TestType = base::StrongAlias<class Tag, int>; |
Istiaque Ahmed | 4832b03b | 2020-01-06 23:58:18 | [diff] [blame] | 226 | TestType input(42); |
| 227 | |
| 228 | base::Pickle pickle; |
| 229 | IPC::WriteParam(&pickle, input); |
| 230 | |
| 231 | base::PickleIterator iter(pickle); |
| 232 | TestType output; |
| 233 | EXPECT_TRUE(IPC::ReadParam(&pickle, &iter, &output)); |
| 234 | |
| 235 | EXPECT_EQ(input, output); |
| 236 | } |
| 237 | |
Robert Sesek | 0291066 | 2020-02-28 20:15:51 | [diff] [blame] | 238 | #if defined(OS_WIN) |
| 239 | TEST(IPCMessageUtilsTest, ScopedHandle) { |
| 240 | HANDLE raw_dupe_handle; |
| 241 | ASSERT_TRUE(::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentProcess(), |
| 242 | ::GetCurrentProcess(), &raw_dupe_handle, 0, |
| 243 | FALSE, DUPLICATE_SAME_ACCESS)); |
| 244 | base::win::ScopedHandle dupe_handle(raw_dupe_handle); |
| 245 | |
| 246 | Message message(0, 0, Message::PRIORITY_LOW); |
| 247 | WriteParam(&message, dupe_handle); |
| 248 | |
| 249 | base::PickleIterator iter(message); |
| 250 | base::win::ScopedHandle read_handle; |
| 251 | EXPECT_TRUE(ReadParam(&message, &iter, &read_handle)); |
| 252 | EXPECT_TRUE(read_handle.IsValid()); |
| 253 | } |
| 254 | #endif // defined(OS_WIN) |
| 255 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 256 | } // namespace |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 257 | } // namespace IPC |