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