1 // Copyright 2011 The Chromium Authors
2 // 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 "base/check_op.h"
10 #include "ipc/ipc_message.h"
11 #include "ipc/ipc_message_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 #define IPC_MESSAGE_IMPL
15 #include "ipc/ipc_sync_message_unittest.h"
16
17 namespace {
18
19 static IPC::Message* g_reply;
20
21 class TestMessageReceiver {
22 public:
23
On_0_1(bool * out1)24 void On_0_1(bool* out1) {
25 *out1 = false;
26 }
27
On_0_2(bool * out1,int * out2)28 void On_0_2(bool* out1, int* out2) {
29 *out1 = true;
30 *out2 = 2;
31 }
32
On_0_3(bool * out1,int * out2,std::string * out3)33 void On_0_3(bool* out1, int* out2, std::string* out3) {
34 *out1 = false;
35 *out2 = 3;
36 *out3 = "0_3";
37 }
38
On_1_1(int in1,bool * out1)39 void On_1_1(int in1, bool* out1) {
40 DCHECK_EQ(1, in1);
41 *out1 = true;
42 }
43
On_1_2(bool in1,bool * out1,int * out2)44 void On_1_2(bool in1, bool* out1, int* out2) {
45 DCHECK(!in1);
46 *out1 = true;
47 *out2 = 12;
48 }
49
On_1_3(int in1,std::string * out1,int * out2,bool * out3)50 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
51 DCHECK_EQ(3, in1);
52 *out1 = "1_3";
53 *out2 = 13;
54 *out3 = false;
55 }
56
On_2_1(int in1,bool in2,bool * out1)57 void On_2_1(int in1, bool in2, bool* out1) {
58 DCHECK_EQ(1, in1);
59 DCHECK(!in2);
60 *out1 = true;
61 }
62
On_2_2(bool in1,int in2,bool * out1,int * out2)63 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
64 DCHECK(!in1);
65 DCHECK_EQ(2, in2);
66 *out1 = true;
67 *out2 = 22;
68 }
69
On_2_3(int in1,bool in2,std::string * out1,int * out2,bool * out3)70 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
71 DCHECK_EQ(3, in1);
72 DCHECK(in2);
73 *out1 = "2_3";
74 *out2 = 23;
75 *out3 = false;
76 }
77
On_3_1(int in1,bool in2,const std::string & in3,bool * out1)78 void On_3_1(int in1, bool in2, const std::string& in3, bool* out1) {
79 DCHECK_EQ(1, in1);
80 DCHECK(!in2);
81 DCHECK_EQ("3_1", in3);
82 *out1 = true;
83 }
84
On_3_2(const std::string & in1,bool in2,int in3,bool * out1,int * out2)85 void On_3_2(const std::string& in1,
86 bool in2,
87 int in3,
88 bool* out1,
89 int* out2) {
90 DCHECK_EQ("3_2", in1);
91 DCHECK(!in2);
92 DCHECK_EQ(2, in3);
93 *out1 = true;
94 *out2 = 32;
95 }
96
On_3_3(int in1,const std::string & in2,bool in3,std::string * out1,int * out2,bool * out3)97 void On_3_3(int in1,
98 const std::string& in2,
99 bool in3,
100 std::string* out1,
101 int* out2,
102 bool* out3) {
103 DCHECK_EQ(3, in1);
104 DCHECK_EQ("3_3", in2);
105 DCHECK(in3);
106 *out1 = "3_3";
107 *out2 = 33;
108 *out3 = false;
109 }
110
On_3_4(bool in1,int in2,const std::string & in3,int * out1,bool * out2,std::string * out3,bool * out4)111 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) {
118 DCHECK(in1);
119 DCHECK_EQ(3, in2);
120 DCHECK_EQ("3_4", in3);
121 *out1 = 34;
122 *out2 = true;
123 *out3 = "3_4";
124 *out4 = false;
125 }
126
Send(IPC::Message * message)127 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
OnMessageReceived(const IPC::Message & msg)134 bool OnMessageReceived(const IPC::Message& msg) {
135 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)
148 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
149 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)
161 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
162 IPC_END_MESSAGE_MAP()
163 return true;
164 }
165
166 };
167
Send(IPC::SyncMessage * msg)168 void Send(IPC::SyncMessage* msg) {
169 static TestMessageReceiver receiver;
170
171 std::unique_ptr<IPC::MessageReplyDeserializer> reply_serializer =
172 msg->TakeReplyDeserializer();
173 DCHECK(reply_serializer);
174
175 // "send" the message
176 receiver.OnMessageReceived(*msg);
177 delete msg;
178
179 // get the reply message from the global, and deserialize the output
180 // parameters into the output pointers.
181 DCHECK(g_reply != NULL);
182 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
183 DCHECK(result);
184 delete g_reply;
185 g_reply = NULL;
186 }
187
TEST(IPCSyncMessageTest,Main)188 TEST(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));
197 DCHECK(bool1);
198 DCHECK_EQ(2, int1);
199
200 Send(new Msg_C_0_3(&bool1, &int1, &string1));
201 DCHECK(!bool1);
202 DCHECK_EQ(3, int1);
203 DCHECK_EQ("0_3", string1);
204
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));
211 DCHECK(bool1);
212 DCHECK_EQ(12, int1);
213
214 bool1 = true;
215 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
216 DCHECK_EQ("1_3", string1);
217 DCHECK_EQ(13, int1);
218 DCHECK(!bool1);
219
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));
226 DCHECK(bool1);
227 DCHECK_EQ(22, int1);
228
229 bool1 = true;
230 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
231 DCHECK_EQ("2_3", string1);
232 DCHECK_EQ(23, int1);
233 DCHECK(!bool1);
234
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));
241 DCHECK(bool1);
242 DCHECK_EQ(32, int1);
243
244 bool1 = true;
245 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
246 DCHECK_EQ("3_3", string1);
247 DCHECK_EQ(33, int1);
248 DCHECK(!bool1);
249
250 bool1 = false;
251 bool bool2 = true;
252 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
253 DCHECK_EQ(34, int1);
254 DCHECK(bool1);
255 DCHECK_EQ("3_4", string1);
256 DCHECK(!bool2);
257
258 // 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));
263 DCHECK(bool1);
264 DCHECK_EQ(2, int1);
265
266 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
267 DCHECK(!bool1);
268 DCHECK_EQ(3, int1);
269 DCHECK_EQ("0_3", string1);
270
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));
277 DCHECK(bool1);
278 DCHECK_EQ(12, int1);
279
280 bool1 = true;
281 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
282 DCHECK_EQ("1_3", string1);
283 DCHECK_EQ(13, int1);
284 DCHECK(!bool1);
285
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));
292 DCHECK(bool1);
293 DCHECK_EQ(22, int1);
294
295 bool1 = true;
296 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
297 DCHECK(!bool1);
298 DCHECK_EQ("2_3", string1);
299 DCHECK_EQ(23, int1);
300
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));
307 DCHECK(bool1);
308 DCHECK_EQ(32, int1);
309
310 bool1 = true;
311 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
312 DCHECK_EQ("3_3", string1);
313 DCHECK_EQ(33, int1);
314 DCHECK(!bool1);
315 }
316
317 } // namespace
318