diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-22 01:05:28 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-22 01:05:28 +0000 |
commit | cb80c11a7e7766f77f9fd002959c7548f8548477 (patch) | |
tree | 30f7389dbde3183de897eca492b38edd88ae9d1e /mojo/common | |
parent | 02c1bb2383d86dbee8bc6dc055a615a3c11e8bfd (diff) | |
download | chromium_src-cb80c11a7e7766f77f9fd002959c7548f8548477.zip chromium_src-cb80c11a7e7766f77f9fd002959c7548f8548477.tar.gz chromium_src-cb80c11a7e7766f77f9fd002959c7548f8548477.tar.bz2 |
Revert 252714 "Add some handle read/write helpers to mojo/common..."
> 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
> R=viettrungluu@chromium.org
>
> Review URL: https://codereview.chromium.org/172263006
TBR=yzshen@chromium.org
Review URL: https://codereview.chromium.org/175703004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252718 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r-- | mojo/common/test/multiprocess_test_base_unittest.cc | 60 | ||||
-rw-r--r-- | mojo/common/test/test_utils.h | 44 | ||||
-rw-r--r-- | mojo/common/test/test_utils_posix.cc | 77 | ||||
-rw-r--r-- | mojo/common/test/test_utils_win.cc | 80 |
4 files changed, 50 insertions, 211 deletions
diff --git a/mojo/common/test/multiprocess_test_base_unittest.cc b/mojo/common/test/multiprocess_test_base_unittest.cc index 659b83d..7b0a7ff 100644 --- a/mojo/common/test/multiprocess_test_base_unittest.cc +++ b/mojo/common/test/multiprocess_test_base_unittest.cc @@ -6,19 +6,22 @@ #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. @@ -43,19 +46,57 @@ 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) { - size_t bytes_written = 0; - BlockingWrite(handle, &c, 1, &bytes_written); - return bytes_written == 1; +#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 } +// Note: On POSIX, this method sets the handle to block. bool ReadByte(const embedder::PlatformHandle& handle, char* c) { - size_t bytes_read = 0; - BlockingRead(handle, c, 1, &bytes_read); - return bytes_read == 1; +#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 } -typedef MultiprocessTestBase MultiprocessTestBaseTest; +typedef test::MultiprocessTestBase MultiprocessTestBaseTest; TEST_F(MultiprocessTestBaseTest, RunChild) { if (SkipTest()) @@ -131,5 +172,4 @@ 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 deleted file mode 100644 index 369e2bf..0000000 --- a/mojo/common/test/test_utils.h +++ /dev/null @@ -1,44 +0,0 @@ -// 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 deleted file mode 100644 index e4537d3..0000000 --- a/mojo/common/test/test_utils_posix.cc +++ /dev/null @@ -1,77 +0,0 @@ -// 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 deleted file mode 100644 index 5bfbe2b..0000000 --- a/mojo/common/test/test_utils_win.cc +++ /dev/null @@ -1,80 +0,0 @@ -// 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, 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, 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, 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 |