| 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_helper.h" | 5 #include "mojo/common/test/multiprocess_test_helper.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/common/test/test_utils.h" |
| 10 #include "mojo/system/embedder/scoped_platform_handle.h" | 10 #include "mojo/system/embedder/scoped_platform_handle.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 14 #include <fcntl.h> | 14 #include <fcntl.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #if defined(OS_WIN) | |
| 18 #include "base/win/windows_version.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace mojo { | 17 namespace mojo { |
| 22 namespace test { | 18 namespace test { |
| 23 namespace { | 19 namespace { |
| 24 | 20 |
| 25 // Returns true and logs a warning on Windows prior to Vista. | |
| 26 bool SkipTest() { | |
| 27 #if defined(OS_WIN) | |
| 28 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 29 LOG(WARNING) << "Test skipped: Vista or later needed."; | |
| 30 return true; | |
| 31 } | |
| 32 #endif | |
| 33 | |
| 34 return false; | |
| 35 } | |
| 36 | |
| 37 bool IsNonBlocking(const embedder::PlatformHandle& handle) { | 21 bool IsNonBlocking(const embedder::PlatformHandle& handle) { |
| 38 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 39 // Haven't figured out a way to query whether a HANDLE was created with | 23 // Haven't figured out a way to query whether a HANDLE was created with |
| 40 // FILE_FLAG_OVERLAPPED. | 24 // FILE_FLAG_OVERLAPPED. |
| 41 return true; | 25 return true; |
| 42 #else | 26 #else |
| 43 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; | 27 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; |
| 44 #endif | 28 #endif |
| 45 } | 29 } |
| 46 | 30 |
| 47 bool WriteByte(const embedder::PlatformHandle& handle, char c) { | 31 bool WriteByte(const embedder::PlatformHandle& handle, char c) { |
| 48 size_t bytes_written = 0; | 32 size_t bytes_written = 0; |
| 49 BlockingWrite(handle, &c, 1, &bytes_written); | 33 BlockingWrite(handle, &c, 1, &bytes_written); |
| 50 return bytes_written == 1; | 34 return bytes_written == 1; |
| 51 } | 35 } |
| 52 | 36 |
| 53 bool ReadByte(const embedder::PlatformHandle& handle, char* c) { | 37 bool ReadByte(const embedder::PlatformHandle& handle, char* c) { |
| 54 size_t bytes_read = 0; | 38 size_t bytes_read = 0; |
| 55 BlockingRead(handle, c, 1, &bytes_read); | 39 BlockingRead(handle, c, 1, &bytes_read); |
| 56 return bytes_read == 1; | 40 return bytes_read == 1; |
| 57 } | 41 } |
| 58 | 42 |
| 59 typedef testing::Test MultiprocessTestHelperTest; | 43 typedef testing::Test MultiprocessTestHelperTest; |
| 60 | 44 |
| 61 TEST_F(MultiprocessTestHelperTest, RunChild) { | 45 TEST_F(MultiprocessTestHelperTest, RunChild) { |
| 62 if (SkipTest()) | |
| 63 return; | |
| 64 | |
| 65 MultiprocessTestHelper helper; | 46 MultiprocessTestHelper helper; |
| 66 EXPECT_TRUE(helper.server_platform_handle.is_valid()); | 47 EXPECT_TRUE(helper.server_platform_handle.is_valid()); |
| 67 | 48 |
| 68 helper.StartChild("RunChild"); | 49 helper.StartChild("RunChild"); |
| 69 EXPECT_EQ(123, helper.WaitForChildShutdown()); | 50 EXPECT_EQ(123, helper.WaitForChildShutdown()); |
| 70 } | 51 } |
| 71 | 52 |
| 72 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { | 53 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { |
| 73 CHECK(MultiprocessTestHelper::client_platform_handle.is_valid()); | 54 CHECK(MultiprocessTestHelper::client_platform_handle.is_valid()); |
| 74 return 123; | 55 return 123; |
| 75 } | 56 } |
| 76 | 57 |
| 77 TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) { | 58 TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) { |
| 78 if (SkipTest()) | |
| 79 return; | |
| 80 | |
| 81 MultiprocessTestHelper helper; | 59 MultiprocessTestHelper helper; |
| 82 helper.StartChild("NoSuchTestChildMain"); | 60 helper.StartChild("NoSuchTestChildMain"); |
| 83 int result = helper.WaitForChildShutdown(); | 61 int result = helper.WaitForChildShutdown(); |
| 84 EXPECT_FALSE(result >= 0 && result <= 127); | 62 EXPECT_FALSE(result >= 0 && result <= 127); |
| 85 } | 63 } |
| 86 | 64 |
| 87 TEST_F(MultiprocessTestHelperTest, PassedChannel) { | 65 TEST_F(MultiprocessTestHelperTest, PassedChannel) { |
| 88 if (SkipTest()) | |
| 89 return; | |
| 90 | |
| 91 MultiprocessTestHelper helper; | 66 MultiprocessTestHelper helper; |
| 92 EXPECT_TRUE(helper.server_platform_handle.is_valid()); | 67 EXPECT_TRUE(helper.server_platform_handle.is_valid()); |
| 93 helper.StartChild("PassedChannel"); | 68 helper.StartChild("PassedChannel"); |
| 94 | 69 |
| 95 // Take ownership of the handle. | 70 // Take ownership of the handle. |
| 96 embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass(); | 71 embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass(); |
| 97 | 72 |
| 98 // The handle should be non-blocking. | 73 // The handle should be non-blocking. |
| 99 EXPECT_TRUE(IsNonBlocking(handle.get())); | 74 EXPECT_TRUE(IsNonBlocking(handle.get())); |
| 100 | 75 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 127 | 102 |
| 128 // Write it back, incremented. | 103 // Write it back, incremented. |
| 129 c++; | 104 c++; |
| 130 EXPECT_TRUE(WriteByte(handle.get(), c)); | 105 EXPECT_TRUE(WriteByte(handle.get(), c)); |
| 131 | 106 |
| 132 // And return it, incremented again. | 107 // And return it, incremented again. |
| 133 c++; | 108 c++; |
| 134 return static_cast<int>(c); | 109 return static_cast<int>(c); |
| 135 } | 110 } |
| 136 | 111 |
| 112 TEST_F(MultiprocessTestHelperTest, ChildTestPasses) { |
| 113 MultiprocessTestHelper helper; |
| 114 EXPECT_TRUE(helper.server_platform_handle.is_valid()); |
| 115 helper.StartChild("ChildTestPasses"); |
| 116 EXPECT_TRUE(helper.WaitForChildTestShutdown()); |
| 117 } |
| 118 |
| 119 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses) { |
| 120 ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle.is_valid()); |
| 121 EXPECT_TRUE(IsNonBlocking( |
| 122 MultiprocessTestHelper::client_platform_handle.get())); |
| 123 } |
| 124 |
| 125 TEST_F(MultiprocessTestHelperTest, ChildTestFailsAssert) { |
| 126 MultiprocessTestHelper helper; |
| 127 EXPECT_TRUE(helper.server_platform_handle.is_valid()); |
| 128 helper.StartChild("ChildTestFailsAssert"); |
| 129 EXPECT_FALSE(helper.WaitForChildTestShutdown()); |
| 130 } |
| 131 |
| 132 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert) { |
| 133 ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) |
| 134 << "DISREGARD: Expected failure in child process"; |
| 135 ASSERT_FALSE(IsNonBlocking( |
| 136 MultiprocessTestHelper::client_platform_handle.get())) << "Not reached"; |
| 137 CHECK(false) << "Not reached"; |
| 138 } |
| 139 |
| 140 TEST_F(MultiprocessTestHelperTest, ChildTestFailsExpect) { |
| 141 MultiprocessTestHelper helper; |
| 142 EXPECT_TRUE(helper.server_platform_handle.is_valid()); |
| 143 helper.StartChild("ChildTestFailsExpect"); |
| 144 EXPECT_FALSE(helper.WaitForChildTestShutdown()); |
| 145 } |
| 146 |
| 147 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect) { |
| 148 EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) |
| 149 << "DISREGARD: Expected failure #1 in child process"; |
| 150 EXPECT_FALSE(IsNonBlocking( |
| 151 MultiprocessTestHelper::client_platform_handle.get())) |
| 152 << "DISREGARD: Expected failure #2 in child process"; |
| 153 } |
| 154 |
| 137 } // namespace | 155 } // namespace |
| 138 } // namespace test | 156 } // namespace test |
| 139 } // namespace mojo | 157 } // namespace mojo |
| OLD | NEW |