OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium OS 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. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "base/file_util.h" |
| 7 #include "chromeos/syslog_logging.h" |
| 8 #include "chromeos/test_helpers.h" |
| 9 #include "gmock/gmock.h" |
| 10 #include "gtest/gtest.h" |
| 11 #include "vpn-manager/service_manager.h" |
| 12 |
| 13 using ::chromeos::FindLog; |
| 14 using ::chromeos::GetLog; |
| 15 using ::testing::InSequence; |
| 16 using ::testing::Return; |
| 17 |
| 18 class MockService : public ServiceManager { |
| 19 public: |
| 20 MockService() : ServiceManager("mock") {} |
| 21 MOCK_METHOD0(Start, bool()); |
| 22 MOCK_METHOD0(Stop, void()); |
| 23 MOCK_METHOD0(Poll, int()); |
| 24 MOCK_METHOD0(ProcessOutput, void()); |
| 25 MOCK_METHOD1(IsChild, bool(pid_t pid)); |
| 26 }; |
| 27 |
| 28 class ServiceManagerTest : public ::testing::Test { |
| 29 public: |
| 30 void SetUp() { |
| 31 test_path_ = FilePath("test"); |
| 32 file_util::Delete(test_path_, true); |
| 33 file_util::CreateDirectory(test_path_); |
| 34 temp_path_ = test_path_.Append("tmp").Append("service"); |
| 35 ServiceManager::temp_path_ = new FilePath(temp_path_); |
| 36 ServiceManager::SetLayerOrder(&outer_service_, &inner_service_); |
| 37 chromeos::ClearLog(); |
| 38 } |
| 39 void TearDown() { |
| 40 // Removes and deletes temp path. |
| 41 ServiceManager::DeleteTemp(); |
| 42 } |
| 43 protected: |
| 44 FilePath temp_path_; |
| 45 FilePath test_path_; |
| 46 MockService outer_service_; |
| 47 MockService inner_service_; |
| 48 MockService single_service_; |
| 49 }; |
| 50 |
| 51 TEST_F(ServiceManagerTest, InitializeDirectories) { |
| 52 EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); |
| 53 ServiceManager::InitializeDirectories(); |
| 54 EXPECT_TRUE(file_util::DirectoryExists(temp_path_)); |
| 55 ServiceManager::DeleteTemp(); |
| 56 EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); |
| 57 } |
| 58 |
| 59 TEST_F(ServiceManagerTest, OnStartedInnerSucceeds) { |
| 60 EXPECT_CALL(inner_service_, Start()).WillOnce(Return(true)); |
| 61 EXPECT_FALSE(outer_service_.is_running()); |
| 62 EXPECT_FALSE(outer_service_.was_stopped()); |
| 63 outer_service_.OnStarted(); |
| 64 EXPECT_TRUE(outer_service_.is_running()); |
| 65 EXPECT_FALSE(outer_service_.was_stopped()); |
| 66 } |
| 67 |
| 68 TEST_F(ServiceManagerTest, OnStartedInnerFails) { |
| 69 InSequence unused; |
| 70 EXPECT_CALL(inner_service_, Start()).WillOnce(Return(false)); |
| 71 EXPECT_CALL(outer_service_, Stop()); |
| 72 EXPECT_FALSE(outer_service_.is_running()); |
| 73 outer_service_.OnStarted(); |
| 74 // The outer service keeps saying it's running until its OnStop is called. |
| 75 EXPECT_TRUE(outer_service_.is_running()); |
| 76 EXPECT_TRUE(FindLog("Inner service mock failed")); |
| 77 } |
| 78 |
| 79 TEST_F(ServiceManagerTest, OnStartedNoInner) { |
| 80 EXPECT_FALSE(single_service_.is_running()); |
| 81 EXPECT_FALSE(single_service_.was_stopped()); |
| 82 single_service_.OnStarted(); |
| 83 EXPECT_TRUE(single_service_.is_running()); |
| 84 EXPECT_FALSE(single_service_.was_stopped()); |
| 85 } |
| 86 |
| 87 TEST_F(ServiceManagerTest, OnStoppedFromSuccess) { |
| 88 EXPECT_CALL(outer_service_, Stop()); |
| 89 inner_service_.is_running_ = true; |
| 90 EXPECT_TRUE(inner_service_.is_running()); |
| 91 EXPECT_FALSE(inner_service_.was_stopped()); |
| 92 inner_service_.OnStopped(true); |
| 93 EXPECT_FALSE(inner_service_.is_running()); |
| 94 EXPECT_TRUE(inner_service_.was_stopped()); |
| 95 } |
| 96 |
| 97 TEST_F(ServiceManagerTest, OnStoppedFromFailure) { |
| 98 EXPECT_CALL(outer_service_, Stop()); |
| 99 inner_service_.is_running_ = true; |
| 100 EXPECT_TRUE(inner_service_.is_running()); |
| 101 EXPECT_FALSE(inner_service_.was_stopped()); |
| 102 inner_service_.OnStopped(false); |
| 103 EXPECT_FALSE(inner_service_.is_running()); |
| 104 EXPECT_TRUE(inner_service_.was_stopped()); |
| 105 } |
| 106 |
| 107 TEST_F(ServiceManagerTest, WriteFdToSyslog) { |
| 108 int mypipe[2]; |
| 109 const char message[] = "good morning\npipe\n"; |
| 110 ASSERT_EQ(0, pipe(mypipe)); |
| 111 EXPECT_EQ(strlen(message), write(mypipe[1], message, strlen(message))); |
| 112 ServiceManager::WriteFdToSyslog(mypipe[0], "prefix: "); |
| 113 EXPECT_EQ("prefix: good morning\nprefix: pipe\n", GetLog()); |
| 114 close(mypipe[0]); |
| 115 close(mypipe[1]); |
| 116 } |
| 117 |
| 118 int main(int argc, char** argv) { |
| 119 SetUpTests(&argc, argv, true); |
| 120 return RUN_ALL_TESTS(); |
| 121 } |
OLD | NEW |