[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 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 | |
tfarina | 4da8275 | 2015-09-16 09:56:21 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 10 | #include "ipc/ipc_message.h" |
| 11 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 14 | #define IPC_MESSAGE_IMPL |
| 15 | #include "ipc/ipc_sync_message_unittest.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 17 | namespace { |
| 18 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | static IPC::Message* g_reply; |
| 20 | |
| 21 | class TestMessageReceiver { |
| 22 | public: |
| 23 | |
| 24 | void On_0_1(bool* out1) { |
| 25 | *out1 = false; |
| 26 | } |
| 27 | |
| 28 | void On_0_2(bool* out1, int* out2) { |
| 29 | *out1 = true; |
| 30 | *out2 = 2; |
| 31 | } |
| 32 | |
| 33 | void On_0_3(bool* out1, int* out2, std::string* out3) { |
| 34 | *out1 = false; |
| 35 | *out2 = 3; |
| 36 | *out3 = "0_3"; |
| 37 | } |
| 38 | |
| 39 | void On_1_1(int in1, bool* out1) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 40 | DCHECK_EQ(1, in1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | *out1 = true; |
| 42 | } |
| 43 | |
| 44 | void On_1_2(bool in1, bool* out1, int* out2) { |
| 45 | DCHECK(!in1); |
| 46 | *out1 = true; |
| 47 | *out2 = 12; |
| 48 | } |
| 49 | |
| 50 | void On_1_3(int in1, std::string* out1, int* out2, bool* out3) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 51 | DCHECK_EQ(3, in1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | *out1 = "1_3"; |
| 53 | *out2 = 13; |
| 54 | *out3 = false; |
| 55 | } |
| 56 | |
| 57 | void On_2_1(int in1, bool in2, bool* out1) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 58 | DCHECK_EQ(1, in1); |
| 59 | DCHECK(!in2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | *out1 = true; |
| 61 | } |
| 62 | |
| 63 | void On_2_2(bool in1, int in2, bool* out1, int* out2) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 64 | DCHECK(!in1); |
| 65 | DCHECK_EQ(2, in2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | *out1 = true; |
| 67 | *out2 = 22; |
| 68 | } |
| 69 | |
| 70 | void On_2_3(int in1, bool in2, std::string* out1, int* out2, bool* out3) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 71 | DCHECK_EQ(3, in1); |
| 72 | DCHECK(in2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | *out1 = "2_3"; |
| 74 | *out2 = 23; |
| 75 | *out3 = false; |
| 76 | } |
| 77 | |
ki.stfu | 5e7a0f7 | 2015-09-25 23:17:22 | [diff] [blame] | 78 | void On_3_1(int in1, bool in2, const std::string& in3, bool* out1) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 79 | DCHECK_EQ(1, in1); |
| 80 | DCHECK(!in2); |
| 81 | DCHECK_EQ("3_1", in3); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | *out1 = true; |
| 83 | } |
| 84 | |
ki.stfu | 5e7a0f7 | 2015-09-25 23:17:22 | [diff] [blame] | 85 | void On_3_2(const std::string& in1, |
| 86 | bool in2, |
| 87 | int in3, |
| 88 | bool* out1, |
| 89 | int* out2) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 90 | DCHECK_EQ("3_2", in1); |
| 91 | DCHECK(!in2); |
| 92 | DCHECK_EQ(2, in3); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | *out1 = true; |
| 94 | *out2 = 32; |
| 95 | } |
| 96 | |
ki.stfu | 5e7a0f7 | 2015-09-25 23:17:22 | [diff] [blame] | 97 | void On_3_3(int in1, |
| 98 | const std::string& in2, |
| 99 | bool in3, |
| 100 | std::string* out1, |
| 101 | int* out2, |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 102 | bool* out3) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 103 | DCHECK_EQ(3, in1); |
| 104 | DCHECK_EQ("3_3", in2); |
| 105 | DCHECK(in3); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | *out1 = "3_3"; |
| 107 | *out2 = 33; |
| 108 | *out3 = false; |
| 109 | } |
| 110 | |
ki.stfu | 5e7a0f7 | 2015-09-25 23:17:22 | [diff] [blame] | 111 | void On_3_4(bool in1, |
| 112 | int in2, |
| 113 | const std::string& in3, |
| 114 | int* out1, |
| 115 | bool* out2, |
| 116 | std::string* out3, |
| 117 | bool* out4) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 118 | DCHECK(in1); |
| 119 | DCHECK_EQ(3, in2); |
| 120 | DCHECK_EQ("3_4", in3); |
[email protected] | 5512613 | 2010-08-19 14:53:28 | [diff] [blame] | 121 | *out1 = 34; |
| 122 | *out2 = true; |
| 123 | *out3 = "3_4"; |
| 124 | *out4 = false; |
| 125 | } |
| 126 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | bool Send(IPC::Message* message) { |
| 128 | // gets the reply message, stash in global |
| 129 | DCHECK(g_reply == NULL); |
| 130 | g_reply = message; |
| 131 | return true; |
| 132 | } |
| 133 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 134 | bool OnMessageReceived(const IPC::Message& msg) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | IPC_BEGIN_MESSAGE_MAP(TestMessageReceiver, msg) |
| 136 | IPC_MESSAGE_HANDLER(Msg_C_0_1, On_0_1) |
| 137 | IPC_MESSAGE_HANDLER(Msg_C_0_2, On_0_2) |
| 138 | IPC_MESSAGE_HANDLER(Msg_C_0_3, On_0_3) |
| 139 | IPC_MESSAGE_HANDLER(Msg_C_1_1, On_1_1) |
| 140 | IPC_MESSAGE_HANDLER(Msg_C_1_2, On_1_2) |
| 141 | IPC_MESSAGE_HANDLER(Msg_C_1_3, On_1_3) |
| 142 | IPC_MESSAGE_HANDLER(Msg_C_2_1, On_2_1) |
| 143 | IPC_MESSAGE_HANDLER(Msg_C_2_2, On_2_2) |
| 144 | IPC_MESSAGE_HANDLER(Msg_C_2_3, On_2_3) |
| 145 | IPC_MESSAGE_HANDLER(Msg_C_3_1, On_3_1) |
| 146 | IPC_MESSAGE_HANDLER(Msg_C_3_2, On_3_2) |
| 147 | IPC_MESSAGE_HANDLER(Msg_C_3_3, On_3_3) |
[email protected] | 5512613 | 2010-08-19 14:53:28 | [diff] [blame] | 148 | IPC_MESSAGE_HANDLER(Msg_C_3_4, On_3_4) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | IPC_MESSAGE_HANDLER(Msg_R_0_1, On_0_1) |
| 150 | IPC_MESSAGE_HANDLER(Msg_R_0_2, On_0_2) |
| 151 | IPC_MESSAGE_HANDLER(Msg_R_0_3, On_0_3) |
| 152 | IPC_MESSAGE_HANDLER(Msg_R_1_1, On_1_1) |
| 153 | IPC_MESSAGE_HANDLER(Msg_R_1_2, On_1_2) |
| 154 | IPC_MESSAGE_HANDLER(Msg_R_1_3, On_1_3) |
| 155 | IPC_MESSAGE_HANDLER(Msg_R_2_1, On_2_1) |
| 156 | IPC_MESSAGE_HANDLER(Msg_R_2_2, On_2_2) |
| 157 | IPC_MESSAGE_HANDLER(Msg_R_2_3, On_2_3) |
| 158 | IPC_MESSAGE_HANDLER(Msg_R_3_1, On_3_1) |
| 159 | IPC_MESSAGE_HANDLER(Msg_R_3_2, On_3_2) |
| 160 | IPC_MESSAGE_HANDLER(Msg_R_3_3, On_3_3) |
[email protected] | 751bf4b | 2010-11-05 22:06:31 | [diff] [blame] | 161 | IPC_MESSAGE_HANDLER(Msg_R_3_4, On_3_4) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 163 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | }; |
| 167 | |
| 168 | void Send(IPC::SyncMessage* msg) { |
| 169 | static TestMessageReceiver receiver; |
| 170 | |
| 171 | IPC::MessageReplyDeserializer* reply_serializer = msg->GetReplyDeserializer(); |
| 172 | DCHECK(reply_serializer != NULL); |
| 173 | |
| 174 | // "send" the message |
| 175 | receiver.OnMessageReceived(*msg); |
| 176 | delete msg; |
| 177 | |
| 178 | // get the reply message from the global, and deserialize the output |
| 179 | // parameters into the output pointers. |
| 180 | DCHECK(g_reply != NULL); |
| 181 | bool result = reply_serializer->SerializeOutputParameters(*g_reply); |
| 182 | DCHECK(result); |
| 183 | delete g_reply; |
| 184 | g_reply = NULL; |
| 185 | delete reply_serializer; |
| 186 | } |
| 187 | |
| 188 | TEST(IPCSyncMessageTest, Main) { |
| 189 | bool bool1 = true; |
| 190 | int int1 = 0; |
| 191 | std::string string1; |
| 192 | |
| 193 | Send(new Msg_C_0_1(&bool1)); |
| 194 | DCHECK(!bool1); |
| 195 | |
| 196 | Send(new Msg_C_0_2(&bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 197 | DCHECK(bool1); |
| 198 | DCHECK_EQ(2, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | |
| 200 | Send(new Msg_C_0_3(&bool1, &int1, &string1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 201 | DCHECK(!bool1); |
| 202 | DCHECK_EQ(3, int1); |
| 203 | DCHECK_EQ("0_3", string1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | |
| 205 | bool1 = false; |
| 206 | Send(new Msg_C_1_1(1, &bool1)); |
| 207 | DCHECK(bool1); |
| 208 | |
| 209 | bool1 = false; |
| 210 | Send(new Msg_C_1_2(false, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 211 | DCHECK(bool1); |
| 212 | DCHECK_EQ(12, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | |
| 214 | bool1 = true; |
| 215 | Send(new Msg_C_1_3(3, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 216 | DCHECK_EQ("1_3", string1); |
| 217 | DCHECK_EQ(13, int1); |
| 218 | DCHECK(!bool1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | |
| 220 | bool1 = false; |
| 221 | Send(new Msg_C_2_1(1, false, &bool1)); |
| 222 | DCHECK(bool1); |
| 223 | |
| 224 | bool1 = false; |
| 225 | Send(new Msg_C_2_2(false, 2, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 226 | DCHECK(bool1); |
| 227 | DCHECK_EQ(22, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | |
| 229 | bool1 = true; |
| 230 | Send(new Msg_C_2_3(3, true, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 231 | DCHECK_EQ("2_3", string1); |
| 232 | DCHECK_EQ(23, int1); |
| 233 | DCHECK(!bool1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | |
| 235 | bool1 = false; |
| 236 | Send(new Msg_C_3_1(1, false, "3_1", &bool1)); |
| 237 | DCHECK(bool1); |
| 238 | |
| 239 | bool1 = false; |
| 240 | Send(new Msg_C_3_2("3_2", false, 2, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 241 | DCHECK(bool1); |
| 242 | DCHECK_EQ(32, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
| 244 | bool1 = true; |
| 245 | Send(new Msg_C_3_3(3, "3_3", true, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 246 | DCHECK_EQ("3_3", string1); |
| 247 | DCHECK_EQ(33, int1); |
| 248 | DCHECK(!bool1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 249 | |
[email protected] | 5512613 | 2010-08-19 14:53:28 | [diff] [blame] | 250 | bool1 = false; |
| 251 | bool bool2 = true; |
| 252 | Send(new Msg_C_3_4(true, 3, "3_4", &int1, &bool1, &string1, &bool2)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 253 | DCHECK_EQ(34, int1); |
| 254 | DCHECK(bool1); |
| 255 | DCHECK_EQ("3_4", string1); |
| 256 | DCHECK(!bool2); |
[email protected] | 5512613 | 2010-08-19 14:53:28 | [diff] [blame] | 257 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | // Routed messages, just a copy of the above but with extra routing paramater |
| 259 | Send(new Msg_R_0_1(0, &bool1)); |
| 260 | DCHECK(!bool1); |
| 261 | |
| 262 | Send(new Msg_R_0_2(0, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 263 | DCHECK(bool1); |
| 264 | DCHECK_EQ(2, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | |
| 266 | Send(new Msg_R_0_3(0, &bool1, &int1, &string1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 267 | DCHECK(!bool1); |
| 268 | DCHECK_EQ(3, int1); |
| 269 | DCHECK_EQ("0_3", string1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | |
| 271 | bool1 = false; |
| 272 | Send(new Msg_R_1_1(0, 1, &bool1)); |
| 273 | DCHECK(bool1); |
| 274 | |
| 275 | bool1 = false; |
| 276 | Send(new Msg_R_1_2(0, false, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 277 | DCHECK(bool1); |
| 278 | DCHECK_EQ(12, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
| 280 | bool1 = true; |
| 281 | Send(new Msg_R_1_3(0, 3, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 282 | DCHECK_EQ("1_3", string1); |
| 283 | DCHECK_EQ(13, int1); |
| 284 | DCHECK(!bool1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | |
| 286 | bool1 = false; |
| 287 | Send(new Msg_R_2_1(0, 1, false, &bool1)); |
| 288 | DCHECK(bool1); |
| 289 | |
| 290 | bool1 = false; |
| 291 | Send(new Msg_R_2_2(0, false, 2, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 292 | DCHECK(bool1); |
| 293 | DCHECK_EQ(22, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | |
| 295 | bool1 = true; |
| 296 | Send(new Msg_R_2_3(0, 3, true, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 297 | DCHECK(!bool1); |
| 298 | DCHECK_EQ("2_3", string1); |
| 299 | DCHECK_EQ(23, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | |
| 301 | bool1 = false; |
| 302 | Send(new Msg_R_3_1(0, 1, false, "3_1", &bool1)); |
| 303 | DCHECK(bool1); |
| 304 | |
| 305 | bool1 = false; |
| 306 | Send(new Msg_R_3_2(0, "3_2", false, 2, &bool1, &int1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 307 | DCHECK(bool1); |
| 308 | DCHECK_EQ(32, int1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | |
| 310 | bool1 = true; |
| 311 | Send(new Msg_R_3_3(0, 3, "3_3", true, &string1, &int1, &bool1)); |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 312 | DCHECK_EQ("3_3", string1); |
| 313 | DCHECK_EQ(33, int1); |
| 314 | DCHECK(!bool1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | } |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 316 | |
| 317 | } // namespace |