blob: b373b7625e1df8b11a0f48d09c2dfae710d14b4d [file] [log] [blame]
initial.commit09911bf2008-07-26 23:55:291// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Unit test to make sure that the serialization of synchronous IPC messages
31// works. This ensures that the macros and templates were defined correctly.
32// Doesn't test the IPC channel mechanism.
33
34#include <string.h>
35
36#include "base/basictypes.h"
37#include "chrome/common/ipc_message.h"
38#include "chrome/common/ipc_message_utils.h"
39#include "base/logging.h"
40#include "testing/gtest/include/gtest/gtest.h"
41
42#define IPC_MESSAGE_MACROS_ENUMS
43#include "chrome/common/ipc_sync_message_unittest.h"
44
45// define the classes
46#define IPC_MESSAGE_MACROS_CLASSES
47#include "chrome/common/ipc_sync_message_unittest.h"
48
49
50static IPC::Message* g_reply;
51
52class TestMessageReceiver {
53 public:
54
55 void On_0_1(bool* out1) {
56 *out1 = false;
57 }
58
59 void On_0_2(bool* out1, int* out2) {
60 *out1 = true;
61 *out2 = 2;
62 }
63
64 void On_0_3(bool* out1, int* out2, std::string* out3) {
65 *out1 = false;
66 *out2 = 3;
67 *out3 = "0_3";
68 }
69
70 void On_1_1(int in1, bool* out1) {
71 DCHECK(in1 == 1);
72 *out1 = true;
73 }
74
75 void On_1_2(bool in1, bool* out1, int* out2) {
76 DCHECK(!in1);
77 *out1 = true;
78 *out2 = 12;
79 }
80
81 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
82 DCHECK(in1 == 3);
83 *out1 = "1_3";
84 *out2 = 13;
85 *out3 = false;
86 }
87
88 void On_2_1(int in1, bool in2, bool* out1) {
89 DCHECK(in1 == 1 && !in2);
90 *out1 = true;
91 }
92
93 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
94 DCHECK(!in1 && in2 == 2);
95 *out1 = true;
96 *out2 = 22;
97 }
98
99 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
100 DCHECK(in1 == 3 && in2);
101 *out1 = "2_3";
102 *out2 = 23;
103 *out3 = false;
104 }
105
106 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
107 DCHECK(in1 == 1 && !in2 && in3 == "3_1");
108 *out1 = true;
109 }
110
111 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
112 DCHECK(in1 == "3_2" && !in2 && in3 == 2);
113 *out1 = true;
114 *out2 = 32;
115 }
116
117 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2, bool* out3) {
118 DCHECK(in1 == 3 && in2 == "3_3" && in3);
119 *out1 = "3_3";
120 *out2 = 33;
121 *out3 = false;
122 }
123
124 bool Send(IPC::Message* message) {
125 // gets the reply message, stash in global
126 DCHECK(g_reply == NULL);
127 g_reply = message;
128 return true;
129 }
130
131 void OnMessageReceived(const IPC::Message& msg) {
132 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
133 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
134 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
135 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
136 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
137 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
138 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
139 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
140 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
141 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
142 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
143 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
144 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
145 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
146 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
147 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
148 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
149 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
150 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
151 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
152 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
153 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
154 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
155 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
156 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
157 IPC_END_MESSAGE_MAP()
158 }
159
160};
161
162void Send(IPC::SyncMessage* msg) {
163 static TestMessageReceiver receiver;
164
165 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
166 DCHECK(reply_serializer != NULL);
167
168 // "send" the message
169 receiver.OnMessageReceived(*msg);
170 delete msg;
171
172 // get the reply message from the global, and deserialize the output
173 // parameters into the output pointers.
174 DCHECK(g_reply != NULL);
175 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
176 DCHECK(result);
177 delete g_reply;
178 g_reply = NULL;
179 delete reply_serializer;
180}
181
182TEST(IPCSyncMessageTest, Main) {
183 bool bool1 = true;
184 int int1 = 0;
185 std::string string1;
186
187 Send(new Msg_C_0_1(&bool1));
188 DCHECK(!bool1);
189
190 Send(new Msg_C_0_2(&bool1, &int1));
191 DCHECK(bool1 && int1 == 2);
192
193 Send(new Msg_C_0_3(&bool1, &int1, &string1));
194 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
195
196 bool1 = false;
197 Send(new Msg_C_1_1(1, &bool1));
198 DCHECK(bool1);
199
200 bool1 = false;
201 Send(new Msg_C_1_2(false, &bool1, &int1));
202 DCHECK(bool1 && int1 == 12);
203
204 bool1 = true;
205 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
206 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
207
208 bool1 = false;
209 Send(new Msg_C_2_1(1, false, &bool1));
210 DCHECK(bool1);
211
212 bool1 = false;
213 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
214 DCHECK(bool1 && int1 == 22);
215
216 bool1 = true;
217 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
218 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
219
220 bool1 = false;
221 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
222 DCHECK(bool1);
223
224 bool1 = false;
225 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
226 DCHECK(bool1 && int1 == 32);
227
228 bool1 = true;
229 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
230 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
231
232 // Routed messages, just a copy of the above but with extra routing paramater
233 Send(new Msg_R_0_1(0, &bool1));
234 DCHECK(!bool1);
235
236 Send(new Msg_R_0_2(0, &bool1, &int1));
237 DCHECK(bool1 && int1 == 2);
238
239 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
240 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
241
242 bool1 = false;
243 Send(new Msg_R_1_1(0, 1, &bool1));
244 DCHECK(bool1);
245
246 bool1 = false;
247 Send(new Msg_R_1_2(0, false, &bool1, &int1));
248 DCHECK(bool1 && int1 == 12);
249
250 bool1 = true;
251 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
252 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
253
254 bool1 = false;
255 Send(new Msg_R_2_1(0, 1, false, &bool1));
256 DCHECK(bool1);
257
258 bool1 = false;
259 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
260 DCHECK(bool1 && int1 == 22);
261
262 bool1 = true;
263 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
264 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
265
266 bool1 = false;
267 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
268 DCHECK(bool1);
269
270 bool1 = false;
271 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
272 DCHECK(bool1 && int1 == 32);
273
274 bool1 = true;
275 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
276 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
277}