ipc: Make sure that ChannelReader is destroyed correctly.
[chromium-blink-merge.git] / ipc / ipc_message_utils_unittest.cc
blob35a3e66271dc269f39309c9b2545733f9c892634
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.
5 #include "ipc/ipc_message_utils.h"
7 #include <stdint.h>
9 #include "base/files/file_path.h"
10 #include "ipc/ipc_message.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace IPC {
14 namespace {
16 // Tests nesting of messages as parameters to other messages.
17 TEST(IPCMessageUtilsTest, NestedMessages) {
18 int32_t nested_routing = 12;
19 uint32_t nested_type = 78;
20 int nested_content = 456789;
21 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
22 Message nested_msg(nested_routing, nested_type, nested_priority);
23 nested_msg.set_sync();
24 ParamTraits<int>::Write(&nested_msg, nested_content);
26 // Outer message contains the nested one as its parameter.
27 int32_t outer_routing = 91;
28 uint32_t outer_type = 88;
29 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
30 Message outer_msg(outer_routing, outer_type, outer_priority);
31 ParamTraits<Message>::Write(&outer_msg, nested_msg);
33 // Read back the nested message.
34 base::PickleIterator iter(outer_msg);
35 IPC::Message result_msg;
36 ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg));
38 // Verify nested message headers.
39 EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id());
40 EXPECT_EQ(nested_msg.type(), result_msg.type());
41 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
42 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
44 // Verify nested message content
45 base::PickleIterator nested_iter(nested_msg);
46 int result_content = 0;
47 ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
48 &result_content));
49 EXPECT_EQ(nested_content, result_content);
51 // Try reading past the ends for both messages and make sure it fails.
52 IPC::Message dummy;
53 ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy));
54 ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
55 &result_content));
58 // Tests that detection of various bad parameters is working correctly.
59 TEST(IPCMessageUtilsTest, ParameterValidation) {
60 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
61 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
63 // Change this if ParamTraits<FilePath>::Write() changes.
64 IPC::Message message;
65 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
66 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
68 base::PickleIterator iter(message);
69 base::FilePath ok_path;
70 base::FilePath bad_path;
71 ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path));
72 ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path));
76 TEST(IPCMessageUtilsTest, StackVector) {
77 static const size_t stack_capacity = 5;
78 base::StackVector<double, stack_capacity> stack_vector;
79 for (size_t i = 0; i < 2 * stack_capacity; i++)
80 stack_vector->push_back(i * 2.0);
82 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
83 IPC::WriteParam(&msg, stack_vector);
85 base::StackVector<double, stack_capacity> output;
86 base::PickleIterator iter(msg);
87 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
88 for (size_t i = 0; i < 2 * stack_capacity; i++)
89 EXPECT_EQ(stack_vector[i], output[i]);
92 } // namespace
93 } // namespace IPC