forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland: Add some handle read/write helpers to mojo/common/test/test_u…
…tils.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
- Loading branch information
yzshen@chromium.org
committed
Feb 22, 2014
1 parent
97a8178
commit b8e55f6
Showing
6 changed files
with
222 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters