jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 1 | // Copyright 2014 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 | #include "build/build_config.h" |
| 6 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 9 | #include <memory> |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 10 | |
gab | 0a9b782 | 2017-05-13 04:42:56 +0900 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 12 | #include "base/pickle.h" |
fdoray | 284aae5 | 2016-06-23 04:56:16 +0900 | [diff] [blame] | 13 | #include "base/run_loop.h" |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
| 15 | #include "ipc/ipc_message.h" |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 16 | #include "ipc/ipc_test_base.h" |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 17 | #include "ipc/message_filter.h" |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 18 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 19 | // Get basic type definitions. |
| 20 | #define IPC_MESSAGE_IMPL |
| 21 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 22 | |
| 23 | // Generate constructors. |
| 24 | #include "ipc/struct_constructor_macros.h" |
| 25 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 26 | |
| 27 | // Generate destructors. |
| 28 | #include "ipc/struct_destructor_macros.h" |
| 29 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 30 | |
| 31 | // Generate param traits write methods. |
| 32 | #include "ipc/param_traits_write_macros.h" |
| 33 | namespace IPC { |
| 34 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 35 | } // namespace IPC |
| 36 | |
| 37 | // Generate param traits read methods. |
| 38 | #include "ipc/param_traits_read_macros.h" |
| 39 | namespace IPC { |
| 40 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 41 | } // namespace IPC |
| 42 | |
| 43 | // Generate param traits log methods. |
| 44 | #include "ipc/param_traits_log_macros.h" |
| 45 | namespace IPC { |
| 46 | #include "ipc/ipc_channel_proxy_unittest_messages.h" |
| 47 | } // namespace IPC |
| 48 | |
| 49 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 50 | namespace { |
| 51 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 52 | void CreateRunLoopAndRun(base::RunLoop** run_loop_ptr) { |
| 53 | base::RunLoop run_loop; |
| 54 | *run_loop_ptr = &run_loop; |
| 55 | run_loop.Run(); |
| 56 | *run_loop_ptr = nullptr; |
| 57 | } |
| 58 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 59 | class QuitListener : public IPC::Listener { |
| 60 | public: |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 61 | QuitListener() = default; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 62 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 63 | bool OnMessageReceived(const IPC::Message& message) override { |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 64 | IPC_BEGIN_MESSAGE_MAP(QuitListener, message) |
| 65 | IPC_MESSAGE_HANDLER(WorkerMsg_Quit, OnQuit) |
| 66 | IPC_MESSAGE_HANDLER(TestMsg_BadMessage, OnBadMessage) |
| 67 | IPC_END_MESSAGE_MAP() |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 68 | return true; |
| 69 | } |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 70 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 71 | void OnBadMessageReceived(const IPC::Message& message) override { |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 72 | bad_message_received_ = true; |
| 73 | } |
| 74 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 75 | void OnChannelError() override { CHECK(quit_message_received_); } |
| 76 | |
| 77 | void OnQuit() { |
| 78 | quit_message_received_ = true; |
| 79 | run_loop_->QuitWhenIdle(); |
| 80 | } |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 81 | |
| 82 | void OnBadMessage(const BadType& bad_type) { |
| 83 | // Should never be called since IPC wouldn't be deserialized correctly. |
| 84 | CHECK(false); |
| 85 | } |
| 86 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 87 | bool bad_message_received_ = false; |
| 88 | bool quit_message_received_ = false; |
| 89 | base::RunLoop* run_loop_ = nullptr; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class ChannelReflectorListener : public IPC::Listener { |
| 93 | public: |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 94 | ChannelReflectorListener() = default; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 95 | |
| 96 | void Init(IPC::Channel* channel) { |
| 97 | DCHECK(!channel_); |
| 98 | channel_ = channel; |
| 99 | } |
| 100 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 101 | bool OnMessageReceived(const IPC::Message& message) override { |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 102 | IPC_BEGIN_MESSAGE_MAP(ChannelReflectorListener, message) |
| 103 | IPC_MESSAGE_HANDLER(TestMsg_Bounce, OnTestBounce) |
| 104 | IPC_MESSAGE_HANDLER(TestMsg_SendBadMessage, OnSendBadMessage) |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 105 | IPC_MESSAGE_HANDLER(AutomationMsg_Bounce, OnAutomationBounce) |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 106 | IPC_MESSAGE_HANDLER(WorkerMsg_Bounce, OnBounce) |
| 107 | IPC_MESSAGE_HANDLER(WorkerMsg_Quit, OnQuit) |
| 108 | IPC_END_MESSAGE_MAP() |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 112 | void OnTestBounce() { |
| 113 | channel_->Send(new TestMsg_Bounce()); |
| 114 | } |
| 115 | |
| 116 | void OnSendBadMessage() { |
| 117 | channel_->Send(new TestMsg_BadMessage(BadType())); |
| 118 | } |
| 119 | |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 120 | void OnAutomationBounce() { channel_->Send(new AutomationMsg_Bounce()); } |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 121 | |
| 122 | void OnBounce() { |
| 123 | channel_->Send(new WorkerMsg_Bounce()); |
| 124 | } |
| 125 | |
| 126 | void OnQuit() { |
| 127 | channel_->Send(new WorkerMsg_Quit()); |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 128 | run_loop_->QuitWhenIdle(); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 131 | base::RunLoop* run_loop_ = nullptr; |
| 132 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 133 | private: |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 134 | IPC::Channel* channel_ = nullptr; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 135 | }; |
| 136 | |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 137 | class MessageCountFilter : public IPC::MessageFilter { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 138 | public: |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 139 | enum FilterEvent { |
| 140 | NONE, |
| 141 | FILTER_ADDED, |
| 142 | CHANNEL_CONNECTED, |
| 143 | CHANNEL_ERROR, |
| 144 | CHANNEL_CLOSING, |
| 145 | FILTER_REMOVED |
| 146 | }; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 147 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 148 | MessageCountFilter() = default; |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 149 | MessageCountFilter(uint32_t supported_message_class) |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 150 | : supported_message_class_(supported_message_class), |
| 151 | is_global_filter_(false) {} |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 152 | |
rockot | 690fbbe | 2016-09-17 08:40:30 +0900 | [diff] [blame] | 153 | void OnFilterAdded(IPC::Channel* channel) override { |
| 154 | EXPECT_TRUE(channel); |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 155 | EXPECT_EQ(NONE, last_filter_event_); |
| 156 | last_filter_event_ = FILTER_ADDED; |
| 157 | } |
| 158 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 159 | void OnChannelConnected(int32_t peer_pid) override { |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 160 | EXPECT_EQ(FILTER_ADDED, last_filter_event_); |
| 161 | EXPECT_NE(static_cast<int32_t>(base::kNullProcessId), peer_pid); |
| 162 | last_filter_event_ = CHANNEL_CONNECTED; |
| 163 | } |
| 164 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 165 | void OnChannelError() override { |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 166 | EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_); |
| 167 | last_filter_event_ = CHANNEL_ERROR; |
| 168 | } |
| 169 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 170 | void OnChannelClosing() override { |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 171 | // We may or may not have gotten OnChannelError; if not, the last event has |
| 172 | // to be OnChannelConnected. |
rockot | d76056e | 2016-10-22 07:47:50 +0900 | [diff] [blame] | 173 | EXPECT_NE(FILTER_REMOVED, last_filter_event_); |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 174 | if (last_filter_event_ != CHANNEL_ERROR) |
| 175 | EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_); |
| 176 | last_filter_event_ = CHANNEL_CLOSING; |
| 177 | } |
| 178 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 179 | void OnFilterRemoved() override { |
rockot | d76056e | 2016-10-22 07:47:50 +0900 | [diff] [blame] | 180 | // A filter may be removed at any time, even before the channel is connected |
| 181 | // (and thus before OnFilterAdded is ever able to dispatch.) The only time |
| 182 | // we won't see OnFilterRemoved is immediately after OnFilterAdded, because |
| 183 | // OnChannelConnected is always the next event to fire after that. |
| 184 | EXPECT_NE(FILTER_ADDED, last_filter_event_); |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 185 | last_filter_event_ = FILTER_REMOVED; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 186 | } |
| 187 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 188 | bool OnMessageReceived(const IPC::Message& message) override { |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 189 | // We should always get the OnFilterAdded and OnChannelConnected events |
| 190 | // prior to any messages. |
| 191 | EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_); |
| 192 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 193 | if (!is_global_filter_) { |
| 194 | EXPECT_EQ(supported_message_class_, IPC_MESSAGE_CLASS(message)); |
| 195 | } |
| 196 | ++messages_received_; |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 197 | |
| 198 | if (!message_filtering_enabled_) |
| 199 | return false; |
| 200 | |
| 201 | bool handled = true; |
| 202 | IPC_BEGIN_MESSAGE_MAP(MessageCountFilter, message) |
| 203 | IPC_MESSAGE_HANDLER(TestMsg_BadMessage, OnBadMessage) |
| 204 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 205 | IPC_END_MESSAGE_MAP() |
| 206 | return handled; |
| 207 | } |
| 208 | |
| 209 | void OnBadMessage(const BadType& bad_type) { |
| 210 | // Should never be called since IPC wouldn't be deserialized correctly. |
| 211 | CHECK(false); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 212 | } |
| 213 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 214 | bool GetSupportedMessageClasses( |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 215 | std::vector<uint32_t>* supported_message_classes) const override { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 216 | if (is_global_filter_) |
| 217 | return false; |
| 218 | supported_message_classes->push_back(supported_message_class_); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | void set_message_filtering_enabled(bool enabled) { |
| 223 | message_filtering_enabled_ = enabled; |
| 224 | } |
| 225 | |
| 226 | size_t messages_received() const { return messages_received_; } |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 227 | FilterEvent last_filter_event() const { return last_filter_event_; } |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 228 | |
| 229 | private: |
Chris Watkins | 31bfe1a | 2017-11-30 11:11:59 +0900 | [diff] [blame] | 230 | ~MessageCountFilter() override = default; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 231 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 232 | size_t messages_received_ = 0; |
| 233 | uint32_t supported_message_class_ = 0; |
| 234 | bool is_global_filter_ = true; |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 235 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 236 | FilterEvent last_filter_event_ = NONE; |
| 237 | bool message_filtering_enabled_ = false; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 238 | }; |
| 239 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 240 | class IPCChannelProxyTest : public IPCChannelMojoTestBase { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 241 | public: |
Chris Watkins | 31bfe1a | 2017-11-30 11:11:59 +0900 | [diff] [blame] | 242 | IPCChannelProxyTest() = default; |
| 243 | ~IPCChannelProxyTest() override = default; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 244 | |
dcheng | cd52576 | 2014-12-24 04:59:59 +0900 | [diff] [blame] | 245 | void SetUp() override { |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 246 | IPCChannelMojoTestBase::SetUp(); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 247 | |
| 248 | Init("ChannelProxyClient"); |
| 249 | |
| 250 | thread_.reset(new base::Thread("ChannelProxyTestServerThread")); |
| 251 | base::Thread::Options options; |
| 252 | options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 253 | thread_->StartWithOptions(options); |
| 254 | |
| 255 | listener_.reset(new QuitListener()); |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 256 | channel_proxy_ = IPC::ChannelProxy::Create( |
| 257 | TakeHandle().release(), IPC::Channel::MODE_SERVER, listener_.get(), |
Hajime Hoshi | 9570c52 | 2017-11-09 15:37:09 +0900 | [diff] [blame] | 258 | thread_->task_runner(), base::ThreadTaskRunnerHandle::Get()); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 259 | } |
| 260 | |
dcheng | cd52576 | 2014-12-24 04:59:59 +0900 | [diff] [blame] | 261 | void TearDown() override { |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 262 | channel_proxy_.reset(); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 263 | thread_.reset(); |
| 264 | listener_.reset(); |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 265 | IPCChannelMojoTestBase::TearDown(); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void SendQuitMessageAndWaitForIdle() { |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 269 | sender()->Send(new WorkerMsg_Quit); |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 270 | CreateRunLoopAndRun(&listener_->run_loop_); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 271 | EXPECT_TRUE(WaitForClientShutdown()); |
| 272 | } |
| 273 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 274 | bool DidListenerGetBadMessage() { |
| 275 | return listener_->bad_message_received_; |
| 276 | } |
| 277 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 278 | IPC::ChannelProxy* channel_proxy() { return channel_proxy_.get(); } |
| 279 | IPC::Sender* sender() { return channel_proxy_.get(); } |
| 280 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 281 | private: |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 282 | std::unique_ptr<base::Thread> thread_; |
| 283 | std::unique_ptr<QuitListener> listener_; |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 284 | std::unique_ptr<IPC::ChannelProxy> channel_proxy_; |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 285 | }; |
| 286 | |
amistry | 87fc78f | 2016-05-05 14:12:09 +0900 | [diff] [blame] | 287 | TEST_F(IPCChannelProxyTest, MessageClassFilters) { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 288 | // Construct a filter per message class. |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 289 | std::vector<scoped_refptr<MessageCountFilter>> class_filters; |
| 290 | class_filters.push_back( |
kylechar | 38823cf | 2017-09-26 05:23:56 +0900 | [diff] [blame] | 291 | base::MakeRefCounted<MessageCountFilter>(TestMsgStart)); |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 292 | class_filters.push_back( |
kylechar | 38823cf | 2017-09-26 05:23:56 +0900 | [diff] [blame] | 293 | base::MakeRefCounted<MessageCountFilter>(AutomationMsgStart)); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 294 | for (size_t i = 0; i < class_filters.size(); ++i) |
| 295 | channel_proxy()->AddFilter(class_filters[i].get()); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 296 | |
| 297 | // Send a message for each class; each filter should receive just one message. |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 298 | sender()->Send(new TestMsg_Bounce); |
| 299 | sender()->Send(new AutomationMsg_Bounce); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 300 | |
| 301 | // Send some messages not assigned to a specific or valid message class. |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 302 | sender()->Send(new WorkerMsg_Bounce); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 303 | |
| 304 | // Each filter should have received just the one sent message of the |
| 305 | // corresponding class. |
| 306 | SendQuitMessageAndWaitForIdle(); |
| 307 | for (size_t i = 0; i < class_filters.size(); ++i) |
| 308 | EXPECT_EQ(1U, class_filters[i]->messages_received()); |
| 309 | } |
| 310 | |
amistry | 87fc78f | 2016-05-05 14:12:09 +0900 | [diff] [blame] | 311 | TEST_F(IPCChannelProxyTest, GlobalAndMessageClassFilters) { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 312 | // Add a class and global filter. |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 313 | scoped_refptr<MessageCountFilter> class_filter( |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 314 | new MessageCountFilter(TestMsgStart)); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 315 | class_filter->set_message_filtering_enabled(false); |
| 316 | channel_proxy()->AddFilter(class_filter.get()); |
| 317 | |
| 318 | scoped_refptr<MessageCountFilter> global_filter(new MessageCountFilter()); |
| 319 | global_filter->set_message_filtering_enabled(false); |
| 320 | channel_proxy()->AddFilter(global_filter.get()); |
| 321 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 322 | // A message of class Test should be seen by both the global filter and |
| 323 | // Test-specific filter. |
| 324 | sender()->Send(new TestMsg_Bounce); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 325 | |
| 326 | // A message of a different class should be seen only by the global filter. |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 327 | sender()->Send(new AutomationMsg_Bounce); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 328 | |
| 329 | // Flush all messages. |
| 330 | SendQuitMessageAndWaitForIdle(); |
| 331 | |
| 332 | // The class filter should have received only the class-specific message. |
| 333 | EXPECT_EQ(1U, class_filter->messages_received()); |
| 334 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 335 | // The global filter should have received both messages, as well as the final |
| 336 | // QUIT message. |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 337 | EXPECT_EQ(3U, global_filter->messages_received()); |
| 338 | } |
| 339 | |
amistry | 87fc78f | 2016-05-05 14:12:09 +0900 | [diff] [blame] | 340 | TEST_F(IPCChannelProxyTest, FilterRemoval) { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 341 | // Add a class and global filter. |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 342 | scoped_refptr<MessageCountFilter> class_filter( |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 343 | new MessageCountFilter(TestMsgStart)); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 344 | scoped_refptr<MessageCountFilter> global_filter(new MessageCountFilter()); |
| 345 | |
| 346 | // Add and remove both types of filters. |
| 347 | channel_proxy()->AddFilter(class_filter.get()); |
| 348 | channel_proxy()->AddFilter(global_filter.get()); |
| 349 | channel_proxy()->RemoveFilter(global_filter.get()); |
| 350 | channel_proxy()->RemoveFilter(class_filter.get()); |
| 351 | |
| 352 | // Send some messages; they should not be seen by either filter. |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 353 | sender()->Send(new TestMsg_Bounce); |
Noel Gordon | ac08511 | 2017-07-06 21:48:08 +0900 | [diff] [blame] | 354 | sender()->Send(new AutomationMsg_Bounce); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 355 | |
| 356 | // Ensure that the filters were removed and did not receive any messages. |
| 357 | SendQuitMessageAndWaitForIdle(); |
dmichael@chromium.org | 663ad4f | 2014-04-24 08:54:53 +0900 | [diff] [blame] | 358 | EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, |
| 359 | global_filter->last_filter_event()); |
| 360 | EXPECT_EQ(MessageCountFilter::FILTER_REMOVED, |
| 361 | class_filter->last_filter_event()); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 362 | EXPECT_EQ(0U, class_filter->messages_received()); |
| 363 | EXPECT_EQ(0U, global_filter->messages_received()); |
| 364 | } |
| 365 | |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 366 | TEST_F(IPCChannelProxyTest, BadMessageOnListenerThread) { |
| 367 | scoped_refptr<MessageCountFilter> class_filter( |
| 368 | new MessageCountFilter(TestMsgStart)); |
| 369 | class_filter->set_message_filtering_enabled(false); |
| 370 | channel_proxy()->AddFilter(class_filter.get()); |
| 371 | |
| 372 | sender()->Send(new TestMsg_SendBadMessage()); |
| 373 | |
| 374 | SendQuitMessageAndWaitForIdle(); |
| 375 | EXPECT_TRUE(DidListenerGetBadMessage()); |
| 376 | } |
| 377 | |
| 378 | TEST_F(IPCChannelProxyTest, BadMessageOnIPCThread) { |
| 379 | scoped_refptr<MessageCountFilter> class_filter( |
| 380 | new MessageCountFilter(TestMsgStart)); |
| 381 | class_filter->set_message_filtering_enabled(true); |
| 382 | channel_proxy()->AddFilter(class_filter.get()); |
| 383 | |
| 384 | sender()->Send(new TestMsg_SendBadMessage()); |
| 385 | |
| 386 | SendQuitMessageAndWaitForIdle(); |
| 387 | EXPECT_TRUE(DidListenerGetBadMessage()); |
| 388 | } |
| 389 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 390 | class IPCChannelBadMessageTest : public IPCChannelMojoTestBase { |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 391 | public: |
Daniel Cheng | c764d4d | 2014-12-31 12:48:36 +0900 | [diff] [blame] | 392 | void SetUp() override { |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 393 | IPCChannelMojoTestBase::SetUp(); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 394 | |
| 395 | Init("ChannelProxyClient"); |
| 396 | |
| 397 | listener_.reset(new QuitListener()); |
| 398 | CreateChannel(listener_.get()); |
| 399 | ASSERT_TRUE(ConnectChannel()); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 400 | } |
| 401 | |
Daniel Cheng | c764d4d | 2014-12-31 12:48:36 +0900 | [diff] [blame] | 402 | void TearDown() override { |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 403 | IPCChannelMojoTestBase::TearDown(); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 404 | listener_.reset(); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void SendQuitMessageAndWaitForIdle() { |
| 408 | sender()->Send(new WorkerMsg_Quit); |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 409 | CreateRunLoopAndRun(&listener_->run_loop_); |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 410 | EXPECT_TRUE(WaitForClientShutdown()); |
| 411 | } |
| 412 | |
| 413 | bool DidListenerGetBadMessage() { |
| 414 | return listener_->bad_message_received_; |
| 415 | } |
| 416 | |
| 417 | private: |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 418 | std::unique_ptr<QuitListener> listener_; |
jam@chromium.org | fc09dde | 2014-05-22 01:39:18 +0900 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | TEST_F(IPCChannelBadMessageTest, BadMessage) { |
| 422 | sender()->Send(new TestMsg_SendBadMessage()); |
| 423 | SendQuitMessageAndWaitForIdle(); |
| 424 | EXPECT_TRUE(DidListenerGetBadMessage()); |
| 425 | } |
| 426 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 427 | DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(ChannelProxyClient) { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 428 | ChannelReflectorListener listener; |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 429 | Connect(&listener); |
| 430 | listener.Init(channel()); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 431 | |
Wez | e1bbde0 | 2018-02-27 05:50:47 +0900 | [diff] [blame] | 432 | CreateRunLoopAndRun(&listener.run_loop_); |
| 433 | |
sammc | e3cae21 | 2016-10-27 19:13:59 +0900 | [diff] [blame] | 434 | Close(); |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | } // namespace |