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"
20 static IPC::Message
* g_reply
;
22 class TestMessageReceiver
{
25 void On_0_1(bool* out1
) {
29 void On_0_2(bool* out1
, int* out2
) {
34 void On_0_3(bool* out1
, int* out2
, std::string
* out3
) {
40 void On_1_1(int in1
, bool* out1
) {
45 void On_1_2(bool in1
, bool* out1
, int* out2
) {
51 void On_1_3(int in1
, std::string
* out1
, int* out2
, bool* out3
) {
58 void On_2_1(int in1
, bool in2
, bool* out1
) {
64 void On_2_2(bool in1
, int in2
, bool* out1
, int* out2
) {
71 void On_2_3(int in1
, bool in2
, std::string
* out1
, int* out2
, bool* out3
) {
79 void On_3_1(int in1
, bool in2
, std::string in3
, bool* out1
) {
82 DCHECK_EQ("3_1", in3
);
86 void On_3_2(std::string in1
, bool in2
, int in3
, bool* out1
, int* out2
) {
87 DCHECK_EQ("3_2", in1
);
94 void On_3_3(int in1
, std::string in2
, bool in3
, std::string
* out1
, int* out2
,
97 DCHECK_EQ("3_3", in2
);
104 void On_3_4(bool in1
, int in2
, std::string in3
, int* out1
, bool* out2
,
105 std::string
* out3
, bool* out4
) {
108 DCHECK_EQ("3_4", in3
);
115 bool Send(IPC::Message
* message
) {
116 // gets the reply message, stash in global
117 DCHECK(g_reply
== NULL
);
122 bool OnMessageReceived(const IPC::Message
& msg
) {
123 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
)
136 IPC_MESSAGE_HANDLER(Msg_C_3_4
, On_3_4
)
137 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
)
149 IPC_MESSAGE_HANDLER(Msg_R_3_4
, On_3_4
)
150 IPC_END_MESSAGE_MAP()
156 void Send(IPC::SyncMessage
* msg
) {
157 static TestMessageReceiver receiver
;
159 IPC::MessageReplyDeserializer
* reply_serializer
= msg
->GetReplyDeserializer();
160 DCHECK(reply_serializer
!= NULL
);
162 // "send" the message
163 receiver
.OnMessageReceived(*msg
);
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
);
173 delete reply_serializer
;
176 TEST(IPCSyncMessageTest
, Main
) {
181 Send(new Msg_C_0_1(&bool1
));
184 Send(new Msg_C_0_2(&bool1
, &int1
));
188 Send(new Msg_C_0_3(&bool1
, &int1
, &string1
));
191 DCHECK_EQ("0_3", string1
);
194 Send(new Msg_C_1_1(1, &bool1
));
198 Send(new Msg_C_1_2(false, &bool1
, &int1
));
203 Send(new Msg_C_1_3(3, &string1
, &int1
, &bool1
));
204 DCHECK_EQ("1_3", string1
);
209 Send(new Msg_C_2_1(1, false, &bool1
));
213 Send(new Msg_C_2_2(false, 2, &bool1
, &int1
));
218 Send(new Msg_C_2_3(3, true, &string1
, &int1
, &bool1
));
219 DCHECK_EQ("2_3", string1
);
224 Send(new Msg_C_3_1(1, false, "3_1", &bool1
));
228 Send(new Msg_C_3_2("3_2", false, 2, &bool1
, &int1
));
233 Send(new Msg_C_3_3(3, "3_3", true, &string1
, &int1
, &bool1
));
234 DCHECK_EQ("3_3", string1
);
240 Send(new Msg_C_3_4(true, 3, "3_4", &int1
, &bool1
, &string1
, &bool2
));
243 DCHECK_EQ("3_4", string1
);
246 // Routed messages, just a copy of the above but with extra routing paramater
247 Send(new Msg_R_0_1(0, &bool1
));
250 Send(new Msg_R_0_2(0, &bool1
, &int1
));
254 Send(new Msg_R_0_3(0, &bool1
, &int1
, &string1
));
257 DCHECK_EQ("0_3", string1
);
260 Send(new Msg_R_1_1(0, 1, &bool1
));
264 Send(new Msg_R_1_2(0, false, &bool1
, &int1
));
269 Send(new Msg_R_1_3(0, 3, &string1
, &int1
, &bool1
));
270 DCHECK_EQ("1_3", string1
);
275 Send(new Msg_R_2_1(0, 1, false, &bool1
));
279 Send(new Msg_R_2_2(0, false, 2, &bool1
, &int1
));
284 Send(new Msg_R_2_3(0, 3, true, &string1
, &int1
, &bool1
));
286 DCHECK_EQ("2_3", string1
);
290 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1
));
294 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1
, &int1
));
299 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1
, &int1
, &bool1
));
300 DCHECK_EQ("3_3", string1
);