| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/common/test/multiprocess_test_base.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "build/build_config.h" | |
| 9 #include "mojo/common/test/test_utils.h" | |
| 10 #include "mojo/system/embedder/scoped_platform_handle.h" | |
| 11 | |
| 12 #if defined(OS_POSIX) | |
| 13 #include <fcntl.h> | |
| 14 #endif | |
| 15 | |
| 16 #if defined(OS_WIN) | |
| 17 #include "base/win/windows_version.h" | |
| 18 #endif | |
| 19 | |
| 20 namespace mojo { | |
| 21 namespace test { | |
| 22 namespace { | |
| 23 | |
| 24 // Returns true and logs a warning on Windows prior to Vista. | |
| 25 bool SkipTest() { | |
| 26 #if defined(OS_WIN) | |
| 27 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
| 28 LOG(WARNING) << "Test skipped: Vista or later needed."; | |
| 29 return true; | |
| 30 } | |
| 31 #endif | |
| 32 | |
| 33 return false; | |
| 34 } | |
| 35 | |
| 36 bool IsNonBlocking(const embedder::PlatformHandle& handle) { | |
| 37 #if defined(OS_WIN) | |
| 38 // Haven't figured out a way to query whether a HANDLE was created with | |
| 39 // FILE_FLAG_OVERLAPPED. | |
| 40 return true; | |
| 41 #else | |
| 42 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; | |
| 43 #endif | |
| 44 } | |
| 45 | |
| 46 bool WriteByte(const embedder::PlatformHandle& handle, char c) { | |
| 47 size_t bytes_written = 0; | |
| 48 BlockingWrite(handle, &c, 1, &bytes_written); | |
| 49 return bytes_written == 1; | |
| 50 } | |
| 51 | |
| 52 bool ReadByte(const embedder::PlatformHandle& handle, char* c) { | |
| 53 size_t bytes_read = 0; | |
| 54 BlockingRead(handle, c, 1, &bytes_read); | |
| 55 return bytes_read == 1; | |
| 56 } | |
| 57 | |
| 58 typedef MultiprocessTestBase MultiprocessTestBaseTest; | |
| 59 | |
| 60 TEST_F(MultiprocessTestBaseTest, RunChild) { | |
| 61 if (SkipTest()) | |
| 62 return; | |
| 63 | |
| 64 EXPECT_TRUE(server_platform_handle.is_valid()); | |
| 65 | |
| 66 StartChild("RunChild"); | |
| 67 EXPECT_EQ(123, WaitForChildShutdown()); | |
| 68 } | |
| 69 | |
| 70 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { | |
| 71 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid()); | |
| 72 return 123; | |
| 73 } | |
| 74 | |
| 75 TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) { | |
| 76 if (SkipTest()) | |
| 77 return; | |
| 78 | |
| 79 StartChild("NoSuchTestChildMain"); | |
| 80 int result = WaitForChildShutdown(); | |
| 81 EXPECT_FALSE(result >= 0 && result <= 127); | |
| 82 } | |
| 83 | |
| 84 TEST_F(MultiprocessTestBaseTest, PassedChannel) { | |
| 85 if (SkipTest()) | |
| 86 return; | |
| 87 | |
| 88 EXPECT_TRUE(server_platform_handle.is_valid()); | |
| 89 StartChild("PassedChannel"); | |
| 90 | |
| 91 // Take ownership of the handle. | |
| 92 embedder::ScopedPlatformHandle handle = server_platform_handle.Pass(); | |
| 93 | |
| 94 // The handle should be non-blocking. | |
| 95 EXPECT_TRUE(IsNonBlocking(handle.get())); | |
| 96 | |
| 97 // Write a byte. | |
| 98 const char c = 'X'; | |
| 99 EXPECT_TRUE(WriteByte(handle.get(), c)); | |
| 100 | |
| 101 // It'll echo it back to us, incremented. | |
| 102 char d = 0; | |
| 103 EXPECT_TRUE(ReadByte(handle.get(), &d)); | |
| 104 EXPECT_EQ(c + 1, d); | |
| 105 | |
| 106 // And return it, incremented again. | |
| 107 EXPECT_EQ(c + 2, WaitForChildShutdown()); | |
| 108 } | |
| 109 | |
| 110 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) { | |
| 111 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid()); | |
| 112 | |
| 113 // Take ownership of the handle. | |
| 114 embedder::ScopedPlatformHandle handle = | |
| 115 MultiprocessTestBaseTest::client_platform_handle.Pass(); | |
| 116 | |
| 117 // The handle should be non-blocking. | |
| 118 EXPECT_TRUE(IsNonBlocking(handle.get())); | |
| 119 | |
| 120 // Read a byte. | |
| 121 char c = 0; | |
| 122 EXPECT_TRUE(ReadByte(handle.get(), &c)); | |
| 123 | |
| 124 // Write it back, incremented. | |
| 125 c++; | |
| 126 EXPECT_TRUE(WriteByte(handle.get(), c)); | |
| 127 | |
| 128 // And return it, incremented again. | |
| 129 c++; | |
| 130 return static_cast<int>(c); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 } // namespace test | |
| 135 } // namespace mojo | |
| OLD | NEW |