blob: 9a72aa3aba95655bbe2ffdd16ee7d6979390492d [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]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) {
41 DCHECK(in1 == 1);
42 *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) {
52 DCHECK(in1 == 3);
53 *out1 = "1_3";
54 *out2 = 13;
55 *out3 = false;
56 }
57
58 void On_2_1(int in1, bool in2, bool* out1) {
59 DCHECK(in1 == 1 && !in2);
60 *out1 = true;
61 }
62
63 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
64 DCHECK(!in1 && in2 == 2);
65 *out1 = true;
66 *out2 = 22;
67 }
68
69 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
70 DCHECK(in1 == 3 && in2);
71 *out1 = "2_3";
72 *out2 = 23;
73 *out3 = false;
74 }
75
76 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
77 DCHECK(in1 == 1 && !in2 && in3 == "3_1");
78 *out1 = true;
79 }
80
81 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
82 DCHECK(in1 == "3_2" && !in2 && in3 == 2);
83 *out1 = true;
84 *out2 = 32;
85 }
86
[email protected]d3216442009-03-05 21:07:2787 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
88 bool* out3) {
initial.commit09911bf2008-07-26 23:55:2989 DCHECK(in1 == 3 && in2 == "3_3" && in3);
90 *out1 = "3_3";
91 *out2 = 33;
92 *out3 = false;
93 }
94
[email protected]55126132010-08-19 14:53:2895 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
96 std::string* out3, bool* out4) {
97 DCHECK(in1 && in2 == 3 && in3 == "3_4");
98 *out1 = 34;
99 *out2 = true;
100 *out3 = "3_4";
101 *out4 = false;
102 }
103
initial.commit09911bf2008-07-26 23:55:29104 bool Send(IPC::Message* message) {
105 // gets the reply message, stash in global
106 DCHECK(g_reply == NULL);
107 g_reply = message;
108 return true;
109 }
110
[email protected]a95986a82010-12-24 06:19:28111 bool OnMessageReceived(const IPC::Message& msg) {
initial.commit09911bf2008-07-26 23:55:29112 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
113 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
114 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
115 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
116 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
117 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
118 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
119 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
120 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
121 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
122 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
123 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
124 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
[email protected]55126132010-08-19 14:53:28125 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29126 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
127 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
128 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
129 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
130 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
131 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
132 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
133 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
134 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
135 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
136 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
137 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
[email protected]751bf4b2010-11-05 22:06:31138 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
initial.commit09911bf2008-07-26 23:55:29139 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28140 return true;
initial.commit09911bf2008-07-26 23:55:29141 }
142
143};
144
145void Send(IPC::SyncMessage* msg) {
146 static TestMessageReceiver receiver;
147
148 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
149 DCHECK(reply_serializer != NULL);
150
151 // "send" the message
152 receiver.OnMessageReceived(*msg);
153 delete msg;
154
155 // get the reply message from the global, and deserialize the output
156 // parameters into the output pointers.
157 DCHECK(g_reply != NULL);
158 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
159 DCHECK(result);
160 delete g_reply;
161 g_reply = NULL;
162 delete reply_serializer;
163}
164
165TEST(IPCSyncMessageTest, Main) {
166 bool bool1 = true;
167 int int1 = 0;
168 std::string string1;
169
170 Send(new Msg_C_0_1(&bool1));
171 DCHECK(!bool1);
172
173 Send(new Msg_C_0_2(&bool1, &int1));
174 DCHECK(bool1 && int1 == 2);
175
176 Send(new Msg_C_0_3(&bool1, &int1, &string1));
177 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
178
179 bool1 = false;
180 Send(new Msg_C_1_1(1, &bool1));
181 DCHECK(bool1);
182
183 bool1 = false;
184 Send(new Msg_C_1_2(false, &bool1, &int1));
185 DCHECK(bool1 && int1 == 12);
186
187 bool1 = true;
188 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
189 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
190
191 bool1 = false;
192 Send(new Msg_C_2_1(1, false, &bool1));
193 DCHECK(bool1);
194
195 bool1 = false;
196 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
197 DCHECK(bool1 && int1 == 22);
198
199 bool1 = true;
200 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
201 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
202
203 bool1 = false;
204 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
205 DCHECK(bool1);
206
207 bool1 = false;
208 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
209 DCHECK(bool1 && int1 == 32);
210
211 bool1 = true;
212 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
213 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
214
[email protected]55126132010-08-19 14:53:28215 bool1 = false;
216 bool bool2 = true;
217 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
218 DCHECK(int1 == 34 && bool1 && string1 == "3_4" && !bool2);
219
initial.commit09911bf2008-07-26 23:55:29220 // Routed messages, just a copy of the above but with extra routing paramater
221 Send(new Msg_R_0_1(0, &bool1));
222 DCHECK(!bool1);
223
224 Send(new Msg_R_0_2(0, &bool1, &int1));
225 DCHECK(bool1 && int1 == 2);
226
227 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
228 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
229
230 bool1 = false;
231 Send(new Msg_R_1_1(0, 1, &bool1));
232 DCHECK(bool1);
233
234 bool1 = false;
235 Send(new Msg_R_1_2(0, false, &bool1, &int1));
236 DCHECK(bool1 && int1 == 12);
237
238 bool1 = true;
239 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
240 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
241
242 bool1 = false;
243 Send(new Msg_R_2_1(0, 1, false, &bool1));
244 DCHECK(bool1);
245
246 bool1 = false;
247 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
248 DCHECK(bool1 && int1 == 22);
249
250 bool1 = true;
251 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
252 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
253
254 bool1 = false;
255 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
256 DCHECK(bool1);
257
258 bool1 = false;
259 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
260 DCHECK(bool1 && int1 == 32);
261
262 bool1 = true;
263 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
264 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
265}