blob: 885ac2ebdf05f27eb742ed351ff2034689c3adfa [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]946d1b22009-07-22 23:57:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]22b42c52010-12-20 06:59:235#include "ipc/ipc_message.h"
6
[email protected]946d1b22009-07-22 23:57:217#include <string.h>
8
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]946d1b22009-07-22 23:57:2110#include "base/values.h"
[email protected]946d1b22009-07-22 23:57:2111#include "ipc/ipc_message_utils.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]5636d902014-05-13 23:19:1014// IPC messages for testing ----------------------------------------------------
15
16#define IPC_MESSAGE_IMPL
17#include "ipc/ipc_message_macros.h"
18
19#define IPC_MESSAGE_START TestMsgStart
20
21IPC_MESSAGE_CONTROL0(TestMsgClassEmpty)
22
23IPC_MESSAGE_CONTROL1(TestMsgClassI, int)
24
25IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string)
26
[email protected]2a3aa7b52013-01-11 20:56:2227namespace {
28
[email protected]946d1b22009-07-22 23:57:2129TEST(IPCMessageTest, ListValue) {
[email protected]ea5ef4c2013-06-13 22:50:2730 base::ListValue input;
[email protected]4038a132013-01-30 05:24:0731 input.Set(0, new base::FundamentalValue(42.42));
32 input.Set(1, new base::StringValue("forty"));
33 input.Set(2, base::Value::CreateNullValue());
[email protected]946d1b22009-07-22 23:57:2134
[email protected]753bb252013-11-04 22:28:1235 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2136 IPC::WriteParam(&msg, input);
37
[email protected]ea5ef4c2013-06-13 22:50:2738 base::ListValue output;
brettwbd4d7112015-06-03 04:29:2539 base::PickleIterator iter(msg);
[email protected]946d1b22009-07-22 23:57:2140 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
41
42 EXPECT_TRUE(input.Equals(&output));
43
44 // Also test the corrupt case.
[email protected]753bb252013-11-04 22:28:1245 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2146 bad_msg.WriteInt(99);
brettwbd4d7112015-06-03 04:29:2547 iter = base::PickleIterator(bad_msg);
[email protected]946d1b22009-07-22 23:57:2148 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
49}
50
51TEST(IPCMessageTest, DictionaryValue) {
[email protected]ea5ef4c2013-06-13 22:50:2752 base::DictionaryValue input;
53 input.Set("null", base::Value::CreateNullValue());
[email protected]4038a132013-01-30 05:24:0754 input.Set("bool", new base::FundamentalValue(true));
55 input.Set("int", new base::FundamentalValue(42));
56 input.SetWithoutPathExpansion("int.with.dot", new base::FundamentalValue(43));
[email protected]946d1b22009-07-22 23:57:2157
[email protected]ea5ef4c2013-06-13 22:50:2758 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue());
[email protected]4038a132013-01-30 05:24:0759 subdict->Set("str", new base::StringValue("forty two"));
60 subdict->Set("bool", new base::FundamentalValue(false));
[email protected]946d1b22009-07-22 23:57:2161
[email protected]ea5ef4c2013-06-13 22:50:2762 scoped_ptr<base::ListValue> sublist(new base::ListValue());
[email protected]4038a132013-01-30 05:24:0763 sublist->Set(0, new base::FundamentalValue(42.42));
64 sublist->Set(1, new base::StringValue("forty"));
65 sublist->Set(2, new base::StringValue("two"));
[email protected]99922662010-08-17 16:24:2566 subdict->Set("list", sublist.release());
[email protected]946d1b22009-07-22 23:57:2167
[email protected]99922662010-08-17 16:24:2568 input.Set("dict", subdict.release());
[email protected]946d1b22009-07-22 23:57:2169
[email protected]753bb252013-11-04 22:28:1270 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2171 IPC::WriteParam(&msg, input);
72
[email protected]ea5ef4c2013-06-13 22:50:2773 base::DictionaryValue output;
brettwbd4d7112015-06-03 04:29:2574 base::PickleIterator iter(msg);
[email protected]946d1b22009-07-22 23:57:2175 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
76
77 EXPECT_TRUE(input.Equals(&output));
78
79 // Also test the corrupt case.
[email protected]753bb252013-11-04 22:28:1280 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
[email protected]946d1b22009-07-22 23:57:2181 bad_msg.WriteInt(99);
brettwbd4d7112015-06-03 04:29:2582 iter = base::PickleIterator(bad_msg);
[email protected]946d1b22009-07-22 23:57:2183 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
84}
[email protected]2a3aa7b52013-01-11 20:56:2285
[email protected]5636d902014-05-13 23:19:1086class IPCMessageParameterTest : public testing::Test {
87 public:
88 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {}
89
90 bool OnMessageReceived(const IPC::Message& message) {
[email protected]5636d902014-05-13 23:19:1091 bool handled = true;
92 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message,
[email protected]e44d1342014-05-16 21:29:3393 &extra_param_)
[email protected]5636d902014-05-13 23:19:1094 IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty)
95 IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt)
96 //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync)
97 IPC_MESSAGE_UNHANDLED(handled = false)
98 IPC_END_MESSAGE_MAP()
99
100 return handled;
101 }
102
103 void OnEmpty(std::string* extra_param) {
104 EXPECT_EQ(extra_param, &extra_param_);
105 called_ = true;
106 }
107
108 void OnInt(std::string* extra_param, int foo) {
109 EXPECT_EQ(extra_param, &extra_param_);
110 EXPECT_EQ(foo, 42);
111 called_ = true;
112 }
113
114 /* TODO: handle sync IPCs
115 void OnSync(std::string* extra_param, int foo, std::string* out) {
116 EXPECT_EQ(extra_param, &extra_param_);
117 EXPECT_EQ(foo, 42);
118 called_ = true;
119 *out = std::string("out");
120 }
121
122 bool Send(IPC::Message* reply) {
123 delete reply;
124 return true;
125 }*/
126
127 std::string extra_param_;
128 bool called_;
129};
130
131TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) {
132 TestMsgClassEmpty message;
133 EXPECT_TRUE(OnMessageReceived(message));
134 EXPECT_TRUE(called_);
135}
136
137TEST_F(IPCMessageParameterTest, OneIntegerWithParam) {
138 TestMsgClassI message(42);
139 EXPECT_TRUE(OnMessageReceived(message));
140 EXPECT_TRUE(called_);
141}
142
143/* TODO: handle sync IPCs
144TEST_F(IPCMessageParameterTest, Sync) {
145 std::string output;
146 TestMsgClassIS message(42, &output);
147 EXPECT_TRUE(OnMessageReceived(message));
148 EXPECT_TRUE(called_);
149 EXPECT_EQ(output, std::string("out"));
150}*/
151
[email protected]2a3aa7b52013-01-11 20:56:22152} // namespace