OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/local_data_pipe.h" | 5 #include "mojo/edk/system/local_data_pipe.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
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" |
11 #include "mojo/edk/system/data_pipe.h" | 11 #include "mojo/edk/system/data_pipe.h" |
12 #include "mojo/edk/system/waiter.h" | 12 #include "mojo/edk/system/waiter.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace system { | 16 namespace system { |
17 namespace { | 17 namespace { |
18 | 18 |
19 const uint32_t kSizeOfOptions = | 19 const uint32_t kSizeOfOptions = |
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
21 | 21 |
22 // Validate options. | 22 // Validate options. |
23 TEST(LocalDataPipeTest, Creation) { | 23 TEST(LocalDataPipeTest, Creation) { |
24 // Create using default options. | 24 // Create using default options. |
25 { | 25 { |
26 // Get default options. | 26 // Get default options. |
27 MojoCreateDataPipeOptions default_options = {0}; | 27 MojoCreateDataPipeOptions default_options = {0}; |
28 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 28 EXPECT_EQ( |
29 NullUserPointer(), &default_options)); | 29 MOJO_RESULT_OK, |
| 30 DataPipe::ValidateCreateOptions(NullUserPointer(), &default_options)); |
30 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); | 31 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); |
31 dp->ProducerClose(); | 32 dp->ProducerClose(); |
32 dp->ConsumerClose(); | 33 dp->ConsumerClose(); |
33 } | 34 } |
34 | 35 |
35 // Create using non-default options. | 36 // Create using non-default options. |
36 { | 37 { |
37 const MojoCreateDataPipeOptions options = { | 38 const MojoCreateDataPipeOptions options = { |
38 kSizeOfOptions, // |struct_size|. | 39 kSizeOfOptions, // |struct_size|. |
39 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 40 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 } | 98 } |
98 | 99 |
99 TEST(LocalDataPipeTest, SimpleReadWrite) { | 100 TEST(LocalDataPipeTest, SimpleReadWrite) { |
100 const MojoCreateDataPipeOptions options = { | 101 const MojoCreateDataPipeOptions options = { |
101 kSizeOfOptions, // |struct_size|. | 102 kSizeOfOptions, // |struct_size|. |
102 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 103 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
103 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 104 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
104 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 105 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
105 }; | 106 }; |
106 MojoCreateDataPipeOptions validated_options = {0}; | 107 MojoCreateDataPipeOptions validated_options = {0}; |
107 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 108 EXPECT_EQ(MOJO_RESULT_OK, |
108 MakeUserPointer(&options), &validated_options)); | 109 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 110 &validated_options)); |
109 | 111 |
110 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 112 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
111 | 113 |
112 int32_t elements[10] = {0}; | 114 int32_t elements[10] = {0}; |
113 uint32_t num_bytes = 0; | 115 uint32_t num_bytes = 0; |
114 | 116 |
115 // Try reading; nothing there yet. | 117 // Try reading; nothing there yet. |
116 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); | 118 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); |
117 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 119 EXPECT_EQ( |
118 dp->ConsumerReadData(UserPointer<void>(elements), | 120 MOJO_RESULT_SHOULD_WAIT, |
119 MakeUserPointer(&num_bytes), false, false)); | 121 dp->ConsumerReadData(UserPointer<void>(elements), |
| 122 MakeUserPointer(&num_bytes), |
| 123 false, |
| 124 false)); |
120 | 125 |
121 // Query; nothing there yet. | 126 // Query; nothing there yet. |
122 num_bytes = 0; | 127 num_bytes = 0; |
123 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 128 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
124 EXPECT_EQ(0u, num_bytes); | 129 EXPECT_EQ(0u, num_bytes); |
125 | 130 |
126 // Discard; nothing there yet. | 131 // Discard; nothing there yet. |
127 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0])); | 132 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0])); |
128 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 133 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
129 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); | 134 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); |
130 | 135 |
131 // Read with invalid |num_bytes|. | 136 // Read with invalid |num_bytes|. |
132 num_bytes = sizeof(elements[0]) + 1; | 137 num_bytes = sizeof(elements[0]) + 1; |
133 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 138 EXPECT_EQ( |
134 dp->ConsumerReadData(UserPointer<void>(elements), | 139 MOJO_RESULT_INVALID_ARGUMENT, |
135 MakeUserPointer(&num_bytes), false, false)); | 140 dp->ConsumerReadData(UserPointer<void>(elements), |
| 141 MakeUserPointer(&num_bytes), |
| 142 false, |
| 143 false)); |
136 | 144 |
137 // Write two elements. | 145 // Write two elements. |
138 elements[0] = 123; | 146 elements[0] = 123; |
139 elements[1] = 456; | 147 elements[1] = 456; |
140 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 148 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
141 EXPECT_EQ(MOJO_RESULT_OK, | 149 EXPECT_EQ(MOJO_RESULT_OK, |
142 dp->ProducerWriteData(UserPointer<const void>(elements), | 150 dp->ProducerWriteData(UserPointer<const void>(elements), |
143 MakeUserPointer(&num_bytes), false)); | 151 MakeUserPointer(&num_bytes), |
| 152 false)); |
144 // It should have written everything (even without "all or none"). | 153 // It should have written everything (even without "all or none"). |
145 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes); | 154 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes); |
146 | 155 |
147 // Query. | 156 // Query. |
148 num_bytes = 0; | 157 num_bytes = 0; |
149 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 158 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
150 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes); | 159 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes); |
151 | 160 |
152 // Read one element. | 161 // Read one element. |
153 elements[0] = -1; | 162 elements[0] = -1; |
154 elements[1] = -1; | 163 elements[1] = -1; |
155 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 164 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
156 EXPECT_EQ(MOJO_RESULT_OK, | 165 EXPECT_EQ( |
157 dp->ConsumerReadData(UserPointer<void>(elements), | 166 MOJO_RESULT_OK, |
158 MakeUserPointer(&num_bytes), false, false)); | 167 dp->ConsumerReadData(UserPointer<void>(elements), |
| 168 MakeUserPointer(&num_bytes), |
| 169 false, |
| 170 false)); |
159 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); | 171 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); |
160 EXPECT_EQ(123, elements[0]); | 172 EXPECT_EQ(123, elements[0]); |
161 EXPECT_EQ(-1, elements[1]); | 173 EXPECT_EQ(-1, elements[1]); |
162 | 174 |
163 // Query. | 175 // Query. |
164 num_bytes = 0; | 176 num_bytes = 0; |
165 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 177 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
166 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); | 178 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); |
167 | 179 |
168 // Peek one element. | 180 // Peek one element. |
169 elements[0] = -1; | 181 elements[0] = -1; |
170 elements[1] = -1; | 182 elements[1] = -1; |
171 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 183 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
172 EXPECT_EQ(MOJO_RESULT_OK, | 184 EXPECT_EQ( |
173 dp->ConsumerReadData(UserPointer<void>(elements), | 185 MOJO_RESULT_OK, |
174 MakeUserPointer(&num_bytes), false, true)); | 186 dp->ConsumerReadData(UserPointer<void>(elements), |
| 187 MakeUserPointer(&num_bytes), |
| 188 false, |
| 189 true)); |
175 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); | 190 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); |
176 EXPECT_EQ(456, elements[0]); | 191 EXPECT_EQ(456, elements[0]); |
177 EXPECT_EQ(-1, elements[1]); | 192 EXPECT_EQ(-1, elements[1]); |
178 | 193 |
179 // Query. Still has 1 element remaining. | 194 // Query. Still has 1 element remaining. |
180 num_bytes = 0; | 195 num_bytes = 0; |
181 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 196 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
182 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); | 197 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); |
183 | 198 |
184 // Try to read two elements, with "all or none". | 199 // Try to read two elements, with "all or none". |
185 elements[0] = -1; | 200 elements[0] = -1; |
186 elements[1] = -1; | 201 elements[1] = -1; |
187 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 202 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
188 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 203 EXPECT_EQ( |
189 dp->ConsumerReadData(UserPointer<void>(elements), | 204 MOJO_RESULT_OUT_OF_RANGE, |
190 MakeUserPointer(&num_bytes), true, false)); | 205 dp->ConsumerReadData(UserPointer<void>(elements), |
| 206 MakeUserPointer(&num_bytes), |
| 207 true, |
| 208 false)); |
191 EXPECT_EQ(-1, elements[0]); | 209 EXPECT_EQ(-1, elements[0]); |
192 EXPECT_EQ(-1, elements[1]); | 210 EXPECT_EQ(-1, elements[1]); |
193 | 211 |
194 // Try to read two elements, without "all or none". | 212 // Try to read two elements, without "all or none". |
195 elements[0] = -1; | 213 elements[0] = -1; |
196 elements[1] = -1; | 214 elements[1] = -1; |
197 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 215 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
198 EXPECT_EQ(MOJO_RESULT_OK, | 216 EXPECT_EQ( |
199 dp->ConsumerReadData(UserPointer<void>(elements), | 217 MOJO_RESULT_OK, |
200 MakeUserPointer(&num_bytes), false, false)); | 218 dp->ConsumerReadData(UserPointer<void>(elements), |
| 219 MakeUserPointer(&num_bytes), |
| 220 false, |
| 221 false)); |
201 EXPECT_EQ(456, elements[0]); | 222 EXPECT_EQ(456, elements[0]); |
202 EXPECT_EQ(-1, elements[1]); | 223 EXPECT_EQ(-1, elements[1]); |
203 | 224 |
204 // Query. | 225 // Query. |
205 num_bytes = 0; | 226 num_bytes = 0; |
206 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 227 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
207 EXPECT_EQ(0u, num_bytes); | 228 EXPECT_EQ(0u, num_bytes); |
208 | 229 |
209 dp->ProducerClose(); | 230 dp->ProducerClose(); |
210 dp->ConsumerClose(); | 231 dp->ConsumerClose(); |
211 } | 232 } |
212 | 233 |
213 // Note: The "basic" waiting tests test that the "wait states" are correct in | 234 // Note: The "basic" waiting tests test that the "wait states" are correct in |
214 // various situations; they don't test that waiters are properly awoken on state | 235 // various situations; they don't test that waiters are properly awoken on state |
215 // changes. (For that, we need to use multiple threads.) | 236 // changes. (For that, we need to use multiple threads.) |
216 TEST(LocalDataPipeTest, BasicProducerWaiting) { | 237 TEST(LocalDataPipeTest, BasicProducerWaiting) { |
217 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities | 238 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities |
218 // are strict maximums. This is not guaranteed by the API. | 239 // are strict maximums. This is not guaranteed by the API. |
219 | 240 |
220 const MojoCreateDataPipeOptions options = { | 241 const MojoCreateDataPipeOptions options = { |
221 kSizeOfOptions, // |struct_size|. | 242 kSizeOfOptions, // |struct_size|. |
222 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 243 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
223 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 244 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
224 2 * sizeof(int32_t) // |capacity_num_bytes|. | 245 2 * sizeof(int32_t) // |capacity_num_bytes|. |
225 }; | 246 }; |
226 MojoCreateDataPipeOptions validated_options = {0}; | 247 MojoCreateDataPipeOptions validated_options = {0}; |
227 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 248 EXPECT_EQ(MOJO_RESULT_OK, |
228 MakeUserPointer(&options), &validated_options)); | 249 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 250 &validated_options)); |
229 | 251 |
230 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 252 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
231 Waiter waiter; | 253 Waiter waiter; |
232 uint32_t context = 0; | 254 uint32_t context = 0; |
233 HandleSignalsState hss; | 255 HandleSignalsState hss; |
234 | 256 |
235 // Never readable. | 257 // Never readable. |
236 waiter.Init(); | 258 waiter.Init(); |
237 hss = HandleSignalsState(); | 259 hss = HandleSignalsState(); |
238 EXPECT_EQ( | 260 EXPECT_EQ( |
239 MOJO_RESULT_FAILED_PRECONDITION, | 261 MOJO_RESULT_FAILED_PRECONDITION, |
240 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 262 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); |
241 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 263 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
242 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 264 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
243 | 265 |
244 // Already writable. | 266 // Already writable. |
245 waiter.Init(); | 267 waiter.Init(); |
246 hss = HandleSignalsState(); | 268 hss = HandleSignalsState(); |
247 EXPECT_EQ( | 269 EXPECT_EQ( |
248 MOJO_RESULT_ALREADY_EXISTS, | 270 MOJO_RESULT_ALREADY_EXISTS, |
249 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss)); | 271 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss)); |
250 | 272 |
251 // Write two elements. | 273 // Write two elements. |
252 int32_t elements[2] = {123, 456}; | 274 int32_t elements[2] = {123, 456}; |
253 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 275 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
254 EXPECT_EQ(MOJO_RESULT_OK, | 276 EXPECT_EQ(MOJO_RESULT_OK, |
255 dp->ProducerWriteData(UserPointer<const void>(elements), | 277 dp->ProducerWriteData(UserPointer<const void>(elements), |
256 MakeUserPointer(&num_bytes), true)); | 278 MakeUserPointer(&num_bytes), |
| 279 true)); |
257 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); | 280 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
258 | 281 |
259 // Adding a waiter should now succeed. | 282 // Adding a waiter should now succeed. |
260 waiter.Init(); | 283 waiter.Init(); |
261 ASSERT_EQ( | 284 ASSERT_EQ( |
262 MOJO_RESULT_OK, | 285 MOJO_RESULT_OK, |
263 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); | 286 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); |
264 // And it shouldn't be writable yet. | 287 // And it shouldn't be writable yet. |
265 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 288 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
266 hss = HandleSignalsState(); | 289 hss = HandleSignalsState(); |
267 dp->ProducerRemoveWaiter(&waiter, &hss); | 290 dp->ProducerRemoveWaiter(&waiter, &hss); |
268 EXPECT_EQ(0u, hss.satisfied_signals); | 291 EXPECT_EQ(0u, hss.satisfied_signals); |
269 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 292 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
270 | 293 |
271 // Peek one element. | 294 // Peek one element. |
272 elements[0] = -1; | 295 elements[0] = -1; |
273 elements[1] = -1; | 296 elements[1] = -1; |
274 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 297 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
275 EXPECT_EQ(MOJO_RESULT_OK, | 298 EXPECT_EQ( |
276 dp->ConsumerReadData(UserPointer<void>(elements), | 299 MOJO_RESULT_OK, |
277 MakeUserPointer(&num_bytes), true, true)); | 300 dp->ConsumerReadData(UserPointer<void>(elements), |
| 301 MakeUserPointer(&num_bytes), |
| 302 true, |
| 303 true)); |
278 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 304 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
279 EXPECT_EQ(123, elements[0]); | 305 EXPECT_EQ(123, elements[0]); |
280 EXPECT_EQ(-1, elements[1]); | 306 EXPECT_EQ(-1, elements[1]); |
281 | 307 |
282 // Add a waiter. | 308 // Add a waiter. |
283 waiter.Init(); | 309 waiter.Init(); |
284 ASSERT_EQ( | 310 ASSERT_EQ( |
285 MOJO_RESULT_OK, | 311 MOJO_RESULT_OK, |
286 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); | 312 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr)); |
287 // And it still shouldn't be writable yet. | 313 // And it still shouldn't be writable yet. |
288 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 314 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
289 hss = HandleSignalsState(); | 315 hss = HandleSignalsState(); |
290 dp->ProducerRemoveWaiter(&waiter, &hss); | 316 dp->ProducerRemoveWaiter(&waiter, &hss); |
291 EXPECT_EQ(0u, hss.satisfied_signals); | 317 EXPECT_EQ(0u, hss.satisfied_signals); |
292 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 318 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
293 | 319 |
294 // Do it again. | 320 // Do it again. |
295 waiter.Init(); | 321 waiter.Init(); |
296 ASSERT_EQ( | 322 ASSERT_EQ( |
297 MOJO_RESULT_OK, | 323 MOJO_RESULT_OK, |
298 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, nullptr)); | 324 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, nullptr)); |
299 | 325 |
300 // Read one element. | 326 // Read one element. |
301 elements[0] = -1; | 327 elements[0] = -1; |
302 elements[1] = -1; | 328 elements[1] = -1; |
303 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 329 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
304 EXPECT_EQ(MOJO_RESULT_OK, | 330 EXPECT_EQ( |
305 dp->ConsumerReadData(UserPointer<void>(elements), | 331 MOJO_RESULT_OK, |
306 MakeUserPointer(&num_bytes), true, false)); | 332 dp->ConsumerReadData(UserPointer<void>(elements), |
| 333 MakeUserPointer(&num_bytes), |
| 334 true, |
| 335 false)); |
307 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 336 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
308 EXPECT_EQ(123, elements[0]); | 337 EXPECT_EQ(123, elements[0]); |
309 EXPECT_EQ(-1, elements[1]); | 338 EXPECT_EQ(-1, elements[1]); |
310 | 339 |
311 // Waiting should now succeed. | 340 // Waiting should now succeed. |
312 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 341 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
313 EXPECT_EQ(78u, context); | 342 EXPECT_EQ(78u, context); |
314 hss = HandleSignalsState(); | 343 hss = HandleSignalsState(); |
315 dp->ProducerRemoveWaiter(&waiter, &hss); | 344 dp->ProducerRemoveWaiter(&waiter, &hss); |
316 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 345 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
317 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 346 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
318 | 347 |
319 // Try writing, using a two-phase write. | 348 // Try writing, using a two-phase write. |
320 void* buffer = nullptr; | 349 void* buffer = nullptr; |
321 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 350 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
322 EXPECT_EQ(MOJO_RESULT_OK, | 351 EXPECT_EQ(MOJO_RESULT_OK, |
323 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), | 352 dp->ProducerBeginWriteData( |
324 MakeUserPointer(&num_bytes), false)); | 353 MakeUserPointer(&buffer), MakeUserPointer(&num_bytes), false)); |
325 EXPECT_TRUE(buffer); | 354 EXPECT_TRUE(buffer); |
326 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 355 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
327 | 356 |
328 static_cast<int32_t*>(buffer)[0] = 789; | 357 static_cast<int32_t*>(buffer)[0] = 789; |
329 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( | 358 EXPECT_EQ(MOJO_RESULT_OK, |
330 1u * sizeof(elements[0])))); | 359 dp->ProducerEndWriteData( |
| 360 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
331 | 361 |
332 // Add a waiter. | 362 // Add a waiter. |
333 waiter.Init(); | 363 waiter.Init(); |
334 ASSERT_EQ( | 364 ASSERT_EQ( |
335 MOJO_RESULT_OK, | 365 MOJO_RESULT_OK, |
336 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, nullptr)); | 366 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, nullptr)); |
337 | 367 |
338 // Read one element, using a two-phase read. | 368 // Read one element, using a two-phase read. |
339 const void* read_buffer = nullptr; | 369 const void* read_buffer = nullptr; |
340 num_bytes = 0u; | 370 num_bytes = 0u; |
341 EXPECT_EQ(MOJO_RESULT_OK, | 371 EXPECT_EQ( |
342 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 372 MOJO_RESULT_OK, |
343 MakeUserPointer(&num_bytes), false)); | 373 dp->ConsumerBeginReadData( |
| 374 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), false)); |
344 EXPECT_TRUE(read_buffer); | 375 EXPECT_TRUE(read_buffer); |
345 // Since we only read one element (after having written three in all), the | 376 // Since we only read one element (after having written three in all), the |
346 // two-phase read should only allow us to read one. This checks an | 377 // two-phase read should only allow us to read one. This checks an |
347 // implementation detail! | 378 // implementation detail! |
348 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 379 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
349 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); | 380 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); |
350 EXPECT_EQ( | 381 EXPECT_EQ( |
351 MOJO_RESULT_OK, | 382 MOJO_RESULT_OK, |
352 dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0])))); | 383 dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0])))); |
353 | 384 |
354 // Waiting should succeed. | 385 // Waiting should succeed. |
355 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 386 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
356 EXPECT_EQ(90u, context); | 387 EXPECT_EQ(90u, context); |
357 hss = HandleSignalsState(); | 388 hss = HandleSignalsState(); |
358 dp->ProducerRemoveWaiter(&waiter, &hss); | 389 dp->ProducerRemoveWaiter(&waiter, &hss); |
359 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 390 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
360 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 391 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
361 | 392 |
362 // Write one element. | 393 // Write one element. |
363 elements[0] = 123; | 394 elements[0] = 123; |
364 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 395 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
365 EXPECT_EQ(MOJO_RESULT_OK, | 396 EXPECT_EQ(MOJO_RESULT_OK, |
366 dp->ProducerWriteData(UserPointer<const void>(elements), | 397 dp->ProducerWriteData(UserPointer<const void>(elements), |
367 MakeUserPointer(&num_bytes), false)); | 398 MakeUserPointer(&num_bytes), |
| 399 false)); |
368 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 400 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
369 | 401 |
370 // Add a waiter. | 402 // Add a waiter. |
371 waiter.Init(); | 403 waiter.Init(); |
372 ASSERT_EQ( | 404 ASSERT_EQ( |
373 MOJO_RESULT_OK, | 405 MOJO_RESULT_OK, |
374 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, nullptr)); | 406 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, nullptr)); |
375 | 407 |
376 // Close the consumer. | 408 // Close the consumer. |
377 dp->ConsumerClose(); | 409 dp->ConsumerClose(); |
(...skipping 10 matching lines...) Expand all Loading... |
388 } | 420 } |
389 | 421 |
390 TEST(LocalDataPipeTest, BasicConsumerWaiting) { | 422 TEST(LocalDataPipeTest, BasicConsumerWaiting) { |
391 const MojoCreateDataPipeOptions options = { | 423 const MojoCreateDataPipeOptions options = { |
392 kSizeOfOptions, // |struct_size|. | 424 kSizeOfOptions, // |struct_size|. |
393 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 425 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
394 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 426 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
395 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 427 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
396 }; | 428 }; |
397 MojoCreateDataPipeOptions validated_options = {0}; | 429 MojoCreateDataPipeOptions validated_options = {0}; |
398 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 430 EXPECT_EQ(MOJO_RESULT_OK, |
399 MakeUserPointer(&options), &validated_options)); | 431 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 432 &validated_options)); |
400 | 433 |
401 { | 434 { |
402 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 435 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
403 Waiter waiter; | 436 Waiter waiter; |
404 uint32_t context = 0; | 437 uint32_t context = 0; |
405 HandleSignalsState hss; | 438 HandleSignalsState hss; |
406 | 439 |
407 // Never writable. | 440 // Never writable. |
408 waiter.Init(); | 441 waiter.Init(); |
409 hss = HandleSignalsState(); | 442 hss = HandleSignalsState(); |
410 EXPECT_EQ( | 443 EXPECT_EQ( |
411 MOJO_RESULT_FAILED_PRECONDITION, | 444 MOJO_RESULT_FAILED_PRECONDITION, |
412 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, &hss)); | 445 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, &hss)); |
413 EXPECT_EQ(0u, hss.satisfied_signals); | 446 EXPECT_EQ(0u, hss.satisfied_signals); |
414 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 447 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
415 | 448 |
416 // Not yet readable. | 449 // Not yet readable. |
417 waiter.Init(); | 450 waiter.Init(); |
418 ASSERT_EQ(MOJO_RESULT_OK, | 451 ASSERT_EQ(MOJO_RESULT_OK, |
419 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, | 452 dp->ConsumerAddWaiter( |
420 nullptr)); | 453 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, nullptr)); |
421 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 454 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
422 hss = HandleSignalsState(); | 455 hss = HandleSignalsState(); |
423 dp->ConsumerRemoveWaiter(&waiter, &hss); | 456 dp->ConsumerRemoveWaiter(&waiter, &hss); |
424 EXPECT_EQ(0u, hss.satisfied_signals); | 457 EXPECT_EQ(0u, hss.satisfied_signals); |
425 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 458 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
426 | 459 |
427 // Write two elements. | 460 // Write two elements. |
428 int32_t elements[2] = {123, 456}; | 461 int32_t elements[2] = {123, 456}; |
429 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 462 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
430 EXPECT_EQ(MOJO_RESULT_OK, | 463 EXPECT_EQ(MOJO_RESULT_OK, |
431 dp->ProducerWriteData(UserPointer<const void>(elements), | 464 dp->ProducerWriteData(UserPointer<const void>(elements), |
432 MakeUserPointer(&num_bytes), true)); | 465 MakeUserPointer(&num_bytes), |
| 466 true)); |
433 | 467 |
434 // Should already be readable. | 468 // Should already be readable. |
435 waiter.Init(); | 469 waiter.Init(); |
436 hss = HandleSignalsState(); | 470 hss = HandleSignalsState(); |
437 EXPECT_EQ( | 471 EXPECT_EQ( |
438 MOJO_RESULT_ALREADY_EXISTS, | 472 MOJO_RESULT_ALREADY_EXISTS, |
439 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, &hss)); | 473 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, &hss)); |
440 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 474 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
441 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 475 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
442 | 476 |
443 // Discard one element. | 477 // Discard one element. |
444 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 478 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
445 EXPECT_EQ(MOJO_RESULT_OK, | 479 EXPECT_EQ(MOJO_RESULT_OK, |
446 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 480 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
447 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 481 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
448 | 482 |
449 // Should still be readable. | 483 // Should still be readable. |
450 waiter.Init(); | 484 waiter.Init(); |
451 hss = HandleSignalsState(); | 485 hss = HandleSignalsState(); |
452 EXPECT_EQ( | 486 EXPECT_EQ( |
453 MOJO_RESULT_ALREADY_EXISTS, | 487 MOJO_RESULT_ALREADY_EXISTS, |
454 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); | 488 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); |
455 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 489 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
456 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 490 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
457 | 491 |
458 // Peek one element. | 492 // Peek one element. |
459 elements[0] = -1; | 493 elements[0] = -1; |
460 elements[1] = -1; | 494 elements[1] = -1; |
461 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 495 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
462 EXPECT_EQ(MOJO_RESULT_OK, | 496 EXPECT_EQ( |
463 dp->ConsumerReadData(UserPointer<void>(elements), | 497 MOJO_RESULT_OK, |
464 MakeUserPointer(&num_bytes), true, true)); | 498 dp->ConsumerReadData(UserPointer<void>(elements), |
| 499 MakeUserPointer(&num_bytes), |
| 500 true, |
| 501 true)); |
465 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 502 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
466 EXPECT_EQ(456, elements[0]); | 503 EXPECT_EQ(456, elements[0]); |
467 EXPECT_EQ(-1, elements[1]); | 504 EXPECT_EQ(-1, elements[1]); |
468 | 505 |
469 // Should still be readable. | 506 // Should still be readable. |
470 waiter.Init(); | 507 waiter.Init(); |
471 hss = HandleSignalsState(); | 508 hss = HandleSignalsState(); |
472 EXPECT_EQ( | 509 EXPECT_EQ( |
473 MOJO_RESULT_ALREADY_EXISTS, | 510 MOJO_RESULT_ALREADY_EXISTS, |
474 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); | 511 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss)); |
475 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 512 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
476 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 513 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
477 | 514 |
478 // Read one element. | 515 // Read one element. |
479 elements[0] = -1; | 516 elements[0] = -1; |
480 elements[1] = -1; | 517 elements[1] = -1; |
481 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 518 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
482 EXPECT_EQ(MOJO_RESULT_OK, | 519 EXPECT_EQ( |
483 dp->ConsumerReadData(UserPointer<void>(elements), | 520 MOJO_RESULT_OK, |
484 MakeUserPointer(&num_bytes), true, false)); | 521 dp->ConsumerReadData(UserPointer<void>(elements), |
| 522 MakeUserPointer(&num_bytes), |
| 523 true, |
| 524 false)); |
485 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 525 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
486 EXPECT_EQ(456, elements[0]); | 526 EXPECT_EQ(456, elements[0]); |
487 EXPECT_EQ(-1, elements[1]); | 527 EXPECT_EQ(-1, elements[1]); |
488 | 528 |
489 // Adding a waiter should now succeed. | 529 // Adding a waiter should now succeed. |
490 waiter.Init(); | 530 waiter.Init(); |
491 ASSERT_EQ(MOJO_RESULT_OK, | 531 ASSERT_EQ(MOJO_RESULT_OK, |
492 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, | 532 dp->ConsumerAddWaiter( |
493 nullptr)); | 533 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, nullptr)); |
494 | 534 |
495 // Write one element. | 535 // Write one element. |
496 elements[0] = 789; | 536 elements[0] = 789; |
497 elements[1] = -1; | 537 elements[1] = -1; |
498 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 538 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
499 EXPECT_EQ(MOJO_RESULT_OK, | 539 EXPECT_EQ(MOJO_RESULT_OK, |
500 dp->ProducerWriteData(UserPointer<const void>(elements), | 540 dp->ProducerWriteData(UserPointer<const void>(elements), |
501 MakeUserPointer(&num_bytes), true)); | 541 MakeUserPointer(&num_bytes), |
| 542 true)); |
502 | 543 |
503 // Waiting should now succeed. | 544 // Waiting should now succeed. |
504 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 545 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
505 EXPECT_EQ(90u, context); | 546 EXPECT_EQ(90u, context); |
506 hss = HandleSignalsState(); | 547 hss = HandleSignalsState(); |
507 dp->ConsumerRemoveWaiter(&waiter, &hss); | 548 dp->ConsumerRemoveWaiter(&waiter, &hss); |
508 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 549 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
509 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 550 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
510 | 551 |
511 // Close the producer. | 552 // Close the producer. |
512 dp->ProducerClose(); | 553 dp->ProducerClose(); |
513 | 554 |
514 // Should still be readable. | 555 // Should still be readable. |
515 waiter.Init(); | 556 waiter.Init(); |
516 hss = HandleSignalsState(); | 557 hss = HandleSignalsState(); |
517 EXPECT_EQ( | 558 EXPECT_EQ( |
518 MOJO_RESULT_ALREADY_EXISTS, | 559 MOJO_RESULT_ALREADY_EXISTS, |
519 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 560 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); |
520 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 561 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
521 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 562 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
522 | 563 |
523 // Read one element. | 564 // Read one element. |
524 elements[0] = -1; | 565 elements[0] = -1; |
525 elements[1] = -1; | 566 elements[1] = -1; |
526 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 567 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
527 EXPECT_EQ(MOJO_RESULT_OK, | 568 EXPECT_EQ( |
528 dp->ConsumerReadData(UserPointer<void>(elements), | 569 MOJO_RESULT_OK, |
529 MakeUserPointer(&num_bytes), true, false)); | 570 dp->ConsumerReadData(UserPointer<void>(elements), |
| 571 MakeUserPointer(&num_bytes), |
| 572 true, |
| 573 false)); |
530 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 574 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
531 EXPECT_EQ(789, elements[0]); | 575 EXPECT_EQ(789, elements[0]); |
532 EXPECT_EQ(-1, elements[1]); | 576 EXPECT_EQ(-1, elements[1]); |
533 | 577 |
534 // Should be never-readable. | 578 // Should be never-readable. |
535 waiter.Init(); | 579 waiter.Init(); |
536 hss = HandleSignalsState(); | 580 hss = HandleSignalsState(); |
537 EXPECT_EQ( | 581 EXPECT_EQ( |
538 MOJO_RESULT_FAILED_PRECONDITION, | 582 MOJO_RESULT_FAILED_PRECONDITION, |
539 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); | 583 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); |
(...skipping 10 matching lines...) Expand all Loading... |
550 Waiter waiter; | 594 Waiter waiter; |
551 uint32_t context = 0; | 595 uint32_t context = 0; |
552 HandleSignalsState hss; | 596 HandleSignalsState hss; |
553 | 597 |
554 // Write two elements. | 598 // Write two elements. |
555 int32_t* elements = nullptr; | 599 int32_t* elements = nullptr; |
556 void* buffer = nullptr; | 600 void* buffer = nullptr; |
557 // Request room for three (but we'll only write two). | 601 // Request room for three (but we'll only write two). |
558 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 602 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
559 EXPECT_EQ(MOJO_RESULT_OK, | 603 EXPECT_EQ(MOJO_RESULT_OK, |
560 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), | 604 dp->ProducerBeginWriteData( |
561 MakeUserPointer(&num_bytes), true)); | 605 MakeUserPointer(&buffer), MakeUserPointer(&num_bytes), true)); |
562 EXPECT_TRUE(buffer); | 606 EXPECT_TRUE(buffer); |
563 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); | 607 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); |
564 elements = static_cast<int32_t*>(buffer); | 608 elements = static_cast<int32_t*>(buffer); |
565 elements[0] = 123; | 609 elements[0] = 123; |
566 elements[1] = 456; | 610 elements[1] = 456; |
567 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(static_cast<uint32_t>( | 611 EXPECT_EQ(MOJO_RESULT_OK, |
568 2u * sizeof(elements[0])))); | 612 dp->ProducerEndWriteData( |
| 613 static_cast<uint32_t>(2u * sizeof(elements[0])))); |
569 | 614 |
570 // Should already be readable. | 615 // Should already be readable. |
571 waiter.Init(); | 616 waiter.Init(); |
572 hss = HandleSignalsState(); | 617 hss = HandleSignalsState(); |
573 EXPECT_EQ( | 618 EXPECT_EQ( |
574 MOJO_RESULT_ALREADY_EXISTS, | 619 MOJO_RESULT_ALREADY_EXISTS, |
575 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 620 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); |
576 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 621 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
577 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 622 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
578 | 623 |
579 // Read one element. | 624 // Read one element. |
580 // Request two in all-or-none mode, but only read one. | 625 // Request two in all-or-none mode, but only read one. |
581 const void* read_buffer = nullptr; | 626 const void* read_buffer = nullptr; |
582 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 627 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
583 EXPECT_EQ(MOJO_RESULT_OK, | 628 EXPECT_EQ( |
584 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 629 MOJO_RESULT_OK, |
585 MakeUserPointer(&num_bytes), true)); | 630 dp->ConsumerBeginReadData( |
| 631 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), true)); |
586 EXPECT_TRUE(read_buffer); | 632 EXPECT_TRUE(read_buffer); |
587 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); | 633 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
588 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); | 634 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); |
589 EXPECT_EQ(123, read_elements[0]); | 635 EXPECT_EQ(123, read_elements[0]); |
590 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( | 636 EXPECT_EQ(MOJO_RESULT_OK, |
591 1u * sizeof(elements[0])))); | 637 dp->ConsumerEndReadData( |
| 638 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
592 | 639 |
593 // Should still be readable. | 640 // Should still be readable. |
594 waiter.Init(); | 641 waiter.Init(); |
595 hss = HandleSignalsState(); | 642 hss = HandleSignalsState(); |
596 EXPECT_EQ( | 643 EXPECT_EQ( |
597 MOJO_RESULT_ALREADY_EXISTS, | 644 MOJO_RESULT_ALREADY_EXISTS, |
598 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); | 645 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss)); |
599 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 646 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
600 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 647 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
601 | 648 |
602 // Read one element. | 649 // Read one element. |
603 // Request three, but not in all-or-none mode. | 650 // Request three, but not in all-or-none mode. |
604 read_buffer = nullptr; | 651 read_buffer = nullptr; |
605 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 652 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
606 EXPECT_EQ(MOJO_RESULT_OK, | 653 EXPECT_EQ( |
607 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), | 654 MOJO_RESULT_OK, |
608 MakeUserPointer(&num_bytes), false)); | 655 dp->ConsumerBeginReadData( |
| 656 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), false)); |
609 EXPECT_TRUE(read_buffer); | 657 EXPECT_TRUE(read_buffer); |
610 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 658 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
611 read_elements = static_cast<const int32_t*>(read_buffer); | 659 read_elements = static_cast<const int32_t*>(read_buffer); |
612 EXPECT_EQ(456, read_elements[0]); | 660 EXPECT_EQ(456, read_elements[0]); |
613 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(static_cast<uint32_t>( | 661 EXPECT_EQ(MOJO_RESULT_OK, |
614 1u * sizeof(elements[0])))); | 662 dp->ConsumerEndReadData( |
| 663 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
615 | 664 |
616 // Adding a waiter should now succeed. | 665 // Adding a waiter should now succeed. |
617 waiter.Init(); | 666 waiter.Init(); |
618 ASSERT_EQ(MOJO_RESULT_OK, | 667 ASSERT_EQ(MOJO_RESULT_OK, |
619 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, | 668 dp->ConsumerAddWaiter( |
620 nullptr)); | 669 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, nullptr)); |
621 | 670 |
622 // Close the producer. | 671 // Close the producer. |
623 dp->ProducerClose(); | 672 dp->ProducerClose(); |
624 | 673 |
625 // Should be never-readable. | 674 // Should be never-readable. |
626 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); | 675 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); |
627 EXPECT_EQ(56u, context); | 676 EXPECT_EQ(56u, context); |
628 hss = HandleSignalsState(); | 677 hss = HandleSignalsState(); |
629 dp->ConsumerRemoveWaiter(&waiter, &hss); | 678 dp->ConsumerRemoveWaiter(&waiter, &hss); |
630 EXPECT_EQ(0u, hss.satisfied_signals); | 679 EXPECT_EQ(0u, hss.satisfied_signals); |
631 EXPECT_EQ(0u, hss.satisfiable_signals); | 680 EXPECT_EQ(0u, hss.satisfiable_signals); |
632 | 681 |
633 dp->ConsumerClose(); | 682 dp->ConsumerClose(); |
634 } | 683 } |
635 } | 684 } |
636 | 685 |
637 // Tests that data pipes aren't writable/readable during two-phase writes/reads. | 686 // Tests that data pipes aren't writable/readable during two-phase writes/reads. |
638 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { | 687 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { |
639 const MojoCreateDataPipeOptions options = { | 688 const MojoCreateDataPipeOptions options = { |
640 kSizeOfOptions, // |struct_size|. | 689 kSizeOfOptions, // |struct_size|. |
641 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 690 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
642 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 691 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
643 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 692 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
644 }; | 693 }; |
645 MojoCreateDataPipeOptions validated_options = {0}; | 694 MojoCreateDataPipeOptions validated_options = {0}; |
646 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 695 EXPECT_EQ(MOJO_RESULT_OK, |
647 MakeUserPointer(&options), &validated_options)); | 696 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 697 &validated_options)); |
648 | 698 |
649 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 699 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
650 Waiter waiter; | 700 Waiter waiter; |
651 HandleSignalsState hss; | 701 HandleSignalsState hss; |
652 | 702 |
653 // It should be writable. | 703 // It should be writable. |
654 waiter.Init(); | 704 waiter.Init(); |
655 hss = HandleSignalsState(); | 705 hss = HandleSignalsState(); |
656 EXPECT_EQ( | 706 EXPECT_EQ( |
657 MOJO_RESULT_ALREADY_EXISTS, | 707 MOJO_RESULT_ALREADY_EXISTS, |
658 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 708 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
659 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
660 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 710 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
661 | 711 |
662 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 712 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
663 void* write_ptr = nullptr; | 713 void* write_ptr = nullptr; |
664 EXPECT_EQ(MOJO_RESULT_OK, | 714 EXPECT_EQ( |
665 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 715 MOJO_RESULT_OK, |
666 MakeUserPointer(&num_bytes), false)); | 716 dp->ProducerBeginWriteData( |
| 717 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
667 EXPECT_TRUE(write_ptr); | 718 EXPECT_TRUE(write_ptr); |
668 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 719 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
669 | 720 |
670 // At this point, it shouldn't be writable. | 721 // At this point, it shouldn't be writable. |
671 waiter.Init(); | 722 waiter.Init(); |
672 ASSERT_EQ( | 723 ASSERT_EQ( |
673 MOJO_RESULT_OK, | 724 MOJO_RESULT_OK, |
674 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, nullptr)); | 725 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, nullptr)); |
675 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 726 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
676 hss = HandleSignalsState(); | 727 hss = HandleSignalsState(); |
677 dp->ProducerRemoveWaiter(&waiter, &hss); | 728 dp->ProducerRemoveWaiter(&waiter, &hss); |
678 EXPECT_EQ(0u, hss.satisfied_signals); | 729 EXPECT_EQ(0u, hss.satisfied_signals); |
679 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 730 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
680 | 731 |
681 // It shouldn't be readable yet either. | 732 // It shouldn't be readable yet either. |
682 waiter.Init(); | 733 waiter.Init(); |
683 ASSERT_EQ( | 734 ASSERT_EQ( |
684 MOJO_RESULT_OK, | 735 MOJO_RESULT_OK, |
685 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2, nullptr)); | 736 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2, nullptr)); |
686 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 737 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
687 hss = HandleSignalsState(); | 738 hss = HandleSignalsState(); |
688 dp->ConsumerRemoveWaiter(&waiter, &hss); | 739 dp->ConsumerRemoveWaiter(&waiter, &hss); |
689 EXPECT_EQ(0u, hss.satisfied_signals); | 740 EXPECT_EQ(0u, hss.satisfied_signals); |
690 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 741 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
691 | 742 |
692 static_cast<int32_t*>(write_ptr)[0] = 123; | 743 static_cast<int32_t*>(write_ptr)[0] = 123; |
693 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( | 744 EXPECT_EQ( |
694 static_cast<uint32_t>(1u * sizeof(int32_t)))); | 745 MOJO_RESULT_OK, |
| 746 dp->ProducerEndWriteData(static_cast<uint32_t>(1u * sizeof(int32_t)))); |
695 | 747 |
696 // It should be writable again. | 748 // It should be writable again. |
697 waiter.Init(); | 749 waiter.Init(); |
698 hss = HandleSignalsState(); | 750 hss = HandleSignalsState(); |
699 EXPECT_EQ( | 751 EXPECT_EQ( |
700 MOJO_RESULT_ALREADY_EXISTS, | 752 MOJO_RESULT_ALREADY_EXISTS, |
701 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3, &hss)); | 753 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3, &hss)); |
702 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 754 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
703 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 755 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
704 | 756 |
705 // And readable. | 757 // And readable. |
706 waiter.Init(); | 758 waiter.Init(); |
707 hss = HandleSignalsState(); | 759 hss = HandleSignalsState(); |
708 EXPECT_EQ( | 760 EXPECT_EQ( |
709 MOJO_RESULT_ALREADY_EXISTS, | 761 MOJO_RESULT_ALREADY_EXISTS, |
710 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4, &hss)); | 762 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4, &hss)); |
711 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 763 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
712 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 764 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
713 | 765 |
714 // Start another two-phase write and check that it's readable even in the | 766 // Start another two-phase write and check that it's readable even in the |
715 // middle of it. | 767 // middle of it. |
716 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 768 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
717 write_ptr = nullptr; | 769 write_ptr = nullptr; |
718 EXPECT_EQ(MOJO_RESULT_OK, | 770 EXPECT_EQ( |
719 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 771 MOJO_RESULT_OK, |
720 MakeUserPointer(&num_bytes), false)); | 772 dp->ProducerBeginWriteData( |
| 773 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
721 EXPECT_TRUE(write_ptr); | 774 EXPECT_TRUE(write_ptr); |
722 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 775 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
723 | 776 |
724 // It should be readable. | 777 // It should be readable. |
725 waiter.Init(); | 778 waiter.Init(); |
726 hss = HandleSignalsState(); | 779 hss = HandleSignalsState(); |
727 EXPECT_EQ( | 780 EXPECT_EQ( |
728 MOJO_RESULT_ALREADY_EXISTS, | 781 MOJO_RESULT_ALREADY_EXISTS, |
729 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); | 782 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); |
730 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 783 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
731 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 784 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
732 | 785 |
733 // End the two-phase write without writing anything. | 786 // End the two-phase write without writing anything. |
734 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 787 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
735 | 788 |
736 // Start a two-phase read. | 789 // Start a two-phase read. |
737 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 790 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
738 const void* read_ptr = nullptr; | 791 const void* read_ptr = nullptr; |
739 EXPECT_EQ(MOJO_RESULT_OK, | 792 EXPECT_EQ( |
740 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 793 MOJO_RESULT_OK, |
741 MakeUserPointer(&num_bytes), false)); | 794 dp->ConsumerBeginReadData( |
| 795 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false)); |
742 EXPECT_TRUE(read_ptr); | 796 EXPECT_TRUE(read_ptr); |
743 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); | 797 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); |
744 | 798 |
745 // At this point, it should still be writable. | 799 // At this point, it should still be writable. |
746 waiter.Init(); | 800 waiter.Init(); |
747 hss = HandleSignalsState(); | 801 hss = HandleSignalsState(); |
748 EXPECT_EQ( | 802 EXPECT_EQ( |
749 MOJO_RESULT_ALREADY_EXISTS, | 803 MOJO_RESULT_ALREADY_EXISTS, |
750 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); | 804 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); |
751 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 805 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
(...skipping 28 matching lines...) Expand all Loading... |
780 | 834 |
781 // Test that a "may discard" data pipe is writable even when it's full. | 835 // Test that a "may discard" data pipe is writable even when it's full. |
782 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { | 836 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { |
783 const MojoCreateDataPipeOptions options = { | 837 const MojoCreateDataPipeOptions options = { |
784 kSizeOfOptions, // |struct_size|. | 838 kSizeOfOptions, // |struct_size|. |
785 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 839 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
786 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 840 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
787 1 * sizeof(int32_t) // |capacity_num_bytes|. | 841 1 * sizeof(int32_t) // |capacity_num_bytes|. |
788 }; | 842 }; |
789 MojoCreateDataPipeOptions validated_options = {0}; | 843 MojoCreateDataPipeOptions validated_options = {0}; |
790 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 844 EXPECT_EQ(MOJO_RESULT_OK, |
791 MakeUserPointer(&options), &validated_options)); | 845 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 846 &validated_options)); |
792 | 847 |
793 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 848 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
794 Waiter waiter; | 849 Waiter waiter; |
795 HandleSignalsState hss; | 850 HandleSignalsState hss; |
796 | 851 |
797 // Writable. | 852 // Writable. |
798 waiter.Init(); | 853 waiter.Init(); |
799 hss = HandleSignalsState(); | 854 hss = HandleSignalsState(); |
800 EXPECT_EQ( | 855 EXPECT_EQ( |
801 MOJO_RESULT_ALREADY_EXISTS, | 856 MOJO_RESULT_ALREADY_EXISTS, |
802 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 857 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
803 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 858 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
804 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 859 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
805 | 860 |
806 // Not readable. | 861 // Not readable. |
807 waiter.Init(); | 862 waiter.Init(); |
808 ASSERT_EQ( | 863 ASSERT_EQ( |
809 MOJO_RESULT_OK, | 864 MOJO_RESULT_OK, |
810 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1, nullptr)); | 865 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1, nullptr)); |
811 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); | 866 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr)); |
812 hss = HandleSignalsState(); | 867 hss = HandleSignalsState(); |
813 dp->ConsumerRemoveWaiter(&waiter, &hss); | 868 dp->ConsumerRemoveWaiter(&waiter, &hss); |
814 EXPECT_EQ(0u, hss.satisfied_signals); | 869 EXPECT_EQ(0u, hss.satisfied_signals); |
815 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 870 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
816 | 871 |
817 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 872 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
818 int32_t element = 123; | 873 int32_t element = 123; |
819 EXPECT_EQ(MOJO_RESULT_OK, | 874 EXPECT_EQ(MOJO_RESULT_OK, |
820 dp->ProducerWriteData(UserPointer<const void>(&element), | 875 dp->ProducerWriteData(UserPointer<const void>(&element), |
821 MakeUserPointer(&num_bytes), false)); | 876 MakeUserPointer(&num_bytes), |
| 877 false)); |
822 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 878 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
823 | 879 |
824 // Still writable (even though it's full). | 880 // Still writable (even though it's full). |
825 waiter.Init(); | 881 waiter.Init(); |
826 hss = HandleSignalsState(); | 882 hss = HandleSignalsState(); |
827 EXPECT_EQ( | 883 EXPECT_EQ( |
828 MOJO_RESULT_ALREADY_EXISTS, | 884 MOJO_RESULT_ALREADY_EXISTS, |
829 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2, &hss)); | 885 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2, &hss)); |
830 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 886 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
831 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 887 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
832 | 888 |
833 // Now readable. | 889 // Now readable. |
834 waiter.Init(); | 890 waiter.Init(); |
835 hss = HandleSignalsState(); | 891 hss = HandleSignalsState(); |
836 EXPECT_EQ( | 892 EXPECT_EQ( |
837 MOJO_RESULT_ALREADY_EXISTS, | 893 MOJO_RESULT_ALREADY_EXISTS, |
838 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3, &hss)); | 894 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3, &hss)); |
839 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 895 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
840 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 896 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
841 | 897 |
842 // Overwrite that element. | 898 // Overwrite that element. |
843 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 899 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
844 element = 456; | 900 element = 456; |
845 EXPECT_EQ(MOJO_RESULT_OK, | 901 EXPECT_EQ(MOJO_RESULT_OK, |
846 dp->ProducerWriteData(UserPointer<const void>(&element), | 902 dp->ProducerWriteData(UserPointer<const void>(&element), |
847 MakeUserPointer(&num_bytes), false)); | 903 MakeUserPointer(&num_bytes), |
| 904 false)); |
848 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 905 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
849 | 906 |
850 // Still writable. | 907 // Still writable. |
851 waiter.Init(); | 908 waiter.Init(); |
852 hss = HandleSignalsState(); | 909 hss = HandleSignalsState(); |
853 EXPECT_EQ( | 910 EXPECT_EQ( |
854 MOJO_RESULT_ALREADY_EXISTS, | 911 MOJO_RESULT_ALREADY_EXISTS, |
855 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4, &hss)); | 912 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4, &hss)); |
856 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 913 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
857 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); | 914 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals); |
858 | 915 |
859 // And still readable. | 916 // And still readable. |
860 waiter.Init(); | 917 waiter.Init(); |
861 hss = HandleSignalsState(); | 918 hss = HandleSignalsState(); |
862 EXPECT_EQ( | 919 EXPECT_EQ( |
863 MOJO_RESULT_ALREADY_EXISTS, | 920 MOJO_RESULT_ALREADY_EXISTS, |
864 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); | 921 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss)); |
865 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 922 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
866 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); | 923 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals); |
867 | 924 |
868 // Read that element. | 925 // Read that element. |
869 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); | 926 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); |
870 element = 0; | 927 element = 0; |
871 EXPECT_EQ(MOJO_RESULT_OK, | 928 EXPECT_EQ( |
872 dp->ConsumerReadData(UserPointer<void>(&element), | 929 MOJO_RESULT_OK, |
873 MakeUserPointer(&num_bytes), false, false)); | 930 dp->ConsumerReadData(UserPointer<void>(&element), |
| 931 MakeUserPointer(&num_bytes), |
| 932 false, |
| 933 false)); |
874 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); | 934 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); |
875 EXPECT_EQ(456, element); | 935 EXPECT_EQ(456, element); |
876 | 936 |
877 // Still writable. | 937 // Still writable. |
878 waiter.Init(); | 938 waiter.Init(); |
879 hss = HandleSignalsState(); | 939 hss = HandleSignalsState(); |
880 EXPECT_EQ( | 940 EXPECT_EQ( |
881 MOJO_RESULT_ALREADY_EXISTS, | 941 MOJO_RESULT_ALREADY_EXISTS, |
882 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); | 942 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss)); |
883 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 943 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
(...skipping 20 matching lines...) Expand all Loading... |
904 } | 964 } |
905 | 965 |
906 TEST(LocalDataPipeTest, MayDiscard) { | 966 TEST(LocalDataPipeTest, MayDiscard) { |
907 const MojoCreateDataPipeOptions options = { | 967 const MojoCreateDataPipeOptions options = { |
908 kSizeOfOptions, // |struct_size|. | 968 kSizeOfOptions, // |struct_size|. |
909 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 969 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
910 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 970 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
911 10 * sizeof(int32_t) // |capacity_num_bytes|. | 971 10 * sizeof(int32_t) // |capacity_num_bytes|. |
912 }; | 972 }; |
913 MojoCreateDataPipeOptions validated_options = {0}; | 973 MojoCreateDataPipeOptions validated_options = {0}; |
914 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 974 EXPECT_EQ(MOJO_RESULT_OK, |
915 MakeUserPointer(&options), &validated_options)); | 975 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 976 &validated_options)); |
916 | 977 |
917 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 978 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
918 | 979 |
919 int32_t buffer[100] = {0}; | 980 int32_t buffer[100] = {0}; |
920 uint32_t num_bytes = 0; | 981 uint32_t num_bytes = 0; |
921 | 982 |
922 num_bytes = 20u * sizeof(int32_t); | 983 num_bytes = 20u * sizeof(int32_t); |
923 Seq(0, arraysize(buffer), buffer); | 984 Seq(0, arraysize(buffer), buffer); |
924 // Try writing more than capacity. (This test relies on the implementation | 985 // Try writing more than capacity. (This test relies on the implementation |
925 // enforcing the capacity strictly.) | 986 // enforcing the capacity strictly.) |
926 EXPECT_EQ(MOJO_RESULT_OK, | 987 EXPECT_EQ( |
927 dp->ProducerWriteData(UserPointer<const void>(buffer), | 988 MOJO_RESULT_OK, |
928 MakeUserPointer(&num_bytes), false)); | 989 dp->ProducerWriteData( |
| 990 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false)); |
929 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 991 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
930 | 992 |
931 // Read half of what we wrote. | 993 // Read half of what we wrote. |
932 num_bytes = 5u * sizeof(int32_t); | 994 num_bytes = 5u * sizeof(int32_t); |
933 memset(buffer, 0xab, sizeof(buffer)); | 995 memset(buffer, 0xab, sizeof(buffer)); |
934 EXPECT_EQ(MOJO_RESULT_OK, | 996 EXPECT_EQ(MOJO_RESULT_OK, |
935 dp->ConsumerReadData(UserPointer<void>(buffer), | 997 dp->ConsumerReadData(UserPointer<void>(buffer), |
936 MakeUserPointer(&num_bytes), false, false)); | 998 MakeUserPointer(&num_bytes), |
| 999 false, |
| 1000 false)); |
937 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1001 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
938 int32_t expected_buffer[100]; | 1002 int32_t expected_buffer[100]; |
939 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1003 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
940 Seq(0, 5u, expected_buffer); | 1004 Seq(0, 5u, expected_buffer); |
941 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1005 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
942 // Internally, a circular buffer would now look like: | 1006 // Internally, a circular buffer would now look like: |
943 // -, -, -, -, -, 5, 6, 7, 8, 9 | 1007 // -, -, -, -, -, 5, 6, 7, 8, 9 |
944 | 1008 |
945 // Write a bit more than the space that's available. | 1009 // Write a bit more than the space that's available. |
946 num_bytes = 8u * sizeof(int32_t); | 1010 num_bytes = 8u * sizeof(int32_t); |
947 Seq(100, arraysize(buffer), buffer); | 1011 Seq(100, arraysize(buffer), buffer); |
948 EXPECT_EQ(MOJO_RESULT_OK, | 1012 EXPECT_EQ( |
949 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1013 MOJO_RESULT_OK, |
950 MakeUserPointer(&num_bytes), false)); | 1014 dp->ProducerWriteData( |
| 1015 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false)); |
951 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); | 1016 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); |
952 // Internally, a circular buffer would now look like: | 1017 // Internally, a circular buffer would now look like: |
953 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9 | 1018 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9 |
954 | 1019 |
955 // Read half of what's available. | 1020 // Read half of what's available. |
956 num_bytes = 5u * sizeof(int32_t); | 1021 num_bytes = 5u * sizeof(int32_t); |
957 memset(buffer, 0xab, sizeof(buffer)); | 1022 memset(buffer, 0xab, sizeof(buffer)); |
958 EXPECT_EQ(MOJO_RESULT_OK, | 1023 EXPECT_EQ(MOJO_RESULT_OK, |
959 dp->ConsumerReadData(UserPointer<void>(buffer), | 1024 dp->ConsumerReadData(UserPointer<void>(buffer), |
960 MakeUserPointer(&num_bytes), false, false)); | 1025 MakeUserPointer(&num_bytes), |
| 1026 false, |
| 1027 false)); |
961 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1028 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
962 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1029 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
963 expected_buffer[0] = 8; | 1030 expected_buffer[0] = 8; |
964 expected_buffer[1] = 9; | 1031 expected_buffer[1] = 9; |
965 expected_buffer[2] = 100; | 1032 expected_buffer[2] = 100; |
966 expected_buffer[3] = 101; | 1033 expected_buffer[3] = 101; |
967 expected_buffer[4] = 102; | 1034 expected_buffer[4] = 102; |
968 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1035 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
969 // Internally, a circular buffer would now look like: | 1036 // Internally, a circular buffer would now look like: |
970 // -, -, -, 103, 104, 105, 106, 107, -, - | 1037 // -, -, -, 103, 104, 105, 106, 107, -, - |
971 | 1038 |
972 // Write one integer. | 1039 // Write one integer. |
973 num_bytes = 1u * sizeof(int32_t); | 1040 num_bytes = 1u * sizeof(int32_t); |
974 Seq(200, arraysize(buffer), buffer); | 1041 Seq(200, arraysize(buffer), buffer); |
975 EXPECT_EQ(MOJO_RESULT_OK, | 1042 EXPECT_EQ( |
976 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1043 MOJO_RESULT_OK, |
977 MakeUserPointer(&num_bytes), false)); | 1044 dp->ProducerWriteData( |
| 1045 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false)); |
978 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1046 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
979 // Internally, a circular buffer would now look like: | 1047 // Internally, a circular buffer would now look like: |
980 // -, -, -, 103, 104, 105, 106, 107, 200, - | 1048 // -, -, -, 103, 104, 105, 106, 107, 200, - |
981 | 1049 |
982 // Write five more. | 1050 // Write five more. |
983 num_bytes = 5u * sizeof(int32_t); | 1051 num_bytes = 5u * sizeof(int32_t); |
984 Seq(300, arraysize(buffer), buffer); | 1052 Seq(300, arraysize(buffer), buffer); |
985 EXPECT_EQ(MOJO_RESULT_OK, | 1053 EXPECT_EQ( |
986 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1054 MOJO_RESULT_OK, |
987 MakeUserPointer(&num_bytes), false)); | 1055 dp->ProducerWriteData( |
| 1056 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false)); |
988 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1057 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
989 // Internally, a circular buffer would now look like: | 1058 // Internally, a circular buffer would now look like: |
990 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300 | 1059 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300 |
991 | 1060 |
992 // Read it all. | 1061 // Read it all. |
993 num_bytes = sizeof(buffer); | 1062 num_bytes = sizeof(buffer); |
994 memset(buffer, 0xab, sizeof(buffer)); | 1063 memset(buffer, 0xab, sizeof(buffer)); |
995 EXPECT_EQ(MOJO_RESULT_OK, | 1064 EXPECT_EQ(MOJO_RESULT_OK, |
996 dp->ConsumerReadData(UserPointer<void>(buffer), | 1065 dp->ConsumerReadData(UserPointer<void>(buffer), |
997 MakeUserPointer(&num_bytes), false, false)); | 1066 MakeUserPointer(&num_bytes), |
| 1067 false, |
| 1068 false)); |
998 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1069 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
999 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1070 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1000 expected_buffer[0] = 104; | 1071 expected_buffer[0] = 104; |
1001 expected_buffer[1] = 105; | 1072 expected_buffer[1] = 105; |
1002 expected_buffer[2] = 106; | 1073 expected_buffer[2] = 106; |
1003 expected_buffer[3] = 107; | 1074 expected_buffer[3] = 107; |
1004 expected_buffer[4] = 200; | 1075 expected_buffer[4] = 200; |
1005 expected_buffer[5] = 300; | 1076 expected_buffer[5] = 300; |
1006 expected_buffer[6] = 301; | 1077 expected_buffer[6] = 301; |
1007 expected_buffer[7] = 302; | 1078 expected_buffer[7] = 302; |
1008 expected_buffer[8] = 303; | 1079 expected_buffer[8] = 303; |
1009 expected_buffer[9] = 304; | 1080 expected_buffer[9] = 304; |
1010 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1081 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1011 | 1082 |
1012 // Test two-phase writes, including in all-or-none mode. | 1083 // Test two-phase writes, including in all-or-none mode. |
1013 // Note: Again, the following depends on an implementation detail -- namely | 1084 // Note: Again, the following depends on an implementation detail -- namely |
1014 // that the write pointer will point at the 5th element of the buffer (and the | 1085 // that the write pointer will point at the 5th element of the buffer (and the |
1015 // buffer has exactly the capacity requested). | 1086 // buffer has exactly the capacity requested). |
1016 | 1087 |
1017 num_bytes = 0u; | 1088 num_bytes = 0u; |
1018 void* write_ptr = nullptr; | 1089 void* write_ptr = nullptr; |
1019 EXPECT_EQ(MOJO_RESULT_OK, | 1090 EXPECT_EQ( |
1020 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1091 MOJO_RESULT_OK, |
1021 MakeUserPointer(&num_bytes), false)); | 1092 dp->ProducerBeginWriteData( |
| 1093 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
1022 EXPECT_TRUE(write_ptr); | 1094 EXPECT_TRUE(write_ptr); |
1023 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); | 1095 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); |
1024 Seq(400, 6, static_cast<int32_t*>(write_ptr)); | 1096 Seq(400, 6, static_cast<int32_t*>(write_ptr)); |
1025 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t))); | 1097 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t))); |
1026 // Internally, a circular buffer would now look like: | 1098 // Internally, a circular buffer would now look like: |
1027 // -, -, -, -, 400, 401, 402, 403, 404, 405 | 1099 // -, -, -, -, 400, 401, 402, 403, 404, 405 |
1028 | 1100 |
1029 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none" | 1101 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none" |
1030 // mode. | 1102 // mode. |
1031 num_bytes = 6u * sizeof(int32_t); | 1103 num_bytes = 6u * sizeof(int32_t); |
1032 write_ptr = nullptr; | 1104 write_ptr = nullptr; |
1033 EXPECT_EQ(MOJO_RESULT_OK, | 1105 EXPECT_EQ( |
1034 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1106 MOJO_RESULT_OK, |
1035 MakeUserPointer(&num_bytes), false)); | 1107 dp->ProducerBeginWriteData( |
| 1108 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
1036 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); | 1109 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); |
1037 static_cast<int32_t*>(write_ptr)[0] = 500; | 1110 static_cast<int32_t*>(write_ptr)[0] = 500; |
1038 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t))); | 1111 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t))); |
1039 // Internally, a circular buffer would now look like: | 1112 // Internally, a circular buffer would now look like: |
1040 // 500, -, -, -, 400, 401, 402, 403, 404, 405 | 1113 // 500, -, -, -, 400, 401, 402, 403, 404, 405 |
1041 | 1114 |
1042 // Requesting a 10-element buffer in all-or-none mode fails at this point. | 1115 // Requesting a 10-element buffer in all-or-none mode fails at this point. |
1043 num_bytes = 10u * sizeof(int32_t); | 1116 num_bytes = 10u * sizeof(int32_t); |
1044 write_ptr = nullptr; | 1117 write_ptr = nullptr; |
1045 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1118 EXPECT_EQ( |
1046 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1119 MOJO_RESULT_OUT_OF_RANGE, |
1047 MakeUserPointer(&num_bytes), true)); | 1120 dp->ProducerBeginWriteData( |
| 1121 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1048 | 1122 |
1049 // But requesting, say, a 5-element (up to 9, really) buffer should be okay. | 1123 // But requesting, say, a 5-element (up to 9, really) buffer should be okay. |
1050 // It will discard two elements. | 1124 // It will discard two elements. |
1051 num_bytes = 5u * sizeof(int32_t); | 1125 num_bytes = 5u * sizeof(int32_t); |
1052 write_ptr = nullptr; | 1126 write_ptr = nullptr; |
1053 EXPECT_EQ(MOJO_RESULT_OK, | 1127 EXPECT_EQ( |
1054 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1128 MOJO_RESULT_OK, |
1055 MakeUserPointer(&num_bytes), true)); | 1129 dp->ProducerBeginWriteData( |
| 1130 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1056 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1131 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1057 // Only write 4 elements though. | 1132 // Only write 4 elements though. |
1058 Seq(600, 4, static_cast<int32_t*>(write_ptr)); | 1133 Seq(600, 4, static_cast<int32_t*>(write_ptr)); |
1059 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t))); | 1134 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t))); |
1060 // Internally, a circular buffer would now look like: | 1135 // Internally, a circular buffer would now look like: |
1061 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405 | 1136 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405 |
1062 | 1137 |
1063 // Do this again. Make sure we can get a buffer all the way out to the end of | 1138 // Do this again. Make sure we can get a buffer all the way out to the end of |
1064 // the internal buffer. | 1139 // the internal buffer. |
1065 num_bytes = 5u * sizeof(int32_t); | 1140 num_bytes = 5u * sizeof(int32_t); |
1066 write_ptr = nullptr; | 1141 write_ptr = nullptr; |
1067 EXPECT_EQ(MOJO_RESULT_OK, | 1142 EXPECT_EQ( |
1068 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1143 MOJO_RESULT_OK, |
1069 MakeUserPointer(&num_bytes), true)); | 1144 dp->ProducerBeginWriteData( |
| 1145 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1070 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1146 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1071 // Only write 3 elements though. | 1147 // Only write 3 elements though. |
1072 Seq(700, 3, static_cast<int32_t*>(write_ptr)); | 1148 Seq(700, 3, static_cast<int32_t*>(write_ptr)); |
1073 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t))); | 1149 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t))); |
1074 // Internally, a circular buffer would now look like: | 1150 // Internally, a circular buffer would now look like: |
1075 // 500, 600, 601, 602, 603, 700, 701, 702, -, - | 1151 // 500, 600, 601, 602, 603, 700, 701, 702, -, - |
1076 | 1152 |
1077 // Read everything. | 1153 // Read everything. |
1078 num_bytes = sizeof(buffer); | 1154 num_bytes = sizeof(buffer); |
1079 memset(buffer, 0xab, sizeof(buffer)); | 1155 memset(buffer, 0xab, sizeof(buffer)); |
1080 EXPECT_EQ(MOJO_RESULT_OK, | 1156 EXPECT_EQ(MOJO_RESULT_OK, |
1081 dp->ConsumerReadData(UserPointer<void>(buffer), | 1157 dp->ConsumerReadData(UserPointer<void>(buffer), |
1082 MakeUserPointer(&num_bytes), false, false)); | 1158 MakeUserPointer(&num_bytes), |
| 1159 false, |
| 1160 false)); |
1083 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); | 1161 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); |
1084 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1162 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1085 expected_buffer[0] = 500; | 1163 expected_buffer[0] = 500; |
1086 expected_buffer[1] = 600; | 1164 expected_buffer[1] = 600; |
1087 expected_buffer[2] = 601; | 1165 expected_buffer[2] = 601; |
1088 expected_buffer[3] = 602; | 1166 expected_buffer[3] = 602; |
1089 expected_buffer[4] = 603; | 1167 expected_buffer[4] = 603; |
1090 expected_buffer[5] = 700; | 1168 expected_buffer[5] = 700; |
1091 expected_buffer[6] = 701; | 1169 expected_buffer[6] = 701; |
1092 expected_buffer[7] = 702; | 1170 expected_buffer[7] = 702; |
1093 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1171 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1094 | 1172 |
1095 dp->ProducerClose(); | 1173 dp->ProducerClose(); |
1096 dp->ConsumerClose(); | 1174 dp->ConsumerClose(); |
1097 } | 1175 } |
1098 | 1176 |
1099 TEST(LocalDataPipeTest, AllOrNone) { | 1177 TEST(LocalDataPipeTest, AllOrNone) { |
1100 const MojoCreateDataPipeOptions options = { | 1178 const MojoCreateDataPipeOptions options = { |
1101 kSizeOfOptions, // |struct_size|. | 1179 kSizeOfOptions, // |struct_size|. |
1102 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1180 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1103 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1181 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1104 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1182 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1105 }; | 1183 }; |
1106 MojoCreateDataPipeOptions validated_options = {0}; | 1184 MojoCreateDataPipeOptions validated_options = {0}; |
1107 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1185 EXPECT_EQ(MOJO_RESULT_OK, |
1108 MakeUserPointer(&options), &validated_options)); | 1186 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1187 &validated_options)); |
1109 | 1188 |
1110 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1189 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1111 | 1190 |
1112 // Try writing way too much. | 1191 // Try writing way too much. |
1113 uint32_t num_bytes = 20u * sizeof(int32_t); | 1192 uint32_t num_bytes = 20u * sizeof(int32_t); |
1114 int32_t buffer[100]; | 1193 int32_t buffer[100]; |
1115 Seq(0, arraysize(buffer), buffer); | 1194 Seq(0, arraysize(buffer), buffer); |
1116 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1195 EXPECT_EQ( |
1117 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1196 MOJO_RESULT_OUT_OF_RANGE, |
1118 MakeUserPointer(&num_bytes), true)); | 1197 dp->ProducerWriteData( |
| 1198 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1119 | 1199 |
1120 // Should still be empty. | 1200 // Should still be empty. |
1121 num_bytes = ~0u; | 1201 num_bytes = ~0u; |
1122 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1202 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1123 EXPECT_EQ(0u, num_bytes); | 1203 EXPECT_EQ(0u, num_bytes); |
1124 | 1204 |
1125 // Write some data. | 1205 // Write some data. |
1126 num_bytes = 5u * sizeof(int32_t); | 1206 num_bytes = 5u * sizeof(int32_t); |
1127 Seq(100, arraysize(buffer), buffer); | 1207 Seq(100, arraysize(buffer), buffer); |
1128 EXPECT_EQ(MOJO_RESULT_OK, | 1208 EXPECT_EQ( |
1129 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1209 MOJO_RESULT_OK, |
1130 MakeUserPointer(&num_bytes), true)); | 1210 dp->ProducerWriteData( |
| 1211 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1131 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1212 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1132 | 1213 |
1133 // Half full. | 1214 // Half full. |
1134 num_bytes = 0u; | 1215 num_bytes = 0u; |
1135 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1216 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1136 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1217 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1137 | 1218 |
1138 // Too much. | 1219 // Too much. |
1139 num_bytes = 6u * sizeof(int32_t); | 1220 num_bytes = 6u * sizeof(int32_t); |
1140 Seq(200, arraysize(buffer), buffer); | 1221 Seq(200, arraysize(buffer), buffer); |
1141 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1222 EXPECT_EQ( |
1142 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1223 MOJO_RESULT_OUT_OF_RANGE, |
1143 MakeUserPointer(&num_bytes), true)); | 1224 dp->ProducerWriteData( |
| 1225 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1144 | 1226 |
1145 // Try reading too much. | 1227 // Try reading too much. |
1146 num_bytes = 11u * sizeof(int32_t); | 1228 num_bytes = 11u * sizeof(int32_t); |
1147 memset(buffer, 0xab, sizeof(buffer)); | 1229 memset(buffer, 0xab, sizeof(buffer)); |
1148 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1230 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1149 dp->ConsumerReadData(UserPointer<void>(buffer), | 1231 dp->ConsumerReadData(UserPointer<void>(buffer), |
1150 MakeUserPointer(&num_bytes), true, false)); | 1232 MakeUserPointer(&num_bytes), |
| 1233 true, |
| 1234 false)); |
1151 int32_t expected_buffer[100]; | 1235 int32_t expected_buffer[100]; |
1152 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1236 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1153 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1237 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1154 | 1238 |
1155 // Try discarding too much. | 1239 // Try discarding too much. |
1156 num_bytes = 11u * sizeof(int32_t); | 1240 num_bytes = 11u * sizeof(int32_t); |
1157 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1241 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1158 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1242 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1159 | 1243 |
1160 // Just a little. | 1244 // Just a little. |
1161 num_bytes = 2u * sizeof(int32_t); | 1245 num_bytes = 2u * sizeof(int32_t); |
1162 Seq(300, arraysize(buffer), buffer); | 1246 Seq(300, arraysize(buffer), buffer); |
1163 EXPECT_EQ(MOJO_RESULT_OK, | 1247 EXPECT_EQ( |
1164 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1248 MOJO_RESULT_OK, |
1165 MakeUserPointer(&num_bytes), true)); | 1249 dp->ProducerWriteData( |
| 1250 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1166 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); | 1251 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); |
1167 | 1252 |
1168 // Just right. | 1253 // Just right. |
1169 num_bytes = 3u * sizeof(int32_t); | 1254 num_bytes = 3u * sizeof(int32_t); |
1170 Seq(400, arraysize(buffer), buffer); | 1255 Seq(400, arraysize(buffer), buffer); |
1171 EXPECT_EQ(MOJO_RESULT_OK, | 1256 EXPECT_EQ( |
1172 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1257 MOJO_RESULT_OK, |
1173 MakeUserPointer(&num_bytes), true)); | 1258 dp->ProducerWriteData( |
| 1259 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1174 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); | 1260 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); |
1175 | 1261 |
1176 // Exactly full. | 1262 // Exactly full. |
1177 num_bytes = 0u; | 1263 num_bytes = 0u; |
1178 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1264 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1179 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1265 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1180 | 1266 |
1181 // Read half. | 1267 // Read half. |
1182 num_bytes = 5u * sizeof(int32_t); | 1268 num_bytes = 5u * sizeof(int32_t); |
1183 memset(buffer, 0xab, sizeof(buffer)); | 1269 memset(buffer, 0xab, sizeof(buffer)); |
1184 EXPECT_EQ(MOJO_RESULT_OK, | 1270 EXPECT_EQ(MOJO_RESULT_OK, |
1185 dp->ConsumerReadData(UserPointer<void>(buffer), | 1271 dp->ConsumerReadData(UserPointer<void>(buffer), |
1186 MakeUserPointer(&num_bytes), true, false)); | 1272 MakeUserPointer(&num_bytes), |
| 1273 true, |
| 1274 false)); |
1187 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1275 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1188 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1276 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1189 Seq(100, 5, expected_buffer); | 1277 Seq(100, 5, expected_buffer); |
1190 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1278 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1191 | 1279 |
1192 // Try reading too much again. | 1280 // Try reading too much again. |
1193 num_bytes = 6u * sizeof(int32_t); | 1281 num_bytes = 6u * sizeof(int32_t); |
1194 memset(buffer, 0xab, sizeof(buffer)); | 1282 memset(buffer, 0xab, sizeof(buffer)); |
1195 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1283 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1196 dp->ConsumerReadData(UserPointer<void>(buffer), | 1284 dp->ConsumerReadData(UserPointer<void>(buffer), |
1197 MakeUserPointer(&num_bytes), true, false)); | 1285 MakeUserPointer(&num_bytes), |
| 1286 true, |
| 1287 false)); |
1198 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1288 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1199 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1289 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1200 | 1290 |
1201 // Try discarding too much again. | 1291 // Try discarding too much again. |
1202 num_bytes = 6u * sizeof(int32_t); | 1292 num_bytes = 6u * sizeof(int32_t); |
1203 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1293 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1204 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1294 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1205 | 1295 |
1206 // Discard a little. | 1296 // Discard a little. |
1207 num_bytes = 2u * sizeof(int32_t); | 1297 num_bytes = 2u * sizeof(int32_t); |
1208 EXPECT_EQ(MOJO_RESULT_OK, | 1298 EXPECT_EQ(MOJO_RESULT_OK, |
1209 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1299 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1210 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); | 1300 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); |
1211 | 1301 |
1212 // Three left. | 1302 // Three left. |
1213 num_bytes = 0u; | 1303 num_bytes = 0u; |
1214 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1304 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1215 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); | 1305 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); |
1216 | 1306 |
1217 // Close the producer, then test producer-closed cases. | 1307 // Close the producer, then test producer-closed cases. |
1218 dp->ProducerClose(); | 1308 dp->ProducerClose(); |
1219 | 1309 |
1220 // Try reading too much; "failed precondition" since the producer is closed. | 1310 // Try reading too much; "failed precondition" since the producer is closed. |
1221 num_bytes = 4u * sizeof(int32_t); | 1311 num_bytes = 4u * sizeof(int32_t); |
1222 memset(buffer, 0xab, sizeof(buffer)); | 1312 memset(buffer, 0xab, sizeof(buffer)); |
1223 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1313 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1224 dp->ConsumerReadData(UserPointer<void>(buffer), | 1314 dp->ConsumerReadData(UserPointer<void>(buffer), |
1225 MakeUserPointer(&num_bytes), true, false)); | 1315 MakeUserPointer(&num_bytes), |
| 1316 true, |
| 1317 false)); |
1226 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1318 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1227 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1319 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1228 | 1320 |
1229 // Try discarding too much; "failed precondition" again. | 1321 // Try discarding too much; "failed precondition" again. |
1230 num_bytes = 4u * sizeof(int32_t); | 1322 num_bytes = 4u * sizeof(int32_t); |
1231 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1323 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1232 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1324 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1233 | 1325 |
1234 // Read a little. | 1326 // Read a little. |
1235 num_bytes = 2u * sizeof(int32_t); | 1327 num_bytes = 2u * sizeof(int32_t); |
1236 memset(buffer, 0xab, sizeof(buffer)); | 1328 memset(buffer, 0xab, sizeof(buffer)); |
1237 EXPECT_EQ(MOJO_RESULT_OK, | 1329 EXPECT_EQ(MOJO_RESULT_OK, |
1238 dp->ConsumerReadData(UserPointer<void>(buffer), | 1330 dp->ConsumerReadData(UserPointer<void>(buffer), |
1239 MakeUserPointer(&num_bytes), true, false)); | 1331 MakeUserPointer(&num_bytes), |
| 1332 true, |
| 1333 false)); |
1240 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); | 1334 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); |
1241 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1335 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1242 Seq(400, 2, expected_buffer); | 1336 Seq(400, 2, expected_buffer); |
1243 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1337 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1244 | 1338 |
1245 // Discard the remaining element. | 1339 // Discard the remaining element. |
1246 num_bytes = 1u * sizeof(int32_t); | 1340 num_bytes = 1u * sizeof(int32_t); |
1247 EXPECT_EQ(MOJO_RESULT_OK, | 1341 EXPECT_EQ(MOJO_RESULT_OK, |
1248 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1342 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1249 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1343 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1250 | 1344 |
1251 // Empty again. | 1345 // Empty again. |
1252 num_bytes = ~0u; | 1346 num_bytes = ~0u; |
1253 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1347 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1254 EXPECT_EQ(0u, num_bytes); | 1348 EXPECT_EQ(0u, num_bytes); |
1255 | 1349 |
1256 dp->ConsumerClose(); | 1350 dp->ConsumerClose(); |
1257 } | 1351 } |
1258 | 1352 |
1259 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) { | 1353 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) { |
1260 const MojoCreateDataPipeOptions options = { | 1354 const MojoCreateDataPipeOptions options = { |
1261 kSizeOfOptions, // |struct_size|. | 1355 kSizeOfOptions, // |struct_size|. |
1262 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 1356 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
1263 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1357 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1264 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1358 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1265 }; | 1359 }; |
1266 MojoCreateDataPipeOptions validated_options = {0}; | 1360 MojoCreateDataPipeOptions validated_options = {0}; |
1267 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1361 EXPECT_EQ(MOJO_RESULT_OK, |
1268 MakeUserPointer(&options), &validated_options)); | 1362 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1363 &validated_options)); |
1269 | 1364 |
1270 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1365 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1271 | 1366 |
1272 // Try writing way too much. | 1367 // Try writing way too much. |
1273 uint32_t num_bytes = 20u * sizeof(int32_t); | 1368 uint32_t num_bytes = 20u * sizeof(int32_t); |
1274 int32_t buffer[100]; | 1369 int32_t buffer[100]; |
1275 Seq(0, arraysize(buffer), buffer); | 1370 Seq(0, arraysize(buffer), buffer); |
1276 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1371 EXPECT_EQ( |
1277 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1372 MOJO_RESULT_OUT_OF_RANGE, |
1278 MakeUserPointer(&num_bytes), true)); | 1373 dp->ProducerWriteData( |
| 1374 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1279 | 1375 |
1280 // Write some stuff. | 1376 // Write some stuff. |
1281 num_bytes = 5u * sizeof(int32_t); | 1377 num_bytes = 5u * sizeof(int32_t); |
1282 Seq(100, arraysize(buffer), buffer); | 1378 Seq(100, arraysize(buffer), buffer); |
1283 EXPECT_EQ(MOJO_RESULT_OK, | 1379 EXPECT_EQ( |
1284 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1380 MOJO_RESULT_OK, |
1285 MakeUserPointer(&num_bytes), true)); | 1381 dp->ProducerWriteData( |
| 1382 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1286 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1383 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1287 | 1384 |
1288 // Write lots of stuff (discarding all but "104"). | 1385 // Write lots of stuff (discarding all but "104"). |
1289 num_bytes = 9u * sizeof(int32_t); | 1386 num_bytes = 9u * sizeof(int32_t); |
1290 Seq(200, arraysize(buffer), buffer); | 1387 Seq(200, arraysize(buffer), buffer); |
1291 EXPECT_EQ(MOJO_RESULT_OK, | 1388 EXPECT_EQ( |
1292 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1389 MOJO_RESULT_OK, |
1293 MakeUserPointer(&num_bytes), true)); | 1390 dp->ProducerWriteData( |
| 1391 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1294 EXPECT_EQ(9u * sizeof(int32_t), num_bytes); | 1392 EXPECT_EQ(9u * sizeof(int32_t), num_bytes); |
1295 | 1393 |
1296 // Read one. | 1394 // Read one. |
1297 num_bytes = 1u * sizeof(int32_t); | 1395 num_bytes = 1u * sizeof(int32_t); |
1298 memset(buffer, 0xab, sizeof(buffer)); | 1396 memset(buffer, 0xab, sizeof(buffer)); |
1299 EXPECT_EQ(MOJO_RESULT_OK, | 1397 EXPECT_EQ(MOJO_RESULT_OK, |
1300 dp->ConsumerReadData(UserPointer<void>(buffer), | 1398 dp->ConsumerReadData(UserPointer<void>(buffer), |
1301 MakeUserPointer(&num_bytes), true, false)); | 1399 MakeUserPointer(&num_bytes), |
| 1400 true, |
| 1401 false)); |
1302 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1402 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1303 int32_t expected_buffer[100]; | 1403 int32_t expected_buffer[100]; |
1304 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1404 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1305 expected_buffer[0] = 104; | 1405 expected_buffer[0] = 104; |
1306 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1406 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1307 | 1407 |
1308 // Try reading too many. | 1408 // Try reading too many. |
1309 num_bytes = 10u * sizeof(int32_t); | 1409 num_bytes = 10u * sizeof(int32_t); |
1310 memset(buffer, 0xab, sizeof(buffer)); | 1410 memset(buffer, 0xab, sizeof(buffer)); |
1311 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1411 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1312 dp->ConsumerReadData(UserPointer<void>(buffer), | 1412 dp->ConsumerReadData(UserPointer<void>(buffer), |
1313 MakeUserPointer(&num_bytes), true, false)); | 1413 MakeUserPointer(&num_bytes), |
| 1414 true, |
| 1415 false)); |
1314 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1416 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1315 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1417 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1316 | 1418 |
1317 // Try discarding too many. | 1419 // Try discarding too many. |
1318 num_bytes = 10u * sizeof(int32_t); | 1420 num_bytes = 10u * sizeof(int32_t); |
1319 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1421 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1320 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1422 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1321 | 1423 |
1322 // Discard a bunch. | 1424 // Discard a bunch. |
1323 num_bytes = 4u * sizeof(int32_t); | 1425 num_bytes = 4u * sizeof(int32_t); |
1324 EXPECT_EQ(MOJO_RESULT_OK, | 1426 EXPECT_EQ(MOJO_RESULT_OK, |
1325 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); | 1427 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true)); |
1326 | 1428 |
1327 // Half full. | 1429 // Half full. |
1328 num_bytes = 0u; | 1430 num_bytes = 0u; |
1329 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1431 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1330 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 1432 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
1331 | 1433 |
1332 // Write as much as possible. | 1434 // Write as much as possible. |
1333 num_bytes = 10u * sizeof(int32_t); | 1435 num_bytes = 10u * sizeof(int32_t); |
1334 Seq(300, arraysize(buffer), buffer); | 1436 Seq(300, arraysize(buffer), buffer); |
1335 EXPECT_EQ(MOJO_RESULT_OK, | 1437 EXPECT_EQ( |
1336 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1438 MOJO_RESULT_OK, |
1337 MakeUserPointer(&num_bytes), true)); | 1439 dp->ProducerWriteData( |
| 1440 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1338 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1441 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1339 | 1442 |
1340 // Read everything. | 1443 // Read everything. |
1341 num_bytes = 10u * sizeof(int32_t); | 1444 num_bytes = 10u * sizeof(int32_t); |
1342 memset(buffer, 0xab, sizeof(buffer)); | 1445 memset(buffer, 0xab, sizeof(buffer)); |
1343 EXPECT_EQ(MOJO_RESULT_OK, | 1446 EXPECT_EQ(MOJO_RESULT_OK, |
1344 dp->ConsumerReadData(UserPointer<void>(buffer), | 1447 dp->ConsumerReadData(UserPointer<void>(buffer), |
1345 MakeUserPointer(&num_bytes), true, false)); | 1448 MakeUserPointer(&num_bytes), |
| 1449 true, |
| 1450 false)); |
1346 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1451 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1347 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1452 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1348 Seq(300, 10, expected_buffer); | 1453 Seq(300, 10, expected_buffer); |
1349 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1454 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1350 | 1455 |
1351 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested | 1456 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested |
1352 // in LocalDataPipeTest.MayDiscard. | 1457 // in LocalDataPipeTest.MayDiscard. |
1353 | 1458 |
1354 dp->ProducerClose(); | 1459 dp->ProducerClose(); |
1355 dp->ConsumerClose(); | 1460 dp->ConsumerClose(); |
1356 } | 1461 } |
1357 | 1462 |
1358 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) { | 1463 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) { |
1359 const MojoCreateDataPipeOptions options = { | 1464 const MojoCreateDataPipeOptions options = { |
1360 kSizeOfOptions, // |struct_size|. | 1465 kSizeOfOptions, // |struct_size|. |
1361 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1466 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1362 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1467 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1363 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1468 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1364 }; | 1469 }; |
1365 MojoCreateDataPipeOptions validated_options = {0}; | 1470 MojoCreateDataPipeOptions validated_options = {0}; |
1366 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1471 EXPECT_EQ(MOJO_RESULT_OK, |
1367 MakeUserPointer(&options), &validated_options)); | 1472 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1473 &validated_options)); |
1368 | 1474 |
1369 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1475 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1370 | 1476 |
1371 // Try writing way too much (two-phase). | 1477 // Try writing way too much (two-phase). |
1372 uint32_t num_bytes = 20u * sizeof(int32_t); | 1478 uint32_t num_bytes = 20u * sizeof(int32_t); |
1373 void* write_ptr = nullptr; | 1479 void* write_ptr = nullptr; |
1374 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1480 EXPECT_EQ( |
1375 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1481 MOJO_RESULT_OUT_OF_RANGE, |
1376 MakeUserPointer(&num_bytes), true)); | 1482 dp->ProducerBeginWriteData( |
| 1483 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1377 | 1484 |
1378 // Try writing an amount which isn't a multiple of the element size | 1485 // Try writing an amount which isn't a multiple of the element size |
1379 // (two-phase). | 1486 // (two-phase). |
1380 static_assert(sizeof(int32_t) > 1u, "Wow! int32_t's have size 1"); | 1487 static_assert(sizeof(int32_t) > 1u, "Wow! int32_t's have size 1"); |
1381 num_bytes = 1u; | 1488 num_bytes = 1u; |
1382 write_ptr = nullptr; | 1489 write_ptr = nullptr; |
1383 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1490 EXPECT_EQ( |
1384 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1491 MOJO_RESULT_INVALID_ARGUMENT, |
1385 MakeUserPointer(&num_bytes), true)); | 1492 dp->ProducerBeginWriteData( |
| 1493 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1386 | 1494 |
1387 // Try reading way too much (two-phase). | 1495 // Try reading way too much (two-phase). |
1388 num_bytes = 20u * sizeof(int32_t); | 1496 num_bytes = 20u * sizeof(int32_t); |
1389 const void* read_ptr = nullptr; | 1497 const void* read_ptr = nullptr; |
1390 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1498 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1391 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1499 dp->ConsumerBeginReadData( |
1392 MakeUserPointer(&num_bytes), true)); | 1500 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1393 | 1501 |
1394 // Write half (two-phase). | 1502 // Write half (two-phase). |
1395 num_bytes = 5u * sizeof(int32_t); | 1503 num_bytes = 5u * sizeof(int32_t); |
1396 write_ptr = nullptr; | 1504 write_ptr = nullptr; |
1397 EXPECT_EQ(MOJO_RESULT_OK, | 1505 EXPECT_EQ( |
1398 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1506 MOJO_RESULT_OK, |
1399 MakeUserPointer(&num_bytes), true)); | 1507 dp->ProducerBeginWriteData( |
| 1508 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1400 // May provide more space than requested. | 1509 // May provide more space than requested. |
1401 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); | 1510 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); |
1402 EXPECT_TRUE(write_ptr); | 1511 EXPECT_TRUE(write_ptr); |
1403 Seq(0, 5, static_cast<int32_t*>(write_ptr)); | 1512 Seq(0, 5, static_cast<int32_t*>(write_ptr)); |
1404 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); | 1513 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); |
1405 | 1514 |
1406 // Try reading an amount which isn't a multiple of the element size | 1515 // Try reading an amount which isn't a multiple of the element size |
1407 // (two-phase). | 1516 // (two-phase). |
1408 num_bytes = 1u; | 1517 num_bytes = 1u; |
1409 read_ptr = nullptr; | 1518 read_ptr = nullptr; |
1410 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1519 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1411 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1520 dp->ConsumerBeginReadData( |
1412 MakeUserPointer(&num_bytes), true)); | 1521 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1413 | 1522 |
1414 // Read one (two-phase). | 1523 // Read one (two-phase). |
1415 num_bytes = 1u * sizeof(int32_t); | 1524 num_bytes = 1u * sizeof(int32_t); |
1416 read_ptr = nullptr; | 1525 read_ptr = nullptr; |
1417 EXPECT_EQ(MOJO_RESULT_OK, | 1526 EXPECT_EQ(MOJO_RESULT_OK, |
1418 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1527 dp->ConsumerBeginReadData( |
1419 MakeUserPointer(&num_bytes), true)); | 1528 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1420 EXPECT_GE(num_bytes, 1u * sizeof(int32_t)); | 1529 EXPECT_GE(num_bytes, 1u * sizeof(int32_t)); |
1421 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]); | 1530 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]); |
1422 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t))); | 1531 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t))); |
1423 | 1532 |
1424 // We should have four left, leaving room for six. | 1533 // We should have four left, leaving room for six. |
1425 num_bytes = 0u; | 1534 num_bytes = 0u; |
1426 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1535 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1427 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); | 1536 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); |
1428 | 1537 |
1429 // Assuming a tight circular buffer of the specified capacity, we can't do a | 1538 // Assuming a tight circular buffer of the specified capacity, we can't do a |
1430 // two-phase write of six now. | 1539 // two-phase write of six now. |
1431 num_bytes = 6u * sizeof(int32_t); | 1540 num_bytes = 6u * sizeof(int32_t); |
1432 write_ptr = nullptr; | 1541 write_ptr = nullptr; |
1433 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1542 EXPECT_EQ( |
1434 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1543 MOJO_RESULT_OUT_OF_RANGE, |
1435 MakeUserPointer(&num_bytes), true)); | 1544 dp->ProducerBeginWriteData( |
| 1545 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true)); |
1436 | 1546 |
1437 // Write six elements (simple), filling the buffer. | 1547 // Write six elements (simple), filling the buffer. |
1438 num_bytes = 6u * sizeof(int32_t); | 1548 num_bytes = 6u * sizeof(int32_t); |
1439 int32_t buffer[100]; | 1549 int32_t buffer[100]; |
1440 Seq(100, 6, buffer); | 1550 Seq(100, 6, buffer); |
1441 EXPECT_EQ(MOJO_RESULT_OK, | 1551 EXPECT_EQ( |
1442 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1552 MOJO_RESULT_OK, |
1443 MakeUserPointer(&num_bytes), true)); | 1553 dp->ProducerWriteData( |
| 1554 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true)); |
1444 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); | 1555 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); |
1445 | 1556 |
1446 // We have ten. | 1557 // We have ten. |
1447 num_bytes = 0u; | 1558 num_bytes = 0u; |
1448 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1559 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1449 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1560 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1450 | 1561 |
1451 // But a two-phase read of ten should fail. | 1562 // But a two-phase read of ten should fail. |
1452 num_bytes = 10u * sizeof(int32_t); | 1563 num_bytes = 10u * sizeof(int32_t); |
1453 read_ptr = nullptr; | 1564 read_ptr = nullptr; |
1454 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1565 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1455 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1566 dp->ConsumerBeginReadData( |
1456 MakeUserPointer(&num_bytes), true)); | 1567 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1457 | 1568 |
1458 // Close the producer. | 1569 // Close the producer. |
1459 dp->ProducerClose(); | 1570 dp->ProducerClose(); |
1460 | 1571 |
1461 // A two-phase read of nine should work. | 1572 // A two-phase read of nine should work. |
1462 num_bytes = 9u * sizeof(int32_t); | 1573 num_bytes = 9u * sizeof(int32_t); |
1463 read_ptr = nullptr; | 1574 read_ptr = nullptr; |
1464 EXPECT_EQ(MOJO_RESULT_OK, | 1575 EXPECT_EQ(MOJO_RESULT_OK, |
1465 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1576 dp->ConsumerBeginReadData( |
1466 MakeUserPointer(&num_bytes), true)); | 1577 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1467 EXPECT_GE(num_bytes, 9u * sizeof(int32_t)); | 1578 EXPECT_GE(num_bytes, 9u * sizeof(int32_t)); |
1468 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]); | 1579 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]); |
1469 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]); | 1580 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]); |
1470 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]); | 1581 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]); |
1471 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]); | 1582 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]); |
1472 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]); | 1583 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]); |
1473 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]); | 1584 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]); |
1474 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]); | 1585 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]); |
1475 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]); | 1586 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]); |
1476 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]); | 1587 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]); |
1477 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t))); | 1588 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t))); |
1478 | 1589 |
1479 // A two-phase read of two should fail, with "failed precondition". | 1590 // A two-phase read of two should fail, with "failed precondition". |
1480 num_bytes = 2u * sizeof(int32_t); | 1591 num_bytes = 2u * sizeof(int32_t); |
1481 read_ptr = nullptr; | 1592 read_ptr = nullptr; |
1482 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1593 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1483 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1594 dp->ConsumerBeginReadData( |
1484 MakeUserPointer(&num_bytes), true)); | 1595 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true)); |
1485 | 1596 |
1486 dp->ConsumerClose(); | 1597 dp->ConsumerClose(); |
1487 } | 1598 } |
1488 | 1599 |
1489 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, | 1600 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, |
1490 // respectively, as much as possible, even if it has to "wrap around" the | 1601 // respectively, as much as possible, even if it has to "wrap around" the |
1491 // internal circular buffer. (Note that the two-phase write and read do not do | 1602 // internal circular buffer. (Note that the two-phase write and read do not do |
1492 // this.) | 1603 // this.) |
1493 TEST(LocalDataPipeTest, WrapAround) { | 1604 TEST(LocalDataPipeTest, WrapAround) { |
1494 unsigned char test_data[1000]; | 1605 unsigned char test_data[1000]; |
1495 for (size_t i = 0; i < arraysize(test_data); i++) | 1606 for (size_t i = 0; i < arraysize(test_data); i++) |
1496 test_data[i] = static_cast<unsigned char>(i); | 1607 test_data[i] = static_cast<unsigned char>(i); |
1497 | 1608 |
1498 const MojoCreateDataPipeOptions options = { | 1609 const MojoCreateDataPipeOptions options = { |
1499 kSizeOfOptions, // |struct_size|. | 1610 kSizeOfOptions, // |struct_size|. |
1500 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1611 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1501 1u, // |element_num_bytes|. | 1612 1u, // |element_num_bytes|. |
1502 100u // |capacity_num_bytes|. | 1613 100u // |capacity_num_bytes|. |
1503 }; | 1614 }; |
1504 MojoCreateDataPipeOptions validated_options = {0}; | 1615 MojoCreateDataPipeOptions validated_options = {0}; |
1505 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1616 EXPECT_EQ(MOJO_RESULT_OK, |
1506 MakeUserPointer(&options), &validated_options)); | 1617 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1618 &validated_options)); |
1507 // This test won't be valid if |ValidateCreateOptions()| decides to give the | 1619 // This test won't be valid if |ValidateCreateOptions()| decides to give the |
1508 // pipe more space. | 1620 // pipe more space. |
1509 ASSERT_EQ(100u, validated_options.capacity_num_bytes); | 1621 ASSERT_EQ(100u, validated_options.capacity_num_bytes); |
1510 | 1622 |
1511 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1623 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1512 | 1624 |
1513 // Write 20 bytes. | 1625 // Write 20 bytes. |
1514 uint32_t num_bytes = 20u; | 1626 uint32_t num_bytes = 20u; |
1515 EXPECT_EQ(MOJO_RESULT_OK, | 1627 EXPECT_EQ(MOJO_RESULT_OK, |
1516 dp->ProducerWriteData(UserPointer<const void>(&test_data[0]), | 1628 dp->ProducerWriteData(UserPointer<const void>(&test_data[0]), |
1517 MakeUserPointer(&num_bytes), false)); | 1629 MakeUserPointer(&num_bytes), |
| 1630 false)); |
1518 EXPECT_EQ(20u, num_bytes); | 1631 EXPECT_EQ(20u, num_bytes); |
1519 | 1632 |
1520 // Read 10 bytes. | 1633 // Read 10 bytes. |
1521 unsigned char read_buffer[1000] = {0}; | 1634 unsigned char read_buffer[1000] = {0}; |
1522 num_bytes = 10u; | 1635 num_bytes = 10u; |
1523 EXPECT_EQ(MOJO_RESULT_OK, | 1636 EXPECT_EQ( |
1524 dp->ConsumerReadData(UserPointer<void>(read_buffer), | 1637 MOJO_RESULT_OK, |
1525 MakeUserPointer(&num_bytes), false, false)); | 1638 dp->ConsumerReadData(UserPointer<void>(read_buffer), |
| 1639 MakeUserPointer(&num_bytes), |
| 1640 false, |
| 1641 false)); |
1526 EXPECT_EQ(10u, num_bytes); | 1642 EXPECT_EQ(10u, num_bytes); |
1527 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); | 1643 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); |
1528 | 1644 |
1529 // Check that a two-phase write can now only write (at most) 80 bytes. (This | 1645 // Check that a two-phase write can now only write (at most) 80 bytes. (This |
1530 // checks an implementation detail; this behavior is not guaranteed, but we | 1646 // checks an implementation detail; this behavior is not guaranteed, but we |
1531 // need it for this test.) | 1647 // need it for this test.) |
1532 void* write_buffer_ptr = nullptr; | 1648 void* write_buffer_ptr = nullptr; |
1533 num_bytes = 0u; | 1649 num_bytes = 0u; |
1534 EXPECT_EQ(MOJO_RESULT_OK, | 1650 EXPECT_EQ(MOJO_RESULT_OK, |
1535 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1651 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1536 MakeUserPointer(&num_bytes), false)); | 1652 MakeUserPointer(&num_bytes), |
| 1653 false)); |
1537 EXPECT_TRUE(write_buffer_ptr); | 1654 EXPECT_TRUE(write_buffer_ptr); |
1538 EXPECT_EQ(80u, num_bytes); | 1655 EXPECT_EQ(80u, num_bytes); |
1539 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 1656 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
1540 | 1657 |
1541 // Write as much data as we can (using |ProducerWriteData()|). We should write | 1658 // Write as much data as we can (using |ProducerWriteData()|). We should write |
1542 // 90 bytes. | 1659 // 90 bytes. |
1543 num_bytes = 200u; | 1660 num_bytes = 200u; |
1544 EXPECT_EQ(MOJO_RESULT_OK, | 1661 EXPECT_EQ(MOJO_RESULT_OK, |
1545 dp->ProducerWriteData(UserPointer<const void>(&test_data[20]), | 1662 dp->ProducerWriteData(UserPointer<const void>(&test_data[20]), |
1546 MakeUserPointer(&num_bytes), false)); | 1663 MakeUserPointer(&num_bytes), |
| 1664 false)); |
1547 EXPECT_EQ(90u, num_bytes); | 1665 EXPECT_EQ(90u, num_bytes); |
1548 | 1666 |
1549 // Check that a two-phase read can now only read (at most) 90 bytes. (This | 1667 // Check that a two-phase read can now only read (at most) 90 bytes. (This |
1550 // checks an implementation detail; this behavior is not guaranteed, but we | 1668 // checks an implementation detail; this behavior is not guaranteed, but we |
1551 // need it for this test.) | 1669 // need it for this test.) |
1552 const void* read_buffer_ptr = nullptr; | 1670 const void* read_buffer_ptr = nullptr; |
1553 num_bytes = 0u; | 1671 num_bytes = 0u; |
1554 EXPECT_EQ(MOJO_RESULT_OK, | 1672 EXPECT_EQ(MOJO_RESULT_OK, |
1555 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), | 1673 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
1556 MakeUserPointer(&num_bytes), false)); | 1674 MakeUserPointer(&num_bytes), |
| 1675 false)); |
1557 EXPECT_TRUE(read_buffer_ptr); | 1676 EXPECT_TRUE(read_buffer_ptr); |
1558 EXPECT_EQ(90u, num_bytes); | 1677 EXPECT_EQ(90u, num_bytes); |
1559 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); | 1678 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); |
1560 | 1679 |
1561 // Read as much as possible (using |ConsumerReadData()|). We should read 100 | 1680 // Read as much as possible (using |ConsumerReadData()|). We should read 100 |
1562 // bytes. | 1681 // bytes. |
1563 num_bytes = | 1682 num_bytes = |
1564 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0])); | 1683 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0])); |
1565 memset(read_buffer, 0, num_bytes); | 1684 memset(read_buffer, 0, num_bytes); |
1566 EXPECT_EQ(MOJO_RESULT_OK, | 1685 EXPECT_EQ( |
1567 dp->ConsumerReadData(UserPointer<void>(read_buffer), | 1686 MOJO_RESULT_OK, |
1568 MakeUserPointer(&num_bytes), false, false)); | 1687 dp->ConsumerReadData(UserPointer<void>(read_buffer), |
| 1688 MakeUserPointer(&num_bytes), |
| 1689 false, |
| 1690 false)); |
1569 EXPECT_EQ(100u, num_bytes); | 1691 EXPECT_EQ(100u, num_bytes); |
1570 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u)); | 1692 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u)); |
1571 | 1693 |
1572 dp->ProducerClose(); | 1694 dp->ProducerClose(); |
1573 dp->ConsumerClose(); | 1695 dp->ConsumerClose(); |
1574 } | 1696 } |
1575 | 1697 |
1576 // Tests the behavior of closing the producer or consumer with respect to | 1698 // Tests the behavior of closing the producer or consumer with respect to |
1577 // writes and reads (simple and two-phase). | 1699 // writes and reads (simple and two-phase). |
1578 TEST(LocalDataPipeTest, CloseWriteRead) { | 1700 TEST(LocalDataPipeTest, CloseWriteRead) { |
1579 const char kTestData[] = "hello world"; | 1701 const char kTestData[] = "hello world"; |
1580 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); | 1702 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); |
1581 | 1703 |
1582 const MojoCreateDataPipeOptions options = { | 1704 const MojoCreateDataPipeOptions options = { |
1583 kSizeOfOptions, // |struct_size|. | 1705 kSizeOfOptions, // |struct_size|. |
1584 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1706 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1585 1u, // |element_num_bytes|. | 1707 1u, // |element_num_bytes|. |
1586 1000u // |capacity_num_bytes|. | 1708 1000u // |capacity_num_bytes|. |
1587 }; | 1709 }; |
1588 MojoCreateDataPipeOptions validated_options = {0}; | 1710 MojoCreateDataPipeOptions validated_options = {0}; |
1589 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1711 EXPECT_EQ(MOJO_RESULT_OK, |
1590 MakeUserPointer(&options), &validated_options)); | 1712 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1713 &validated_options)); |
1591 | 1714 |
1592 // Close producer first, then consumer. | 1715 // Close producer first, then consumer. |
1593 { | 1716 { |
1594 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1717 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1595 | 1718 |
1596 // Write some data, so we'll have something to read. | 1719 // Write some data, so we'll have something to read. |
1597 uint32_t num_bytes = kTestDataSize; | 1720 uint32_t num_bytes = kTestDataSize; |
1598 EXPECT_EQ(MOJO_RESULT_OK, | 1721 EXPECT_EQ(MOJO_RESULT_OK, |
1599 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1722 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1600 MakeUserPointer(&num_bytes), false)); | 1723 MakeUserPointer(&num_bytes), |
| 1724 false)); |
1601 EXPECT_EQ(kTestDataSize, num_bytes); | 1725 EXPECT_EQ(kTestDataSize, num_bytes); |
1602 | 1726 |
1603 // Write it again, so we'll have something left over. | 1727 // Write it again, so we'll have something left over. |
1604 num_bytes = kTestDataSize; | 1728 num_bytes = kTestDataSize; |
1605 EXPECT_EQ(MOJO_RESULT_OK, | 1729 EXPECT_EQ(MOJO_RESULT_OK, |
1606 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1730 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1607 MakeUserPointer(&num_bytes), false)); | 1731 MakeUserPointer(&num_bytes), |
| 1732 false)); |
1608 EXPECT_EQ(kTestDataSize, num_bytes); | 1733 EXPECT_EQ(kTestDataSize, num_bytes); |
1609 | 1734 |
1610 // Start two-phase write. | 1735 // Start two-phase write. |
1611 void* write_buffer_ptr = nullptr; | 1736 void* write_buffer_ptr = nullptr; |
1612 num_bytes = 0u; | 1737 num_bytes = 0u; |
1613 EXPECT_EQ(MOJO_RESULT_OK, | 1738 EXPECT_EQ(MOJO_RESULT_OK, |
1614 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1739 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1615 MakeUserPointer(&num_bytes), false)); | 1740 MakeUserPointer(&num_bytes), |
| 1741 false)); |
1616 EXPECT_TRUE(write_buffer_ptr); | 1742 EXPECT_TRUE(write_buffer_ptr); |
1617 EXPECT_GT(num_bytes, 0u); | 1743 EXPECT_GT(num_bytes, 0u); |
1618 | 1744 |
1619 // Start two-phase read. | 1745 // Start two-phase read. |
1620 const void* read_buffer_ptr = nullptr; | 1746 const void* read_buffer_ptr = nullptr; |
1621 num_bytes = 0u; | 1747 num_bytes = 0u; |
1622 EXPECT_EQ(MOJO_RESULT_OK, | 1748 EXPECT_EQ(MOJO_RESULT_OK, |
1623 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), | 1749 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
1624 MakeUserPointer(&num_bytes), false)); | 1750 MakeUserPointer(&num_bytes), |
| 1751 false)); |
1625 EXPECT_TRUE(read_buffer_ptr); | 1752 EXPECT_TRUE(read_buffer_ptr); |
1626 EXPECT_EQ(2u * kTestDataSize, num_bytes); | 1753 EXPECT_EQ(2u * kTestDataSize, num_bytes); |
1627 | 1754 |
1628 // Close the producer. | 1755 // Close the producer. |
1629 dp->ProducerClose(); | 1756 dp->ProducerClose(); |
1630 | 1757 |
1631 // The consumer can finish its two-phase read. | 1758 // The consumer can finish its two-phase read. |
1632 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); | 1759 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); |
1633 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize)); | 1760 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize)); |
1634 | 1761 |
1635 // And start another. | 1762 // And start another. |
1636 read_buffer_ptr = nullptr; | 1763 read_buffer_ptr = nullptr; |
1637 num_bytes = 0u; | 1764 num_bytes = 0u; |
1638 EXPECT_EQ(MOJO_RESULT_OK, | 1765 EXPECT_EQ(MOJO_RESULT_OK, |
1639 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), | 1766 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
1640 MakeUserPointer(&num_bytes), false)); | 1767 MakeUserPointer(&num_bytes), |
| 1768 false)); |
1641 EXPECT_TRUE(read_buffer_ptr); | 1769 EXPECT_TRUE(read_buffer_ptr); |
1642 EXPECT_EQ(kTestDataSize, num_bytes); | 1770 EXPECT_EQ(kTestDataSize, num_bytes); |
1643 | 1771 |
1644 // Close the consumer, which cancels the two-phase read. | 1772 // Close the consumer, which cancels the two-phase read. |
1645 dp->ConsumerClose(); | 1773 dp->ConsumerClose(); |
1646 } | 1774 } |
1647 | 1775 |
1648 // Close consumer first, then producer. | 1776 // Close consumer first, then producer. |
1649 { | 1777 { |
1650 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1778 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1651 | 1779 |
1652 // Write some data, so we'll have something to read. | 1780 // Write some data, so we'll have something to read. |
1653 uint32_t num_bytes = kTestDataSize; | 1781 uint32_t num_bytes = kTestDataSize; |
1654 EXPECT_EQ(MOJO_RESULT_OK, | 1782 EXPECT_EQ(MOJO_RESULT_OK, |
1655 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1783 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1656 MakeUserPointer(&num_bytes), false)); | 1784 MakeUserPointer(&num_bytes), |
| 1785 false)); |
1657 EXPECT_EQ(kTestDataSize, num_bytes); | 1786 EXPECT_EQ(kTestDataSize, num_bytes); |
1658 | 1787 |
1659 // Start two-phase write. | 1788 // Start two-phase write. |
1660 void* write_buffer_ptr = nullptr; | 1789 void* write_buffer_ptr = nullptr; |
1661 num_bytes = 0u; | 1790 num_bytes = 0u; |
1662 EXPECT_EQ(MOJO_RESULT_OK, | 1791 EXPECT_EQ(MOJO_RESULT_OK, |
1663 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1792 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1664 MakeUserPointer(&num_bytes), false)); | 1793 MakeUserPointer(&num_bytes), |
| 1794 false)); |
1665 EXPECT_TRUE(write_buffer_ptr); | 1795 EXPECT_TRUE(write_buffer_ptr); |
1666 ASSERT_GT(num_bytes, kTestDataSize); | 1796 ASSERT_GT(num_bytes, kTestDataSize); |
1667 | 1797 |
1668 // Start two-phase read. | 1798 // Start two-phase read. |
1669 const void* read_buffer_ptr = nullptr; | 1799 const void* read_buffer_ptr = nullptr; |
1670 num_bytes = 0u; | 1800 num_bytes = 0u; |
1671 EXPECT_EQ(MOJO_RESULT_OK, | 1801 EXPECT_EQ(MOJO_RESULT_OK, |
1672 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), | 1802 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
1673 MakeUserPointer(&num_bytes), false)); | 1803 MakeUserPointer(&num_bytes), |
| 1804 false)); |
1674 EXPECT_TRUE(read_buffer_ptr); | 1805 EXPECT_TRUE(read_buffer_ptr); |
1675 EXPECT_EQ(kTestDataSize, num_bytes); | 1806 EXPECT_EQ(kTestDataSize, num_bytes); |
1676 | 1807 |
1677 // Close the consumer. | 1808 // Close the consumer. |
1678 dp->ConsumerClose(); | 1809 dp->ConsumerClose(); |
1679 | 1810 |
1680 // Actually write some data. (Note: Premature freeing of the buffer would | 1811 // Actually write some data. (Note: Premature freeing of the buffer would |
1681 // probably only be detected under ASAN or similar.) | 1812 // probably only be detected under ASAN or similar.) |
1682 memcpy(write_buffer_ptr, kTestData, kTestDataSize); | 1813 memcpy(write_buffer_ptr, kTestData, kTestDataSize); |
1683 // Note: Even though the consumer has been closed, ending the two-phase | 1814 // Note: Even though the consumer has been closed, ending the two-phase |
1684 // write will report success. | 1815 // write will report success. |
1685 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(kTestDataSize)); | 1816 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(kTestDataSize)); |
1686 | 1817 |
1687 // But trying to write should result in failure. | 1818 // But trying to write should result in failure. |
1688 num_bytes = kTestDataSize; | 1819 num_bytes = kTestDataSize; |
1689 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1820 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1690 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1821 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1691 MakeUserPointer(&num_bytes), false)); | 1822 MakeUserPointer(&num_bytes), |
| 1823 false)); |
1692 | 1824 |
1693 // As will trying to start another two-phase write. | 1825 // As will trying to start another two-phase write. |
1694 write_buffer_ptr = nullptr; | 1826 write_buffer_ptr = nullptr; |
1695 num_bytes = 0u; | 1827 num_bytes = 0u; |
1696 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1828 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1697 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1829 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1698 MakeUserPointer(&num_bytes), false)); | 1830 MakeUserPointer(&num_bytes), |
| 1831 false)); |
1699 | 1832 |
1700 dp->ProducerClose(); | 1833 dp->ProducerClose(); |
1701 } | 1834 } |
1702 | 1835 |
1703 // Test closing the consumer first, then the producer, with an active | 1836 // Test closing the consumer first, then the producer, with an active |
1704 // two-phase write. | 1837 // two-phase write. |
1705 { | 1838 { |
1706 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1839 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1707 | 1840 |
1708 // Start two-phase write. | 1841 // Start two-phase write. |
1709 void* write_buffer_ptr = nullptr; | 1842 void* write_buffer_ptr = nullptr; |
1710 uint32_t num_bytes = 0u; | 1843 uint32_t num_bytes = 0u; |
1711 EXPECT_EQ(MOJO_RESULT_OK, | 1844 EXPECT_EQ(MOJO_RESULT_OK, |
1712 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1845 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1713 MakeUserPointer(&num_bytes), false)); | 1846 MakeUserPointer(&num_bytes), |
| 1847 false)); |
1714 EXPECT_TRUE(write_buffer_ptr); | 1848 EXPECT_TRUE(write_buffer_ptr); |
1715 ASSERT_GT(num_bytes, kTestDataSize); | 1849 ASSERT_GT(num_bytes, kTestDataSize); |
1716 | 1850 |
1717 dp->ConsumerClose(); | 1851 dp->ConsumerClose(); |
1718 dp->ProducerClose(); | 1852 dp->ProducerClose(); |
1719 } | 1853 } |
1720 | 1854 |
1721 // Test closing the producer and then trying to read (with no data). | 1855 // Test closing the producer and then trying to read (with no data). |
1722 { | 1856 { |
1723 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1857 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1724 | 1858 |
1725 // Write some data, so we'll have something to read. | 1859 // Write some data, so we'll have something to read. |
1726 uint32_t num_bytes = kTestDataSize; | 1860 uint32_t num_bytes = kTestDataSize; |
1727 EXPECT_EQ(MOJO_RESULT_OK, | 1861 EXPECT_EQ(MOJO_RESULT_OK, |
1728 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1862 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1729 MakeUserPointer(&num_bytes), false)); | 1863 MakeUserPointer(&num_bytes), |
| 1864 false)); |
1730 EXPECT_EQ(kTestDataSize, num_bytes); | 1865 EXPECT_EQ(kTestDataSize, num_bytes); |
1731 | 1866 |
1732 // Close the producer. | 1867 // Close the producer. |
1733 dp->ProducerClose(); | 1868 dp->ProducerClose(); |
1734 | 1869 |
1735 // Peek that data. | 1870 // Peek that data. |
1736 char buffer[1000]; | 1871 char buffer[1000]; |
1737 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 1872 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
1738 EXPECT_EQ(MOJO_RESULT_OK, | 1873 EXPECT_EQ( |
1739 dp->ConsumerReadData(UserPointer<void>(buffer), | 1874 MOJO_RESULT_OK, |
1740 MakeUserPointer(&num_bytes), false, true)); | 1875 dp->ConsumerReadData(UserPointer<void>(buffer), |
| 1876 MakeUserPointer(&num_bytes), |
| 1877 false, |
| 1878 true)); |
1741 EXPECT_EQ(kTestDataSize, num_bytes); | 1879 EXPECT_EQ(kTestDataSize, num_bytes); |
1742 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize)); | 1880 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize)); |
1743 | 1881 |
1744 // Read that data. | 1882 // Read that data. |
1745 memset(buffer, 0, 1000); | 1883 memset(buffer, 0, 1000); |
1746 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 1884 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
1747 EXPECT_EQ(MOJO_RESULT_OK, | 1885 EXPECT_EQ( |
1748 dp->ConsumerReadData(UserPointer<void>(buffer), | 1886 MOJO_RESULT_OK, |
1749 MakeUserPointer(&num_bytes), false, false)); | 1887 dp->ConsumerReadData(UserPointer<void>(buffer), |
| 1888 MakeUserPointer(&num_bytes), |
| 1889 false, |
| 1890 false)); |
1750 EXPECT_EQ(kTestDataSize, num_bytes); | 1891 EXPECT_EQ(kTestDataSize, num_bytes); |
1751 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize)); | 1892 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize)); |
1752 | 1893 |
1753 // A second read should fail. | 1894 // A second read should fail. |
1754 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 1895 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
1755 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1896 EXPECT_EQ( |
1756 dp->ConsumerReadData(UserPointer<void>(buffer), | 1897 MOJO_RESULT_FAILED_PRECONDITION, |
1757 MakeUserPointer(&num_bytes), false, false)); | 1898 dp->ConsumerReadData(UserPointer<void>(buffer), |
| 1899 MakeUserPointer(&num_bytes), |
| 1900 false, |
| 1901 false)); |
1758 | 1902 |
1759 // A two-phase read should also fail. | 1903 // A two-phase read should also fail. |
1760 const void* read_buffer_ptr = nullptr; | 1904 const void* read_buffer_ptr = nullptr; |
1761 num_bytes = 0u; | 1905 num_bytes = 0u; |
1762 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1906 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1763 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), | 1907 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
1764 MakeUserPointer(&num_bytes), false)); | 1908 MakeUserPointer(&num_bytes), |
| 1909 false)); |
1765 | 1910 |
1766 // Ditto for discard. | 1911 // Ditto for discard. |
1767 num_bytes = 10u; | 1912 num_bytes = 10u; |
1768 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1913 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1769 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); | 1914 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); |
1770 | 1915 |
1771 dp->ConsumerClose(); | 1916 dp->ConsumerClose(); |
1772 } | 1917 } |
1773 } | 1918 } |
1774 | 1919 |
1775 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) { | 1920 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) { |
1776 const MojoCreateDataPipeOptions options = { | 1921 const MojoCreateDataPipeOptions options = { |
1777 kSizeOfOptions, // |struct_size|. | 1922 kSizeOfOptions, // |struct_size|. |
1778 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1923 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1779 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1924 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1780 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1925 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1781 }; | 1926 }; |
1782 MojoCreateDataPipeOptions validated_options = {0}; | 1927 MojoCreateDataPipeOptions validated_options = {0}; |
1783 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1928 EXPECT_EQ(MOJO_RESULT_OK, |
1784 MakeUserPointer(&options), &validated_options)); | 1929 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 1930 &validated_options)); |
1785 | 1931 |
1786 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1932 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1787 | 1933 |
1788 // No data. | 1934 // No data. |
1789 uint32_t num_bytes = 1000u; | 1935 uint32_t num_bytes = 1000u; |
1790 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1936 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1791 EXPECT_EQ(0u, num_bytes); | 1937 EXPECT_EQ(0u, num_bytes); |
1792 | 1938 |
1793 // Try "ending" a two-phase write when one isn't active. | 1939 // Try "ending" a two-phase write when one isn't active. |
1794 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1940 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1795 dp->ProducerEndWriteData(1u * sizeof(int32_t))); | 1941 dp->ProducerEndWriteData(1u * sizeof(int32_t))); |
1796 | 1942 |
1797 // Still no data. | 1943 // Still no data. |
1798 num_bytes = 1000u; | 1944 num_bytes = 1000u; |
1799 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1945 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1800 EXPECT_EQ(0u, num_bytes); | 1946 EXPECT_EQ(0u, num_bytes); |
1801 | 1947 |
1802 // Try ending a two-phase write with an invalid amount (too much). | 1948 // Try ending a two-phase write with an invalid amount (too much). |
1803 num_bytes = 0u; | 1949 num_bytes = 0u; |
1804 void* write_ptr = nullptr; | 1950 void* write_ptr = nullptr; |
1805 EXPECT_EQ(MOJO_RESULT_OK, | 1951 EXPECT_EQ( |
1806 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1952 MOJO_RESULT_OK, |
1807 MakeUserPointer(&num_bytes), false)); | 1953 dp->ProducerBeginWriteData( |
| 1954 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
1808 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1955 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1809 dp->ProducerEndWriteData(num_bytes + | 1956 dp->ProducerEndWriteData(num_bytes + |
1810 static_cast<uint32_t>(sizeof(int32_t)))); | 1957 static_cast<uint32_t>(sizeof(int32_t)))); |
1811 | 1958 |
1812 // But the two-phase write still ended. | 1959 // But the two-phase write still ended. |
1813 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); | 1960 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); |
1814 | 1961 |
1815 // Still no data. | 1962 // Still no data. |
1816 num_bytes = 1000u; | 1963 num_bytes = 1000u; |
1817 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1964 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1818 EXPECT_EQ(0u, num_bytes); | 1965 EXPECT_EQ(0u, num_bytes); |
1819 | 1966 |
1820 // Try ending a two-phase write with an invalid amount (not a multiple of the | 1967 // Try ending a two-phase write with an invalid amount (not a multiple of the |
1821 // element size). | 1968 // element size). |
1822 num_bytes = 0u; | 1969 num_bytes = 0u; |
1823 write_ptr = nullptr; | 1970 write_ptr = nullptr; |
1824 EXPECT_EQ(MOJO_RESULT_OK, | 1971 EXPECT_EQ( |
1825 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1972 MOJO_RESULT_OK, |
1826 MakeUserPointer(&num_bytes), false)); | 1973 dp->ProducerBeginWriteData( |
| 1974 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false)); |
1827 EXPECT_GE(num_bytes, 1u); | 1975 EXPECT_GE(num_bytes, 1u); |
1828 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u)); | 1976 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u)); |
1829 | 1977 |
1830 // But the two-phase write still ended. | 1978 // But the two-phase write still ended. |
1831 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); | 1979 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); |
1832 | 1980 |
1833 // Still no data. | 1981 // Still no data. |
1834 num_bytes = 1000u; | 1982 num_bytes = 1000u; |
1835 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1983 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1836 EXPECT_EQ(0u, num_bytes); | 1984 EXPECT_EQ(0u, num_bytes); |
1837 | 1985 |
1838 // Now write some data, so we'll be able to try reading. | 1986 // Now write some data, so we'll be able to try reading. |
1839 int32_t element = 123; | 1987 int32_t element = 123; |
1840 num_bytes = 1u * sizeof(int32_t); | 1988 num_bytes = 1u * sizeof(int32_t); |
1841 EXPECT_EQ(MOJO_RESULT_OK, | 1989 EXPECT_EQ(MOJO_RESULT_OK, |
1842 dp->ProducerWriteData(UserPointer<const void>(&element), | 1990 dp->ProducerWriteData(UserPointer<const void>(&element), |
1843 MakeUserPointer(&num_bytes), false)); | 1991 MakeUserPointer(&num_bytes), |
| 1992 false)); |
1844 | 1993 |
1845 // One element available. | 1994 // One element available. |
1846 num_bytes = 0u; | 1995 num_bytes = 0u; |
1847 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1996 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1848 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1997 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1849 | 1998 |
1850 // Try "ending" a two-phase read when one isn't active. | 1999 // Try "ending" a two-phase read when one isn't active. |
1851 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 2000 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1852 dp->ConsumerEndReadData(1u * sizeof(int32_t))); | 2001 dp->ConsumerEndReadData(1u * sizeof(int32_t))); |
1853 | 2002 |
1854 // Still one element available. | 2003 // Still one element available. |
1855 num_bytes = 0u; | 2004 num_bytes = 0u; |
1856 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 2005 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1857 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 2006 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1858 | 2007 |
1859 // Try ending a two-phase read with an invalid amount (too much). | 2008 // Try ending a two-phase read with an invalid amount (too much). |
1860 num_bytes = 0u; | 2009 num_bytes = 0u; |
1861 const void* read_ptr = nullptr; | 2010 const void* read_ptr = nullptr; |
1862 EXPECT_EQ(MOJO_RESULT_OK, | 2011 EXPECT_EQ( |
1863 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 2012 MOJO_RESULT_OK, |
1864 MakeUserPointer(&num_bytes), false)); | 2013 dp->ConsumerBeginReadData( |
| 2014 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false)); |
1865 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 2015 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1866 dp->ConsumerEndReadData(num_bytes + | 2016 dp->ConsumerEndReadData(num_bytes + |
1867 static_cast<uint32_t>(sizeof(int32_t)))); | 2017 static_cast<uint32_t>(sizeof(int32_t)))); |
1868 | 2018 |
1869 // Still one element available. | 2019 // Still one element available. |
1870 num_bytes = 0u; | 2020 num_bytes = 0u; |
1871 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 2021 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1872 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 2022 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1873 | 2023 |
1874 // Try ending a two-phase read with an invalid amount (not a multiple of the | 2024 // Try ending a two-phase read with an invalid amount (not a multiple of the |
1875 // element size). | 2025 // element size). |
1876 num_bytes = 0u; | 2026 num_bytes = 0u; |
1877 read_ptr = nullptr; | 2027 read_ptr = nullptr; |
1878 EXPECT_EQ(MOJO_RESULT_OK, | 2028 EXPECT_EQ( |
1879 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 2029 MOJO_RESULT_OK, |
1880 MakeUserPointer(&num_bytes), false)); | 2030 dp->ConsumerBeginReadData( |
| 2031 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false)); |
1881 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 2032 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1882 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); | 2033 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); |
1883 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u)); | 2034 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u)); |
1884 | 2035 |
1885 // Still one element available. | 2036 // Still one element available. |
1886 num_bytes = 0u; | 2037 num_bytes = 0u; |
1887 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 2038 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1888 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 2039 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1889 | 2040 |
1890 dp->ProducerClose(); | 2041 dp->ProducerClose(); |
1891 dp->ConsumerClose(); | 2042 dp->ConsumerClose(); |
1892 } | 2043 } |
1893 | 2044 |
1894 // Tests that even with "may discard", the data won't change under a two-phase | 2045 // Tests that even with "may discard", the data won't change under a two-phase |
1895 // read. | 2046 // read. |
1896 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two | 2047 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two |
1897 // related issues: First, we don't recognize that the data given to | 2048 // related issues: First, we don't recognize that the data given to |
1898 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|, | 2049 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|, |
1899 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the | 2050 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the |
1900 // |ProducerWriteData()| then changes the data underneath the two-phase read.) | 2051 // |ProducerWriteData()| then changes the data underneath the two-phase read.) |
1901 TEST(LocalDataPipeTest, DISABLED_MayDiscardTwoPhaseConsistent) { | 2052 TEST(LocalDataPipeTest, DISABLED_MayDiscardTwoPhaseConsistent) { |
1902 const MojoCreateDataPipeOptions options = { | 2053 const MojoCreateDataPipeOptions options = { |
1903 kSizeOfOptions, // |struct_size|. | 2054 kSizeOfOptions, // |struct_size|. |
1904 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 2055 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
1905 1, // |element_num_bytes|. | 2056 1, // |element_num_bytes|. |
1906 2 // |capacity_num_bytes|. | 2057 2 // |capacity_num_bytes|. |
1907 }; | 2058 }; |
1908 MojoCreateDataPipeOptions validated_options = {0}; | 2059 MojoCreateDataPipeOptions validated_options = {0}; |
1909 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 2060 EXPECT_EQ(MOJO_RESULT_OK, |
1910 MakeUserPointer(&options), &validated_options)); | 2061 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
| 2062 &validated_options)); |
1911 | 2063 |
1912 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 2064 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
1913 | 2065 |
1914 // Write some elements. | 2066 // Write some elements. |
1915 char elements[2] = {'a', 'b'}; | 2067 char elements[2] = {'a', 'b'}; |
1916 uint32_t num_bytes = 2u; | 2068 uint32_t num_bytes = 2u; |
1917 EXPECT_EQ(MOJO_RESULT_OK, | 2069 EXPECT_EQ(MOJO_RESULT_OK, |
1918 dp->ProducerWriteData(UserPointer<const void>(elements), | 2070 dp->ProducerWriteData(UserPointer<const void>(elements), |
1919 MakeUserPointer(&num_bytes), false)); | 2071 MakeUserPointer(&num_bytes), |
| 2072 false)); |
1920 EXPECT_EQ(2u, num_bytes); | 2073 EXPECT_EQ(2u, num_bytes); |
1921 | 2074 |
1922 // Begin reading. | 2075 // Begin reading. |
1923 const void* read_ptr = nullptr; | 2076 const void* read_ptr = nullptr; |
1924 num_bytes = 2u; | 2077 num_bytes = 2u; |
1925 EXPECT_EQ(MOJO_RESULT_OK, | 2078 EXPECT_EQ( |
1926 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 2079 MOJO_RESULT_OK, |
1927 MakeUserPointer(&num_bytes), false)); | 2080 dp->ConsumerBeginReadData( |
| 2081 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false)); |
1928 EXPECT_EQ(2u, num_bytes); | 2082 EXPECT_EQ(2u, num_bytes); |
1929 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); | 2083 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); |
1930 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); | 2084 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); |
1931 | 2085 |
1932 // Try to write some more. But nothing should be discardable right now. | 2086 // Try to write some more. But nothing should be discardable right now. |
1933 elements[0] = 'x'; | 2087 elements[0] = 'x'; |
1934 elements[1] = 'y'; | 2088 elements[1] = 'y'; |
1935 num_bytes = 2u; | 2089 num_bytes = 2u; |
1936 // TODO(vtl): This should be: | 2090 // TODO(vtl): This should be: |
1937 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 2091 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
1938 // dp->ProducerWriteData(elements, &num_bytes, false)); | 2092 // dp->ProducerWriteData(elements, &num_bytes, false)); |
1939 // but we incorrectly think that the bytes being read are discardable. Letting | 2093 // but we incorrectly think that the bytes being read are discardable. Letting |
1940 // this through reveals the significant consequence. | 2094 // this through reveals the significant consequence. |
1941 EXPECT_EQ(MOJO_RESULT_OK, | 2095 EXPECT_EQ(MOJO_RESULT_OK, |
1942 dp->ProducerWriteData(UserPointer<const void>(elements), | 2096 dp->ProducerWriteData(UserPointer<const void>(elements), |
1943 MakeUserPointer(&num_bytes), false)); | 2097 MakeUserPointer(&num_bytes), |
| 2098 false)); |
1944 | 2099 |
1945 // Check that our read buffer hasn't changed underneath us. | 2100 // Check that our read buffer hasn't changed underneath us. |
1946 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); | 2101 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); |
1947 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); | 2102 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); |
1948 | 2103 |
1949 // End reading. | 2104 // End reading. |
1950 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 2105 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
1951 | 2106 |
1952 // Now writing should succeed. | 2107 // Now writing should succeed. |
1953 EXPECT_EQ(MOJO_RESULT_OK, | 2108 EXPECT_EQ(MOJO_RESULT_OK, |
1954 dp->ProducerWriteData(UserPointer<const void>(elements), | 2109 dp->ProducerWriteData(UserPointer<const void>(elements), |
1955 MakeUserPointer(&num_bytes), false)); | 2110 MakeUserPointer(&num_bytes), |
| 2111 false)); |
1956 | 2112 |
1957 // And if we read, we should get the new values. | 2113 // And if we read, we should get the new values. |
1958 read_ptr = nullptr; | 2114 read_ptr = nullptr; |
1959 num_bytes = 2u; | 2115 num_bytes = 2u; |
1960 EXPECT_EQ(MOJO_RESULT_OK, | 2116 EXPECT_EQ( |
1961 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 2117 MOJO_RESULT_OK, |
1962 MakeUserPointer(&num_bytes), false)); | 2118 dp->ConsumerBeginReadData( |
| 2119 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false)); |
1963 EXPECT_EQ(2u, num_bytes); | 2120 EXPECT_EQ(2u, num_bytes); |
1964 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); | 2121 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); |
1965 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]); | 2122 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]); |
1966 | 2123 |
1967 // End reading. | 2124 // End reading. |
1968 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 2125 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
1969 | 2126 |
1970 dp->ProducerClose(); | 2127 dp->ProducerClose(); |
1971 dp->ConsumerClose(); | 2128 dp->ConsumerClose(); |
1972 } | 2129 } |
1973 | 2130 |
1974 } // namespace | 2131 } // namespace |
1975 } // namespace system | 2132 } // namespace system |
1976 } // namespace mojo | 2133 } // namespace mojo |
OLD | NEW |