OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 5 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 embedder::SimplePlatformSupport platform_support_; | 51 embedder::SimplePlatformSupport platform_support_; |
52 | 52 |
53 DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcherTest); | 53 DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcherTest); |
54 }; | 54 }; |
55 | 55 |
56 // Tests valid inputs to |ValidateCreateOptions()|. | 56 // Tests valid inputs to |ValidateCreateOptions()|. |
57 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsValid) { | 57 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsValid) { |
58 // Default options. | 58 // Default options. |
59 { | 59 { |
60 MojoCreateSharedBufferOptions validated_options = {}; | 60 MojoCreateSharedBufferOptions validated_options = {}; |
61 EXPECT_EQ(MOJO_RESULT_OK, | 61 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::ValidateCreateOptions( |
62 SharedBufferDispatcher::ValidateCreateOptions( | 62 NullUserPointer(), &validated_options)); |
63 NullUserPointer(), &validated_options)); | |
64 RevalidateCreateOptions(validated_options); | 63 RevalidateCreateOptions(validated_options); |
65 } | 64 } |
66 | 65 |
67 // Different flags. | 66 // Different flags. |
68 MojoCreateSharedBufferOptionsFlags flags_values[] = { | 67 MojoCreateSharedBufferOptionsFlags flags_values[] = { |
69 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE}; | 68 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE}; |
70 for (size_t i = 0; i < arraysize(flags_values); i++) { | 69 for (size_t i = 0; i < arraysize(flags_values); i++) { |
71 const MojoCreateSharedBufferOptionsFlags flags = flags_values[i]; | 70 const MojoCreateSharedBufferOptionsFlags flags = flags_values[i]; |
72 | 71 |
73 // Different capacities (size 1). | 72 // Different capacities (size 1). |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 }; | 107 }; |
109 MojoCreateSharedBufferOptions unused; | 108 MojoCreateSharedBufferOptions unused; |
110 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, | 109 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, |
111 SharedBufferDispatcher::ValidateCreateOptions( | 110 SharedBufferDispatcher::ValidateCreateOptions( |
112 MakeUserPointer(&options), &unused)); | 111 MakeUserPointer(&options), &unused)); |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) { | 115 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) { |
117 scoped_refptr<SharedBufferDispatcher> dispatcher; | 116 scoped_refptr<SharedBufferDispatcher> dispatcher; |
118 EXPECT_EQ(MOJO_RESULT_OK, | 117 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
119 SharedBufferDispatcher::Create( | 118 platform_support(), |
120 platform_support(), | 119 SharedBufferDispatcher::kDefaultCreateOptions, |
121 SharedBufferDispatcher::kDefaultCreateOptions, | 120 100, &dispatcher)); |
122 100, | |
123 &dispatcher)); | |
124 ASSERT_TRUE(dispatcher.get()); | 121 ASSERT_TRUE(dispatcher.get()); |
125 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher->GetType()); | 122 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher->GetType()); |
126 | 123 |
127 // Make a couple of mappings. | 124 // Make a couple of mappings. |
128 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1; | 125 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1; |
129 EXPECT_EQ( | 126 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( |
130 MOJO_RESULT_OK, | 127 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1)); |
131 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1)); | |
132 ASSERT_TRUE(mapping1); | 128 ASSERT_TRUE(mapping1); |
133 ASSERT_TRUE(mapping1->GetBase()); | 129 ASSERT_TRUE(mapping1->GetBase()); |
134 EXPECT_EQ(100u, mapping1->GetLength()); | 130 EXPECT_EQ(100u, mapping1->GetLength()); |
135 // Write something. | 131 // Write something. |
136 static_cast<char*>(mapping1->GetBase())[50] = 'x'; | 132 static_cast<char*>(mapping1->GetBase())[50] = 'x'; |
137 | 133 |
138 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2; | 134 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2; |
139 EXPECT_EQ( | 135 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( |
140 MOJO_RESULT_OK, | 136 50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2)); |
141 dispatcher->MapBuffer(50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2)); | |
142 ASSERT_TRUE(mapping2); | 137 ASSERT_TRUE(mapping2); |
143 ASSERT_TRUE(mapping2->GetBase()); | 138 ASSERT_TRUE(mapping2->GetBase()); |
144 EXPECT_EQ(50u, mapping2->GetLength()); | 139 EXPECT_EQ(50u, mapping2->GetLength()); |
145 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); | 140 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); |
146 | 141 |
147 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 142 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
148 | 143 |
149 // Check that we can still read/write to mappings after the dispatcher has | 144 // Check that we can still read/write to mappings after the dispatcher has |
150 // gone away. | 145 // gone away. |
151 static_cast<char*>(mapping2->GetBase())[1] = 'y'; | 146 static_cast<char*>(mapping2->GetBase())[1] = 'y'; |
152 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); | 147 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); |
153 } | 148 } |
154 | 149 |
155 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) { | 150 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) { |
156 scoped_refptr<SharedBufferDispatcher> dispatcher1; | 151 scoped_refptr<SharedBufferDispatcher> dispatcher1; |
157 EXPECT_EQ(MOJO_RESULT_OK, | 152 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
158 SharedBufferDispatcher::Create( | 153 platform_support(), |
159 platform_support(), | 154 SharedBufferDispatcher::kDefaultCreateOptions, |
160 SharedBufferDispatcher::kDefaultCreateOptions, | 155 100, &dispatcher1)); |
161 100, | |
162 &dispatcher1)); | |
163 | 156 |
164 // Map and write something. | 157 // Map and write something. |
165 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 158 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; |
166 EXPECT_EQ( | 159 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->MapBuffer( |
167 MOJO_RESULT_OK, | 160 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
168 dispatcher1->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | |
169 static_cast<char*>(mapping->GetBase())[0] = 'x'; | 161 static_cast<char*>(mapping->GetBase())[0] = 'x'; |
170 mapping.reset(); | 162 mapping.reset(); |
171 | 163 |
172 // Duplicate |dispatcher1| and then close it. | 164 // Duplicate |dispatcher1| and then close it. |
173 scoped_refptr<Dispatcher> dispatcher2; | 165 scoped_refptr<Dispatcher> dispatcher2; |
174 EXPECT_EQ( | 166 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle( |
175 MOJO_RESULT_OK, | 167 NullUserPointer(), &dispatcher2)); |
176 dispatcher1->DuplicateBufferHandle(NullUserPointer(), &dispatcher2)); | |
177 ASSERT_TRUE(dispatcher2.get()); | 168 ASSERT_TRUE(dispatcher2.get()); |
178 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType()); | 169 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType()); |
179 | 170 |
180 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); | 171 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); |
181 | 172 |
182 // Map |dispatcher2| and read something. | 173 // Map |dispatcher2| and read something. |
183 EXPECT_EQ( | 174 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->MapBuffer( |
184 MOJO_RESULT_OK, | 175 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
185 dispatcher2->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | |
186 EXPECT_EQ('x', static_cast<char*>(mapping->GetBase())[0]); | 176 EXPECT_EQ('x', static_cast<char*>(mapping->GetBase())[0]); |
187 | 177 |
188 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close()); | 178 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close()); |
189 } | 179 } |
190 | 180 |
191 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsValid) { | 181 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsValid) { |
192 scoped_refptr<SharedBufferDispatcher> dispatcher1; | 182 scoped_refptr<SharedBufferDispatcher> dispatcher1; |
193 EXPECT_EQ(MOJO_RESULT_OK, | 183 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
194 SharedBufferDispatcher::Create( | 184 platform_support(), |
195 platform_support(), | 185 SharedBufferDispatcher::kDefaultCreateOptions, |
196 SharedBufferDispatcher::kDefaultCreateOptions, | 186 100, &dispatcher1)); |
197 100, | |
198 &dispatcher1)); | |
199 | 187 |
200 MojoDuplicateBufferHandleOptions options[] = { | 188 MojoDuplicateBufferHandleOptions options[] = { |
201 {sizeof(MojoDuplicateBufferHandleOptions), | 189 {sizeof(MojoDuplicateBufferHandleOptions), |
202 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}, | 190 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}, |
203 {sizeof(MojoDuplicateBufferHandleOptionsFlags), ~0u}}; | 191 {sizeof(MojoDuplicateBufferHandleOptionsFlags), ~0u}}; |
204 for (size_t i = 0; i < arraysize(options); i++) { | 192 for (size_t i = 0; i < arraysize(options); i++) { |
205 scoped_refptr<Dispatcher> dispatcher2; | 193 scoped_refptr<Dispatcher> dispatcher2; |
206 EXPECT_EQ(MOJO_RESULT_OK, | 194 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle( |
207 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options[i]), | 195 MakeUserPointer(&options[i]), &dispatcher2)); |
208 &dispatcher2)); | |
209 ASSERT_TRUE(dispatcher2.get()); | 196 ASSERT_TRUE(dispatcher2.get()); |
210 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType()); | 197 EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType()); |
211 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close()); | 198 EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close()); |
212 } | 199 } |
213 | 200 |
214 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); | 201 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); |
215 } | 202 } |
216 | 203 |
217 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsInvalid) { | 204 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandleOptionsInvalid) { |
218 scoped_refptr<SharedBufferDispatcher> dispatcher1; | 205 scoped_refptr<SharedBufferDispatcher> dispatcher1; |
219 EXPECT_EQ(MOJO_RESULT_OK, | 206 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
220 SharedBufferDispatcher::Create( | 207 platform_support(), |
221 platform_support(), | 208 SharedBufferDispatcher::kDefaultCreateOptions, |
222 SharedBufferDispatcher::kDefaultCreateOptions, | 209 100, &dispatcher1)); |
223 100, | |
224 &dispatcher1)); | |
225 | 210 |
226 // Invalid |struct_size|. | 211 // Invalid |struct_size|. |
227 { | 212 { |
228 MojoDuplicateBufferHandleOptions options = { | 213 MojoDuplicateBufferHandleOptions options = { |
229 1u, MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}; | 214 1u, MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}; |
230 scoped_refptr<Dispatcher> dispatcher2; | 215 scoped_refptr<Dispatcher> dispatcher2; |
231 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 216 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
232 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options), | 217 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options), |
233 &dispatcher2)); | 218 &dispatcher2)); |
234 EXPECT_FALSE(dispatcher2.get()); | 219 EXPECT_FALSE(dispatcher2.get()); |
235 } | 220 } |
236 | 221 |
237 // Unknown |flags|. | 222 // Unknown |flags|. |
238 { | 223 { |
239 MojoDuplicateBufferHandleOptions options = { | 224 MojoDuplicateBufferHandleOptions options = { |
240 sizeof(MojoDuplicateBufferHandleOptions), ~0u}; | 225 sizeof(MojoDuplicateBufferHandleOptions), ~0u}; |
241 scoped_refptr<Dispatcher> dispatcher2; | 226 scoped_refptr<Dispatcher> dispatcher2; |
242 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, | 227 EXPECT_EQ(MOJO_RESULT_UNIMPLEMENTED, |
243 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options), | 228 dispatcher1->DuplicateBufferHandle(MakeUserPointer(&options), |
244 &dispatcher2)); | 229 &dispatcher2)); |
245 EXPECT_FALSE(dispatcher2.get()); | 230 EXPECT_FALSE(dispatcher2.get()); |
246 } | 231 } |
247 | 232 |
248 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); | 233 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close()); |
249 } | 234 } |
250 | 235 |
251 TEST_F(SharedBufferDispatcherTest, CreateInvalidNumBytes) { | 236 TEST_F(SharedBufferDispatcherTest, CreateInvalidNumBytes) { |
252 // Size too big. | 237 // Size too big. |
253 scoped_refptr<SharedBufferDispatcher> dispatcher; | 238 scoped_refptr<SharedBufferDispatcher> dispatcher; |
254 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, | 239 EXPECT_EQ( |
255 SharedBufferDispatcher::Create( | 240 MOJO_RESULT_RESOURCE_EXHAUSTED, |
256 platform_support(), | 241 SharedBufferDispatcher::Create( |
257 SharedBufferDispatcher::kDefaultCreateOptions, | 242 platform_support(), SharedBufferDispatcher::kDefaultCreateOptions, |
258 std::numeric_limits<uint64_t>::max(), | 243 std::numeric_limits<uint64_t>::max(), &dispatcher)); |
259 &dispatcher)); | |
260 EXPECT_FALSE(dispatcher.get()); | 244 EXPECT_FALSE(dispatcher.get()); |
261 | 245 |
262 // Zero size. | 246 // Zero size. |
263 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 247 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
264 SharedBufferDispatcher::Create( | 248 SharedBufferDispatcher::Create( |
265 platform_support(), | 249 platform_support(), |
266 SharedBufferDispatcher::kDefaultCreateOptions, | 250 SharedBufferDispatcher::kDefaultCreateOptions, 0, &dispatcher)); |
267 0, | |
268 &dispatcher)); | |
269 EXPECT_FALSE(dispatcher.get()); | 251 EXPECT_FALSE(dispatcher.get()); |
270 } | 252 } |
271 | 253 |
272 TEST_F(SharedBufferDispatcherTest, MapBufferInvalidArguments) { | 254 TEST_F(SharedBufferDispatcherTest, MapBufferInvalidArguments) { |
273 scoped_refptr<SharedBufferDispatcher> dispatcher; | 255 scoped_refptr<SharedBufferDispatcher> dispatcher; |
274 EXPECT_EQ(MOJO_RESULT_OK, | 256 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
275 SharedBufferDispatcher::Create( | 257 platform_support(), |
276 platform_support(), | 258 SharedBufferDispatcher::kDefaultCreateOptions, |
277 SharedBufferDispatcher::kDefaultCreateOptions, | 259 100, &dispatcher)); |
278 100, | |
279 &dispatcher)); | |
280 | 260 |
281 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 261 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; |
282 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 262 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
283 dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 263 dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
284 EXPECT_FALSE(mapping); | 264 EXPECT_FALSE(mapping); |
285 | 265 |
286 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 266 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
287 dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 267 dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
288 EXPECT_FALSE(mapping); | 268 EXPECT_FALSE(mapping); |
289 | 269 |
290 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 270 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
291 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 271 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
292 EXPECT_FALSE(mapping); | 272 EXPECT_FALSE(mapping); |
293 | 273 |
294 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 274 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
295 } | 275 } |
296 | 276 |
297 } // namespace | 277 } // namespace |
298 } // namespace system | 278 } // namespace system |
299 } // namespace mojo | 279 } // namespace mojo |
OLD | NEW |