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

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

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

Powered by Google App Engine
This is Rietveld 408576698