[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 5 | #include "ipc/ipc_message.h" |
| 6 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 11 | #include "ipc/ipc_message_utils.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 14 | // IPC messages for testing ---------------------------------------------------- |
| 15 | |
| 16 | #define IPC_MESSAGE_IMPL |
| 17 | #include "ipc/ipc_message_macros.h" |
| 18 | |
| 19 | #define IPC_MESSAGE_START TestMsgStart |
| 20 | |
| 21 | IPC_MESSAGE_CONTROL0(TestMsgClassEmpty) |
| 22 | |
| 23 | IPC_MESSAGE_CONTROL1(TestMsgClassI, int) |
| 24 | |
| 25 | IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS, int, std::string) |
| 26 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 29 | TEST(IPCMessageTest, ListValue) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 30 | base::ListValue input; |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 31 | input.Set(0, new base::FundamentalValue(42.42)); |
| 32 | input.Set(1, new base::StringValue("forty")); |
| 33 | input.Set(2, base::Value::CreateNullValue()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 34 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 35 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 36 | IPC::WriteParam(&msg, input); |
| 37 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 38 | base::ListValue output; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame^] | 39 | base::PickleIterator iter(msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 40 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 41 | |
| 42 | EXPECT_TRUE(input.Equals(&output)); |
| 43 | |
| 44 | // Also test the corrupt case. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 45 | IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 46 | bad_msg.WriteInt(99); |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame^] | 47 | iter = base::PickleIterator(bad_msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 48 | EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 49 | } |
| 50 | |
| 51 | TEST(IPCMessageTest, DictionaryValue) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 52 | base::DictionaryValue input; |
| 53 | input.Set("null", base::Value::CreateNullValue()); |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 54 | input.Set("bool", new base::FundamentalValue(true)); |
| 55 | input.Set("int", new base::FundamentalValue(42)); |
| 56 | input.SetWithoutPathExpansion("int.with.dot", new base::FundamentalValue(43)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 57 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 58 | scoped_ptr<base::DictionaryValue> subdict(new base::DictionaryValue()); |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 59 | subdict->Set("str", new base::StringValue("forty two")); |
| 60 | subdict->Set("bool", new base::FundamentalValue(false)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 61 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 62 | scoped_ptr<base::ListValue> sublist(new base::ListValue()); |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 63 | sublist->Set(0, new base::FundamentalValue(42.42)); |
| 64 | sublist->Set(1, new base::StringValue("forty")); |
| 65 | sublist->Set(2, new base::StringValue("two")); |
[email protected] | 9992266 | 2010-08-17 16:24:25 | [diff] [blame] | 66 | subdict->Set("list", sublist.release()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 67 | |
[email protected] | 9992266 | 2010-08-17 16:24:25 | [diff] [blame] | 68 | input.Set("dict", subdict.release()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 69 | |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 70 | IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 71 | IPC::WriteParam(&msg, input); |
| 72 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 73 | base::DictionaryValue output; |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame^] | 74 | base::PickleIterator iter(msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 75 | EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 76 | |
| 77 | EXPECT_TRUE(input.Equals(&output)); |
| 78 | |
| 79 | // Also test the corrupt case. |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 80 | IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 81 | bad_msg.WriteInt(99); |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame^] | 82 | iter = base::PickleIterator(bad_msg); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 83 | EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 84 | } |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 85 | |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 86 | class IPCMessageParameterTest : public testing::Test { |
| 87 | public: |
| 88 | IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {} |
| 89 | |
| 90 | bool OnMessageReceived(const IPC::Message& message) { |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 91 | bool handled = true; |
| 92 | IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest, message, |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 93 | &extra_param_) |
[email protected] | 5636d90 | 2014-05-13 23:19:10 | [diff] [blame] | 94 | IPC_MESSAGE_HANDLER(TestMsgClassEmpty, OnEmpty) |
| 95 | IPC_MESSAGE_HANDLER(TestMsgClassI, OnInt) |
| 96 | //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync) |
| 97 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 98 | IPC_END_MESSAGE_MAP() |
| 99 | |
| 100 | return handled; |
| 101 | } |
| 102 | |
| 103 | void OnEmpty(std::string* extra_param) { |
| 104 | EXPECT_EQ(extra_param, &extra_param_); |
| 105 | called_ = true; |
| 106 | } |
| 107 | |
| 108 | void OnInt(std::string* extra_param, int foo) { |
| 109 | EXPECT_EQ(extra_param, &extra_param_); |
| 110 | EXPECT_EQ(foo, 42); |
| 111 | called_ = true; |
| 112 | } |
| 113 | |
| 114 | /* TODO: handle sync IPCs |
| 115 | void OnSync(std::string* extra_param, int foo, std::string* out) { |
| 116 | EXPECT_EQ(extra_param, &extra_param_); |
| 117 | EXPECT_EQ(foo, 42); |
| 118 | called_ = true; |
| 119 | *out = std::string("out"); |
| 120 | } |
| 121 | |
| 122 | bool Send(IPC::Message* reply) { |
| 123 | delete reply; |
| 124 | return true; |
| 125 | }*/ |
| 126 | |
| 127 | std::string extra_param_; |
| 128 | bool called_; |
| 129 | }; |
| 130 | |
| 131 | TEST_F(IPCMessageParameterTest, EmptyDispatcherWithParam) { |
| 132 | TestMsgClassEmpty message; |
| 133 | EXPECT_TRUE(OnMessageReceived(message)); |
| 134 | EXPECT_TRUE(called_); |
| 135 | } |
| 136 | |
| 137 | TEST_F(IPCMessageParameterTest, OneIntegerWithParam) { |
| 138 | TestMsgClassI message(42); |
| 139 | EXPECT_TRUE(OnMessageReceived(message)); |
| 140 | EXPECT_TRUE(called_); |
| 141 | } |
| 142 | |
| 143 | /* TODO: handle sync IPCs |
| 144 | TEST_F(IPCMessageParameterTest, Sync) { |
| 145 | std::string output; |
| 146 | TestMsgClassIS message(42, &output); |
| 147 | EXPECT_TRUE(OnMessageReceived(message)); |
| 148 | EXPECT_TRUE(called_); |
| 149 | EXPECT_EQ(output, std::string("out")); |
| 150 | }*/ |
| 151 | |
[email protected] | 2a3aa7b5 | 2013-01-11 20:56:22 | [diff] [blame] | 152 | } // namespace |