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/edk/test/multiprocess_test_helper.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "mojo/edk/embedder/scoped_platform_handle.h"
10 #include "mojo/edk/test/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
21 bool IsNonBlocking(const embedder::PlatformHandle
& handle
) {
23 // Haven't figured out a way to query whether a HANDLE was created with
24 // FILE_FLAG_OVERLAPPED.
27 return fcntl(handle
.fd
, F_GETFL
) & O_NONBLOCK
;
31 bool WriteByte(const embedder::PlatformHandle
& handle
, char c
) {
32 size_t bytes_written
= 0;
33 BlockingWrite(handle
, &c
, 1, &bytes_written
);
34 return bytes_written
== 1;
37 bool ReadByte(const embedder::PlatformHandle
& handle
, char* c
) {
38 size_t bytes_read
= 0;
39 BlockingRead(handle
, c
, 1, &bytes_read
);
40 return bytes_read
== 1;
43 typedef testing::Test MultiprocessTestHelperTest
;
45 #if defined(OS_ANDROID)
46 // Android multi-process tests are not executing the new process. This is flaky.
47 #define MAYBE_RunChild DISABLED_RunChild
49 #define MAYBE_RunChild RunChild
50 #endif // defined(OS_ANDROID)
51 TEST_F(MultiprocessTestHelperTest
, MAYBE_RunChild
) {
52 MultiprocessTestHelper helper
;
53 EXPECT_TRUE(helper
.server_platform_handle
.is_valid());
55 helper
.StartChild("RunChild");
56 EXPECT_EQ(123, helper
.WaitForChildShutdown());
59 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild
) {
60 CHECK(MultiprocessTestHelper::client_platform_handle
.is_valid());
64 #if defined(OS_ANDROID)
65 // Android multi-process tests are not executing the new process. This is flaky.
66 #define MAYBE_TestChildMainNotFound DISABLED_TestChildMainNotFound
68 #define MAYBE_TestChildMainNotFound TestChildMainNotFound
69 #endif // defined(OS_ANDROID)
70 TEST_F(MultiprocessTestHelperTest
, MAYBE_TestChildMainNotFound
) {
71 MultiprocessTestHelper helper
;
72 helper
.StartChild("NoSuchTestChildMain");
73 int result
= helper
.WaitForChildShutdown();
74 EXPECT_FALSE(result
>= 0 && result
<= 127);
77 #if defined(OS_ANDROID)
78 // Android multi-process tests are not executing the new process. This is flaky.
79 #define MAYBE_PassedChannel DISABLED_PassedChannel
81 #define MAYBE_PassedChannel PassedChannel
82 #endif // defined(OS_ANDROID)
83 TEST_F(MultiprocessTestHelperTest
, MAYBE_PassedChannel
) {
84 MultiprocessTestHelper helper
;
85 EXPECT_TRUE(helper
.server_platform_handle
.is_valid());
86 helper
.StartChild("PassedChannel");
88 // Take ownership of the handle.
89 embedder::ScopedPlatformHandle handle
= helper
.server_platform_handle
.Pass();
91 // The handle should be non-blocking.
92 EXPECT_TRUE(IsNonBlocking(handle
.get()));
96 EXPECT_TRUE(WriteByte(handle
.get(), c
));
98 // It'll echo it back to us, incremented.
100 EXPECT_TRUE(ReadByte(handle
.get(), &d
));
103 // And return it, incremented again.
104 EXPECT_EQ(c
+ 2, helper
.WaitForChildShutdown());
107 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel
) {
108 CHECK(MultiprocessTestHelper::client_platform_handle
.is_valid());
110 // Take ownership of the handle.
111 embedder::ScopedPlatformHandle handle
=
112 MultiprocessTestHelper::client_platform_handle
.Pass();
114 // The handle should be non-blocking.
115 EXPECT_TRUE(IsNonBlocking(handle
.get()));
119 EXPECT_TRUE(ReadByte(handle
.get(), &c
));
121 // Write it back, incremented.
123 EXPECT_TRUE(WriteByte(handle
.get(), c
));
125 // And return it, incremented again.
127 return static_cast<int>(c
);
130 #if defined(OS_ANDROID)
131 // Android multi-process tests are not executing the new process. This is flaky.
132 #define MAYBE_ChildTestPasses DISABLED_ChildTestPasses
134 #define MAYBE_ChildTestPasses ChildTestPasses
135 #endif // defined(OS_ANDROID)
136 TEST_F(MultiprocessTestHelperTest
, MAYBE_ChildTestPasses
) {
137 MultiprocessTestHelper helper
;
138 EXPECT_TRUE(helper
.server_platform_handle
.is_valid());
139 helper
.StartChild("ChildTestPasses");
140 EXPECT_TRUE(helper
.WaitForChildTestShutdown());
143 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses
) {
144 ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle
.is_valid());
146 IsNonBlocking(MultiprocessTestHelper::client_platform_handle
.get()));
149 #if defined(OS_ANDROID)
150 // Android multi-process tests are not executing the new process. This is flaky.
151 #define MAYBE_ChildTestFailsAssert DISABLED_ChildTestFailsAssert
153 #define MAYBE_ChildTestFailsAssert ChildTestFailsAssert
154 #endif // defined(OS_ANDROID)
155 TEST_F(MultiprocessTestHelperTest
, MAYBE_ChildTestFailsAssert
) {
156 MultiprocessTestHelper helper
;
157 EXPECT_TRUE(helper
.server_platform_handle
.is_valid());
158 helper
.StartChild("ChildTestFailsAssert");
159 EXPECT_FALSE(helper
.WaitForChildTestShutdown());
162 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert
) {
163 ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle
.is_valid())
164 << "DISREGARD: Expected failure in child process";
166 IsNonBlocking(MultiprocessTestHelper::client_platform_handle
.get()))
168 CHECK(false) << "Not reached";
171 #if defined(OS_ANDROID)
172 // Android multi-process tests are not executing the new process. This is flaky.
173 #define MAYBE_ChildTestFailsExpect DISABLED_ChildTestFailsExpect
175 #define MAYBE_ChildTestFailsExpect ChildTestFailsExpect
176 #endif // defined(OS_ANDROID)
177 TEST_F(MultiprocessTestHelperTest
, MAYBE_ChildTestFailsExpect
) {
178 MultiprocessTestHelper helper
;
179 EXPECT_TRUE(helper
.server_platform_handle
.is_valid());
180 helper
.StartChild("ChildTestFailsExpect");
181 EXPECT_FALSE(helper
.WaitForChildTestShutdown());
184 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect
) {
185 EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle
.is_valid())
186 << "DISREGARD: Expected failure #1 in child process";
188 IsNonBlocking(MultiprocessTestHelper::client_platform_handle
.get()))
189 << "DISREGARD: Expected failure #2 in child process";