summaryrefslogtreecommitdiffstats
path: root/mojo/common
diff options
context:
space:
mode:
authoryzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-22 21:01:54 +0000
committeryzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-22 21:01:54 +0000
commitb8e55f6d84f2766a075ec93d4f2701dc91731209 (patch)
tree6094e964a846dd34f653380a8649f67f35aba0b1 /mojo/common
parent97a8178b5345c1041af29819dcbc20d748ee6a02 (diff)
downloadchromium_src-b8e55f6d84f2766a075ec93d4f2701dc91731209.zip
chromium_src-b8e55f6d84f2766a075ec93d4f2701dc91731209.tar.gz
chromium_src-b8e55f6d84f2766a075ec93d4f2701dc91731209.tar.bz2
Reland: Add some handle read/write helpers to mojo/common/test/test_utils.h
This helps to refactor raw_channel_posix_unittest to run on multiple platforms. TEST=mojo_common_unittests,mojo_system_unittests BUG=None TBR=viettrungluu@chromium.org Review URL: https://codereview.chromium.org/176063002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252822 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r--mojo/common/test/multiprocess_test_base_unittest.cc60
-rw-r--r--mojo/common/test/test_utils.h44
-rw-r--r--mojo/common/test/test_utils_posix.cc77
-rw-r--r--mojo/common/test/test_utils_win.cc80
4 files changed, 211 insertions, 50 deletions
diff --git a/mojo/common/test/multiprocess_test_base_unittest.cc b/mojo/common/test/multiprocess_test_base_unittest.cc
index 7b0a7ff..659b83d 100644
--- a/mojo/common/test/multiprocess_test_base_unittest.cc
+++ b/mojo/common/test/multiprocess_test_base_unittest.cc
@@ -6,22 +6,19 @@
#include "base/logging.h"
#include "build/build_config.h"
+#include "mojo/common/test/test_utils.h"
#include "mojo/system/embedder/scoped_platform_handle.h"
#if defined(OS_POSIX)
#include <fcntl.h>
-#include <unistd.h>
-
-#include "base/posix/eintr_wrapper.h"
#endif
#if defined(OS_WIN)
-#include <windows.h>
-
#include "base/win/windows_version.h"
#endif
namespace mojo {
+namespace test {
namespace {
// Returns true and logs a warning on Windows prior to Vista.
@@ -46,57 +43,19 @@ bool IsNonBlocking(const embedder::PlatformHandle& handle) {
#endif
}
-// Note: On POSIX, this method sets the handle to block.
bool WriteByte(const embedder::PlatformHandle& handle, char c) {
-#if defined(OS_WIN)
- DWORD num_bytes_written = 0;
- OVERLAPPED overlapped = { 0 };
-
- if (!WriteFile(handle.handle, &c, 1, &num_bytes_written, &overlapped)) {
- if (GetLastError() != ERROR_IO_PENDING)
- return false;
-
- if (GetOverlappedResult(handle.handle, &overlapped, &num_bytes_written,
- TRUE)) {
- return num_bytes_written == 1;
- }
-
- return false;
- }
- return num_bytes_written == 1;
-#else
- // We're lazy. Set it to block.
- PCHECK(fcntl(handle.fd, F_SETFL, 0) == 0);
-
- return HANDLE_EINTR(write(handle.fd, &c, 1)) == 1;
-#endif
+ size_t bytes_written = 0;
+ BlockingWrite(handle, &c, 1, &bytes_written);
+ return bytes_written == 1;
}
-// Note: On POSIX, this method sets the handle to block.
bool ReadByte(const embedder::PlatformHandle& handle, char* c) {
-#if defined(OS_WIN)
- DWORD num_bytes_read = 0;
- OVERLAPPED overlapped = { 0 };
-
- if (!ReadFile(handle.handle, c, 1, &num_bytes_read, &overlapped)) {
- if (GetLastError() != ERROR_IO_PENDING)
- return false;
-
- if (GetOverlappedResult(handle.handle, &overlapped, &num_bytes_read, TRUE))
- return num_bytes_read == 1;
-
- return false;
- }
- return num_bytes_read == 1;
-#else
- // We're lazy. Set it to block.
- PCHECK(fcntl(handle.fd, F_SETFL, 0) == 0);
-
- return HANDLE_EINTR(read(handle.fd, c, 1)) == 1;
-#endif
+ size_t bytes_read = 0;
+ BlockingRead(handle, c, 1, &bytes_read);
+ return bytes_read == 1;
}
-typedef test::MultiprocessTestBase MultiprocessTestBaseTest;
+typedef MultiprocessTestBase MultiprocessTestBaseTest;
TEST_F(MultiprocessTestBaseTest, RunChild) {
if (SkipTest())
@@ -172,4 +131,5 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) {
}
} // namespace
+} // namespace test
} // namespace mojo
diff --git a/mojo/common/test/test_utils.h b/mojo/common/test/test_utils.h
new file mode 100644
index 0000000..369e2bf
--- /dev/null
+++ b/mojo/common/test/test_utils.h
@@ -0,0 +1,44 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_COMMON_TEST_TEST_UTILS_H_
+#define MOJO_COMMON_TEST_TEST_UTILS_H_
+
+#include <stddef.h>
+
+namespace mojo {
+
+namespace embedder {
+struct PlatformHandle;
+}
+
+namespace test {
+
+// On success, |bytes_written| is updated to the number of bytes written;
+// otherwise it is untouched.
+bool BlockingWrite(const embedder::PlatformHandle& handle,
+ const void* buffer,
+ size_t bytes_to_write,
+ size_t* bytes_written);
+
+// On success, |bytes_read| is updated to the number of bytes read; otherwise it
+// is untouched.
+bool BlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read);
+
+// If the read is done successfully or would block, the function returns true
+// and updates |bytes_read| to the number of bytes read (0 if the read would
+// block); otherwise it returns false and leaves |bytes_read| untouched.
+// |handle| must already be in non-blocking mode.
+bool NonBlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read);
+
+} // namespace test
+} // namespace mojo
+
+#endif // MOJO_COMMON_TEST_TEST_UTILS_H_
diff --git a/mojo/common/test/test_utils_posix.cc b/mojo/common/test/test_utils_posix.cc
new file mode 100644
index 0000000..e4537d3
--- /dev/null
+++ b/mojo/common/test/test_utils_posix.cc
@@ -0,0 +1,77 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/test/test_utils.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "base/posix/eintr_wrapper.h"
+#include "mojo/system/embedder/platform_handle.h"
+
+namespace mojo {
+namespace test {
+
+bool BlockingWrite(const embedder::PlatformHandle& handle,
+ const void* buffer,
+ size_t bytes_to_write,
+ size_t* bytes_written) {
+ int original_flags = fcntl(handle.fd, F_GETFL);
+ if (original_flags == -1 ||
+ fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
+ return false;
+ }
+
+ ssize_t result = HANDLE_EINTR(write(handle.fd, buffer, bytes_to_write));
+
+ fcntl(handle.fd, F_SETFL, original_flags);
+
+ if (result < 0)
+ return false;
+
+ *bytes_written = result;
+ return true;
+}
+
+bool BlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read) {
+ int original_flags = fcntl(handle.fd, F_GETFL);
+ if (original_flags == -1 ||
+ fcntl(handle.fd, F_SETFL, original_flags & (~O_NONBLOCK)) != 0) {
+ return false;
+ }
+
+ ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
+
+ fcntl(handle.fd, F_SETFL, original_flags);
+
+ if (result < 0)
+ return false;
+
+ *bytes_read = result;
+ return true;
+}
+
+bool NonBlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read) {
+ ssize_t result = HANDLE_EINTR(read(handle.fd, buffer, buffer_size));
+
+ if (result < 0) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK)
+ return false;
+
+ *bytes_read = 0;
+ } else {
+ *bytes_read = result;
+ }
+
+ return true;
+}
+
+} // namespace test
+} // namespace mojo
diff --git a/mojo/common/test/test_utils_win.cc b/mojo/common/test/test_utils_win.cc
new file mode 100644
index 0000000..d4559dc
--- /dev/null
+++ b/mojo/common/test/test_utils_win.cc
@@ -0,0 +1,80 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/test/test_utils.h"
+
+#include <windows.h>
+
+#include "mojo/system/embedder/platform_handle.h"
+
+namespace mojo {
+namespace test {
+
+bool BlockingWrite(const embedder::PlatformHandle& handle,
+ const void* buffer,
+ size_t bytes_to_write,
+ size_t* bytes_written) {
+ OVERLAPPED overlapped = { 0 };
+ DWORD bytes_written_dword = 0;
+
+ if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write),
+ &bytes_written_dword, &overlapped)) {
+ if (GetLastError() != ERROR_IO_PENDING ||
+ !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword,
+ TRUE)) {
+ return false;
+ }
+ }
+
+ *bytes_written = bytes_written_dword;
+ return true;
+}
+
+bool BlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read) {
+ OVERLAPPED overlapped = { 0 };
+ DWORD bytes_read_dword = 0;
+
+ if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size),
+ &bytes_read_dword, &overlapped)) {
+ if (GetLastError() != ERROR_IO_PENDING ||
+ !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword,
+ TRUE)) {
+ return false;
+ }
+ }
+
+ *bytes_read = bytes_read_dword;
+ return true;
+}
+
+bool NonBlockingRead(const embedder::PlatformHandle& handle,
+ void* buffer,
+ size_t buffer_size,
+ size_t* bytes_read) {
+ OVERLAPPED overlapped = { 0 };
+ DWORD bytes_read_dword = 0;
+
+ if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size),
+ &bytes_read_dword, &overlapped)) {
+ if (GetLastError() != ERROR_IO_PENDING)
+ return false;
+
+ CancelIo(handle.handle);
+
+ if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword,
+ TRUE)) {
+ *bytes_read = 0;
+ return true;
+ }
+ }
+
+ *bytes_read = bytes_read_dword;
+ return true;
+}
+
+} // namespace test
+} // namespace mojo