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"
17 #include "base/win/windows_version.h"
24 // Returns true and logs a warning on Windows prior to Vista.
27 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
28 LOG(WARNING
) << "Test skipped: Vista or later needed.";
36 bool IsNonBlocking(const embedder::PlatformHandle
& handle
) {
38 // Haven't figured out a way to query whether a HANDLE was created with
39 // FILE_FLAG_OVERLAPPED.
42 return fcntl(handle
.fd
, F_GETFL
) & O_NONBLOCK
;
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
) {
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());
75 TEST_F(MultiprocessTestBaseTest
, TestChildMainNotFound
) {
79 StartChild("NoSuchTestChildMain");
80 int result
= WaitForChildShutdown();
81 EXPECT_FALSE(result
>= 0 && result
<= 127);
84 TEST_F(MultiprocessTestBaseTest
, PassedChannel
) {
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()));
99 EXPECT_TRUE(WriteByte(handle
.get(), c
));
101 // It'll echo it back to us, incremented.
103 EXPECT_TRUE(ReadByte(handle
.get(), &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()));
122 EXPECT_TRUE(ReadByte(handle
.get(), &c
));
124 // Write it back, incremented.
126 EXPECT_TRUE(WriteByte(handle
.get(), c
));
128 // And return it, incremented again.
130 return static_cast<int>(c
);