[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" |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 16 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 17 | #include "ipc/ipc_message.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 20 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 21 | namespace { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 22 | |
| 23 | // Tests nesting of messages as parameters to other messages. |
| 24 | TEST(IPCMessageUtilsTest, NestedMessages) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 25 | int32_t nested_routing = 12; |
| 26 | uint32_t nested_type = 78; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 27 | int nested_content = 456789; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 28 | Message::PriorityValue nested_priority = Message::PRIORITY_HIGH; |
| 29 | Message nested_msg(nested_routing, nested_type, nested_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 30 | nested_msg.set_sync(); |
| 31 | ParamTraits<int>::Write(&nested_msg, nested_content); |
| 32 | |
| 33 | // Outer message contains the nested one as its parameter. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 34 | int32_t outer_routing = 91; |
| 35 | uint32_t outer_type = 88; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 36 | Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL; |
| 37 | Message outer_msg(outer_routing, outer_type, outer_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 38 | ParamTraits<Message>::Write(&outer_msg, nested_msg); |
| 39 | |
| 40 | // Read back the nested message. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 41 | base::PickleIterator iter(outer_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 42 | 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] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 48 | EXPECT_EQ(nested_msg.priority(), result_msg.priority()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 49 | EXPECT_EQ(nested_msg.flags(), result_msg.flags()); |
| 50 | |
| 51 | // Verify nested message content |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 52 | base::PickleIterator nested_iter(nested_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 53 | 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] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 65 | // Tests that detection of various bad parameters is working correctly. |
| 66 | TEST(IPCMessageUtilsTest, ParameterValidation) { |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 67 | base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5); |
| 68 | base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5); |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 69 | |
| 70 | // Change this if ParamTraits<FilePath>::Write() changes. |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 71 | IPC::Message message; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 72 | ParamTraits<base::FilePath::StringType>::Write(&message, ok_string); |
| 73 | ParamTraits<base::FilePath::StringType>::Write(&message, bad_string); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 74 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 75 | base::PickleIterator iter(message); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 76 | 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] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 80 | } |
| 81 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 82 | |
| 83 | TEST(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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 99 | TEST(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 | |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 106 | base::PickleIterator iter(message); |
| 107 | IPC::ChannelHandle result_handle; |
| 108 | EXPECT_TRUE(IPC::ReadParam(&message, &iter, &result_handle)); |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 109 | EXPECT_EQ(channel_handle.mojo_handle, result_handle.mojo_handle); |
| 110 | } |
| 111 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 112 | TEST(IPCMessageUtilsTest, OptionalUnset) { |
| 113 | base::Optional<int> opt; |
| 114 | base::Pickle pickle; |
| 115 | IPC::WriteParam(&pickle, opt); |
| 116 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 117 | 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 | |
| 127 | TEST(IPCMessageUtilsTest, OptionalSet) { |
| 128 | base::Optional<int> opt(10); |
| 129 | base::Pickle pickle; |
| 130 | IPC::WriteParam(&pickle, opt); |
| 131 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 132 | 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 Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 143 | template <typename SharedMemoryRegionType> |
| 144 | class SharedMemoryRegionTypedTest : public ::testing::Test {}; |
| 145 | |
| 146 | typedef ::testing::Types<base::WritableSharedMemoryRegion, |
| 147 | base::UnsafeSharedMemoryRegion, |
| 148 | base::ReadOnlySharedMemoryRegion> |
| 149 | AllSharedMemoryRegionTypes; |
Victor Costan | ebc5273 | 2019-02-15 02:39:47 | [diff] [blame] | 150 | TYPED_TEST_SUITE(SharedMemoryRegionTypedTest, AllSharedMemoryRegionTypes); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 151 | |
| 152 | TYPED_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 | |
| 176 | TYPED_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 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 189 | TEST(IPCMessageUtilsTest, UnguessableTokenTest) { |
| 190 | base::UnguessableToken token = base::UnguessableToken::Create(); |
| 191 | base::Pickle pickle; |
| 192 | IPC::WriteParam(&pickle, token); |
| 193 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 194 | 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 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 204 | TEST(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 | |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 212 | 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 Ahmed | 4832b03b | 2020-01-06 23:58:18 | [diff] [blame] | 219 | TEST(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] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 233 | } // namespace |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 234 | } // namespace IPC |