rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / mojo / common / test / multiprocess_test_base_unittest.cc
blob659b83dbb1629d9cda3b97987899ec3e9f363406
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.
5 #include "mojo/common/test/multiprocess_test_base.h"
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"
12 #if defined(OS_POSIX)
13 #include <fcntl.h>
14 #endif
16 #if defined(OS_WIN)
17 #include "base/win/windows_version.h"
18 #endif
20 namespace mojo {
21 namespace test {
22 namespace {
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;
31 #endif
33 return false;
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
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;
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;
58 typedef MultiprocessTestBase MultiprocessTestBaseTest;
60 TEST_F(MultiprocessTestBaseTest, RunChild) {
61 if (SkipTest())
62 return;
64 EXPECT_TRUE(server_platform_handle.is_valid());
66 StartChild("RunChild");
67 EXPECT_EQ(123, WaitForChildShutdown());
70 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) {
71 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid());
72 return 123;
75 TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) {
76 if (SkipTest())
77 return;
79 StartChild("NoSuchTestChildMain");
80 int result = WaitForChildShutdown();
81 EXPECT_FALSE(result >= 0 && result <= 127);
84 TEST_F(MultiprocessTestBaseTest, PassedChannel) {
85 if (SkipTest())
86 return;
88 EXPECT_TRUE(server_platform_handle.is_valid());
89 StartChild("PassedChannel");
91 // Take ownership of the handle.
92 embedder::ScopedPlatformHandle handle = server_platform_handle.Pass();
94 // The handle should be non-blocking.
95 EXPECT_TRUE(IsNonBlocking(handle.get()));
97 // Write a byte.
98 const char c = 'X';
99 EXPECT_TRUE(WriteByte(handle.get(), c));
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);
106 // And return it, incremented again.
107 EXPECT_EQ(c + 2, WaitForChildShutdown());
110 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) {
111 CHECK(MultiprocessTestBaseTest::client_platform_handle.is_valid());
113 // Take ownership of the handle.
114 embedder::ScopedPlatformHandle handle =
115 MultiprocessTestBaseTest::client_platform_handle.Pass();
117 // The handle should be non-blocking.
118 EXPECT_TRUE(IsNonBlocking(handle.get()));
120 // Read a byte.
121 char c = 0;
122 EXPECT_TRUE(ReadByte(handle.get(), &c));
124 // Write it back, incremented.
125 c++;
126 EXPECT_TRUE(WriteByte(handle.get(), c));
128 // And return it, incremented again.
129 c++;
130 return static_cast<int>(c);
133 } // namespace
134 } // namespace test
135 } // namespace mojo