1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/mojo/stash_backend.h"
#include <stdint.h>
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "mojo/shell/public/interfaces/interface_provider.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace {
// Create a data pipe, write some data to the producer handle and return the
// consumer handle.
mojo::ScopedHandle CreateReadableHandle() {
mojo::ScopedDataPipeConsumerHandle consumer_handle;
mojo::ScopedDataPipeProducerHandle producer_handle;
MojoCreateDataPipeOptions options = {
sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1,
};
MojoResult result =
mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle);
EXPECT_EQ(MOJO_RESULT_OK, result);
uint32_t num_bytes = 1;
result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes,
MOJO_WRITE_DATA_FLAG_NONE);
EXPECT_EQ(MOJO_RESULT_OK, result);
EXPECT_EQ(1u, num_bytes);
return mojo::ScopedHandle::From(std::move(consumer_handle));
}
} // namespace
class StashServiceTest : public testing::Test {
public:
enum Event {
EVENT_NONE,
EVENT_STASH_RETRIEVED,
EVENT_HANDLE_READY,
};
StashServiceTest() {}
void SetUp() override {
expecting_error_ = false;
expected_event_ = EVENT_NONE;
stash_backend_.reset(new StashBackend(base::Bind(
&StashServiceTest::OnHandleReadyToRead, base::Unretained(this))));
stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_));
stash_service_.set_connection_error_handler(base::Bind(&OnConnectionError));
handles_ready_ = 0;
}
static void OnConnectionError() { FAIL() << "Unexpected connection error"; }
mojo::Array<StashedObjectPtr> RetrieveStash() {
mojo::Array<StashedObjectPtr> stash;
stash_service_->RetrieveStash(base::Bind(
&StashServiceTest::StashRetrieved, base::Unretained(this), &stash));
WaitForEvent(EVENT_STASH_RETRIEVED);
return stash;
}
void StashRetrieved(mojo::Array<StashedObjectPtr>* output,
mojo::Array<StashedObjectPtr> stash) {
*output = std::move(stash);
EventReceived(EVENT_STASH_RETRIEVED);
}
void WaitForEvent(Event event) {
expected_event_ = event;
base::RunLoop run_loop;
stop_run_loop_ = run_loop.QuitClosure();
run_loop.Run();
}
void EventReceived(Event event) {
if (event == expected_event_ && !stop_run_loop_.is_null())
stop_run_loop_.Run();
}
void OnHandleReadyToRead() {
handles_ready_++;
EventReceived(EVENT_HANDLE_READY);
}
protected:
base::MessageLoop message_loop_;
base::Closure stop_run_loop_;
scoped_ptr<StashBackend> stash_backend_;
Event expected_event_;
bool expecting_error_;
mojo::InterfacePtr<StashService> stash_service_;
int handles_ready_;
private:
DISALLOW_COPY_AND_ASSIGN(StashServiceTest);
};
// Test that adding stashed objects in multiple calls can all be retrieved by a
// Retrieve call.
TEST_F(StashServiceTest, AddTwiceAndRetrieve) {
mojo::Array<StashedObjectPtr> stashed_objects;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->data.push_back(1);
stashed_object->stashed_handles = mojo::Array<mojo::ScopedHandle>();
stashed_objects.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stashed_objects));
stashed_object = StashedObject::New();
stashed_object->id = "test type2";
stashed_object->data.push_back(2);
stashed_object->data.push_back(3);
stashed_object->stashed_handles = mojo::Array<mojo::ScopedHandle>();
stashed_objects.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stashed_objects));
stashed_objects = RetrieveStash();
ASSERT_EQ(2u, stashed_objects.size());
EXPECT_EQ("test type", stashed_objects[0]->id);
EXPECT_EQ(0u, stashed_objects[0]->stashed_handles.size());
EXPECT_EQ(1u, stashed_objects[0]->data.size());
EXPECT_EQ(1, stashed_objects[0]->data[0]);
EXPECT_EQ("test type2", stashed_objects[1]->id);
EXPECT_EQ(0u, stashed_objects[1]->stashed_handles.size());
EXPECT_EQ(2u, stashed_objects[1]->data.size());
EXPECT_EQ(2, stashed_objects[1]->data[0]);
EXPECT_EQ(3, stashed_objects[1]->data[1]);
}
// Test that handles survive a round-trip through the stash.
TEST_F(StashServiceTest, StashAndRetrieveHandles) {
mojo::Array<StashedObjectPtr> stashed_objects;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->data.push_back(1);
mojo::ScopedDataPipeConsumerHandle consumer;
mojo::ScopedDataPipeProducerHandle producer;
MojoCreateDataPipeOptions options = {
sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1,
};
mojo::CreateDataPipe(&options, &producer, &consumer);
uint32_t num_bytes = 1;
MojoResult result = mojo::WriteDataRaw(
producer.get(), "1", &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
ASSERT_EQ(MOJO_RESULT_OK, result);
ASSERT_EQ(1u, num_bytes);
stashed_object->stashed_handles.push_back(
mojo::ScopedHandle::From(std::move(producer)));
stashed_object->stashed_handles.push_back(
mojo::ScopedHandle::From(std::move(consumer)));
stashed_objects.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stashed_objects));
stashed_objects = RetrieveStash();
ASSERT_EQ(1u, stashed_objects.size());
EXPECT_EQ("test type", stashed_objects[0]->id);
ASSERT_EQ(2u, stashed_objects[0]->stashed_handles.size());
consumer = mojo::ScopedDataPipeConsumerHandle::From(
std::move(stashed_objects[0]->stashed_handles[1]));
result = mojo::Wait(
consumer.get(), MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
nullptr);
ASSERT_EQ(MOJO_RESULT_OK, result);
char data = '\0';
result = mojo::ReadDataRaw(
consumer.get(), &data, &num_bytes, MOJO_READ_DATA_FLAG_ALL_OR_NONE);
ASSERT_EQ(MOJO_RESULT_OK, result);
ASSERT_EQ(1u, num_bytes);
EXPECT_EQ('1', data);
}
TEST_F(StashServiceTest, RetrieveWithoutStashing) {
mojo::Array<StashedObjectPtr> stashed_objects = RetrieveStash();
ASSERT_TRUE(!stashed_objects.is_null());
EXPECT_EQ(0u, stashed_objects.size());
}
TEST_F(StashServiceTest, NotifyOnReadableHandle) {
mojo::Array<StashedObjectPtr> stash_entries;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->data.push_back(0);
stashed_object->monitor_handles = true;
mojo::shell::mojom::InterfaceProviderPtr service_provider;
// Stash the ServiceProvider request. When we make a call on
// |service_provider|, the stashed handle will become readable.
stashed_object->stashed_handles.push_back(mojo::ScopedHandle::From(
mojo::GetProxy(&service_provider).PassMessagePipe()));
stash_entries.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stash_entries));
mojo::MessagePipe pipe;
service_provider->GetInterface("", std::move(pipe.handle0));
WaitForEvent(EVENT_HANDLE_READY);
EXPECT_EQ(1, handles_ready_);
}
TEST_F(StashServiceTest, NotifyOnReadableDataPipeHandle) {
mojo::Array<StashedObjectPtr> stash_entries;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->monitor_handles = true;
MojoCreateDataPipeOptions options = {
sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1,
};
mojo::ScopedDataPipeConsumerHandle consumer_handle;
mojo::ScopedDataPipeProducerHandle producer_handle;
uint32_t num_bytes = 1;
MojoResult result =
mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle);
ASSERT_EQ(MOJO_RESULT_OK, result);
result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes,
MOJO_WRITE_DATA_FLAG_NONE);
ASSERT_EQ(MOJO_RESULT_OK, result);
ASSERT_EQ(1u, num_bytes);
stashed_object->stashed_handles.push_back(
mojo::ScopedHandle::From(std::move(producer_handle)));
stashed_object->stashed_handles.push_back(
mojo::ScopedHandle::From(std::move(consumer_handle)));
stashed_object->data.push_back(1);
stash_entries.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stash_entries));
WaitForEvent(EVENT_HANDLE_READY);
EXPECT_EQ(1, handles_ready_);
}
TEST_F(StashServiceTest, NotifyOncePerStashOnReadableHandles) {
mojo::Array<StashedObjectPtr> stash_entries;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->data.push_back(1);
stashed_object->monitor_handles = true;
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stash_entries.push_back(std::move(stashed_object));
stashed_object = StashedObject::New();
stashed_object->id = "another test type";
stashed_object->data.push_back(2);
stashed_object->monitor_handles = true;
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stash_entries.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stash_entries));
WaitForEvent(EVENT_HANDLE_READY);
EXPECT_EQ(1, handles_ready_);
stashed_object = StashedObject::New();
stashed_object->id = "yet another test type";
stashed_object->data.push_back(3);
stashed_object->monitor_handles = true;
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stashed_object->stashed_handles.push_back(CreateReadableHandle());
stash_entries.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stash_entries));
stash_service_->AddToStash(RetrieveStash());
WaitForEvent(EVENT_HANDLE_READY);
EXPECT_EQ(2, handles_ready_);
}
// Test that a stash service discards stashed objects when the backend no longer
// exists.
TEST_F(StashServiceTest, ServiceWithDeletedBackend) {
stash_backend_.reset();
stash_service_.set_connection_error_handler(base::Bind(&OnConnectionError));
mojo::Array<StashedObjectPtr> stashed_objects;
StashedObjectPtr stashed_object(StashedObject::New());
stashed_object->id = "test type";
stashed_object->data.push_back(1);
mojo::MessagePipe message_pipe;
stashed_object->stashed_handles.push_back(
mojo::ScopedHandle::From(std::move(message_pipe.handle0)));
stashed_objects.push_back(std::move(stashed_object));
stash_service_->AddToStash(std::move(stashed_objects));
stashed_objects = RetrieveStash();
ASSERT_EQ(0u, stashed_objects.size());
// Check that the stashed handle has been closed.
MojoResult result =
mojo::Wait(message_pipe.handle1.get(),
MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, nullptr);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
}
} // namespace extensions
|