| OLD | NEW |
| 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/common/test/multiprocess_test_base.h" | 5 #include "mojo/common/test/multiprocess_test_base.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "mojo/common/test/test_utils.h" |
| 9 #include "mojo/system/embedder/scoped_platform_handle.h" | 10 #include "mojo/system/embedder/scoped_platform_handle.h" |
| 10 | 11 |
| 11 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
| 12 #include <fcntl.h> | 13 #include <fcntl.h> |
| 13 #include <unistd.h> | |
| 14 | |
| 15 #include "base/posix/eintr_wrapper.h" | |
| 16 #endif | 14 #endif |
| 17 | 15 |
| 18 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 19 #include <windows.h> | |
| 20 | |
| 21 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
| 22 #endif | 18 #endif |
| 23 | 19 |
| 24 namespace mojo { | 20 namespace mojo { |
| 21 namespace test { |
| 25 namespace { | 22 namespace { |
| 26 | 23 |
| 27 // Returns true and logs a warning on Windows prior to Vista. | 24 // Returns true and logs a warning on Windows prior to Vista. |
| 28 bool SkipTest() { | 25 bool SkipTest() { |
| 29 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 30 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | 27 if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
| 31 LOG(WARNING) << "Test skipped: Vista or later needed."; | 28 LOG(WARNING) << "Test skipped: Vista or later needed."; |
| 32 return true; | 29 return true; |
| 33 } | 30 } |
| 34 #endif | 31 #endif |
| 35 | 32 |
| 36 return false; | 33 return false; |
| 37 } | 34 } |
| 38 | 35 |
| 39 bool IsNonBlocking(const embedder::PlatformHandle& handle) { | 36 bool IsNonBlocking(const embedder::PlatformHandle& handle) { |
| 40 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 41 // Haven't figured out a way to query whether a HANDLE was created with | 38 // Haven't figured out a way to query whether a HANDLE was created with |
| 42 // FILE_FLAG_OVERLAPPED. | 39 // FILE_FLAG_OVERLAPPED. |
| 43 return true; | 40 return true; |
| 44 #else | 41 #else |
| 45 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; | 42 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; |
| 46 #endif | 43 #endif |
| 47 } | 44 } |
| 48 | 45 |
| 49 // Note: On POSIX, this method sets the handle to block. | |
| 50 bool WriteByte(const embedder::PlatformHandle& handle, char c) { | 46 bool WriteByte(const embedder::PlatformHandle& handle, char c) { |
| 51 #if defined(OS_WIN) | 47 size_t bytes_written = 0; |
| 52 DWORD num_bytes_written = 0; | 48 BlockingWrite(handle, &c, 1, &bytes_written); |
| 53 OVERLAPPED overlapped = { 0 }; | 49 return bytes_written == 1; |
| 54 | |
| 55 if (!WriteFile(handle.handle, &c, 1, &num_bytes_written, &overlapped)) { | |
| 56 if (GetLastError() != ERROR_IO_PENDING) | |
| 57 return false; | |
| 58 | |
| 59 if (GetOverlappedResult(handle.handle, &overlapped, &num_bytes_written, | |
| 60 TRUE)) { | |
| 61 return num_bytes_written == 1; | |
| 62 } | |
| 63 | |
| 64 return false; | |
| 65 } | |
| 66 return num_bytes_written == 1; | |
| 67 #else | |
| 68 // We're lazy. Set it to block. | |
| 69 PCHECK(fcntl(handle.fd, F_SETFL, 0) == 0); | |
| 70 | |
| 71 return HANDLE_EINTR(write(handle.fd, &c, 1)) == 1; | |
| 72 #endif | |
| 73 } | 50 } |
| 74 | 51 |
| 75 // Note: On POSIX, this method sets the handle to block. | |
| 76 bool ReadByte(const embedder::PlatformHandle& handle, char* c) { | 52 bool ReadByte(const embedder::PlatformHandle& handle, char* c) { |
| 77 #if defined(OS_WIN) | 53 size_t bytes_read = 0; |
| 78 DWORD num_bytes_read = 0; | 54 BlockingRead(handle, c, 1, &bytes_read); |
| 79 OVERLAPPED overlapped = { 0 }; | 55 return bytes_read == 1; |
| 80 | |
| 81 if (!ReadFile(handle.handle, c, 1, &num_bytes_read, &overlapped)) { | |
| 82 if (GetLastError() != ERROR_IO_PENDING) | |
| 83 return false; | |
| 84 | |
| 85 if (GetOverlappedResult(handle.handle, &overlapped, &num_bytes_read, TRUE)) | |
| 86 return num_bytes_read == 1; | |
| 87 | |
| 88 return false; | |
| 89 } | |
| 90 return num_bytes_read == 1; | |
| 91 #else | |
| 92 // We're lazy. Set it to block. | |
| 93 PCHECK(fcntl(handle.fd, F_SETFL, 0) == 0); | |
| 94 | |
| 95 return HANDLE_EINTR(read(handle.fd, c, 1)) == 1; | |
| 96 #endif | |
| 97 } | 56 } |
| 98 | 57 |
| 99 typedef test::MultiprocessTestBase MultiprocessTestBaseTest; | 58 typedef MultiprocessTestBase MultiprocessTestBaseTest; |
| 100 | 59 |
| 101 TEST_F(MultiprocessTestBaseTest, RunChild) { | 60 TEST_F(MultiprocessTestBaseTest, RunChild) { |
| 102 if (SkipTest()) | 61 if (SkipTest()) |
| 103 return; | 62 return; |
| 104 | 63 |
| 105 EXPECT_TRUE(server_platform_handle.is_valid()); | 64 EXPECT_TRUE(server_platform_handle.is_valid()); |
| 106 | 65 |
| 107 StartChild("RunChild"); | 66 StartChild("RunChild"); |
| 108 EXPECT_EQ(123, WaitForChildShutdown()); | 67 EXPECT_EQ(123, WaitForChildShutdown()); |
| 109 } | 68 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // Write it back, incremented. | 124 // Write it back, incremented. |
| 166 c++; | 125 c++; |
| 167 EXPECT_TRUE(WriteByte(handle.get(), c)); | 126 EXPECT_TRUE(WriteByte(handle.get(), c)); |
| 168 | 127 |
| 169 // And return it, incremented again. | 128 // And return it, incremented again. |
| 170 c++; | 129 c++; |
| 171 return static_cast<int>(c); | 130 return static_cast<int>(c); |
| 172 } | 131 } |
| 173 | 132 |
| 174 } // namespace | 133 } // namespace |
| 134 } // namespace test |
| 175 } // namespace mojo | 135 } // namespace mojo |
| OLD | NEW |