blob: ae44acb228191db14b792239f9c79bde14ce3416 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294//
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.
8
9#include <string.h>
10
11#include "base/basictypes.h"
12#include "chrome/common/ipc_message.h"
13#include "chrome/common/ipc_message_utils.h"
14#include "base/logging.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17#define IPC_MESSAGE_MACROS_ENUMS
18#include "chrome/common/ipc_sync_message_unittest.h"
19
20// define the classes
21#define IPC_MESSAGE_MACROS_CLASSES
22#include "chrome/common/ipc_sync_message_unittest.h"
23
24
25static IPC::Message* g_reply;
26
27class TestMessageReceiver {
28 public:
29
30 void On_0_1(bool* out1) {
31 *out1 = false;
32 }
33
34 void On_0_2(bool* out1, int* out2) {
35 *out1 = true;
36 *out2 = 2;
37 }
38
39 void On_0_3(bool* out1, int* out2, std::string* out3) {
40 *out1 = false;
41 *out2 = 3;
42 *out3 = "0_3";
43 }
44
45 void On_1_1(int in1, bool* out1) {
46 DCHECK(in1 == 1);
47 *out1 = true;
48 }
49
50 void On_1_2(bool in1, bool* out1, int* out2) {
51 DCHECK(!in1);
52 *out1 = true;
53 *out2 = 12;
54 }
55
56 void On_1_3(int in1, std::string* out1, int* out2, bool* out3) {
57 DCHECK(in1 == 3);
58 *out1 = "1_3";
59 *out2 = 13;
60 *out3 = false;
61 }
62
63 void On_2_1(int in1, bool in2, bool* out1) {
64 DCHECK(in1 == 1 && !in2);
65 *out1 = true;
66 }
67
68 void On_2_2(bool in1, int in2, bool* out1, int* out2) {
69 DCHECK(!in1 && in2 == 2);
70 *out1 = true;
71 *out2 = 22;
72 }
73
74 void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) {
75 DCHECK(in1 == 3 && in2);
76 *out1 = "2_3";
77 *out2 = 23;
78 *out3 = false;
79 }
80
81 void On_3_1(int in1, bool in2, std::string in3, bool* out1) {
82 DCHECK(in1 == 1 && !in2 && in3 == "3_1");
83 *out1 = true;
84 }
85
86 void On_3_2(std::string in1, bool in2, int in3, bool* out1, int* out2) {
87 DCHECK(in1 == "3_2" && !in2 && in3 == 2);
88 *out1 = true;
89 *out2 = 32;
90 }
91
92 void On_3_3(int in1, std::string in2, bool in3, std::string* out1, int* out2, bool* out3) {
93 DCHECK(in1 == 3 && in2 == "3_3" && in3);
94 *out1 = "3_3";
95 *out2 = 33;
96 *out3 = false;
97 }
98
99 bool Send(IPC::Message* message) {
100 // gets the reply message, stash in global
101 DCHECK(g_reply == NULL);
102 g_reply = message;
103 return true;
104 }
105
106 void OnMessageReceived(const IPC::Message& msg) {
107 IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg)
108 IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1)
109 IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2)
110 IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3)
111 IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1)
112 IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2)
113 IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3)
114 IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1)
115 IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2)
116 IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3)
117 IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1)
118 IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2)
119 IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3)
120 IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1)
121 IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2)
122 IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3)
123 IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1)
124 IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2)
125 IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3)
126 IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1)
127 IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2)
128 IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3)
129 IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1)
130 IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2)
131 IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3)
132 IPC_END_MESSAGE_MAP()
133 }
134
135};
136
137void Send(IPC::SyncMessage* msg) {
138 static TestMessageReceiver receiver;
139
140 IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer();
141 DCHECK(reply_serializer != NULL);
142
143 // "send" the message
144 receiver.OnMessageReceived(*msg);
145 delete msg;
146
147 // get the reply message from the global, and deserialize the output
148 // parameters into the output pointers.
149 DCHECK(g_reply != NULL);
150 bool result = reply_serializer->SerializeOutputParameters(*g_reply);
151 DCHECK(result);
152 delete g_reply;
153 g_reply = NULL;
154 delete reply_serializer;
155}
156
157TEST(IPCSyncMessageTest, Main) {
158 bool bool1 = true;
159 int int1 = 0;
160 std::string string1;
161
162 Send(new Msg_C_0_1(&bool1));
163 DCHECK(!bool1);
164
165 Send(new Msg_C_0_2(&bool1, &int1));
166 DCHECK(bool1 && int1 == 2);
167
168 Send(new Msg_C_0_3(&bool1, &int1, &string1));
169 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
170
171 bool1 = false;
172 Send(new Msg_C_1_1(1, &bool1));
173 DCHECK(bool1);
174
175 bool1 = false;
176 Send(new Msg_C_1_2(false, &bool1, &int1));
177 DCHECK(bool1 && int1 == 12);
178
179 bool1 = true;
180 Send(new Msg_C_1_3(3, &string1, &int1, &bool1));
181 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
182
183 bool1 = false;
184 Send(new Msg_C_2_1(1, false, &bool1));
185 DCHECK(bool1);
186
187 bool1 = false;
188 Send(new Msg_C_2_2(false, 2, &bool1, &int1));
189 DCHECK(bool1 && int1 == 22);
190
191 bool1 = true;
192 Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1));
193 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
194
195 bool1 = false;
196 Send(new Msg_C_3_1(1, false, "3_1", &bool1));
197 DCHECK(bool1);
198
199 bool1 = false;
200 Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1));
201 DCHECK(bool1 && int1 == 32);
202
203 bool1 = true;
204 Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1));
205 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
206
207 // Routed messages, just a copy of the above but with extra routing paramater
208 Send(new Msg_R_0_1(0, &bool1));
209 DCHECK(!bool1);
210
211 Send(new Msg_R_0_2(0, &bool1, &int1));
212 DCHECK(bool1 && int1 == 2);
213
214 Send(new Msg_R_0_3(0, &bool1, &int1, &string1));
215 DCHECK(!bool1 && int1 == 3 && string1 == "0_3");
216
217 bool1 = false;
218 Send(new Msg_R_1_1(0, 1, &bool1));
219 DCHECK(bool1);
220
221 bool1 = false;
222 Send(new Msg_R_1_2(0, false, &bool1, &int1));
223 DCHECK(bool1 && int1 == 12);
224
225 bool1 = true;
226 Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1));
227 DCHECK(string1 == "1_3" && int1 == 13 && !bool1);
228
229 bool1 = false;
230 Send(new Msg_R_2_1(0, 1, false, &bool1));
231 DCHECK(bool1);
232
233 bool1 = false;
234 Send(new Msg_R_2_2(0, false, 2, &bool1, &int1));
235 DCHECK(bool1 && int1 == 22);
236
237 bool1 = true;
238 Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1));
239 DCHECK(string1 == "2_3" && int1 == 23 && !bool1);
240
241 bool1 = false;
242 Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1));
243 DCHECK(bool1);
244
245 bool1 = false;
246 Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1));
247 DCHECK(bool1 && int1 == 32);
248
249 bool1 = true;
250 Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1));
251 DCHECK(string1 == "3_3" && int1 == 33 && !bool1);
252}
license.botbf09a502008-08-24 00:55:55253