diff options
-rw-r--r-- | base/global_descriptors_posix.cc | 6 | ||||
-rw-r--r-- | chrome/browser/importer/firefox_importer_unittest_utils_mac.cc | 3 | ||||
-rw-r--r-- | chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc | 5 | ||||
-rw-r--r-- | chrome/nacl/nacl_helper_linux.cc | 7 | ||||
-rw-r--r-- | ipc/ipc.gyp | 4 | ||||
-rw-r--r-- | ipc/ipc_fuzzing_tests.cc | 3 | ||||
-rw-r--r-- | ipc/ipc_multiprocess_test.cc | 19 | ||||
-rw-r--r-- | ipc/ipc_multiprocess_test.h | 23 | ||||
-rw-r--r-- | ipc/ipc_send_fds_test.cc | 5 | ||||
-rw-r--r-- | ipc/ipc_tests.cc | 5 | ||||
-rw-r--r-- | ipc/sync_socket_unittest.cc | 3 | ||||
-rw-r--r-- | testing/multiprocess_func_list.cc | 36 | ||||
-rw-r--r-- | testing/multiprocess_func_list.h | 21 |
13 files changed, 110 insertions, 30 deletions
diff --git a/base/global_descriptors_posix.cc b/base/global_descriptors_posix.cc index d8884a5..50526ec 100644 --- a/base/global_descriptors_posix.cc +++ b/base/global_descriptors_posix.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -34,9 +34,7 @@ int GlobalDescriptors::MaybeGet(Key key) const { return i->second; } - // In order to make unittests pass, we define a default mapping from keys to - // descriptors by adding a fixed offset: - return kBaseDescriptor + key; + return -1; } void GlobalDescriptors::Set(Key key, int fd) { diff --git a/chrome/browser/importer/firefox_importer_unittest_utils_mac.cc b/chrome/browser/importer/firefox_importer_unittest_utils_mac.cc index b1297ee..b9e6210 100644 --- a/chrome/browser/importer/firefox_importer_unittest_utils_mac.cc +++ b/chrome/browser/importer/firefox_importer_unittest_utils_mac.cc @@ -15,6 +15,7 @@ #include "ipc/ipc_channel.h" #include "ipc/ipc_descriptors.h" #include "ipc/ipc_message.h" +#include "ipc/ipc_multiprocess_test.h" #include "ipc/ipc_switches.h" #include "testing/multiprocess_func_list.h" @@ -263,7 +264,7 @@ class FFDecryptorClientChannelListener : public IPC::Listener { }; // Entry function in child process. -MULTIPROCESS_TEST_MAIN(NSSDecrypterChildProcess) { +MULTIPROCESS_IPC_TEST_MAIN(NSSDecrypterChildProcess) { MessageLoopForIO main_message_loop; FFDecryptorClientChannelListener listener; diff --git a/chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc b/chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc index 381bb6f..f0136d5 100644 --- a/chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc +++ b/chrome/browser/printing/cloud_print/test/cloud_print_proxy_process_browsertest.cc @@ -34,6 +34,7 @@ #include "content/public/browser/notification_service.h" #include "content/public/test/test_browser_thread.h" #include "ipc/ipc_descriptors.h" +#include "ipc/ipc_multiprocess_test.h" #include "ipc/ipc_switches.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -264,7 +265,7 @@ void SetServiceEnabledExpectations(MockServiceIPCServer* server) { server->SetServiceEnabledExpectations(); } -MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { +MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { return CloudPrintMockService_Main( base::Bind(&SetServiceEnabledExpectations)); } @@ -273,7 +274,7 @@ void SetServiceWillBeDisabledExpectations(MockServiceIPCServer* server) { server->SetWillBeDisabledExpectations(); } -MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { +MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { return CloudPrintMockService_Main( base::Bind(&SetServiceWillBeDisabledExpectations)); } diff --git a/chrome/nacl/nacl_helper_linux.cc b/chrome/nacl/nacl_helper_linux.cc index 4926e62..7964340 100644 --- a/chrome/nacl/nacl_helper_linux.cc +++ b/chrome/nacl/nacl_helper_linux.cc @@ -19,12 +19,14 @@ #include "base/at_exit.h" #include "base/command_line.h" #include "base/eintr_wrapper.h" +#include "base/global_descriptors_posix.h" #include "base/logging.h" #include "base/message_loop.h" #include "base/posix/unix_domain_socket.h" #include "base/rand_util.h" #include "chrome/nacl/nacl_listener.h" #include "crypto/nss_util.h" +#include "ipc/ipc_descriptors.h" #include "ipc/ipc_switches.h" namespace { @@ -39,8 +41,9 @@ void BecomeNaClLoader(const std::vector<int>& child_fds, // don't need zygote FD any more if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) != 0) LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; - // Set up browser descriptor as expected by Chrome on fd 3 - // The zygote takes care of putting the sandbox IPC channel on fd 5 + // Set up browser descriptor on fd 3 and IPC as expected by Chrome. + base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, + kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); int zfd = dup2(child_fds[kNaClBrowserFDIndex], kNaClBrowserDescriptor); if (zfd != kNaClBrowserDescriptor) { LOG(ERROR) << "Could not initialize kNaClBrowserDescriptor"; diff --git a/ipc/ipc.gyp b/ipc/ipc.gyp index 5ed8b66..5f1762e 100644 --- a/ipc/ipc.gyp +++ b/ipc/ipc.gyp @@ -33,6 +33,7 @@ 'type': '<(gtest_target_type)', 'dependencies': [ 'ipc', + 'test_support_ipc', '../base/base.gyp:base', '../base/base.gyp:base_i18n', '../base/base.gyp:test_support_base', @@ -83,8 +84,11 @@ 'dependencies': [ 'ipc', '../base/base.gyp:base', + '../testing/gtest.gyp:gtest', ], 'sources': [ + 'ipc_multiprocess_test.cc', + 'ipc_multiprocess_test.h', 'ipc_test_sink.cc', 'ipc_test_sink.h', ], diff --git a/ipc/ipc_fuzzing_tests.cc b/ipc/ipc_fuzzing_tests.cc index 32fe71c..34394bf 100644 --- a/ipc/ipc_fuzzing_tests.cc +++ b/ipc/ipc_fuzzing_tests.cc @@ -11,6 +11,7 @@ #include "base/threading/platform_thread.h" #include "ipc/ipc_channel.h" #include "ipc/ipc_channel_proxy.h" +#include "ipc/ipc_multiprocess_test.h" #include "ipc/ipc_tests.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/multiprocess_func_list.h" @@ -246,7 +247,7 @@ class FuzzerClientListener : public SimpleListener { // Runs the fuzzing server child mode. Returns when the preset number // of messages have been received. -MULTIPROCESS_TEST_MAIN(RunFuzzServer) { +MULTIPROCESS_IPC_TEST_MAIN(RunFuzzServer) { MessageLoopForIO main_message_loop; FuzzerServerListener listener; IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_CLIENT, &listener); diff --git a/ipc/ipc_multiprocess_test.cc b/ipc/ipc_multiprocess_test.cc new file mode 100644 index 0000000..d3c46df --- /dev/null +++ b/ipc/ipc_multiprocess_test.cc @@ -0,0 +1,19 @@ +// Copyright (c) 2012 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 "build/build_config.h" + +#include "ipc/ipc_multiprocess_test.h" + +#if defined(OS_POSIX) +#include "base/global_descriptors_posix.h" +#include "ipc/ipc_descriptors.h" +#endif + +void MultiProcessTestIPCSetUp() { +#if defined(OS_POSIX) + base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, + kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); +#endif +} diff --git a/ipc/ipc_multiprocess_test.h b/ipc/ipc_multiprocess_test.h new file mode 100644 index 0000000..5364eeb --- /dev/null +++ b/ipc/ipc_multiprocess_test.h @@ -0,0 +1,23 @@ +// Copyright (c) 2012 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. + +#ifndef IPC_IPC_MULTIPROCESS_TEST_H_ +#define IPC_IPC_MULTIPROCESS_TEST_H_ +#pragma once + +#include "base/test/multiprocess_test.h" + +// Use this macro when your sub-process is using an IPCChannel to communicate +// with the test process. +// See comment below for MultiProcessTestIPCSetUp() on why this is needed. +#define MULTIPROCESS_IPC_TEST_MAIN(test_main) \ + MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, MultiProcessTestIPCSetUp) + +// Setup function used by MULTIPROCESS_IPC_TEST_MAIN. +// Registers the IPC channel as a global descriptor in the child process. This +// is needed on POSIX as the IPCChannel when created looks for a specific global +// descriptor to establish the connection to the parent process. +void MultiProcessTestIPCSetUp(); + +#endif // IPC_IPC_MULTIPROCESS_TEST_H_ diff --git a/ipc/ipc_send_fds_test.cc b/ipc/ipc_send_fds_test.cc index 124a3dc..0f587c9a 100644 --- a/ipc/ipc_send_fds_test.cc +++ b/ipc/ipc_send_fds_test.cc @@ -18,6 +18,7 @@ extern "C" { #include "base/message_loop.h" #include "ipc/ipc_channel.h" #include "ipc/ipc_message_utils.h" +#include "ipc/ipc_multiprocess_test.h" #include "testing/multiprocess_func_list.h" #if defined(OS_POSIX) @@ -132,7 +133,7 @@ int TestDescriptorClient(ino_t expected_inode_num) { // --------------------------------------------------------------------------- #if defined(OS_MACOSX) // TODO(port): Make this test cross-platform. -MULTIPROCESS_TEST_MAIN(RunTestDescriptorClientSandboxed) { +MULTIPROCESS_IPC_TEST_MAIN(RunTestDescriptorClientSandboxed) { struct stat st; const int fd = open(kDevZeroPath, O_RDONLY); fstat(fd, &st); @@ -177,7 +178,7 @@ TEST_F(IPCChannelTest, DescriptorTestSandboxed) { } #endif // defined(OS_MACOSX) -MULTIPROCESS_TEST_MAIN(RunTestDescriptorClient) { +MULTIPROCESS_IPC_TEST_MAIN(RunTestDescriptorClient) { struct stat st; const int fd = open(kDevZeroPath, O_RDONLY); fstat(fd, &st); diff --git a/ipc/ipc_tests.cc b/ipc/ipc_tests.cc index 763a592..fc2de57 100644 --- a/ipc/ipc_tests.cc +++ b/ipc/ipc_tests.cc @@ -28,6 +28,7 @@ #include "ipc/ipc_channel.h" #include "ipc/ipc_channel_proxy.h" #include "ipc/ipc_message_utils.h" +#include "ipc/ipc_multiprocess_test.h" #include "ipc/ipc_sender.h" #include "ipc/ipc_switches.h" #include "testing/multiprocess_func_list.h" @@ -402,7 +403,7 @@ TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) { base::CloseProcessHandle(process_handle); } -MULTIPROCESS_TEST_MAIN(RunTestClient) { +MULTIPROCESS_IPC_TEST_MAIN(RunTestClient) { MessageLoopForIO main_message_loop; MyChannelListener channel_listener; @@ -572,7 +573,7 @@ TEST_F(IPCChannelTest, Performance) { } // This message loop bounces all messages back to the sender -MULTIPROCESS_TEST_MAIN(RunReflector) { +MULTIPROCESS_IPC_TEST_MAIN(RunReflector) { MessageLoopForIO main_message_loop; IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL); ChannelReflectorListener channel_reflector_listener(&chan); diff --git a/ipc/sync_socket_unittest.cc b/ipc/sync_socket_unittest.cc index 6aa3330..c8c8187 100644 --- a/ipc/sync_socket_unittest.cc +++ b/ipc/sync_socket_unittest.cc @@ -13,6 +13,7 @@ #include "base/process_util.h" #include "base/threading/thread.h" #include "ipc/ipc_channel_proxy.h" +#include "ipc/ipc_multiprocess_test.h" #include "ipc/ipc_tests.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/multiprocess_func_list.h" @@ -108,7 +109,7 @@ class SyncSocketServerListener : public IPC::Channel::Listener { // Runs the fuzzing server child mode. Returns when the preset number // of messages have been received. -MULTIPROCESS_TEST_MAIN(RunSyncSocketServer) { +MULTIPROCESS_IPC_TEST_MAIN(RunSyncSocketServer) { MessageLoopForIO main_message_loop; SyncSocketServerListener listener; IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_CLIENT, &listener); diff --git a/testing/multiprocess_func_list.cc b/testing/multiprocess_func_list.cc index 1e19704..49ae07d 100644 --- a/testing/multiprocess_func_list.cc +++ b/testing/multiprocess_func_list.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -12,29 +12,43 @@ namespace multi_process_function_list { namespace { -typedef std::map<std::string, ChildFunctionPtr> MultiProcessTestMap; +struct ProcessFunctions { + ProcessFunctions() : main(NULL), setup(NULL) {} + ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup) + : main(main), + setup(setup) { + } + TestMainFunctionPtr main; + SetupFunctionPtr setup; +}; + +typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap; // Retrieve a reference to the global 'func name' -> func ptr map. -MultiProcessTestMap &GetMultiprocessFuncMap() { +MultiProcessTestMap& GetMultiprocessFuncMap() { static MultiProcessTestMap test_name_to_func_ptr_map; return test_name_to_func_ptr_map; } } // namespace -AppendMultiProcessTest::AppendMultiProcessTest(std::string test_name, - ChildFunctionPtr func_ptr) { - GetMultiprocessFuncMap()[test_name] = func_ptr; +AppendMultiProcessTest::AppendMultiProcessTest( + std::string test_name, + TestMainFunctionPtr main_func_ptr, + SetupFunctionPtr setup_func_ptr) { + GetMultiprocessFuncMap()[test_name] = + ProcessFunctions(main_func_ptr, setup_func_ptr); } int InvokeChildProcessTest(std::string test_name) { - MultiProcessTestMap &func_lookup_table = GetMultiprocessFuncMap(); + MultiProcessTestMap& func_lookup_table = GetMultiprocessFuncMap(); MultiProcessTestMap::iterator it = func_lookup_table.find(test_name); if (it != func_lookup_table.end()) { - ChildFunctionPtr func = it->second; - if (func) { - return (*func)(); - } + const ProcessFunctions& process_functions = it->second; + if (process_functions.setup) + (*process_functions.setup)(); + if (process_functions.main) + return (*process_functions.main)(); } return -1; diff --git a/testing/multiprocess_func_list.h b/testing/multiprocess_func_list.h index 6ba8495..f806d53 100644 --- a/testing/multiprocess_func_list.h +++ b/testing/multiprocess_func_list.h @@ -1,4 +1,4 @@ -// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -29,13 +29,20 @@ namespace multi_process_function_list { // Type for child process main functions. -typedef int (*ChildFunctionPtr)(); +typedef int (*TestMainFunctionPtr)(); + +// Type for child setup functions. +typedef void (*SetupFunctionPtr)(); // Helper class to append a test function to the global mapping. // Used by the MULTIPROCESS_TEST_MAIN macro. class AppendMultiProcessTest { public: - AppendMultiProcessTest(std::string test_name, ChildFunctionPtr func_ptr); + // |main_func_ptr| is the main function that is run in the child process. + // |setup_func_ptr| is a function run when the global mapping is added. + AppendMultiProcessTest(std::string test_name, + TestMainFunctionPtr main_func_ptr, + SetupFunctionPtr setup_func_ptr); }; // Invoke the main function of a test previously registered with @@ -45,10 +52,16 @@ int InvokeChildProcessTest(std::string test_name); // This macro creates a global MultiProcessTest::AppendMultiProcessTest object // whose constructor does the work of adding the global mapping. #define MULTIPROCESS_TEST_MAIN(test_main) \ + MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, NULL) + +// Same as above but lets callers specify a setup method that is run in the +// child process, just before the main function is run. This facilitates +// adding a generic one-time setup function for multiple tests. +#define MULTIPROCESS_TEST_MAIN_WITH_SETUP(test_main, test_setup) \ int test_main(); \ namespace { \ multi_process_function_list::AppendMultiProcessTest \ - AddMultiProcessTest##_##test_main(#test_main, (test_main)); \ + AddMultiProcessTest##_##test_main(#test_main, (test_main), (test_setup)); \ } \ int test_main() |