blob: d170b7fcc5fc28c0d8a233d6429a195940484dbc [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
9#include <string.h>
10
11#include "base/basictypes.h"
12#include "ipc/ipc_message.h"
13#include "ipc/ipc_message_utils.h"
14#include "base/logging.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
jam@chromium.org86a8de12010-12-09 08:34:16 +090017#define IPC_MESSAGE_IMPL
18#include "ipc/ipc_sync_message_unittest.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090019
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +090020namespace {
21
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090022static IPC::Message* g_reply;
23
24class TestMessageReceiver {
25 public:
26
27 void On_0_1(bool* out1) {
28 *out1 = false;
29 }
30
31 void On_0_2(bool* out1, int* out2) {
32 *out1 = true;
33 *out2 = 2;
34 }
35
36 void On_0_3(bool* out1, int* out2, std::string* out3) {
37 *out1 = false;
38 *out2 = 3;
39 *out3 = "0_3";
40 }
41
42 void On_1_1(int in1, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090043 DCHECK_EQ(1, in1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090044 *out1 = true;
45 }
46
47 void On_1_2(bool in1, bool* out1, int* out2) {
48 DCHECK(!in1);
49 *out1 = true;
50 *out2 = 12;
51 }
52
53 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090054 DCHECK_EQ(3, in1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090055 *out1 = "1_3";
56 *out2 = 13;
57 *out3 = false;
58 }
59
60 void On_2_1(int in1, bool in2, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090061 DCHECK_EQ(1, in1);
62 DCHECK(!in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090063 *out1 = true;
64 }
65
66 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090067 DCHECK(!in1);
68 DCHECK_EQ(2, in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090069 *out1 = true;
70 *out2 = 22;
71 }
72
73 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090074 DCHECK_EQ(3, in1);
75 DCHECK(in2);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090076 *out1 = "2_3";
77 *out2 = 23;
78 *out3 = false;
79 }
80
81 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090082 DCHECK_EQ(1, in1);
83 DCHECK(!in2);
84 DCHECK_EQ("3_1", in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090085 *out1 = true;
86 }
87
88 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090089 DCHECK_EQ("3_2", in1);
90 DCHECK(!in2);
91 DCHECK_EQ(2, in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090092 *out1 = true;
93 *out2 = 32;
94 }
95
96 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
97 bool* out3) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +090098 DCHECK_EQ(3, in1);
99 DCHECK_EQ("3_3", in2);
100 DCHECK(in3);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900101 *out1 = "3_3";
102 *out2 = 33;
103 *out3 = false;
104 }
105
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900106 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
107 std::string* out3, bool* out4) {
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900108 DCHECK(in1);
109 DCHECK_EQ(3, in2);
110 DCHECK_EQ("3_4", in3);
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900111 *out1 = 34;
112 *out2 = true;
113 *out3 = "3_4";
114 *out4 = false;
115 }
116
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900117 bool Send(IPC::Message* message) {
118 // gets the reply message, stash in global
119 DCHECK(g_reply == NULL);
120 g_reply = message;
121 return true;
122 }
123
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900124 bool OnMessageReceived(const IPC::Message& msg) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900125 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
126 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
127 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
128 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
129 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
130 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
131 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
132 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
133 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
134 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
135 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
136 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
137 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900138 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900139 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
140 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
141 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
142 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
143 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
144 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
145 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
146 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
147 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
148 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
149 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
150 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
hansl@google.com2f8b0302010-11-06 07:06:31 +0900151 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900152 IPC_END_MESSAGE_MAP()
jam@chromium.org8a2c7842010-12-24 15:19:28 +0900153 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900154 }
155
156};
157
158void Send(IPC::SyncMessage* msg) {
159 static TestMessageReceiver receiver;
160
161 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
162 DCHECK(reply_serializer != NULL);
163
164 // "send" the message
165 receiver.OnMessageReceived(*msg);
166 delete msg;
167
168 // get the reply message from the global, and deserialize the output
169 // parameters into the output pointers.
170 DCHECK(g_reply != NULL);
171 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
172 DCHECK(result);
173 delete g_reply;
174 g_reply = NULL;
175 delete reply_serializer;
176}
177
178TEST(IPCSyncMessageTest, Main) {
179 bool bool1 = true;
180 int int1 = 0;
181 std::string string1;
182
183 Send(new Msg_C_0_1(&bool1));
184 DCHECK(!bool1);
185
186 Send(new Msg_C_0_2(&bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900187 DCHECK(bool1);
188 DCHECK_EQ(2, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900189
190 Send(new Msg_C_0_3(&bool1, &int1, &string1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900191 DCHECK(!bool1);
192 DCHECK_EQ(3, int1);
193 DCHECK_EQ("0_3", string1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900194
195 bool1 = false;
196 Send(new Msg_C_1_1(1, &bool1));
197 DCHECK(bool1);
198
199 bool1 = false;
200 Send(new Msg_C_1_2(false, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900201 DCHECK(bool1);
202 DCHECK_EQ(12, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900203
204 bool1 = true;
205 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900206 DCHECK_EQ("1_3", string1);
207 DCHECK_EQ(13, int1);
208 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900209
210 bool1 = false;
211 Send(new Msg_C_2_1(1, false, &bool1));
212 DCHECK(bool1);
213
214 bool1 = false;
215 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900216 DCHECK(bool1);
217 DCHECK_EQ(22, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900218
219 bool1 = true;
220 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900221 DCHECK_EQ("2_3", string1);
222 DCHECK_EQ(23, int1);
223 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900224
225 bool1 = false;
226 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
227 DCHECK(bool1);
228
229 bool1 = false;
230 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900231 DCHECK(bool1);
232 DCHECK_EQ(32, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900233
234 bool1 = true;
235 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900236 DCHECK_EQ("3_3", string1);
237 DCHECK_EQ(33, int1);
238 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900239
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900240 bool1 = false;
241 bool bool2 = true;
242 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900243 DCHECK_EQ(34, int1);
244 DCHECK(bool1);
245 DCHECK_EQ("3_4", string1);
246 DCHECK(!bool2);
bauerb@chromium.orgcc4e8542010-08-19 23:53:28 +0900247
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900248 // Routed messages, just a copy of the above but with extra routing paramater
249 Send(new Msg_R_0_1(0, &bool1));
250 DCHECK(!bool1);
251
252 Send(new Msg_R_0_2(0, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900253 DCHECK(bool1);
254 DCHECK_EQ(2, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900255
256 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900257 DCHECK(!bool1);
258 DCHECK_EQ(3, int1);
259 DCHECK_EQ("0_3", string1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900260
261 bool1 = false;
262 Send(new Msg_R_1_1(0, 1, &bool1));
263 DCHECK(bool1);
264
265 bool1 = false;
266 Send(new Msg_R_1_2(0, false, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900267 DCHECK(bool1);
268 DCHECK_EQ(12, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900269
270 bool1 = true;
271 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900272 DCHECK_EQ("1_3", string1);
273 DCHECK_EQ(13, int1);
274 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900275
276 bool1 = false;
277 Send(new Msg_R_2_1(0, 1, false, &bool1));
278 DCHECK(bool1);
279
280 bool1 = false;
281 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900282 DCHECK(bool1);
283 DCHECK_EQ(22, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900284
285 bool1 = true;
286 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900287 DCHECK(!bool1);
288 DCHECK_EQ("2_3", string1);
289 DCHECK_EQ(23, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900290
291 bool1 = false;
292 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
293 DCHECK(bool1);
294
295 bool1 = false;
296 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900297 DCHECK(bool1);
298 DCHECK_EQ(32, int1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900299
300 bool1 = true;
301 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
kushi.p@gmail.com90594e32011-04-29 03:20:09 +0900302 DCHECK_EQ("3_3", string1);
303 DCHECK_EQ(33, int1);
304 DCHECK(!bool1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900305}
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +0900306
307} // namespace