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

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
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_ = TakeBadMessageCallback();
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
56 mojo::Binding<TestBadMessages> binding_;
57 std::vector<int32_t> received_values_;
yzshen1 2016/08/03 17:10:57 This is not used.
Ken Rockot(use gerrit already) 2016/08/03 19:13:26 Removed
58
59 DISALLOW_COPY_AND_ASSIGN(TestBadMessagesImpl);
60 };
61
62 class ReportBadMessageTest : public testing::Test {
63 public:
64 ReportBadMessageTest() {}
65
66 void SetUp() override {
67 mojo::edk::SetDefaultProcessErrorCallback(
68 base::Bind(&ReportBadMessageTest::OnProcessError,
69 base::Unretained(this)));
70
71 impl_.BindImpl(GetProxy(&proxy_));
72 }
73
74 TestBadMessages* proxy() { return proxy_.get(); }
75
76 TestBadMessagesImpl* impl() { return &impl_; }
77
78 void SetErrorHandler(const base::Closure& handler) {
79 error_handler_ = handler;
80 }
81
82 private:
83 void OnProcessError(const std::string& error) {
84 if (!error_handler_.is_null())
85 error_handler_.Run();
86 }
87
88 TestBadMessagesPtr proxy_;
89 TestBadMessagesImpl impl_;
90 base::Closure error_handler_;
91 base::MessageLoop message_loop;
92 };
93
94 TEST_F(ReportBadMessageTest, Request) {
95 // Verify that basic immediate error reporting works.
96 bool error = false;
97 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
98 EXPECT_TRUE(proxy()->RejectSync());
99 EXPECT_TRUE(error);
100 }
101
102 TEST_F(ReportBadMessageTest, RequestAsync) {
103 bool error = false;
104 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
105
106 // This should capture a bad message reporting callback in the impl.
107 base::RunLoop loop;
108 proxy()->RejectEventually(loop.QuitClosure());
109 loop.Run();
110
111 EXPECT_FALSE(error);
112
113 // Now we can run the callback and it should trigger a bad message report.
114 DCHECK(!impl()->bad_message_callback().is_null());
115 impl()->bad_message_callback().Run("bad!");
116 EXPECT_TRUE(error);
117 }
118
119 TEST_F(ReportBadMessageTest, Response) {
120 bool error = false;
121 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
122
123 base::RunLoop loop;
124 proxy()->RequestResponse(
125 base::Bind([] (const base::Closure& quit) {
126 // Report a bad message inside the response callback. This should
127 // trigger the error handler.
128 ReportBadMessage("no way!");
129 quit.Run();
130 },
131 loop.QuitClosure()));
132 loop.Run();
133
134 EXPECT_TRUE(error);
135 }
136
137 TEST_F(ReportBadMessageTest, ResponseAsync) {
138 bool error = false;
139 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
140
141 ReportBadMessageCallback bad_message_callback;
142 base::RunLoop loop;
143 proxy()->RequestResponse(
144 base::Bind([] (const base::Closure& quit,
145 ReportBadMessageCallback* callback) {
146 // Capture the bad message callback inside the response callback.
147 *callback = TakeBadMessageCallback();
148 quit.Run();
149 },
150 loop.QuitClosure(), &bad_message_callback));
151 loop.Run();
152
153 EXPECT_FALSE(error);
154
155 // Invoking this callback should report a bad message and trigger the error
156 // handler immediately.
157 bad_message_callback.Run("this message is bad and should feel bad");
158 EXPECT_TRUE(error);
159 }
160
161 TEST_F(ReportBadMessageTest, ResponseSync) {
162 bool error = false;
163 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
164
165 SyncMessageResponseContext context;
166 proxy()->RequestResponseSync();
167
168 EXPECT_FALSE(error);
169 context.ReportBadMessage("i don't like this response");
170 EXPECT_TRUE(error);
171 }
172
173 TEST_F(ReportBadMessageTest, ResponseSyncDeferred) {
174 bool error = false;
175 SetErrorHandler(base::Bind([] (bool* flag) { *flag = true; }, &error));
176
177 ReportBadMessageCallback bad_message_callback;
178 {
179 SyncMessageResponseContext context;
180 proxy()->RequestResponseSync();
181 bad_message_callback = context.TakeBadMessageCallback();
182 }
183
184 EXPECT_FALSE(error);
185 bad_message_callback.Run("nope nope nope");
186 EXPECT_TRUE(error);
187 }
188
189 } // namespace
190 } // namespace test
191 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698