diff options
author | jamesr <jamesr@chromium.org> | 2014-10-02 21:26:48 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-10-03 04:27:07 +0000 |
commit | a03ae49d253d7d1f517e2f92146a6ba201c78aa6 (patch) | |
tree | df4eae1239d49490fa4e85d184b9000224a7a31c /mojo/edk/test/multiprocess_test_helper_unittest.cc | |
parent | 24b9789375c3c23d0d10e5197c6a7674946ea44b (diff) | |
download | chromium_src-a03ae49d253d7d1f517e2f92146a6ba201c78aa6.zip chromium_src-a03ae49d253d7d1f517e2f92146a6ba201c78aa6.tar.gz chromium_src-a03ae49d253d7d1f517e2f92146a6ba201c78aa6.tar.bz2 |
Move mojo edk into mojo/edk
This creates a mojo/edk directory which contains the "embedder developer
kit" aka the set of code needed to embed mojo code.
mojo/edk/embedder = code from mojo/embedder
mojo/edk/system = code from mojo/system
mojo/edk/test = code used to test the previous two, from mojo/common/test
mojo/edk/ can only depend on mojo/public/, base/ and itself.
R=viettrungluu@chromium.org
TBR=sky@chromium.org for file renames
Committed: https://chromium.googlesource.com/chromium/src/+/ee7ff197a98da4636f33bd713de784948b487bd4
Review URL: https://codereview.chromium.org/621153003
Cr-Commit-Position: refs/heads/master@{#297986}
Diffstat (limited to 'mojo/edk/test/multiprocess_test_helper_unittest.cc')
-rw-r--r-- | mojo/edk/test/multiprocess_test_helper_unittest.cc | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/mojo/edk/test/multiprocess_test_helper_unittest.cc b/mojo/edk/test/multiprocess_test_helper_unittest.cc new file mode 100644 index 0000000..ec2ba22 --- /dev/null +++ b/mojo/edk/test/multiprocess_test_helper_unittest.cc @@ -0,0 +1,157 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/edk/test/multiprocess_test_helper.h" + +#include "base/logging.h" +#include "build/build_config.h" +#include "mojo/edk/embedder/scoped_platform_handle.h" +#include "mojo/edk/test/test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_POSIX) +#include <fcntl.h> +#endif + +namespace mojo { +namespace test { +namespace { + +bool IsNonBlocking(const embedder::PlatformHandle& handle) { +#if defined(OS_WIN) + // Haven't figured out a way to query whether a HANDLE was created with + // FILE_FLAG_OVERLAPPED. + return true; +#else + return fcntl(handle.fd, F_GETFL) & O_NONBLOCK; +#endif +} + +bool WriteByte(const embedder::PlatformHandle& handle, char c) { + size_t bytes_written = 0; + BlockingWrite(handle, &c, 1, &bytes_written); + return bytes_written == 1; +} + +bool ReadByte(const embedder::PlatformHandle& handle, char* c) { + size_t bytes_read = 0; + BlockingRead(handle, c, 1, &bytes_read); + return bytes_read == 1; +} + +typedef testing::Test MultiprocessTestHelperTest; + +TEST_F(MultiprocessTestHelperTest, RunChild) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + + helper.StartChild("RunChild"); + EXPECT_EQ(123, helper.WaitForChildShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { + CHECK(MultiprocessTestHelper::client_platform_handle.is_valid()); + return 123; +} + +TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) { + MultiprocessTestHelper helper; + helper.StartChild("NoSuchTestChildMain"); + int result = helper.WaitForChildShutdown(); + EXPECT_FALSE(result >= 0 && result <= 127); +} + +TEST_F(MultiprocessTestHelperTest, PassedChannel) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("PassedChannel"); + + // Take ownership of the handle. + embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass(); + + // The handle should be non-blocking. + EXPECT_TRUE(IsNonBlocking(handle.get())); + + // Write a byte. + const char c = 'X'; + EXPECT_TRUE(WriteByte(handle.get(), c)); + + // It'll echo it back to us, incremented. + char d = 0; + EXPECT_TRUE(ReadByte(handle.get(), &d)); + EXPECT_EQ(c + 1, d); + + // And return it, incremented again. + EXPECT_EQ(c + 2, helper.WaitForChildShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) { + CHECK(MultiprocessTestHelper::client_platform_handle.is_valid()); + + // Take ownership of the handle. + embedder::ScopedPlatformHandle handle = + MultiprocessTestHelper::client_platform_handle.Pass(); + + // The handle should be non-blocking. + EXPECT_TRUE(IsNonBlocking(handle.get())); + + // Read a byte. + char c = 0; + EXPECT_TRUE(ReadByte(handle.get(), &c)); + + // Write it back, incremented. + c++; + EXPECT_TRUE(WriteByte(handle.get(), c)); + + // And return it, incremented again. + c++; + return static_cast<int>(c); +} + +TEST_F(MultiprocessTestHelperTest, ChildTestPasses) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestPasses"); + EXPECT_TRUE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses) { + ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle.is_valid()); + EXPECT_TRUE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())); +} + +TEST_F(MultiprocessTestHelperTest, ChildTestFailsAssert) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestFailsAssert"); + EXPECT_FALSE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert) { + ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) + << "DISREGARD: Expected failure in child process"; + ASSERT_FALSE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())) << "Not reached"; + CHECK(false) << "Not reached"; +} + +TEST_F(MultiprocessTestHelperTest, ChildTestFailsExpect) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestFailsExpect"); + EXPECT_FALSE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect) { + EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) + << "DISREGARD: Expected failure #1 in child process"; + EXPECT_FALSE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())) + << "DISREGARD: Expected failure #2 in child process"; +} + +} // namespace +} // namespace test +} // namespace mojo |