Update V8 to version 4.7.57.
[chromium-blink-merge.git] / ipc / ipc_sync_message_unittest.cc
blob2e3e3240418ae433cd64a477ff35534107489d22
1 // Copyright (c) 2011 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.
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.
9 #include "base/logging.h"
10 #include "ipc/ipc_message.h"
11 #include "ipc/ipc_message_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #define IPC_MESSAGE_IMPL
15 #include "ipc/ipc_sync_message_unittest.h"
17 namespace {
19 static IPC::Message* g_reply;
21 class TestMessageReceiver {
22 public:
24 void On_0_1(bool* out1) {
25 *out1 = false;
28 void On_0_2(bool* out1, int* out2) {
29 *out1 = true;
30 *out2 = 2;
33 void On_0_3(bool* out1, int* out2, std::string* out3) {
34 *out1 = false;
35 *out2 = 3;
36 *out3 = "0_3";
39 void On_1_1(int in1, bool* out1) {
40 DCHECK_EQ(1, in1);
41 *out1 = true;
44 void On_1_2(bool in1, bool* out1, int* out2) {
45 DCHECK(!in1);
46 *out1 = true;
47 *out2 = 12;
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;
57 void On_2_1(int in1, bool in2, bool* out1) {
58 DCHECK_EQ(1, in1);
59 DCHECK(!in2);
60 *out1 = true;
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;
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;
78 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
79 DCHECK_EQ(1, in1);
80 DCHECK(!in2);
81 DCHECK_EQ("3_1", in3);
82 *out1 = true;
85 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
86 DCHECK_EQ("3_2", in1);
87 DCHECK(!in2);
88 DCHECK_EQ(2, in3);
89 *out1 = true;
90 *out2 = 32;
93 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
94 bool* out3) {
95 DCHECK_EQ(3, in1);
96 DCHECK_EQ("3_3", in2);
97 DCHECK(in3);
98 *out1 = "3_3";
99 *out2 = 33;
100 *out3 = false;
103 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
104 std::string* out3, bool* out4) {
105 DCHECK(in1);
106 DCHECK_EQ(3, in2);
107 DCHECK_EQ("3_4", in3);
108 *out1 = 34;
109 *out2 = true;
110 *out3 = "3_4";
111 *out4 = false;
114 bool Send(IPC::Message* message) {
115 // gets the reply message, stash in global
116 DCHECK(g_reply == NULL);
117 g_reply = message;
118 return true;
121 bool OnMessageReceived(const IPC::Message& msg) {
122 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
123 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
124 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
125 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
126 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
127 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
128 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
129 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
130 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
131 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
132 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
133 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
134 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
135 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
136 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
137 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
138 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
139 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
140 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
141 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
142 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
143 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
144 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
145 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
146 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
147 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
148 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
149 IPC_END_MESSAGE_MAP()
150 return true;
155 void Send(IPC::SyncMessage* msg) {
156 static TestMessageReceiver receiver;
158 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
159 DCHECK(reply_serializer != NULL);
161 // "send" the message
162 receiver.OnMessageReceived(*msg);
163 delete msg;
165 // get the reply message from the global, and deserialize the output
166 // parameters into the output pointers.
167 DCHECK(g_reply != NULL);
168 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
169 DCHECK(result);
170 delete g_reply;
171 g_reply = NULL;
172 delete reply_serializer;
175 TEST(IPCSyncMessageTest, Main) {
176 bool bool1 = true;
177 int int1 = 0;
178 std::string string1;
180 Send(new Msg_C_0_1(&bool1));
181 DCHECK(!bool1);
183 Send(new Msg_C_0_2(&bool1, &int1));
184 DCHECK(bool1);
185 DCHECK_EQ(2, int1);
187 Send(new Msg_C_0_3(&bool1, &int1, &string1));
188 DCHECK(!bool1);
189 DCHECK_EQ(3, int1);
190 DCHECK_EQ("0_3", string1);
192 bool1 = false;
193 Send(new Msg_C_1_1(1, &bool1));
194 DCHECK(bool1);
196 bool1 = false;
197 Send(new Msg_C_1_2(false, &bool1, &int1));
198 DCHECK(bool1);
199 DCHECK_EQ(12, int1);
201 bool1 = true;
202 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
203 DCHECK_EQ("1_3", string1);
204 DCHECK_EQ(13, int1);
205 DCHECK(!bool1);
207 bool1 = false;
208 Send(new Msg_C_2_1(1, false, &bool1));
209 DCHECK(bool1);
211 bool1 = false;
212 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
213 DCHECK(bool1);
214 DCHECK_EQ(22, int1);
216 bool1 = true;
217 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
218 DCHECK_EQ("2_3", string1);
219 DCHECK_EQ(23, int1);
220 DCHECK(!bool1);
222 bool1 = false;
223 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
224 DCHECK(bool1);
226 bool1 = false;
227 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
228 DCHECK(bool1);
229 DCHECK_EQ(32, int1);
231 bool1 = true;
232 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
233 DCHECK_EQ("3_3", string1);
234 DCHECK_EQ(33, int1);
235 DCHECK(!bool1);
237 bool1 = false;
238 bool bool2 = true;
239 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
240 DCHECK_EQ(34, int1);
241 DCHECK(bool1);
242 DCHECK_EQ("3_4", string1);
243 DCHECK(!bool2);
245 // Routed messages, just a copy of the above but with extra routing paramater
246 Send(new Msg_R_0_1(0, &bool1));
247 DCHECK(!bool1);
249 Send(new Msg_R_0_2(0, &bool1, &int1));
250 DCHECK(bool1);
251 DCHECK_EQ(2, int1);
253 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
254 DCHECK(!bool1);
255 DCHECK_EQ(3, int1);
256 DCHECK_EQ("0_3", string1);
258 bool1 = false;
259 Send(new Msg_R_1_1(0, 1, &bool1));
260 DCHECK(bool1);
262 bool1 = false;
263 Send(new Msg_R_1_2(0, false, &bool1, &int1));
264 DCHECK(bool1);
265 DCHECK_EQ(12, int1);
267 bool1 = true;
268 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
269 DCHECK_EQ("1_3", string1);
270 DCHECK_EQ(13, int1);
271 DCHECK(!bool1);
273 bool1 = false;
274 Send(new Msg_R_2_1(0, 1, false, &bool1));
275 DCHECK(bool1);
277 bool1 = false;
278 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
279 DCHECK(bool1);
280 DCHECK_EQ(22, int1);
282 bool1 = true;
283 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
284 DCHECK(!bool1);
285 DCHECK_EQ("2_3", string1);
286 DCHECK_EQ(23, int1);
288 bool1 = false;
289 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
290 DCHECK(bool1);
292 bool1 = false;
293 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
294 DCHECK(bool1);
295 DCHECK_EQ(32, int1);
297 bool1 = true;
298 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
299 DCHECK_EQ("3_3", string1);
300 DCHECK_EQ(33, int1);
301 DCHECK(!bool1);
304 } // namespace