blob: 7b513f73db0e865b64404451b7524f5fa167e3a2 [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
9#include <string.h>
10
11#include "base/basictypes.h"
[email protected]946d1b22009-07-22 23:57:2112#include "ipc/ipc_message.h"
13#include "ipc/ipc_message_utils.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]21fa3a12010-12-08 23:34:1617#define IPC_MESSAGE_IMPL
18#include "ipc/ipc_sync_message_unittest.h"
initial.commit09911bf2008-07-26 23:55:2919
20static IPC::Message* g_reply;
21
22class TestMessageReceiver {
23 public:
24
25 void On_0_1(bool* out1) {
26 *out1 = false;
27 }
28
29 void On_0_2(bool* out1, int* out2) {
30 *out1 = true;
31 *out2 = 2;
32 }
33
34 void On_0_3(bool* out1, int* out2, std::string* out3) {
35 *out1 = false;
36 *out2 = 3;
37 *out3 = "0_3";
38 }
39
40 void On_1_1(int in1, bool* out1) {
[email protected]5e0be642011-04-28 18:20:0941 DCHECK_EQ(1, in1);
initial.commit09911bf2008-07-26 23:55:2942 *out1 = true;
43 }
44
45 void On_1_2(bool in1, bool* out1, int* out2) {
46 DCHECK(!in1);
47 *out1 = true;
48 *out2 = 12;
49 }
50
51 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
[email protected]5e0be642011-04-28 18:20:0952 DCHECK_EQ(3, in1);
initial.commit09911bf2008-07-26 23:55:2953 *out1 = "1_3";
54 *out2 = 13;
55 *out3 = false;
56 }
57
58 void On_2_1(int in1, bool in2, bool* out1) {
[email protected]5e0be642011-04-28 18:20:0959 DCHECK_EQ(1, in1);
60 DCHECK(!in2);
initial.commit09911bf2008-07-26 23:55:2961 *out1 = true;
62 }
63
64 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
[email protected]5e0be642011-04-28 18:20:0965 DCHECK(!in1);
66 DCHECK_EQ(2, in2);
initial.commit09911bf2008-07-26 23:55:2967 *out1 = true;
68 *out2 = 22;
69 }
70
71 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
[email protected]5e0be642011-04-28 18:20:0972 DCHECK_EQ(3, in1);
73 DCHECK(in2);
initial.commit09911bf2008-07-26 23:55:2974 *out1 = "2_3";
75 *out2 = 23;
76 *out3 = false;
77 }
78
79 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
[email protected]5e0be642011-04-28 18:20:0980 DCHECK_EQ(1, in1);
81 DCHECK(!in2);
82 DCHECK_EQ("3_1", in3);
initial.commit09911bf2008-07-26 23:55:2983 *out1 = true;
84 }
85
86 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
[email protected]5e0be642011-04-28 18:20:0987 DCHECK_EQ("3_2", in1);
88 DCHECK(!in2);
89 DCHECK_EQ(2, in3);
initial.commit09911bf2008-07-26 23:55:2990 *out1 = true;
91 *out2 = 32;
92 }
93
[email protected]d3216442009-03-05 21:07:2794 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
95 bool* out3) {
[email protected]5e0be642011-04-28 18:20:0996 DCHECK_EQ(3, in1);
97 DCHECK_EQ("3_3", in2);
98 DCHECK(in3);
initial.commit09911bf2008-07-26 23:55:2999 *out1 = "3_3";
100 *out2 = 33;
101 *out3 = false;
102 }
103
[email protected]55126132010-08-19 14:53:28104 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
105 std::string* out3, bool* out4) {
[email protected]5e0be642011-04-28 18:20:09106 DCHECK(in1);
107 DCHECK_EQ(3, in2);
108 DCHECK_EQ("3_4", in3);
[email protected]55126132010-08-19 14:53:28109 *out1 = 34;
110 *out2 = true;
111 *out3 = "3_4";
112 *out4 = false;
113 }
114
initial.commit09911bf2008-07-26 23:55:29115 bool Send(IPC::Message* message) {
116 // gets the reply message, stash in global
117 DCHECK(g_reply == NULL);
118 g_reply = message;
119 return true;
120 }
121
[email protected]a95986a82010-12-24 06:19:28122 bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29123 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
124 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
125 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
126 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
127 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
128 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
129 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
130 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
131 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
132 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
133 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
134 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
135 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
[email protected]55126132010-08-19 14:53:28136 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29137 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
138 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
139 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
140 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
141 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
142 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
143 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
144 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
145 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
146 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
147 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
148 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
[email protected]751bf4b2010-11-05 22:06:31149 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29150 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28151 return true;
initial.commit09911bf2008-07-26 23:55:29152 }
153
154};
155
156void Send(IPC::SyncMessage* msg) {
157 static TestMessageReceiver receiver;
158
159 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
160 DCHECK(reply_serializer != NULL);
161
162 // "send" the message
163 receiver.OnMessageReceived(*msg);
164 delete msg;
165
166 // get the reply message from the global, and deserialize the output
167 // parameters into the output pointers.
168 DCHECK(g_reply != NULL);
169 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
170 DCHECK(result);
171 delete g_reply;
172 g_reply = NULL;
173 delete reply_serializer;
174}
175
176TEST(IPCSyncMessageTest, Main) {
177 bool bool1 = true;
178 int int1 = 0;
179 std::string string1;
180
181 Send(new Msg_C_0_1(&bool1));
182 DCHECK(!bool1);
183
184 Send(new Msg_C_0_2(&bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09185 DCHECK(bool1);
186 DCHECK_EQ(2, int1);
initial.commit09911bf2008-07-26 23:55:29187
188 Send(new Msg_C_0_3(&bool1, &int1, &string1));
[email protected]5e0be642011-04-28 18:20:09189 DCHECK(!bool1);
190 DCHECK_EQ(3, int1);
191 DCHECK_EQ("0_3", string1);
initial.commit09911bf2008-07-26 23:55:29192
193 bool1 = false;
194 Send(new Msg_C_1_1(1, &bool1));
195 DCHECK(bool1);
196
197 bool1 = false;
198 Send(new Msg_C_1_2(false, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09199 DCHECK(bool1);
200 DCHECK_EQ(12, int1);
initial.commit09911bf2008-07-26 23:55:29201
202 bool1 = true;
203 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09204 DCHECK_EQ("1_3", string1);
205 DCHECK_EQ(13, int1);
206 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29207
208 bool1 = false;
209 Send(new Msg_C_2_1(1, false, &bool1));
210 DCHECK(bool1);
211
212 bool1 = false;
213 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09214 DCHECK(bool1);
215 DCHECK_EQ(22, int1);
initial.commit09911bf2008-07-26 23:55:29216
217 bool1 = true;
218 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09219 DCHECK_EQ("2_3", string1);
220 DCHECK_EQ(23, int1);
221 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29222
223 bool1 = false;
224 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
225 DCHECK(bool1);
226
227 bool1 = false;
228 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09229 DCHECK(bool1);
230 DCHECK_EQ(32, int1);
initial.commit09911bf2008-07-26 23:55:29231
232 bool1 = true;
233 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09234 DCHECK_EQ("3_3", string1);
235 DCHECK_EQ(33, int1);
236 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29237
[email protected]55126132010-08-19 14:53:28238 bool1 = false;
239 bool bool2 = true;
240 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
[email protected]5e0be642011-04-28 18:20:09241 DCHECK_EQ(34, int1);
242 DCHECK(bool1);
243 DCHECK_EQ("3_4", string1);
244 DCHECK(!bool2);
[email protected]55126132010-08-19 14:53:28245
initial.commit09911bf2008-07-26 23:55:29246 // Routed messages, just a copy of the above but with extra routing paramater
247 Send(new Msg_R_0_1(0, &bool1));
248 DCHECK(!bool1);
249
250 Send(new Msg_R_0_2(0, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09251 DCHECK(bool1);
252 DCHECK_EQ(2, int1);
initial.commit09911bf2008-07-26 23:55:29253
254 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
[email protected]5e0be642011-04-28 18:20:09255 DCHECK(!bool1);
256 DCHECK_EQ(3, int1);
257 DCHECK_EQ("0_3", string1);
initial.commit09911bf2008-07-26 23:55:29258
259 bool1 = false;
260 Send(new Msg_R_1_1(0, 1, &bool1));
261 DCHECK(bool1);
262
263 bool1 = false;
264 Send(new Msg_R_1_2(0, false, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09265 DCHECK(bool1);
266 DCHECK_EQ(12, int1);
initial.commit09911bf2008-07-26 23:55:29267
268 bool1 = true;
269 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09270 DCHECK_EQ("1_3", string1);
271 DCHECK_EQ(13, int1);
272 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29273
274 bool1 = false;
275 Send(new Msg_R_2_1(0, 1, false, &bool1));
276 DCHECK(bool1);
277
278 bool1 = false;
279 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09280 DCHECK(bool1);
281 DCHECK_EQ(22, int1);
initial.commit09911bf2008-07-26 23:55:29282
283 bool1 = true;
284 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09285 DCHECK(!bool1);
286 DCHECK_EQ("2_3", string1);
287 DCHECK_EQ(23, int1);
initial.commit09911bf2008-07-26 23:55:29288
289 bool1 = false;
290 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
291 DCHECK(bool1);
292
293 bool1 = false;
294 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
[email protected]5e0be642011-04-28 18:20:09295 DCHECK(bool1);
296 DCHECK_EQ(32, int1);
initial.commit09911bf2008-07-26 23:55:29297
298 bool1 = true;
299 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
[email protected]5e0be642011-04-28 18:20:09300 DCHECK_EQ("3_3", string1);
301 DCHECK_EQ(33, int1);
302 DCHECK(!bool1);
initial.commit09911bf2008-07-26 23:55:29303}