[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" |
amistry | c7a7a76 | 2016-04-08 04:21:54 | [diff] [blame] | 11 | #include "base/json/json_reader.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 12 | #include "ipc/ipc_message.h" |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 15 | namespace IPC { |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 16 | namespace { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 17 | |
| 18 | // Tests nesting of messages as parameters to other messages. |
| 19 | TEST(IPCMessageUtilsTest, NestedMessages) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 20 | int32_t nested_routing = 12; |
| 21 | uint32_t nested_type = 78; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 22 | int nested_content = 456789; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 23 | Message::PriorityValue nested_priority = Message::PRIORITY_HIGH; |
| 24 | Message nested_msg(nested_routing, nested_type, nested_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 25 | nested_msg.set_sync(); |
| 26 | ParamTraits<int>::Write(&nested_msg, nested_content); |
| 27 | |
| 28 | // Outer message contains the nested one as its parameter. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 29 | int32_t outer_routing = 91; |
| 30 | uint32_t outer_type = 88; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 31 | Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL; |
| 32 | Message outer_msg(outer_routing, outer_type, outer_priority); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 33 | ParamTraits<Message>::Write(&outer_msg, nested_msg); |
| 34 | |
| 35 | // Read back the nested message. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 36 | base::PickleIterator iter(outer_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 37 | IPC::Message result_msg; |
| 38 | ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg)); |
| 39 | |
| 40 | // Verify nested message headers. |
| 41 | EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id()); |
| 42 | EXPECT_EQ(nested_msg.type(), result_msg.type()); |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 43 | EXPECT_EQ(nested_msg.priority(), result_msg.priority()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 44 | EXPECT_EQ(nested_msg.flags(), result_msg.flags()); |
| 45 | |
| 46 | // Verify nested message content |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 47 | base::PickleIterator nested_iter(nested_msg); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 48 | int result_content = 0; |
| 49 | ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter, |
| 50 | &result_content)); |
| 51 | EXPECT_EQ(nested_content, result_content); |
| 52 | |
| 53 | // Try reading past the ends for both messages and make sure it fails. |
| 54 | IPC::Message dummy; |
| 55 | ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy)); |
| 56 | ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter, |
| 57 | &result_content)); |
| 58 | } |
| 59 | |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 60 | // Tests that detection of various bad parameters is working correctly. |
| 61 | TEST(IPCMessageUtilsTest, ParameterValidation) { |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 62 | base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5); |
| 63 | base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5); |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 64 | |
| 65 | // Change this if ParamTraits<FilePath>::Write() changes. |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 66 | IPC::Message message; |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 67 | ParamTraits<base::FilePath::StringType>::Write(&message, ok_string); |
| 68 | ParamTraits<base::FilePath::StringType>::Write(&message, bad_string); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 69 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 70 | base::PickleIterator iter(message); |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 71 | base::FilePath ok_path; |
| 72 | base::FilePath bad_path; |
| 73 | ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path)); |
| 74 | ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path)); |
[email protected] | 10cbaf0 | 2013-01-03 04:11:54 | [diff] [blame] | 75 | } |
| 76 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 77 | |
| 78 | TEST(IPCMessageUtilsTest, StackVector) { |
| 79 | static const size_t stack_capacity = 5; |
| 80 | base::StackVector<double, stack_capacity> stack_vector; |
| 81 | for (size_t i = 0; i < 2 * stack_capacity; i++) |
| 82 | stack_vector->push_back(i * 2.0); |
| 83 | |
| 84 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 85 | IPC::WriteParam(&msg, stack_vector); |
| 86 | |
| 87 | base::StackVector<double, stack_capacity> output; |
| 88 | base::PickleIterator iter(msg); |
| 89 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 90 | for (size_t i = 0; i < 2 * stack_capacity; i++) |
| 91 | EXPECT_EQ(stack_vector[i], output[i]); |
| 92 | } |
| 93 | |
rockot | 0457af10 | 2016-02-05 02:12:32 | [diff] [blame] | 94 | // Tests that PickleSizer and Pickle agree on the size of a complex base::Value. |
| 95 | TEST(IPCMessageUtilsTest, ValueSize) { |
| 96 | scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 97 | value->SetWithoutPathExpansion("foo", new base::FundamentalValue(42)); |
| 98 | value->SetWithoutPathExpansion("bar", new base::FundamentalValue(3.14)); |
| 99 | value->SetWithoutPathExpansion("baz", new base::StringValue("hello")); |
| 100 | value->SetWithoutPathExpansion("qux", base::Value::CreateNullValue()); |
| 101 | |
| 102 | scoped_ptr<base::DictionaryValue> nested_dict(new base::DictionaryValue); |
| 103 | nested_dict->SetWithoutPathExpansion("foobar", new base::FundamentalValue(5)); |
| 104 | value->SetWithoutPathExpansion("nested", std::move(nested_dict)); |
| 105 | |
| 106 | scoped_ptr<base::ListValue> list_value(new base::ListValue); |
| 107 | list_value->Append(new base::StringValue("im a string")); |
| 108 | list_value->Append(new base::StringValue("im another string")); |
| 109 | value->SetWithoutPathExpansion("awesome-list", std::move(list_value)); |
| 110 | |
| 111 | base::Pickle pickle; |
| 112 | IPC::WriteParam(&pickle, *value); |
| 113 | |
| 114 | base::PickleSizer sizer; |
| 115 | IPC::GetParamSize(&sizer, *value); |
| 116 | |
| 117 | EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); |
| 118 | } |
| 119 | |
amistry | c7a7a76 | 2016-04-08 04:21:54 | [diff] [blame] | 120 | TEST(IPCMessageUtilsTest, JsonValueSize) { |
| 121 | const char kJson[] = "[ { \"foo\": \"bar\", \"baz\": 1234.0 } ]"; |
| 122 | std::unique_ptr<base::Value> json_value = base::JSONReader::Read(kJson); |
| 123 | EXPECT_NE(nullptr, json_value); |
| 124 | base::ListValue value; |
| 125 | value.Append(std::move(json_value)); |
| 126 | |
| 127 | base::Pickle pickle; |
| 128 | IPC::WriteParam(&pickle, value); |
| 129 | |
| 130 | base::PickleSizer sizer; |
| 131 | IPC::GetParamSize(&sizer, value); |
| 132 | |
| 133 | EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); |
| 134 | } |
| 135 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 136 | } // namespace |
[email protected] | 14062da | 2013-02-15 20:05:17 | [diff] [blame] | 137 | } // namespace IPC |