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.
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"
19 static IPC::Message
* g_reply
;
21 class TestMessageReceiver
{
24 void On_0_1(bool* out1
) {
28 void On_0_2(bool* out1
, int* out2
) {
33 void On_0_3(bool* out1
, int* out2
, std::string
* out3
) {
39 void On_1_1(int in1
, bool* out1
) {
44 void On_1_2(bool in1
, bool* out1
, int* out2
) {
50 void On_1_3(int in1
, std::string
* out1
, int* out2
, bool* out3
) {
57 void On_2_1(int in1
, bool in2
, bool* out1
) {
63 void On_2_2(bool in1
, int in2
, bool* out1
, int* out2
) {
70 void On_2_3(int in1
, bool in2
, std::string
* out1
, int* out2
, bool* out3
) {
78 void On_3_1(int in1
, bool in2
, std::string in3
, bool* out1
) {
81 DCHECK_EQ("3_1", in3
);
85 void On_3_2(std::string in1
, bool in2
, int in3
, bool* out1
, int* out2
) {
86 DCHECK_EQ("3_2", in1
);
93 void On_3_3(int in1
, std::string in2
, bool in3
, std::string
* out1
, int* out2
,
96 DCHECK_EQ("3_3", in2
);
103 void On_3_4(bool in1
, int in2
, std::string in3
, int* out1
, bool* out2
,
104 std::string
* out3
, bool* out4
) {
107 DCHECK_EQ("3_4", in3
);
114 bool Send(IPC::Message
* message
) {
115 // gets the reply message, stash in global
116 DCHECK(g_reply
== NULL
);
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()
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
);
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
);
172 delete reply_serializer
;
175 TEST(IPCSyncMessageTest
, Main
) {
180 Send(new Msg_C_0_1(&bool1
));
183 Send(new Msg_C_0_2(&bool1
, &int1
));
187 Send(new Msg_C_0_3(&bool1
, &int1
, &string1
));
190 DCHECK_EQ("0_3", string1
);
193 Send(new Msg_C_1_1(1, &bool1
));
197 Send(new Msg_C_1_2(false, &bool1
, &int1
));
202 Send(new Msg_C_1_3(3, &string1
, &int1
, &bool1
));
203 DCHECK_EQ("1_3", string1
);
208 Send(new Msg_C_2_1(1, false, &bool1
));
212 Send(new Msg_C_2_2(false, 2, &bool1
, &int1
));
217 Send(new Msg_C_2_3(3, true, &string1
, &int1
, &bool1
));
218 DCHECK_EQ("2_3", string1
);
223 Send(new Msg_C_3_1(1, false, "3_1", &bool1
));
227 Send(new Msg_C_3_2("3_2", false, 2, &bool1
, &int1
));
232 Send(new Msg_C_3_3(3, "3_3", true, &string1
, &int1
, &bool1
));
233 DCHECK_EQ("3_3", string1
);
239 Send(new Msg_C_3_4(true, 3, "3_4", &int1
, &bool1
, &string1
, &bool2
));
242 DCHECK_EQ("3_4", string1
);
245 // Routed messages, just a copy of the above but with extra routing paramater
246 Send(new Msg_R_0_1(0, &bool1
));
249 Send(new Msg_R_0_2(0, &bool1
, &int1
));
253 Send(new Msg_R_0_3(0, &bool1
, &int1
, &string1
));
256 DCHECK_EQ("0_3", string1
);
259 Send(new Msg_R_1_1(0, 1, &bool1
));
263 Send(new Msg_R_1_2(0, false, &bool1
, &int1
));
268 Send(new Msg_R_1_3(0, 3, &string1
, &int1
, &bool1
));
269 DCHECK_EQ("1_3", string1
);
274 Send(new Msg_R_2_1(0, 1, false, &bool1
));
278 Send(new Msg_R_2_2(0, false, 2, &bool1
, &int1
));
283 Send(new Msg_R_2_3(0, 3, true, &string1
, &int1
, &bool1
));
285 DCHECK_EQ("2_3", string1
);
289 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1
));
293 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1
, &int1
));
298 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1
, &int1
, &bool1
));
299 DCHECK_EQ("3_3", string1
);