blob: 506f4c034b265e1977ed938c1db3361b67184b50 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]82e5ee82009-04-03 02:29:4517
[email protected]946d1b22009-07-22 23:57:2118#define MESSAGES_INTERNAL_FILE "ipc/ipc_sync_message_unittest.h"
19#include "ipc/ipc_message_macros.h"
initial.commit09911bf2008-07-26 23:55:2920
21static IPC::Message* g_reply;
22
23class TestMessageReceiver {
24 public:
25
26 void On_0_1(bool* out1) {
27 *out1 = false;
28 }
29
30 void On_0_2(bool* out1, int* out2) {
31 *out1 = true;
32 *out2 = 2;
33 }
34
35 void On_0_3(bool* out1, int* out2, std::string* out3) {
36 *out1 = false;
37 *out2 = 3;
38 *out3 = "0_3";
39 }
40
41 void On_1_1(int in1, bool* out1) {
42 DCHECK(in1 == 1);
43 *out1 = true;
44 }
45
46 void On_1_2(bool in1, bool* out1, int* out2) {
47 DCHECK(!in1);
48 *out1 = true;
49 *out2 = 12;
50 }
51
52 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
53 DCHECK(in1 == 3);
54 *out1 = "1_3";
55 *out2 = 13;
56 *out3 = false;
57 }
58
59 void On_2_1(int in1, bool in2, bool* out1) {
60 DCHECK(in1 == 1 && !in2);
61 *out1 = true;
62 }
63
64 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
65 DCHECK(!in1 && in2 == 2);
66 *out1 = true;
67 *out2 = 22;
68 }
69
70 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
71 DCHECK(in1 == 3 && in2);
72 *out1 = "2_3";
73 *out2 = 23;
74 *out3 = false;
75 }
76
77 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
78 DCHECK(in1 == 1 && !in2 && in3 == "3_1");
79 *out1 = true;
80 }
81
82 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
83 DCHECK(in1 == "3_2" && !in2 && in3 == 2);
84 *out1 = true;
85 *out2 = 32;
86 }
87
[email protected]d3216442009-03-05 21:07:2788 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
89 bool* out3) {
initial.commit09911bf2008-07-26 23:55:2990 DCHECK(in1 == 3 && in2 == "3_3" && in3);
91 *out1 = "3_3";
92 *out2 = 33;
93 *out3 = false;
94 }
95
[email protected]55126132010-08-19 14:53:2896 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
97 std::string* out3, bool* out4) {
98 DCHECK(in1 && in2 == 3 && in3 == "3_4");
99 *out1 = 34;
100 *out2 = true;
101 *out3 = "3_4";
102 *out4 = false;
103 }
104
initial.commit09911bf2008-07-26 23:55:29105 bool Send(IPC::Message* message) {
106 // gets the reply message, stash in global
107 DCHECK(g_reply == NULL);
108 g_reply = message;
109 return true;
110 }
111
112 void OnMessageReceived(const IPC::Message& msg) {
113 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
114 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
115 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
116 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
117 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
118 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
119 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
120 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
121 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
122 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
123 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
124 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
125 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
[email protected]55126132010-08-19 14:53:28126 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29127 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
128 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
129 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
130 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
131 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
132 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
133 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
134 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
135 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
136 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
137 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
138 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
139 IPC_END_MESSAGE_MAP()
140 }
141
142};
143
144void Send(IPC::SyncMessage* msg) {
145 static TestMessageReceiver receiver;
146
147 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
148 DCHECK(reply_serializer != NULL);
149
150 // "send" the message
151 receiver.OnMessageReceived(*msg);
152 delete msg;
153
154 // get the reply message from the global, and deserialize the output
155 // parameters into the output pointers.
156 DCHECK(g_reply != NULL);
157 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
158 DCHECK(result);
159 delete g_reply;
160 g_reply = NULL;
161 delete reply_serializer;
162}
163
164TEST(IPCSyncMessageTest, Main) {
165 bool bool1 = true;
166 int int1 = 0;
167 std::string string1;
168
169 Send(new Msg_C_0_1(&bool1));
170 DCHECK(!bool1);
171
172 Send(new Msg_C_0_2(&bool1, &int1));
173 DCHECK(bool1 && int1 == 2);
174
175 Send(new Msg_C_0_3(&bool1, &int1, &string1));
176 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
177
178 bool1 = false;
179 Send(new Msg_C_1_1(1, &bool1));
180 DCHECK(bool1);
181
182 bool1 = false;
183 Send(new Msg_C_1_2(false, &bool1, &int1));
184 DCHECK(bool1 && int1 == 12);
185
186 bool1 = true;
187 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
188 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
189
190 bool1 = false;
191 Send(new Msg_C_2_1(1, false, &bool1));
192 DCHECK(bool1);
193
194 bool1 = false;
195 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
196 DCHECK(bool1 && int1 == 22);
197
198 bool1 = true;
199 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
200 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
201
202 bool1 = false;
203 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
204 DCHECK(bool1);
205
206 bool1 = false;
207 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
208 DCHECK(bool1 && int1 == 32);
209
210 bool1 = true;
211 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
212 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
213
[email protected]55126132010-08-19 14:53:28214 bool1 = false;
215 bool bool2 = true;
216 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
217 DCHECK(int1 == 34 && bool1 && string1 == "3_4" && !bool2);
218
initial.commit09911bf2008-07-26 23:55:29219 // Routed messages, just a copy of the above but with extra routing paramater
220 Send(new Msg_R_0_1(0, &bool1));
221 DCHECK(!bool1);
222
223 Send(new Msg_R_0_2(0, &bool1, &int1));
224 DCHECK(bool1 && int1 == 2);
225
226 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
227 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
228
229 bool1 = false;
230 Send(new Msg_R_1_1(0, 1, &bool1));
231 DCHECK(bool1);
232
233 bool1 = false;
234 Send(new Msg_R_1_2(0, false, &bool1, &int1));
235 DCHECK(bool1 && int1 == 12);
236
237 bool1 = true;
238 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
239 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
240
241 bool1 = false;
242 Send(new Msg_R_2_1(0, 1, false, &bool1));
243 DCHECK(bool1);
244
245 bool1 = false;
246 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
247 DCHECK(bool1 && int1 == 22);
248
249 bool1 = true;
250 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
251 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
252
253 bool1 = false;
254 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
255 DCHECK(bool1);
256
257 bool1 = false;
258 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
259 DCHECK(bool1 && int1 == 32);
260
261 bool1 = true;
262 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
263 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
264}