diff options
author | scottbyer@chromium.org <scottbyer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 23:22:48 +0000 |
---|---|---|
committer | scottbyer@chromium.org <scottbyer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 23:22:48 +0000 |
commit | 8e9bdb10c7ecd199837ba1afa807897f8edfa29a (patch) | |
tree | 55745f96ac822179e395e988480862765759eef1 | |
parent | e2dd00f441bdf782f0965cc40ef4f61b3f16082f (diff) | |
download | chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.zip chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.tar.gz chromium_src-8e9bdb10c7ecd199837ba1afa807897f8edfa29a.tar.bz2 |
Refactor mock launchd into it's own file for use by another test.
This is a pre-factoring to fixing up the multi-process unit tests from
http://codereview.chromium.org/8905023/ to also work on the Mac. A follow-on
changelist will enhance MockLaunchd to allow for easier re-use of
service/helper process tests across platforms.
BUG=98049
TEST=none
Review URL: http://codereview.chromium.org/8970006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117720 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/chrome_tests.gypi | 2 | ||||
-rw-r--r-- | chrome/common/mac/mock_launchd.cc | 145 | ||||
-rw-r--r-- | chrome/common/mac/mock_launchd.h | 74 | ||||
-rw-r--r-- | chrome/common/service_process_util_unittest.cc | 179 |
4 files changed, 227 insertions, 173 deletions
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 3fb7c8d..ce4452d 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -2030,6 +2030,8 @@ 'common/json_schema_validator_unittest_base.h', 'common/json_value_serializer_unittest.cc', 'common/mac/cfbundle_blocker_unittest.mm', + 'common/mac/mock_launchd.cc', + 'common/mac/mock_launchd.h', 'common/mac/objc_method_swizzle_unittest.mm', 'common/mac/objc_zombie_unittest.mm', 'common/metrics/metrics_log_manager_unittest.cc', diff --git a/chrome/common/mac/mock_launchd.cc b/chrome/common/mac/mock_launchd.cc new file mode 100644 index 0000000..f9310df --- /dev/null +++ b/chrome/common/mac/mock_launchd.cc @@ -0,0 +1,145 @@ +// 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 "chrome/common/mac/mock_launchd.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/mac/scoped_cftyperef.h" +#include "base/message_loop.h" +#include "base/stringprintf.h" +#include "base/sys_string_conversions.h" +#include "chrome/common/mac/launchd.h" +#include "testing/gtest/include/gtest/gtest.h" + +// static +bool MockLaunchd::MakeABundle(const FilePath& dst, + const std::string& name, + FilePath* bundle_root, + FilePath* executable) { + *bundle_root = dst.Append(name + std::string(".app")); + FilePath contents = bundle_root->AppendASCII("Contents"); + FilePath mac_os = contents.AppendASCII("MacOS"); + *executable = mac_os.Append(name); + FilePath info_plist = contents.Append("Info.plist"); + + if (!file_util::CreateDirectory(mac_os)) { + return false; + } + const char *data = "#! testbundle\n"; + int len = strlen(data); + if (file_util::WriteFile(*executable, data, len) != len) { + return false; + } + if (chmod(executable->value().c_str(), 0555) != 0) { + return false; + } + + const char* info_plist_format = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " + "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n" + "<dict>\n" + " <key>CFBundleDevelopmentRegion</key>\n" + " <string>English</string>\n" + " <key>CFBundleIdentifier</key>\n" + " <string>com.test.%s</string>\n" + " <key>CFBundleInfoDictionaryVersion</key>\n" + " <string>6.0</string>\n" + " <key>CFBundleExecutable</key>\n" + " <string>%s</string>\n" + " <key>CFBundleVersion</key>\n" + " <string>1</string>\n" + "</dict>\n" + "</plist>\n"; + std::string info_plist_data = base::StringPrintf(info_plist_format, + name.c_str(), + name.c_str()); + len = info_plist_data.length(); + if (file_util::WriteFile(info_plist, info_plist_data.c_str(), len) != len) { + return false; + } + const UInt8* bundle_root_path = + reinterpret_cast<const UInt8*>(bundle_root->value().c_str()); + base::mac::ScopedCFTypeRef<CFURLRef> url( + CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, + bundle_root_path, + bundle_root->value().length(), + true)); + base::mac::ScopedCFTypeRef<CFBundleRef> bundle( + CFBundleCreate(kCFAllocatorDefault, url)); + return bundle.get(); +} + +CFDictionaryRef MockLaunchd::CopyExports() { + ADD_FAILURE(); + return NULL; +} + +CFDictionaryRef MockLaunchd::CopyJobDictionary(CFStringRef label) { + ADD_FAILURE(); + return NULL; +} + +CFDictionaryRef MockLaunchd::CopyDictionaryByCheckingIn(CFErrorRef* error) { + checkin_called_ = true; + CFStringRef program = CFSTR(LAUNCH_JOBKEY_PROGRAM); + CFStringRef program_args = CFSTR(LAUNCH_JOBKEY_PROGRAMARGUMENTS); + const void *keys[] = { program, program_args }; + base::mac::ScopedCFTypeRef<CFStringRef> path( + base::SysUTF8ToCFStringRef(file_.value())); + const void *array_values[] = { path.get() }; + base::mac::ScopedCFTypeRef<CFArrayRef> args( + CFArrayCreate(kCFAllocatorDefault, + array_values, + 1, + &kCFTypeArrayCallBacks)); + const void *values[] = { path, args }; + return CFDictionaryCreate(kCFAllocatorDefault, + keys, + values, + arraysize(keys), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); +} + +bool MockLaunchd::RemoveJob(CFStringRef label, CFErrorRef* error) { + remove_called_ = true; + message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + return true; +} + +bool MockLaunchd::RestartJob(Domain domain, + Type type, + CFStringRef name, + CFStringRef session_type) { + restart_called_ = true; + message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + return true; +} + +CFMutableDictionaryRef MockLaunchd::CreatePlistFromFile( + Domain domain, + Type type, + CFStringRef name) { + base::mac::ScopedCFTypeRef<CFDictionaryRef> dict( + CopyDictionaryByCheckingIn(NULL)); + return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, dict); +} + +bool MockLaunchd::WritePlistToFile(Domain domain, + Type type, + CFStringRef name, + CFDictionaryRef dict) { + write_called_ = true; + return true; +} + +bool MockLaunchd::DeletePlist(Domain domain, + Type type, + CFStringRef name) { + delete_called_ = true; + return true; +} diff --git a/chrome/common/mac/mock_launchd.h b/chrome/common/mac/mock_launchd.h new file mode 100644 index 0000000..5d043c6 --- /dev/null +++ b/chrome/common/mac/mock_launchd.h @@ -0,0 +1,74 @@ +// 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 CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ +#define CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ +#pragma once + +#include <launch.h> + +#include "base/file_path.h" +#include "base/mac/scoped_cftyperef.h" +#include "chrome/common/mac/launchd.h" + +class MessageLoop; + +// TODO(dmaclach): Write this in terms of a real mock. +// http://crbug.com/76923 +class MockLaunchd : public Launchd { + public: + static bool MakeABundle(const FilePath& dst, + const std::string& name, + FilePath* bundle_root, + FilePath* executable); + + MockLaunchd(const FilePath& file, MessageLoop* loop) + : file_(file), + message_loop_(loop), + restart_called_(false), + remove_called_(false), + checkin_called_(false), + write_called_(false), + delete_called_(false) { + } + virtual ~MockLaunchd() { } + + virtual CFDictionaryRef CopyExports() OVERRIDE; + virtual CFDictionaryRef CopyJobDictionary(CFStringRef label) OVERRIDE; + virtual CFDictionaryRef CopyDictionaryByCheckingIn(CFErrorRef* error) + OVERRIDE; + virtual bool RemoveJob(CFStringRef label, CFErrorRef* error) OVERRIDE; + virtual bool RestartJob(Domain domain, + Type type, + CFStringRef name, + CFStringRef session_type) OVERRIDE; + virtual CFMutableDictionaryRef CreatePlistFromFile( + Domain domain, + Type type, + CFStringRef name) OVERRIDE; + virtual bool WritePlistToFile(Domain domain, + Type type, + CFStringRef name, + CFDictionaryRef dict) OVERRIDE; + virtual bool DeletePlist(Domain domain, + Type type, + CFStringRef name) OVERRIDE; + + bool restart_called() const { return restart_called_; } + bool remove_called() const { return remove_called_; } + bool checkin_called() const { return checkin_called_; } + bool write_called() const { return write_called_; } + bool delete_called() const { return delete_called_; } + + private: + FilePath file_; + MessageLoop* message_loop_; + bool restart_called_; + bool remove_called_; + bool checkin_called_; + bool write_called_; + bool delete_called_; +}; + +#endif // CHROME_COMMON_MAC_MOCK_LAUNCHD_H_ diff --git a/chrome/common/service_process_util_unittest.cc b/chrome/common/service_process_util_unittest.cc index 84db35b..eccc4ff 100644 --- a/chrome/common/service_process_util_unittest.cc +++ b/chrome/common/service_process_util_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 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. @@ -239,125 +239,16 @@ MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown) { #include <CoreFoundation/CoreFoundation.h> -#include <launch.h> -#include <sys/stat.h> - #include "base/file_path.h" #include "base/file_util.h" #include "base/mac/mac_util.h" -#include "base/mac/scoped_cftyperef.h" -#include "base/message_loop.h" #include "base/scoped_temp_dir.h" -#include "base/stringprintf.h" -#include "base/sys_string_conversions.h" #include "base/test/test_timeouts.h" #include "base/threading/thread.h" #include "chrome/common/mac/launchd.h" +#include "chrome/common/mac/mock_launchd.h" #include "testing/gtest/include/gtest/gtest.h" -// TODO(dmaclach): Write this in terms of a real mock. -// http://crbug.com/76923 -class MockLaunchd : public Launchd { - public: - MockLaunchd(const FilePath& file, MessageLoop* loop) - : file_(file), - message_loop_(loop), - restart_called_(false), - remove_called_(false), - checkin_called_(false), - write_called_(false), - delete_called_(false) { - } - virtual ~MockLaunchd() { } - - virtual CFDictionaryRef CopyExports() OVERRIDE { - ADD_FAILURE(); - return NULL; - } - - virtual CFDictionaryRef CopyJobDictionary(CFStringRef label) OVERRIDE { - ADD_FAILURE(); - return NULL; - } - - virtual CFDictionaryRef CopyDictionaryByCheckingIn(CFErrorRef* error) - OVERRIDE { - checkin_called_ = true; - CFStringRef program = CFSTR(LAUNCH_JOBKEY_PROGRAM); - CFStringRef program_args = CFSTR(LAUNCH_JOBKEY_PROGRAMARGUMENTS); - const void *keys[] = { program, program_args }; - base::mac::ScopedCFTypeRef<CFStringRef> path( - base::SysUTF8ToCFStringRef(file_.value())); - const void *array_values[] = { path.get() }; - base::mac::ScopedCFTypeRef<CFArrayRef> args( - CFArrayCreate(kCFAllocatorDefault, - array_values, - 1, - &kCFTypeArrayCallBacks)); - const void *values[] = { path, args }; - return CFDictionaryCreate(kCFAllocatorDefault, - keys, - values, - arraysize(keys), - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - } - - virtual bool RemoveJob(CFStringRef label, CFErrorRef* error) OVERRIDE { - remove_called_ = true; - message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); - return true; - } - - virtual bool RestartJob(Domain domain, - Type type, - CFStringRef name, - CFStringRef session_type) OVERRIDE { - restart_called_ = true; - message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); - return true; - } - - virtual CFMutableDictionaryRef CreatePlistFromFile( - Domain domain, - Type type, - CFStringRef name) OVERRIDE { - base::mac::ScopedCFTypeRef<CFDictionaryRef> dict( - CopyDictionaryByCheckingIn(NULL)); - return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, dict); - } - - virtual bool WritePlistToFile(Domain domain, - Type type, - CFStringRef name, - CFDictionaryRef dict) OVERRIDE { - write_called_ = true; - return true; - } - - virtual bool DeletePlist(Domain domain, - Type type, - CFStringRef name) OVERRIDE { - delete_called_ = true; - return true; - } - - bool restart_called() const { return restart_called_; } - bool remove_called() const { return remove_called_; } - bool checkin_called() const { return checkin_called_; } - bool write_called() const { return write_called_; } - bool delete_called() const { return delete_called_; } - - private: - FilePath file_; - MessageLoop* message_loop_; - bool restart_called_; - bool remove_called_; - bool checkin_called_; - bool write_called_; - bool delete_called_; -}; - class ServiceProcessStateFileManipulationTest : public ::testing::Test { protected: ServiceProcessStateFileManipulationTest() @@ -370,10 +261,10 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test { options.message_loop_type = MessageLoop::TYPE_IO; ASSERT_TRUE(io_thread_.StartWithOptions(options)); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - ASSERT_TRUE(MakeABundle(GetTempDirPath(), - "Test", - &bundle_path_, - &executable_path_)); + ASSERT_TRUE(MockLaunchd::MakeABundle(GetTempDirPath(), + "Test", + &bundle_path_, + &executable_path_)); mock_launchd_.reset(new MockLaunchd(executable_path_, &loop_)); scoped_launchd_instance_.reset( new Launchd::ScopedInstance(mock_launchd_.get())); @@ -386,64 +277,6 @@ class ServiceProcessStateFileManipulationTest : public ::testing::Test { TestTimeouts::action_max_timeout_ms()); } - bool MakeABundle(const FilePath& dst, - const std::string& name, - FilePath* bundle_root, - FilePath* executable) { - *bundle_root = dst.Append(name + std::string(".app")); - FilePath contents = bundle_root->AppendASCII("Contents"); - FilePath mac_os = contents.AppendASCII("MacOS"); - *executable = mac_os.Append(name); - FilePath info_plist = contents.Append("Info.plist"); - - if (!file_util::CreateDirectory(mac_os)) { - return false; - } - const char *data = "#! testbundle\n"; - int len = strlen(data); - if (file_util::WriteFile(*executable, data, len) != len) { - return false; - } - if (chmod(executable->value().c_str(), 0555) != 0) { - return false; - } - - const char* info_plist_format = - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" - "<plist version=\"1.0\">\n" - "<dict>\n" - " <key>CFBundleDevelopmentRegion</key>\n" - " <string>English</string>\n" - " <key>CFBundleIdentifier</key>\n" - " <string>com.test.%s</string>\n" - " <key>CFBundleInfoDictionaryVersion</key>\n" - " <string>6.0</string>\n" - " <key>CFBundleExecutable</key>\n" - " <string>%s</string>\n" - " <key>CFBundleVersion</key>\n" - " <string>1</string>\n" - "</dict>\n" - "</plist>\n"; - std::string info_plist_data = base::StringPrintf(info_plist_format, - name.c_str(), - name.c_str()); - len = info_plist_data.length(); - if (file_util::WriteFile(info_plist, info_plist_data.c_str(), len) != len) { - return false; - } - const UInt8* bundle_root_path = - reinterpret_cast<const UInt8*>(bundle_root->value().c_str()); - base::mac::ScopedCFTypeRef<CFURLRef> url( - CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, - bundle_root_path, - bundle_root->value().length(), - true)); - base::mac::ScopedCFTypeRef<CFBundleRef> bundle( - CFBundleCreate(kCFAllocatorDefault, url)); - return bundle.get(); - } - const MockLaunchd* mock_launchd() const { return mock_launchd_.get(); } const FilePath& executable_path() const { return executable_path_; } const FilePath& bundle_path() const { return bundle_path_; } |