rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / mojo / common / test / test_utils_posix.cc
blobe4537d3692e435d4a13104d3db331aaa3eb1ab81
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/common/test/test_utils.h"
7 #include <fcntl.h>
8 #include <unistd.h>
10 #include "base/posix/eintr_wrapper.h"
11 #include "mojo/system/embedder/platform_handle.h"
13 namespace mojo {
14 namespace test {
16 bool BlockingWrite(const embedder::PlatformHandle& handle,
17 const void* buffer,
18 size_t bytes_to_write,
19 size_t* bytes_written) {
20 int original_flags = fcntl(handle.fd, F_GETFL);
21 if (original_flags == -1 ||
22 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
23 return false;
26 ssize_t result = HANDLE_EINTR(write(handle.fd, buffer, bytes_to_write));
28 fcntl(handle.fd, F_SETFL, original_flags);
30 if (result < 0)
31 return false;
33 *bytes_written = result;
34 return true;
37 bool BlockingRead(const embedder::PlatformHandle& handle,
38 void* buffer,
39 size_t buffer_size,
40 size_t* bytes_read) {
41 int original_flags = fcntl(handle.fd, F_GETFL);
42 if (original_flags == -1 ||
43 fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
44 return false;
47 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
49 fcntl(handle.fd, F_SETFL, original_flags);
51 if (result < 0)
52 return false;
54 *bytes_read = result;
55 return true;
58 bool NonBlockingRead(const embedder::PlatformHandle& handle,
59 void* buffer,
60 size_t buffer_size,
61 size_t* bytes_read) {
62 ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
64 if (result < 0) {
65 if (errno != EAGAIN && errno != EWOULDBLOCK)
66 return false;
68 *bytes_read = 0;
69 } else {
70 *bytes_read = result;
73 return true;
76 } // namespace test
77 } // namespace mojo