blob: c8edb0c50d973520338bc67f29b4eb42ca6521aa [file] [log] [blame]
[email protected]5e0be642011-04-28 18:20:091// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294//
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
tfarina4da82752015-09-16 09:56:219#include "base/logging.h"
[email protected]946d1b22009-07-22 23:57:2110#include "ipc/ipc_message.h"
11#include "ipc/ipc_message_utils.h"
initial.commit09911bf2008-07-26 23:55:2912#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]21fa3a12010-12-08 23:34:1614#define IPC_MESSAGE_IMPL
15#include "ipc/ipc_sync_message_unittest.h"
initial.commit09911bf2008-07-26 23:55:2916
[email protected]2a3aa7b52013-01-11 20:56:2217namespace {
18
initial.commit09911bf2008-07-26 23:55:2919static 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) {
[email protected]5e0be642011-04-28 18:20:0940 DCHECK_EQ(1, in1);
initial.commit09911bf2008-07-26 23:55:2941 *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) {
[email protected]5e0be642011-04-28 18:20:0951 DCHECK_EQ(3, in1);
initial.commit09911bf2008-07-26 23:55:2952 *out1 = "1_3";
53 *out2 = 13;
54 *out3 = false;
55 }
56
57 void On_2_1(int in1, bool in2, bool* out1) {
[email protected]5e0be642011-04-28 18:20:0958 DCHECK_EQ(1, in1);
59 DCHECK(!in2);
initial.commit09911bf2008-07-26 23:55:2960 *out1 = true;
61 }
62
63 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
[email protected]5e0be642011-04-28 18:20:0964 DCHECK(!in1);
65 DCHECK_EQ(2, in2);
initial.commit09911bf2008-07-26 23:55:2966 *out1 = true;
67 *out2 = 22;
68 }
69
70 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
[email protected]5e0be642011-04-28 18:20:0971 DCHECK_EQ(3, in1);
72 DCHECK(in2);
initial.commit09911bf2008-07-26 23:55:2973 *out1 = "2_3";
74 *out2 = 23;
75 *out3 = false;
76 }
77
ki.stfu5e7a0f72015-09-25 23:17:2278 void On_3_1(int in1, bool in2, const std::string& in3, bool* out1) {
[email protected]5e0be642011-04-28 18:20:0979 DCHECK_EQ(1, in1);
80 DCHECK(!in2);
81 DCHECK_EQ("3_1", in3);
initial.commit09911bf2008-07-26 23:55:2982 *out1 = true;
83 }
84
ki.stfu5e7a0f72015-09-25 23:17:2285 void On_3_2(const std::string& in1,
86 bool in2,
87 int in3,
88 bool* out1,
89 int* out2) {
[email protected]5e0be642011-04-28 18:20:0990 DCHECK_EQ("3_2", in1);
91 DCHECK(!in2);
92 DCHECK_EQ(2, in3);
initial.commit09911bf2008-07-26 23:55:2993 *out1 = true;
94 *out2 = 32;
95 }
96
ki.stfu5e7a0f72015-09-25 23:17:2297 void On_3_3(int in1,
98 const std::string& in2,
99 bool in3,
100 std::string* out1,
101 int* out2,
[email protected]d3216442009-03-05 21:07:27102 bool* out3) {
[email protected]5e0be642011-04-28 18:20:09103 DCHECK_EQ(3, in1);
104 DCHECK_EQ("3_3", in2);
105 DCHECK(in3);
initial.commit09911bf2008-07-26 23:55:29106 *out1 = "3_3";
107 *out2 = 33;
108 *out3 = false;
109 }
110
ki.stfu5e7a0f72015-09-25 23:17:22111 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) {
[email protected]5e0be642011-04-28 18:20:09118 DCHECK(in1);
119 DCHECK_EQ(3, in2);
120 DCHECK_EQ("3_4", in3);
[email protected]55126132010-08-19 14:53:28121 *out1 = 34;
122 *out2 = true;
123 *out3 = "3_4";
124 *out4 = false;
125 }
126
initial.commit09911bf2008-07-26 23:55:29127 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
[email protected]a95986a82010-12-24 06:19:28134 bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29135 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)
[email protected]55126132010-08-19 14:53:28148 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29149 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)
[email protected]751bf4b2010-11-05 22:06:31161 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29162 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28163 return true;
initial.commit09911bf2008-07-26 23:55:29164 }
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));
[email protected]5e0be642011-04-28 18:20:09197 DCHECK(bool1);
198 DCHECK_EQ(2, int1);
initial.commit09911bf2008-07-26 23:55:29199
200 Send(new Msg_C_0_3(&bool1, &int1, &string1));
[email protected]5e0be642011-04-28 18:20:09201 DCHECK(!bool1);
202 DCHECK_EQ(3, int1);
203 DCHECK_EQ("0_3", string1);
initial.commit09911bf2008-07-26 23:55:29204
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));
[email protected]5e0be642011-04-28 18:20:09211 DCHECK(bool1);
212 DCHECK_EQ(12, int1);
initial.commit09911bf2008-07-26 23:55:29213
214 bool1 = true;
215 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09216 DCHECK_EQ("1_3", string1);
217 DCHECK_EQ(13, int1);
218 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29219
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));
[email protected]5e0be642011-04-28 18:20:09226 DCHECK(bool1);
227 DCHECK_EQ(22, int1);
initial.commit09911bf2008-07-26 23:55:29228
229 bool1 = true;
230 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09231 DCHECK_EQ("2_3", string1);
232 DCHECK_EQ(23, int1);
233 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29234
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));
[email protected]5e0be642011-04-28 18:20:09241 DCHECK(bool1);
242 DCHECK_EQ(32, int1);
initial.commit09911bf2008-07-26 23:55:29243
244 bool1 = true;
245 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09246 DCHECK_EQ("3_3", string1);
247 DCHECK_EQ(33, int1);
248 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29249
[email protected]55126132010-08-19 14:53:28250 bool1 = false;
251 bool bool2 = true;
252 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
[email protected]5e0be642011-04-28 18:20:09253 DCHECK_EQ(34, int1);
254 DCHECK(bool1);
255 DCHECK_EQ("3_4", string1);
256 DCHECK(!bool2);
[email protected]55126132010-08-19 14:53:28257
initial.commit09911bf2008-07-26 23:55:29258 // 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));
[email protected]5e0be642011-04-28 18:20:09263 DCHECK(bool1);
264 DCHECK_EQ(2, int1);
initial.commit09911bf2008-07-26 23:55:29265
266 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
[email protected]5e0be642011-04-28 18:20:09267 DCHECK(!bool1);
268 DCHECK_EQ(3, int1);
269 DCHECK_EQ("0_3", string1);
initial.commit09911bf2008-07-26 23:55:29270
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));
[email protected]5e0be642011-04-28 18:20:09277 DCHECK(bool1);
278 DCHECK_EQ(12, int1);
initial.commit09911bf2008-07-26 23:55:29279
280 bool1 = true;
281 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09282 DCHECK_EQ("1_3", string1);
283 DCHECK_EQ(13, int1);
284 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29285
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));
[email protected]5e0be642011-04-28 18:20:09292 DCHECK(bool1);
293 DCHECK_EQ(22, int1);
initial.commit09911bf2008-07-26 23:55:29294
295 bool1 = true;
296 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09297 DCHECK(!bool1);
298 DCHECK_EQ("2_3", string1);
299 DCHECK_EQ(23, int1);
initial.commit09911bf2008-07-26 23:55:29300
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));
[email protected]5e0be642011-04-28 18:20:09307 DCHECK(bool1);
308 DCHECK_EQ(32, int1);
initial.commit09911bf2008-07-26 23:55:29309
310 bool1 = true;
311 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09312 DCHECK_EQ("3_3", string1);
313 DCHECK_EQ(33, int1);
314 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29315}
[email protected]2a3aa7b52013-01-11 20:56:22316
317} // namespace