blob: a73e2e7e2fce047a10b1e6135f49b385777c1738 [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
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]34d48612012-06-29 00:05:048#include "ipc/ipc_message.h"
[email protected]34d48612012-06-29 00:05:049#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]14062da2013-02-15 20:05:1711namespace IPC {
[email protected]2a3aa7b52013-01-11 20:56:2212namespace {
[email protected]34d48612012-06-29 00:05:0413
14// Tests nesting of messages as parameters to other messages.
15TEST(IPCMessageUtilsTest, NestedMessages) {
16 int32 nested_routing = 12;
17 uint32 nested_type = 78;
18 int nested_content = 456789;
[email protected]753bb252013-11-04 22:28:1219 Message::PriorityValue nested_priority = Message::PRIORITY_HIGH;
20 Message nested_msg(nested_routing, nested_type, nested_priority);
[email protected]34d48612012-06-29 00:05:0421 nested_msg.set_sync();
22 ParamTraits<int>::Write(&nested_msg, nested_content);
23
24 // Outer message contains the nested one as its parameter.
25 int32 outer_routing = 91;
26 uint32 outer_type = 88;
[email protected]753bb252013-11-04 22:28:1227 Message::PriorityValue outer_priority = Message::PRIORITY_NORMAL;
28 Message outer_msg(outer_routing, outer_type, outer_priority);
[email protected]34d48612012-06-29 00:05:0429 ParamTraits<Message>::Write(&outer_msg, nested_msg);
30
31 // Read back the nested message.
brettwbd4d7112015-06-03 04:29:2532 base::PickleIterator iter(outer_msg);
[email protected]34d48612012-06-29 00:05:0433 IPC::Message result_msg;
34 ASSERT_TRUE(ParamTraits<Message>::Read(&outer_msg, &iter, &result_msg));
35
36 // Verify nested message headers.
37 EXPECT_EQ(nested_msg.routing_id(), result_msg.routing_id());
38 EXPECT_EQ(nested_msg.type(), result_msg.type());
[email protected]753bb252013-11-04 22:28:1239 EXPECT_EQ(nested_msg.priority(), result_msg.priority());
[email protected]34d48612012-06-29 00:05:0440 EXPECT_EQ(nested_msg.flags(), result_msg.flags());
41
42 // Verify nested message content
brettwbd4d7112015-06-03 04:29:2543 base::PickleIterator nested_iter(nested_msg);
[email protected]34d48612012-06-29 00:05:0444 int result_content = 0;
45 ASSERT_TRUE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
46 &result_content));
47 EXPECT_EQ(nested_content, result_content);
48
49 // Try reading past the ends for both messages and make sure it fails.
50 IPC::Message dummy;
51 ASSERT_FALSE(ParamTraits<Message>::Read(&outer_msg, &iter, &dummy));
52 ASSERT_FALSE(ParamTraits<int>::Read(&nested_msg, &nested_iter,
53 &result_content));
54}
55
[email protected]10cbaf02013-01-03 04:11:5456// Tests that detection of various bad parameters is working correctly.
57TEST(IPCMessageUtilsTest, ParameterValidation) {
[email protected]6d4b67a2013-02-10 04:49:3058 base::FilePath::StringType ok_string(FILE_PATH_LITERAL("hello"), 5);
59 base::FilePath::StringType bad_string(FILE_PATH_LITERAL("hel\0o"), 5);
[email protected]aeae59f2013-01-28 13:47:5560
61 // Change this if ParamTraits<FilePath>::Write() changes.
[email protected]10cbaf02013-01-03 04:11:5462 IPC::Message message;
[email protected]6d4b67a2013-02-10 04:49:3063 ParamTraits<base::FilePath::StringType>::Write(&message, ok_string);
64 ParamTraits<base::FilePath::StringType>::Write(&message, bad_string);
[email protected]10cbaf02013-01-03 04:11:5465
brettwbd4d7112015-06-03 04:29:2566 base::PickleIterator iter(message);
[email protected]6d4b67a2013-02-10 04:49:3067 base::FilePath ok_path;
68 base::FilePath bad_path;
69 ASSERT_TRUE(ParamTraits<base::FilePath>::Read(&message, &iter, &ok_path));
70 ASSERT_FALSE(ParamTraits<base::FilePath>::Read(&message, &iter, &bad_path));
[email protected]10cbaf02013-01-03 04:11:5471}
72
[email protected]2a3aa7b52013-01-11 20:56:2273} // namespace
[email protected]14062da2013-02-15 20:05:1774} // namespace IPC