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