blob: 659047d206106053409601765b93947c88315d6f [file] [log] [blame]
[email protected]34d48612012-06-29 00:05:041// 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]14062da2013-02-15 20:05:175#include "ipc/ipc_message_utils.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
9
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
amistryc7a7a762016-04-08 04:21:5411#include "base/json/json_reader.h"
[email protected]34d48612012-06-29 00:05:0412#include "ipc/ipc_message.h"
[email protected]34d48612012-06-29 00:05:0413#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]14062da2013-02-15 20:05:1715namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2216namespace {
[email protected]34d48612012-06-29 00:05:0417
18// Tests nesting of messages as parameters to other messages.
19TEST(IPCMessageUtilsTest, NestedMessages) {
tfarina10a5c062015-09-04 18:47:5720 int32_t nested_routing = 12;
21 uint32_t nested_type = 78;
[email protected]34d48612012-06-29 00:05:0422 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1223 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
24 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0425 nested_msg.set_sync();
26 ParamTraits<int>::Write(&nested_msg, nested_content);
27
28 // Outer message contains the nested one as its parameter.
tfarina10a5c062015-09-04 18:47:5729 int32_t outer_routing = 91;
30 uint32_t outer_type = 88;
[email protected]753bb252013-11-04 22:28:1231 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
32 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0433 ParamTraits<Message>::Write(&outer_msg, nested_msg);
34
35 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2536 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0437 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]753bb252013-11-04 22:28:1243 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0444 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
45
46 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2547 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0448 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]10cbaf02013-01-03 04:11:5460// Tests that detection of various bad parameters is working correctly.
61TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3062 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
63 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5564
65 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5466 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3067 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
68 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5469
brettwbd4d7112015-06-03 04:29:2570 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3071 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]10cbaf02013-01-03 04:11:5475}
76
miletus1edf97f92015-07-23 19:42:3677
78TEST(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
rockot0457af102016-02-05 02:12:3294// Tests that PickleSizer and Pickle agree on the size of a complex base::Value.
95TEST(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
amistryc7a7a762016-04-08 04:21:54120TEST(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]2a3aa7b52013-01-11 20:56:22136} // namespace
[email protected]14062da2013-02-15 20:05:17137} // namespace IPC