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