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.
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.
11 #include "base/basictypes.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "base/logging.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #define IPC_MESSAGE_IMPL
18 #include "ipc/ipc_sync_message_unittest.h"
22 static IPC::Message* g_reply;
24 class TestMessageReceiver {
27 void On_0_1(bool* out1) {
31 void On_0_2(bool* out1, int* out2) {
36 void On_0_3(bool* out1, int* out2, std::string* out3) {
42 void On_1_1(int in1, bool* out1) {
47 void On_1_2(bool in1, bool* out1, int* out2) {
53 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
60 void On_2_1(int in1, bool in2, bool* out1) {
66 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
73 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
81 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
84 DCHECK_EQ("3_1", in3);
88 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
89 DCHECK_EQ("3_2", in1);
96 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2,
99 DCHECK_EQ("3_3", in2);
106 void On_3_4(bool in1, int in2, std::string in3, int* out1, bool* out2,
107 std::string* out3, bool* out4) {
110 DCHECK_EQ("3_4", in3);
117 bool Send(IPC::Message* message) {
118 // gets the reply message, stash in global
119 DCHECK(g_reply == NULL);
124 bool OnMessageReceived(const IPC::Message& msg) {
125 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
126 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
127 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
128 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
129 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
130 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
131 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
132 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
133 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
134 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
135 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
136 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
137 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
138 IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4)
139 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
140 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
141 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
142 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
143 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
144 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
145 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
146 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
147 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
148 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
149 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
150 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
151 IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4)
152 IPC_END_MESSAGE_MAP()
158 void Send(IPC::SyncMessage* msg) {
159 static TestMessageReceiver receiver;
161 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
162 DCHECK(reply_serializer != NULL);
164 // "send" the message
165 receiver.OnMessageReceived(*msg);
168 // get the reply message from the global, and deserialize the output
169 // parameters into the output pointers.
170 DCHECK(g_reply != NULL);
171 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
175 delete reply_serializer;
178 TEST(IPCSyncMessageTest, Main) {
183 Send(new Msg_C_0_1(&bool1));
186 Send(new Msg_C_0_2(&bool1, &int1));
190 Send(new Msg_C_0_3(&bool1, &int1, &string1));
193 DCHECK_EQ("0_3", string1);
196 Send(new Msg_C_1_1(1, &bool1));
200 Send(new Msg_C_1_2(false, &bool1, &int1));
205 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
206 DCHECK_EQ("1_3", string1);
211 Send(new Msg_C_2_1(1, false, &bool1));
215 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
220 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
221 DCHECK_EQ("2_3", string1);
226 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
230 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
235 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
236 DCHECK_EQ("3_3", string1);
242 Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2));
245 DCHECK_EQ("3_4", string1);
248 // Routed messages, just a copy of the above but with extra routing paramater
249 Send(new Msg_R_0_1(0, &bool1));
252 Send(new Msg_R_0_2(0, &bool1, &int1));
256 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
259 DCHECK_EQ("0_3", string1);
262 Send(new Msg_R_1_1(0, 1, &bool1));
266 Send(new Msg_R_1_2(0, false, &bool1, &int1));
271 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
272 DCHECK_EQ("1_3", string1);
277 Send(new Msg_R_2_1(0, 1, false, &bool1));
281 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
286 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
288 DCHECK_EQ("2_3", string1);
292 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
296 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
301 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
302 DCHECK_EQ("3_3", string1);