[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> |
| 9 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 11 | #include "ipc/ipc_message.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 14 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 15 | namespace { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 16 | |
| 17 | // Tests nesting of messages as parameters to other messages. |
| 18 | TEST(IPCMessageUtilsTest, NestedMessages) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 19 | int32_t nested_routing = 12; |
| 20 | uint32_t nested_type = 78; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 21 | int nested_content = 456789; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 22 | Message::PriorityValue nested_priority = Message::PRIORITY_HIGH; |
| 23 | Message nested_msg(nested_routing, nested_type, nested_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 24 | nested_msg.set_sync(); |
| 25 | ParamTraits<int>::Write(&nested_msg, nested_content); |
| 26 | |
| 27 | // Outer message contains the nested one as its parameter. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 28 | int32_t outer_routing = 91; |
| 29 | uint32_t outer_type = 88; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 30 | Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL; |
| 31 | Message outer_msg(outer_routing, outer_type, outer_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 32 | ParamTraits<Message>::Write(&outer_msg, nested_msg); |
| 33 | |
| 34 | // Read back the nested message. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 35 | base::PickleIterator iter(outer_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 36 | IPC::Message result_msg; |
| 37 | ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg)); |
| 38 | |
| 39 | // Verify nested message headers. |
| 40 | EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id()); |
| 41 | EXPECT_EQ(nested_msg.type(), result_msg.type()); |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 42 | EXPECT_EQ(nested_msg.priority(), result_msg.priority()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 43 | EXPECT_EQ(nested_msg.flags(), result_msg.flags()); |
| 44 | |
| 45 | // Verify nested message content |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 46 | base::PickleIterator nested_iter(nested_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 47 | int result_content = 0; |
| 48 | ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter, |
| 49 | &result_content)); |
| 50 | EXPECT_EQ(nested_content, result_content); |
| 51 | |
| 52 | // Try reading past the ends for both messages and make sure it fails. |
| 53 | IPC::Message dummy; |
| 54 | ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy)); |
| 55 | ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter, |
| 56 | &result_content)); |
| 57 | } |
| 58 | |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 59 | // Tests that detection of various bad parameters is working correctly. |
| 60 | TEST(IPCMessageUtilsTest, ParameterValidation) { |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 61 | base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5); |
| 62 | base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5); |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 63 | |
| 64 | // Change this if ParamTraits<FilePath>::Write() changes. |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 65 | IPC::Message message; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 66 | ParamTraits<base::FilePath::StringType>::Write(&message, ok_string); |
| 67 | ParamTraits<base::FilePath::StringType>::Write(&message, bad_string); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 68 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 69 | base::PickleIterator iter(message); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 70 | base::FilePath ok_path; |
| 71 | base::FilePath bad_path; |
| 72 | ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path)); |
| 73 | ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path)); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 74 | } |
| 75 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 76 | |
| 77 | TEST(IPCMessageUtilsTest, StackVector) { |
| 78 | static const size_t stack_capacity = 5; |
| 79 | base::StackVector<double, stack_capacity> stack_vector; |
| 80 | for (size_t i = 0; i < 2 * stack_capacity; i++) |
| 81 | stack_vector->push_back(i * 2.0); |
| 82 | |
| 83 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 84 | IPC::WriteParam(&msg, stack_vector); |
| 85 | |
| 86 | base::StackVector<double, stack_capacity> output; |
| 87 | base::PickleIterator iter(msg); |
| 88 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 89 | for (size_t i = 0; i < 2 * stack_capacity; i++) |
| 90 | EXPECT_EQ(stack_vector[i], output[i]); |
| 91 | } |
| 92 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 93 | } // namespace |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 94 | } // namespace IPC |