Chromium Code Reviews| Index: service_manager_test.cc |
| diff --git a/service_manager_test.cc b/service_manager_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c597bf1c5deb14967016af0d27ce430b6c47f5e |
| --- /dev/null |
| +++ b/service_manager_test.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright (c) 2011 The Chromium OS 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 "base/command_line.h" |
| +#include "base/file_util.h" |
| +#include "chromeos/syslog_logging.h" |
| +#include "chromeos/test_helpers.h" |
| +#include "gmock/gmock.h" |
| +#include "gtest/gtest.h" |
| +#include "vpn-manager/service_manager.h" |
|
petkov
2011/03/04 18:42:56
add a blank line before
|
| + |
| +using ::chromeos::FindLog; |
| +using ::chromeos::GetLog; |
| +using ::testing::InSequence; |
| +using ::testing::Return; |
| + |
| +class MockService : public ServiceManager { |
| + public: |
| + MockService() : ServiceManager("mock") {} |
| + MOCK_METHOD0(Start, bool()); |
| + MOCK_METHOD0(Stop, void()); |
| +}; |
| + |
| +class ServiceManagerTest : public ::testing::Test { |
| + public: |
| + void SetUp() { |
| + test_path_ = FilePath("test"); |
| + file_util::Delete(test_path_, true); |
| + file_util::CreateDirectory(test_path_); |
| + temp_path_ = test_path_.Append("tmp").Append("service"); |
| + ServiceManager::temp_path_ = temp_path_; |
| + ServiceManager::SetLayerOrder(&outer_service_, &inner_service_); |
| + chromeos::ClearLog(); |
| + } |
| + protected: |
| + FilePath temp_path_; |
| + FilePath test_path_; |
| + MockService outer_service_; |
| + MockService inner_service_; |
| +}; |
| + |
| +TEST_F(ServiceManagerTest, Initialize) { |
| + EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); |
| + ServiceManager::Initialize(); |
| + EXPECT_TRUE(file_util::DirectoryExists(temp_path_)); |
| + ServiceManager::DeleteTemp(); |
| + EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, OnStartedInnerSucceeds) { |
| + EXPECT_CALL(inner_service_, Start()).WillOnce(Return(true)); |
| + EXPECT_FALSE(outer_service_.is_running()); |
| + EXPECT_FALSE(outer_service_.was_stopped()); |
| + outer_service_.OnStarted(); |
| + EXPECT_TRUE(outer_service_.is_running()); |
| + EXPECT_FALSE(outer_service_.was_stopped()); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, OnStartedInnerFails) { |
| + InSequence unused; |
| + EXPECT_CALL(inner_service_, Start()).WillOnce(Return(false)); |
| + EXPECT_CALL(outer_service_, Stop()); |
| + EXPECT_FALSE(outer_service_.is_running()); |
| + outer_service_.OnStarted(); |
| + // The outer service keeps saying it's running until its OnStop is called. |
| + EXPECT_TRUE(outer_service_.is_running()); |
| + EXPECT_TRUE(FindLog("Inner service mock failed")); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, OnStartedNoInner) { |
| + inner_service_.OnStarted(); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, OnStoppedFromSuccess) { |
| + EXPECT_CALL(outer_service_, Stop()); |
| + inner_service_.is_running_ = true; |
| + EXPECT_TRUE(inner_service_.is_running()); |
| + EXPECT_FALSE(inner_service_.was_stopped()); |
| + inner_service_.OnStopped(true); |
| + EXPECT_FALSE(inner_service_.is_running()); |
| + EXPECT_TRUE(inner_service_.was_stopped()); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, OnStoppedFromFailure) { |
| + EXPECT_CALL(outer_service_, Stop()); |
| + inner_service_.is_running_ = true; |
| + EXPECT_TRUE(inner_service_.is_running()); |
| + EXPECT_FALSE(inner_service_.was_stopped()); |
| + inner_service_.OnStopped(false); |
| + EXPECT_FALSE(inner_service_.is_running()); |
| + EXPECT_TRUE(inner_service_.was_stopped()); |
| +} |
| + |
| +TEST_F(ServiceManagerTest, WriteFdToSyslog) { |
| + int mypipe[2]; |
| + const char message[] = "good morning\npipe\n"; |
| + ASSERT_EQ(0, pipe(mypipe)); |
| + EXPECT_EQ(strlen(message), write(mypipe[1], message, strlen(message))); |
| + ServiceManager::WriteFdToSyslog(mypipe[0], "prefix: "); |
| + EXPECT_EQ("prefix: good morning\nprefix: pipe\n", GetLog()); |
| + close(mypipe[0]); |
| + close(mypipe[1]); |
| +} |
| + |
| +int main(int argc, char** argv) { |
| + SetUpTests(&argc, argv, true); |
| + return RUN_ALL_TESTS(); |
| +} |