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" | |
petkov
2011/03/04 18:42:56
add a blank line before
| |
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 }; | |
24 | |
25 class ServiceManagerTest : public ::testing::Test { | |
26 public: | |
27 void SetUp() { | |
28 test_path_ = FilePath("test"); | |
29 file_util::Delete(test_path_, true); | |
30 file_util::CreateDirectory(test_path_); | |
31 temp_path_ = test_path_.Append("tmp").Append("service"); | |
32 ServiceManager::temp_path_ = temp_path_; | |
33 ServiceManager::SetLayerOrder(&outer_service_, &inner_service_); | |
34 chromeos::ClearLog(); | |
35 } | |
36 protected: | |
37 FilePath temp_path_; | |
38 FilePath test_path_; | |
39 MockService outer_service_; | |
40 MockService inner_service_; | |
41 }; | |
42 | |
43 TEST_F(ServiceManagerTest, Initialize) { | |
44 EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); | |
45 ServiceManager::Initialize(); | |
46 EXPECT_TRUE(file_util::DirectoryExists(temp_path_)); | |
47 ServiceManager::DeleteTemp(); | |
48 EXPECT_FALSE(file_util::DirectoryExists(temp_path_)); | |
49 } | |
50 | |
51 TEST_F(ServiceManagerTest, OnStartedInnerSucceeds) { | |
52 EXPECT_CALL(inner_service_, Start()).WillOnce(Return(true)); | |
53 EXPECT_FALSE(outer_service_.is_running()); | |
54 EXPECT_FALSE(outer_service_.was_stopped()); | |
55 outer_service_.OnStarted(); | |
56 EXPECT_TRUE(outer_service_.is_running()); | |
57 EXPECT_FALSE(outer_service_.was_stopped()); | |
58 } | |
59 | |
60 TEST_F(ServiceManagerTest, OnStartedInnerFails) { | |
61 InSequence unused; | |
62 EXPECT_CALL(inner_service_, Start()).WillOnce(Return(false)); | |
63 EXPECT_CALL(outer_service_, Stop()); | |
64 EXPECT_FALSE(outer_service_.is_running()); | |
65 outer_service_.OnStarted(); | |
66 // The outer service keeps saying it's running until its OnStop is called. | |
67 EXPECT_TRUE(outer_service_.is_running()); | |
68 EXPECT_TRUE(FindLog("Inner service mock failed")); | |
69 } | |
70 | |
71 TEST_F(ServiceManagerTest, OnStartedNoInner) { | |
72 inner_service_.OnStarted(); | |
73 } | |
74 | |
75 TEST_F(ServiceManagerTest, OnStoppedFromSuccess) { | |
76 EXPECT_CALL(outer_service_, Stop()); | |
77 inner_service_.is_running_ = true; | |
78 EXPECT_TRUE(inner_service_.is_running()); | |
79 EXPECT_FALSE(inner_service_.was_stopped()); | |
80 inner_service_.OnStopped(true); | |
81 EXPECT_FALSE(inner_service_.is_running()); | |
82 EXPECT_TRUE(inner_service_.was_stopped()); | |
83 } | |
84 | |
85 TEST_F(ServiceManagerTest, OnStoppedFromFailure) { | |
86 EXPECT_CALL(outer_service_, Stop()); | |
87 inner_service_.is_running_ = true; | |
88 EXPECT_TRUE(inner_service_.is_running()); | |
89 EXPECT_FALSE(inner_service_.was_stopped()); | |
90 inner_service_.OnStopped(false); | |
91 EXPECT_FALSE(inner_service_.is_running()); | |
92 EXPECT_TRUE(inner_service_.was_stopped()); | |
93 } | |
94 | |
95 TEST_F(ServiceManagerTest, WriteFdToSyslog) { | |
96 int mypipe[2]; | |
97 const char message[] = "good morning\npipe\n"; | |
98 ASSERT_EQ(0, pipe(mypipe)); | |
99 EXPECT_EQ(strlen(message), write(mypipe[1], message, strlen(message))); | |
100 ServiceManager::WriteFdToSyslog(mypipe[0], "prefix: "); | |
101 EXPECT_EQ("prefix: good morning\nprefix: pipe\n", GetLog()); | |
102 close(mypipe[0]); | |
103 close(mypipe[1]); | |
104 } | |
105 | |
106 int main(int argc, char** argv) { | |
107 SetUpTests(&argc, argv, true); | |
108 return RUN_ALL_TESTS(); | |
109 } | |
OLD | NEW |