Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: mojo/public/cpp/bindings/tests/report_bad_message_unittest.cc

Issue 2202893002: Mojo C++ Bindings: Add helpers for bad message reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 "base/bind.h"
6 #include "base/callback.h"
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "mojo/edk/embedder/embedder.h"
11 #include "mojo/public/cpp/bindings/binding.h"
12 #include "mojo/public/cpp/bindings/message.h"
13 #include "mojo/public/interfaces/bindings/tests/test_bad_messages.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace test {
18 namespace {
19
20 class TestBadMessagesImpl : public TestBadMessages {
21 public:
22 TestBadMessagesImpl() : binding_(this) {}
23 ~TestBadMessagesImpl() override {}
24
25 void BindImpl(TestBadMessagesRequest request) {
26 binding_.Bind(std::move(request));
27 }
28
29 const ReportBadMessageCallback& bad_message_callback() {
30 return bad_message_callback_;
31 }
32
33 private:
34 // TestBadMessages:
35 void RejectEventually(const RejectEventuallyCallback& callback) override {
36 bad_message_callback_ = GetBadMessageCallback();
37 callback.Run();
38 }
39
40 void RequestResponse(const RequestResponseCallback& callback) override {
41 callback.Run();
42 }
43
44 void RejectSync(const RejectSyncCallback& callback) override {
45 callback.Run();
46 ReportBadMessage("go away");
47 }
48
49 void RequestResponseSync(
50 const RequestResponseSyncCallback& callback) override {
51 callback.Run();
52 }
53
54 ReportBadMessageCallback bad_message_callback_;
55 mojo::Binding<TestBadMessages> binding_;
56
57 DISALLOW_COPY_AND_ASSIGN(TestBadMessagesImpl);
58 };
59
60 class ReportBadMessageTest : public testing::Test {
61 public:
62 ReportBadMessageTest() {}
63
64 void SetUp() override {
65 mojo::edk::SetDefaultProcessErrorCallback(
66 base::Bind(&ReportBadMessageTest::OnProcessError,
67 base::Unretained(this)));
68
69 impl_.BindImpl(GetProxy(&proxy_));
70 }
71
72 TestBadMessages* proxy() { return proxy_.get(); }
73
74 TestBadMessagesImpl* impl() { return &impl_; }
75
76 void SetErrorHandler(const base::Closure& handler) {
77 error_handler_ = handler;
78 }
79
80 private:
81 void OnProcessError(const std::string& error) {
82 if (!error_handler_.is_null())
83 error_handler_.Run();
84 }
85
86 TestBadMessagesPtr proxy_;
87 TestBadMessagesImpl impl_;
88 base::Closure error_handler_;
89 base::MessageLoop message_loop;
90 };
91
92 TEST_F(ReportBadMessageTest, Request) {
93 // Verify that basic immediate error reporting works.
94 bool error = false;
95 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
96 EXPECT_TRUE(proxy()->RejectSync());
97 EXPECT_TRUE(error);
98 }
99
100 TEST_F(ReportBadMessageTest, RequestAsync) {
101 bool error = false;
102 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
103
104 // This should capture a bad message reporting callback in the impl.
105 base::RunLoop loop;
106 proxy()->RejectEventually(loop.QuitClosure());
107 loop.Run();
108
109 EXPECT_FALSE(error);
110
111 // Now we can run the callback and it should trigger a bad message report.
112 DCHECK(!impl()->bad_message_callback().is_null());
113 impl()->bad_message_callback().Run("bad!");
114 EXPECT_TRUE(error);
115 }
116
117 TEST_F(ReportBadMessageTest, Response) {
118 bool error = false;
119 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
120
121 base::RunLoop loop;
122 proxy()->RequestResponse(
123 base::Bind([] (const base::Closure& quit) {
124 // Report a bad message inside the response callback. This should
125 // trigger the error handler.
126 ReportBadMessage("no way!");
127 quit.Run();
128 },
129 loop.QuitClosure()));
130 loop.Run();
131
132 EXPECT_TRUE(error);
133 }
134
135 TEST_F(ReportBadMessageTest, ResponseAsync) {
136 bool error = false;
137 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
138
139 ReportBadMessageCallback bad_message_callback;
140 base::RunLoop loop;
141 proxy()->RequestResponse(
142 base::Bind([] (const base::Closure& quit,
143 ReportBadMessageCallback* callback) {
144 // Capture the bad message callback inside the response callback.
145 *callback = GetBadMessageCallback();
146 quit.Run();
147 },
148 loop.QuitClosure(), &bad_message_callback));
149 loop.Run();
150
151 EXPECT_FALSE(error);
152
153 // Invoking this callback should report a bad message and trigger the error
154 // handler immediately.
155 bad_message_callback.Run("this message is bad and should feel bad");
156 EXPECT_TRUE(error);
157 }
158
159 TEST_F(ReportBadMessageTest, ResponseSync) {
160 bool error = false;
161 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
162
163 SyncMessageResponseContext context;
164 proxy()->RequestResponseSync();
165
166 EXPECT_FALSE(error);
167 context.ReportBadMessage("i don't like this response");
168 EXPECT_TRUE(error);
169 }
170
171 TEST_F(ReportBadMessageTest, ResponseSyncDeferred) {
172 bool error = false;
173 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
174
175 ReportBadMessageCallback bad_message_callback;
176 {
177 SyncMessageResponseContext context;
178 proxy()->RequestResponseSync();
179 bad_message_callback = context.GetBadMessageCallback();
180 }
181
182 EXPECT_FALSE(error);
183 bad_message_callback.Run("nope nope nope");
184 EXPECT_TRUE(error);
185 }
186
187 } // namespace
188 } // namespace test
189 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/interfaces/bindings/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698