blob: f3aa31a516045c5339195575a8f84d75d3dbeb24 [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"
[email protected]34d48612012-06-29 00:05:0411#include "ipc/ipc_message.h"
[email protected]34d48612012-06-29 00:05:0412#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]14062da2013-02-15 20:05:1714namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2215namespace {
[email protected]34d48612012-06-29 00:05:0416
17// Tests nesting of messages as parameters to other messages.
18TEST(IPCMessageUtilsTest, NestedMessages) {
tfarina10a5c062015-09-04 18:47:5719 int32_t nested_routing = 12;
20 uint32_t nested_type = 78;
[email protected]34d48612012-06-29 00:05:0421 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1222 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
23 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0424 nested_msg.set_sync();
25 ParamTraits<int>::Write(&nested_msg, nested_content);
26
27 // Outer message contains the nested one as its parameter.
tfarina10a5c062015-09-04 18:47:5728 int32_t outer_routing = 91;
29 uint32_t outer_type = 88;
[email protected]753bb252013-11-04 22:28:1230 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
31 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0432 ParamTraits<Message>::Write(&outer_msg, nested_msg);
33
34 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2535 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0436 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]753bb252013-11-04 22:28:1242 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0443 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
44
45 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2546 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0447 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]10cbaf02013-01-03 04:11:5459// Tests that detection of various bad parameters is working correctly.
60TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3061 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
62 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5563
64 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5465 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3066 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
67 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5468
brettwbd4d7112015-06-03 04:29:2569 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3070 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]10cbaf02013-01-03 04:11:5474}
75
miletus1edf97f92015-07-23 19:42:3676
77TEST(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]2a3aa7b52013-01-11 20:56:2293} // namespace
[email protected]14062da2013-02-15 20:05:1794} // namespace IPC