ipc_sync_message_unittest.cc 7.5 KB

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