Make RuleBasedHostResolverProc methods to add/remove rules threadsafe.
[chromium-blink-merge.git] / ipc / ipc_message_unittest.cc
blobaea705449cce9c0daa7a094b86504df94c5e92a8
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.h"
7 #include <string.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "ipc/ipc_message_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // IPC messages for testing ----------------------------------------------------
17 #define IPC_MESSAGE_IMPL
18 #include "ipc/ipc_message_macros.h"
20 #define IPC_MESSAGE_START TestMsgStart
22 IPC_MESSAGE_CONTROL0(TestMsgClassEmpty)
24 IPC_MESSAGE_CONTROL1(TestMsgClassI, int)
26 IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string)
28 namespace {
30 TEST(IPCMessageTest, BasicMessageTest) {
31 int v1 = 10;
32 std::string v2("foobar");
33 base::string16 v3(base::ASCIIToUTF16("hello world"));
35 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
36 EXPECT_TRUE(m.WriteInt(v1));
37 EXPECT_TRUE(m.WriteString(v2));
38 EXPECT_TRUE(m.WriteString16(v3));
40 base::PickleIterator iter(m);
42 int vi;
43 std::string vs;
44 base::string16 vs16;
46 EXPECT_TRUE(iter.ReadInt(&vi));
47 EXPECT_EQ(v1, vi);
49 EXPECT_TRUE(iter.ReadString(&vs));
50 EXPECT_EQ(v2, vs);
52 EXPECT_TRUE(iter.ReadString16(&vs16));
53 EXPECT_EQ(v3, vs16);
55 // should fail
56 EXPECT_FALSE(iter.ReadInt(&vi));
57 EXPECT_FALSE(iter.ReadString(&vs));
58 EXPECT_FALSE(iter.ReadString16(&vs16));
61 TEST(IPCMessageTest, ListValue) {
62 base::ListValue input;
63 input.Set(0, new base::FundamentalValue(42.42));
64 input.Set(1, new base::StringValue("forty"));
65 input.Set(2, base::Value::CreateNullValue());
67 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
68 IPC::WriteParam(&msg, input);
70 base::ListValue output;
71 base::PickleIterator iter(msg);
72 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
74 EXPECT_TRUE(input.Equals(&output));
76 // Also test the corrupt case.
77 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
78 bad_msg.WriteInt(99);
79 iter = base::PickleIterator(bad_msg);
80 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
83 TEST(IPCMessageTest, DictionaryValue) {
84 base::DictionaryValue input;
85 input.Set("null", base::Value::CreateNullValue());
86 input.Set("bool", new base::FundamentalValue(true));
87 input.Set("int", new base::FundamentalValue(42));
88 input.SetWithoutPathExpansion("int.with.dot", new base::FundamentalValue(43));
90 scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue());
91 subdict->Set("str", new base::StringValue("forty two"));
92 subdict->Set("bool", new base::FundamentalValue(false));
94 scoped_ptr<base::ListValue> sublist(new base::ListValue());
95 sublist->Set(0, new base::FundamentalValue(42.42));
96 sublist->Set(1, new base::StringValue("forty"));
97 sublist->Set(2, new base::StringValue("two"));
98 subdict->Set("list", sublist.release());
100 input.Set("dict", subdict.release());
102 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
103 IPC::WriteParam(&msg, input);
105 base::DictionaryValue output;
106 base::PickleIterator iter(msg);
107 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
109 EXPECT_TRUE(input.Equals(&output));
111 // Also test the corrupt case.
112 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
113 bad_msg.WriteInt(99);
114 iter = base::PickleIterator(bad_msg);
115 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
118 class IPCMessageParameterTest : public testing::Test {
119 public:
120 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {}
122 bool OnMessageReceived(const IPC::Message& message) {
123 bool handled = true;
124 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message,
125 &extra_param_)
126 IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty)
127 IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt)
128 //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync)
129 IPC_MESSAGE_UNHANDLED(handled = false)
130 IPC_END_MESSAGE_MAP()
132 return handled;
135 void OnEmpty(std::string* extra_param) {
136 EXPECT_EQ(extra_param, &extra_param_);
137 called_ = true;
140 void OnInt(std::string* extra_param, int foo) {
141 EXPECT_EQ(extra_param, &extra_param_);
142 EXPECT_EQ(foo, 42);
143 called_ = true;
146 /* TODO: handle sync IPCs
147 void OnSync(std::string* extra_param, int foo, std::string* out) {
148 EXPECT_EQ(extra_param, &extra_param_);
149 EXPECT_EQ(foo, 42);
150 called_ = true;
151 *out = std::string("out");
154 bool Send(IPC::Message* reply) {
155 delete reply;
156 return true;
159 std::string extra_param_;
160 bool called_;
163 TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) {
164 TestMsgClassEmpty message;
165 EXPECT_TRUE(OnMessageReceived(message));
166 EXPECT_TRUE(called_);
169 #if defined(OS_ANDROID)
170 #define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam
171 #else
172 #define MAYBE_OneIntegerWithParam OneIntegerWithParam
173 #endif
174 TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) {
175 TestMsgClassI message(42);
176 EXPECT_TRUE(OnMessageReceived(message));
177 EXPECT_TRUE(called_);
180 /* TODO: handle sync IPCs
181 TEST_F(IPCMessageParameterTest, Sync) {
182 std::string output;
183 TestMsgClassIS message(42, &output);
184 EXPECT_TRUE(OnMessageReceived(message));
185 EXPECT_TRUE(called_);
186 EXPECT_EQ(output, std::string("out"));
189 } // namespace