blob: c8edb0c50d973520338bc67f29b4eb42ca6521aa [file] [log] [blame]
kushi.p@gmail.com90594e32011-04-29 03:20:09 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Unit test to make sure that the serialization of synchronous IPC messages
6// works. This ensures that the macros and templates were defined correctly.
7// Doesn't test the IPC channel mechanism.
8
tfarina2d565d32015-09-16 18:56:21 +09009#include "base/logging.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090010#include "ipc/ipc_message.h"
11#include "ipc/ipc_message_utils.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090012#include "testing/gtest/include/gtest/gtest.h"
13
jam@chromium.org86a8de12010-12-09 08:34:16 +090014#define IPC_MESSAGE_IMPL
15#include "ipc/ipc_sync_message_unittest.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090016
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +090017namespace {
18
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090019static IPC::Message* g_reply;
20
21class TestMessageReceiver {
22 public:
23
24 void On_0_1(bool* out1) {
25 *out1 = false;
26 }
27
28 void On_0_2(bool* out1, int* out2) {
29 *out1 = true;
30 *out2 = 2;
31 }
32
33 void On_0_3(bool* out1, int* out2, std::string* out3) {
34 *out1 = false;
35 *out2 = 3;
36 *out3 = "0_3";
37 }
38
39 void On_1_1(int in1, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090040 DCHECK_EQ(1, in1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090041 *out1 = true;
42 }
43
44 void On_1_2(bool in1, bool* out1, int* out2) {
45 DCHECK(!in1);
46 *out1 = true;
47 *out2 = 12;
48 }
49
50 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090051 DCHECK_EQ(3, in1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090052 *out1 = "1_3";
53 *out2 = 13;
54 *out3 = false;
55 }
56
57 void On_2_1(int in1, bool in2, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090058 DCHECK_EQ(1, in1);
59 DCHECK(!in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090060 *out1 = true;
61 }
62
63 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090064 DCHECK(!in1);
65 DCHECK_EQ(2, in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090066 *out1 = true;
67 *out2 = 22;
68 }
69
70 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090071 DCHECK_EQ(3, in1);
72 DCHECK(in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090073 *out1 = "2_3";
74 *out2 = 23;
75 *out3 = false;
76 }
77
ki.stfub139e7b2015-09-26 08:17:22 +090078 void On_3_1(int in1, bool in2, const std::string& in3, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090079 DCHECK_EQ(1, in1);
80 DCHECK(!in2);
81 DCHECK_EQ("3_1", in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090082 *out1 = true;
83 }
84
ki.stfub139e7b2015-09-26 08:17:22 +090085 void On_3_2(const std::string& in1,
86 bool in2,
87 int in3,
88 bool* out1,
89 int* out2) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090090 DCHECK_EQ("3_2", in1);
91 DCHECK(!in2);
92 DCHECK_EQ(2, in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090093 *out1 = true;
94 *out2 = 32;
95 }
96
ki.stfub139e7b2015-09-26 08:17:22 +090097 void On_3_3(int in1,
98 const std::string& in2,
99 bool in3,
100 std::string* out1,
101 int* out2,
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900102 bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900103 DCHECK_EQ(3, in1);
104 DCHECK_EQ("3_3", in2);
105 DCHECK(in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900106 *out1 = "3_3";
107 *out2 = 33;
108 *out3 = false;
109 }
110
ki.stfub139e7b2015-09-26 08:17:22 +0900111 void On_3_4(bool in1,
112 int in2,
113 const std::string& in3,
114 int* out1,
115 bool* out2,
116 std::string* out3,
117 bool* out4) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900118 DCHECK(in1);
119 DCHECK_EQ(3, in2);
120 DCHECK_EQ("3_4", in3);
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900121 *out1 = 34;
122 *out2 = true;
123 *out3 = "3_4";
124 *out4 = false;
125 }
126
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900127 bool Send(IPC::Message* message) {
128 // gets the reply message, stash in global
129 DCHECK(g_reply == NULL);
130 g_reply = message;
131 return true;
132 }
133
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900134 bool OnMessageReceived(const IPC::Message& msg) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900135 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
136 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
137 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
138 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
139 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
140 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
141 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
142 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
143 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
144 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
145 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
146 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
147 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900148 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900149 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
150 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
151 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
152 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
153 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
154 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
155 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
156 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
157 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
158 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
159 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
160 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
hansl@google.com2f8b0302010-11-06 07:06:31 +0900161 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900162 IPC_END_MESSAGE_MAP()
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900163 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900164 }
165
166};
167
168void Send(IPC::SyncMessage* msg) {
169 static TestMessageReceiver receiver;
170
171 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
172 DCHECK(reply_serializer != NULL);
173
174 // "send" the message
175 receiver.OnMessageReceived(*msg);
176 delete msg;
177
178 // get the reply message from the global, and deserialize the output
179 // parameters into the output pointers.
180 DCHECK(g_reply != NULL);
181 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
182 DCHECK(result);
183 delete g_reply;
184 g_reply = NULL;
185 delete reply_serializer;
186}
187
188TEST(IPCSyncMessageTest, Main) {
189 bool bool1 = true;
190 int int1 = 0;
191 std::string string1;
192
193 Send(new Msg_C_0_1(&bool1));
194 DCHECK(!bool1);
195
196 Send(new Msg_C_0_2(&bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900197 DCHECK(bool1);
198 DCHECK_EQ(2, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900199
200 Send(new Msg_C_0_3(&bool1, &int1, &string1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900201 DCHECK(!bool1);
202 DCHECK_EQ(3, int1);
203 DCHECK_EQ("0_3", string1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900204
205 bool1 = false;
206 Send(new Msg_C_1_1(1, &bool1));
207 DCHECK(bool1);
208
209 bool1 = false;
210 Send(new Msg_C_1_2(false, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900211 DCHECK(bool1);
212 DCHECK_EQ(12, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900213
214 bool1 = true;
215 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900216 DCHECK_EQ("1_3", string1);
217 DCHECK_EQ(13, int1);
218 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900219
220 bool1 = false;
221 Send(new Msg_C_2_1(1, false, &bool1));
222 DCHECK(bool1);
223
224 bool1 = false;
225 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900226 DCHECK(bool1);
227 DCHECK_EQ(22, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900228
229 bool1 = true;
230 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900231 DCHECK_EQ("2_3", string1);
232 DCHECK_EQ(23, int1);
233 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900234
235 bool1 = false;
236 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
237 DCHECK(bool1);
238
239 bool1 = false;
240 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900241 DCHECK(bool1);
242 DCHECK_EQ(32, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900243
244 bool1 = true;
245 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900246 DCHECK_EQ("3_3", string1);
247 DCHECK_EQ(33, int1);
248 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900249
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900250 bool1 = false;
251 bool bool2 = true;
252 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900253 DCHECK_EQ(34, int1);
254 DCHECK(bool1);
255 DCHECK_EQ("3_4", string1);
256 DCHECK(!bool2);
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900257
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900258 // Routed messages, just a copy of the above but with extra routing paramater
259 Send(new Msg_R_0_1(0, &bool1));
260 DCHECK(!bool1);
261
262 Send(new Msg_R_0_2(0, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900263 DCHECK(bool1);
264 DCHECK_EQ(2, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900265
266 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900267 DCHECK(!bool1);
268 DCHECK_EQ(3, int1);
269 DCHECK_EQ("0_3", string1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900270
271 bool1 = false;
272 Send(new Msg_R_1_1(0, 1, &bool1));
273 DCHECK(bool1);
274
275 bool1 = false;
276 Send(new Msg_R_1_2(0, false, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900277 DCHECK(bool1);
278 DCHECK_EQ(12, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900279
280 bool1 = true;
281 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900282 DCHECK_EQ("1_3", string1);
283 DCHECK_EQ(13, int1);
284 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900285
286 bool1 = false;
287 Send(new Msg_R_2_1(0, 1, false, &bool1));
288 DCHECK(bool1);
289
290 bool1 = false;
291 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900292 DCHECK(bool1);
293 DCHECK_EQ(22, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900294
295 bool1 = true;
296 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900297 DCHECK(!bool1);
298 DCHECK_EQ("2_3", string1);
299 DCHECK_EQ(23, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900300
301 bool1 = false;
302 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
303 DCHECK(bool1);
304
305 bool1 = false;
306 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900307 DCHECK(bool1);
308 DCHECK_EQ(32, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900309
310 bool1 = true;
311 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900312 DCHECK_EQ("3_3", string1);
313 DCHECK_EQ(33, int1);
314 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900315}
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +0900316
317} // namespace