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

Side by Side Diff: mojo/edk/system/shared_buffer_dispatcher_unittest.cc

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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/edk/system/shared_buffer_dispatcher.cc ('k') | mojo/edk/system/simple_dispatcher.h » ('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 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 "mojo/edk/system/shared_buffer_dispatcher.h"
6
7 #include <limits>
8
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "mojo/edk/embedder/platform_shared_buffer.h"
12 #include "mojo/edk/embedder/simple_platform_support.h"
13 #include "mojo/edk/system/dispatcher.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace system {
18 namespace {
19
20 // NOTE(vtl): There's currently not much to test for in
21 // |SharedBufferDispatcher::ValidateCreateOptions()|, but the tests should be
22 // expanded if/when options are added, so I've kept the general form of the
23 // tests from data_pipe_unittest.cc.
24
25 const uint32_t kSizeOfCreateOptions = sizeof(MojoCreateSharedBufferOptions);
26
27 // Does a cursory sanity check of |validated_options|. Calls
28 // |ValidateCreateOptions()| on already-validated options. The validated options
29 // should be valid, and the revalidated copy should be the same.
30 void RevalidateCreateOptions(
31 const MojoCreateSharedBufferOptions& validated_options) {
32 EXPECT_EQ(kSizeOfCreateOptions, validated_options.struct_size);
33 // Nothing to check for flags.
34
35 MojoCreateSharedBufferOptions revalidated_options = {};
36 EXPECT_EQ(MOJO_RESULT_OK,
37 SharedBufferDispatcher::ValidateCreateOptions(
38 MakeUserPointer(&validated_options), &revalidated_options));
39 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size);
40 EXPECT_EQ(validated_options.flags, revalidated_options.flags);
41 }
42
43 class SharedBufferDispatcherTest : public testing::Test {
44 public:
45 SharedBufferDispatcherTest() {}
46 ~SharedBufferDispatcherTest() override {}
47
48 embedder::PlatformSupport* platform_support() { return &platform_support_; }
49
50 private:
51 embedder::SimplePlatformSupport platform_support_;
52
53 DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcherTest);
54 };
55
56 // Tests valid inputs to |ValidateCreateOptions()|.
57 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsValid) {
58 // Default options.
59 {
60 MojoCreateSharedBufferOptions validated_options = {};
61 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::ValidateCreateOptions(
62 NullUserPointer(), &validated_options));
63 RevalidateCreateOptions(validated_options);
64 }
65
66 // Different flags.
67 MojoCreateSharedBufferOptionsFlags flags_values[] = {
68 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE};
69 for (size_t i = 0; i < arraysize(flags_values); i++) {
70 const MojoCreateSharedBufferOptionsFlags flags = flags_values[i];
71
72 // Different capacities (size 1).
73 for (uint32_t capacity = 1; capacity <= 100 * 1000 * 1000; capacity *= 10) {
74 MojoCreateSharedBufferOptions options = {
75 kSizeOfCreateOptions, // |struct_size|.
76 flags // |flags|.
77 };
78 MojoCreateSharedBufferOptions validated_options = {};
79 EXPECT_EQ(MOJO_RESULT_OK,
80 SharedBufferDispatcher::ValidateCreateOptions(
81 MakeUserPointer(&options), &validated_options))
82 << capacity;
83 RevalidateCreateOptions(validated_options);
84 EXPECT_EQ(options.flags, validated_options.flags);
85 }
86 }
87 }
88
89 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsInvalid) {
90 // Invalid |struct_size|.
91 {
92 MojoCreateSharedBufferOptions options = {
93 1, // |struct_size|.
94 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE // |flags|.
95 };
96 MojoCreateSharedBufferOptions unused;
97 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
98 SharedBufferDispatcher::ValidateCreateOptions(
99 MakeUserPointer(&options), &unused));
100 }
101
102 // Unknown |flags|.
103 {
104 MojoCreateSharedBufferOptions options = {
105 kSizeOfCreateOptions, // |struct_size|.
106 ~0u // |flags|.
107 };
108 MojoCreateSharedBufferOptions unused;
109 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED,
110 SharedBufferDispatcher::ValidateCreateOptions(
111 MakeUserPointer(&options), &unused));
112 }
113 }
114
115 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) {
116 scoped_refptr<SharedBufferDispatcher> dispatcher;
117 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create(
118 platform_support(),
119 SharedBufferDispatcher::kDefaultCreateOptions,
120 100, &dispatcher));
121 ASSERT_TRUE(dispatcher);
122 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher->GetType());
123
124 // Make a couple of mappings.
125 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1;
126 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer(
127 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1));
128 ASSERT_TRUE(mapping1);
129 ASSERT_TRUE(mapping1->GetBase());
130 EXPECT_EQ(100u, mapping1->GetLength());
131 // Write something.
132 static_cast<char*>(mapping1->GetBase())[50] = 'x';
133
134 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2;
135 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer(
136 50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2));
137 ASSERT_TRUE(mapping2);
138 ASSERT_TRUE(mapping2->GetBase());
139 EXPECT_EQ(50u, mapping2->GetLength());
140 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]);
141
142 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
143
144 // Check that we can still read/write to mappings after the dispatcher has
145 // gone away.
146 static_cast<char*>(mapping2->GetBase())[1] = 'y';
147 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]);
148 }
149
150 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) {
151 scoped_refptr<SharedBufferDispatcher> dispatcher1;
152 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create(
153 platform_support(),
154 SharedBufferDispatcher::kDefaultCreateOptions,
155 100, &dispatcher1));
156
157 // Map and write something.
158 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping;
159 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->MapBuffer(
160 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
161 static_cast<char*>(mapping->GetBase())[0] = 'x';
162 mapping.reset();
163
164 // Duplicate |dispatcher1| and then close it.
165 scoped_refptr<Dispatcher> dispatcher2;
166 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle(
167 NullUserPointer(), &dispatcher2));
168 ASSERT_TRUE(dispatcher2);
169 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType());
170
171 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close());
172
173 // Map |dispatcher2| and read something.
174 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->MapBuffer(
175 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
176 EXPECT_EQ('x', static_cast<char*>(mapping->GetBase())[0]);
177
178 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close());
179 }
180
181 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsValid) {
182 scoped_refptr<SharedBufferDispatcher> dispatcher1;
183 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create(
184 platform_support(),
185 SharedBufferDispatcher::kDefaultCreateOptions,
186 100, &dispatcher1));
187
188 MojoDuplicateBufferHandleOptions options[] = {
189 {sizeof(MojoDuplicateBufferHandleOptions),
190 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE},
191 {sizeof(MojoDuplicateBufferHandleOptionsFlags), ~0u}};
192 for (size_t i = 0; i < arraysize(options); i++) {
193 scoped_refptr<Dispatcher> dispatcher2;
194 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle(
195 MakeUserPointer(&options[i]), &dispatcher2));
196 ASSERT_TRUE(dispatcher2);
197 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType());
198 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close());
199 }
200
201 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close());
202 }
203
204 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsInvalid) {
205 scoped_refptr<SharedBufferDispatcher> dispatcher1;
206 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create(
207 platform_support(),
208 SharedBufferDispatcher::kDefaultCreateOptions,
209 100, &dispatcher1));
210
211 // Invalid |struct_size|.
212 {
213 MojoDuplicateBufferHandleOptions options = {
214 1u, MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE};
215 scoped_refptr<Dispatcher> dispatcher2;
216 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
217 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options),
218 &dispatcher2));
219 EXPECT_FALSE(dispatcher2);
220 }
221
222 // Unknown |flags|.
223 {
224 MojoDuplicateBufferHandleOptions options = {
225 sizeof(MojoDuplicateBufferHandleOptions), ~0u};
226 scoped_refptr<Dispatcher> dispatcher2;
227 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED,
228 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options),
229 &dispatcher2));
230 EXPECT_FALSE(dispatcher2);
231 }
232
233 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close());
234 }
235
236 TEST_F(SharedBufferDispatcherTest, CreateInvalidNumBytes) {
237 // Size too big.
238 scoped_refptr<SharedBufferDispatcher> dispatcher;
239 EXPECT_EQ(
240 MOJO_RESULT_RESOURCE_EXHAUSTED,
241 SharedBufferDispatcher::Create(
242 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions,
243 std::numeric_limits<uint64_t>::max(), &dispatcher));
244 EXPECT_FALSE(dispatcher);
245
246 // Zero size.
247 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
248 SharedBufferDispatcher::Create(
249 platform_support(),
250 SharedBufferDispatcher::kDefaultCreateOptions, 0, &dispatcher));
251 EXPECT_FALSE(dispatcher);
252 }
253
254 TEST_F(SharedBufferDispatcherTest, MapBufferInvalidArguments) {
255 scoped_refptr<SharedBufferDispatcher> dispatcher;
256 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create(
257 platform_support(),
258 SharedBufferDispatcher::kDefaultCreateOptions,
259 100, &dispatcher));
260
261 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping;
262 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
263 dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
264 EXPECT_FALSE(mapping);
265
266 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
267 dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
268 EXPECT_FALSE(mapping);
269
270 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
271 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
272 EXPECT_FALSE(mapping);
273
274 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
275 }
276
277 } // namespace
278 } // namespace system
279 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/shared_buffer_dispatcher.cc ('k') | mojo/edk/system/simple_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698