ipc_message_unittest.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright (c) 2012 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. #include "ipc/ipc_message.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <limits>
  9. #include <memory>
  10. #include <utility>
  11. #include "base/memory/ptr_util.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "base/values.h"
  14. #include "build/build_config.h"
  15. #include "ipc/ipc_message_utils.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. // IPC messages for testing ----------------------------------------------------
  18. #define IPC_MESSAGE_IMPL
  19. #include "ipc/ipc_message_macros.h"
  20. #include "ipc/ipc_message_start.h"
  21. #define IPC_MESSAGE_START TestMsgStart
  22. IPC_MESSAGE_CONTROL0(TestMsgClassEmpty)
  23. IPC_MESSAGE_CONTROL1(TestMsgClassI, int)
  24. IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string)
  25. namespace IPC {
  26. TEST(IPCMessageTest, BasicMessageTest) {
  27. int v1 = 10;
  28. std::string v2("foobar");
  29. std::u16string v3(u"hello world");
  30. IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
  31. m.WriteInt(v1);
  32. m.WriteString(v2);
  33. m.WriteString16(v3);
  34. base::PickleIterator iter(m);
  35. int vi;
  36. std::string vs;
  37. std::u16string vs16;
  38. EXPECT_TRUE(iter.ReadInt(&vi));
  39. EXPECT_EQ(v1, vi);
  40. EXPECT_TRUE(iter.ReadString(&vs));
  41. EXPECT_EQ(v2, vs);
  42. EXPECT_TRUE(iter.ReadString16(&vs16));
  43. EXPECT_EQ(v3, vs16);
  44. // should fail
  45. EXPECT_FALSE(iter.ReadInt(&vi));
  46. EXPECT_FALSE(iter.ReadString(&vs));
  47. EXPECT_FALSE(iter.ReadString16(&vs16));
  48. }
  49. TEST(IPCMessageTest, Value) {
  50. auto expect_value_equals = [](const base::Value& input) {
  51. IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  52. IPC::WriteParam(&msg, input);
  53. base::Value output;
  54. base::PickleIterator iter(msg);
  55. EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)) << input;
  56. EXPECT_EQ(input, output);
  57. };
  58. expect_value_equals(base::Value("foo"));
  59. expect_value_equals(base::Value(42));
  60. expect_value_equals(base::Value(0.07));
  61. expect_value_equals(base::Value(true));
  62. expect_value_equals(base::Value(base::Value::BlobStorage({'a', 'b', 'c'})));
  63. {
  64. base::Value dict(base::Value::Type::DICTIONARY);
  65. dict.SetIntKey("key1", 42);
  66. dict.SetStringKey("key2", "hi");
  67. expect_value_equals(dict);
  68. }
  69. {
  70. base::Value list(base::Value::Type::LIST);
  71. list.GetList().Append(42);
  72. list.GetList().Append("hello");
  73. expect_value_equals(list);
  74. }
  75. // Also test the corrupt case.
  76. IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  77. bad_msg.WriteInt(99);
  78. base::PickleIterator iter(bad_msg);
  79. base::Value output;
  80. EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
  81. }
  82. TEST(IPCMessageTest, ListValue) {
  83. base::ListValue input;
  84. input.GetList().Append(42.42);
  85. input.GetList().Append("forty");
  86. input.GetList().Append(base::Value());
  87. IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  88. IPC::WriteParam(&msg, input);
  89. base::ListValue output;
  90. base::PickleIterator iter(msg);
  91. EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
  92. EXPECT_EQ(input, output);
  93. // Also test the corrupt case.
  94. IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  95. bad_msg.WriteInt(99);
  96. iter = base::PickleIterator(bad_msg);
  97. EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
  98. }
  99. TEST(IPCMessageTest, DictionaryValue) {
  100. base::DictionaryValue input;
  101. input.SetKey("null", base::Value());
  102. input.SetBoolean("bool", true);
  103. input.GetDict().Set("int", 42);
  104. input.SetKey("int.with.dot", base::Value(43));
  105. base::DictionaryValue subdict;
  106. subdict.SetString("str", "forty two");
  107. subdict.SetBoolean("bool", false);
  108. base::ListValue sublist;
  109. sublist.GetList().Append(42.42);
  110. sublist.GetList().Append("forty");
  111. sublist.GetList().Append("two");
  112. subdict.SetKey("list", std::move(sublist));
  113. input.SetKey("dict", std::move(subdict));
  114. IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  115. IPC::WriteParam(&msg, input);
  116. base::DictionaryValue output;
  117. base::PickleIterator iter(msg);
  118. EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output));
  119. EXPECT_EQ(input, output);
  120. // Also test the corrupt case.
  121. IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  122. bad_msg.WriteInt(99);
  123. iter = base::PickleIterator(bad_msg);
  124. EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output));
  125. }
  126. TEST(IPCMessageTest, FindNext) {
  127. IPC::Message message;
  128. message.WriteString("Goooooooogle");
  129. message.WriteInt(111);
  130. std::vector<char> message_data(message.size() + 7);
  131. memcpy(message_data.data(), message.data(), message.size());
  132. const char* data_start = message_data.data();
  133. const char* data_end = data_start + message.size();
  134. IPC::Message::NextMessageInfo next;
  135. // Data range contains the entire message plus some extra bytes
  136. IPC::Message::FindNext(data_start, data_end + 1, &next);
  137. EXPECT_TRUE(next.message_found);
  138. EXPECT_EQ(next.message_size, message.size());
  139. EXPECT_EQ(next.pickle_end, data_end);
  140. EXPECT_EQ(next.message_end, data_end);
  141. // Data range exactly contains the entire message
  142. IPC::Message::FindNext(data_start, data_end, &next);
  143. EXPECT_TRUE(next.message_found);
  144. EXPECT_EQ(next.message_size, message.size());
  145. EXPECT_EQ(next.pickle_end, data_end);
  146. EXPECT_EQ(next.message_end, data_end);
  147. // Data range doesn't contain the entire message
  148. // (but contains the message header)
  149. IPC::Message::FindNext(data_start, data_end - 1, &next);
  150. EXPECT_FALSE(next.message_found);
  151. EXPECT_EQ(next.message_size, message.size());
  152. // Data range doesn't contain the message header
  153. // (but contains the pickle header)
  154. IPC::Message::FindNext(data_start,
  155. data_start + sizeof(IPC::Message::Header) - 1,
  156. &next);
  157. EXPECT_FALSE(next.message_found);
  158. EXPECT_EQ(next.message_size, 0u);
  159. // Data range doesn't contain the pickle header
  160. IPC::Message::FindNext(data_start,
  161. data_start + sizeof(base::Pickle::Header) - 1,
  162. &next);
  163. EXPECT_FALSE(next.message_found);
  164. EXPECT_EQ(next.message_size, 0u);
  165. }
  166. TEST(IPCMessageTest, FindNextOverflow) {
  167. IPC::Message message;
  168. message.WriteString("Data");
  169. message.WriteInt(777);
  170. const char* data_start = reinterpret_cast<const char*>(message.data());
  171. const char* data_end = data_start + message.size();
  172. IPC::Message::NextMessageInfo next;
  173. // Payload size is negative (defeats 'start + size > end' check)
  174. message.header()->payload_size = static_cast<uint32_t>(-1);
  175. IPC::Message::FindNext(data_start, data_end, &next);
  176. EXPECT_FALSE(next.message_found);
  177. if (sizeof(size_t) > sizeof(uint32_t)) {
  178. // No overflow, just insane message size
  179. EXPECT_EQ(next.message_size,
  180. message.header()->payload_size + sizeof(IPC::Message::Header));
  181. } else {
  182. // Actual overflow, reported as max size_t
  183. EXPECT_EQ(next.message_size, std::numeric_limits<size_t>::max());
  184. }
  185. // Payload size is max positive integer (defeats size < 0 check, while
  186. // still potentially causing overflow down the road).
  187. message.header()->payload_size = std::numeric_limits<int32_t>::max();
  188. IPC::Message::FindNext(data_start, data_end, &next);
  189. EXPECT_FALSE(next.message_found);
  190. EXPECT_EQ(next.message_size,
  191. message.header()->payload_size + sizeof(IPC::Message::Header));
  192. }
  193. namespace {
  194. class IPCMessageParameterTest : public testing::Test {
  195. public:
  196. IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {}
  197. bool OnMessageReceived(const IPC::Message& message) {
  198. bool handled = true;
  199. IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message,
  200. &extra_param_)
  201. IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty)
  202. IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt)
  203. //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync)
  204. IPC_MESSAGE_UNHANDLED(handled = false)
  205. IPC_END_MESSAGE_MAP()
  206. return handled;
  207. }
  208. void OnEmpty(std::string* extra_param) {
  209. EXPECT_EQ(extra_param, &extra_param_);
  210. called_ = true;
  211. }
  212. void OnInt(std::string* extra_param, int foo) {
  213. EXPECT_EQ(extra_param, &extra_param_);
  214. EXPECT_EQ(foo, 42);
  215. called_ = true;
  216. }
  217. /* TODO: handle sync IPCs
  218. void OnSync(std::string* extra_param, int foo, std::string* out) {
  219. EXPECT_EQ(extra_param, &extra_param_);
  220. EXPECT_EQ(foo, 42);
  221. called_ = true;
  222. *out = std::string("out");
  223. }
  224. bool Send(IPC::Message* reply) {
  225. delete reply;
  226. return true;
  227. }*/
  228. std::string extra_param_;
  229. bool called_;
  230. };
  231. } // namespace
  232. TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) {
  233. TestMsgClassEmpty message;
  234. EXPECT_TRUE(OnMessageReceived(message));
  235. EXPECT_TRUE(called_);
  236. }
  237. #if BUILDFLAG(IS_ANDROID)
  238. #define MAYBE_OneIntegerWithParam DISABLED_OneIntegerWithParam
  239. #else
  240. #define MAYBE_OneIntegerWithParam OneIntegerWithParam
  241. #endif
  242. TEST_F(IPCMessageParameterTest, MAYBE_OneIntegerWithParam) {
  243. TestMsgClassI message(42);
  244. EXPECT_TRUE(OnMessageReceived(message));
  245. EXPECT_TRUE(called_);
  246. }
  247. /* TODO: handle sync IPCs
  248. TEST_F(IPCMessageParameterTest, Sync) {
  249. std::string output;
  250. TestMsgClassIS message(42, &output);
  251. EXPECT_TRUE(OnMessageReceived(message));
  252. EXPECT_TRUE(called_);
  253. EXPECT_EQ(output, std::string("out"));
  254. }*/
  255. } // namespace IPC