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