Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1023)

Unified Diff: service_manager_test.cc

Issue 6508016: vpn-manager: Add l2tp/ipsec vpn manager (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vpn-manager.git@master
Patch Set: respond to petkov Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« service_manager.cc ('K') | « service_manager.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service_manager_test.cc
diff --git a/service_manager_test.cc b/service_manager_test.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fbf4c9f1f2615e8c4d87f227c7078bff5b3918f1
--- /dev/null
+++ b/service_manager_test.cc
@@ -0,0 +1,121 @@
+// 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"
+
+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());
+ MOCK_METHOD0(Poll, int());
+ MOCK_METHOD0(ProcessOutput, void());
+ MOCK_METHOD1(IsChild, bool(pid_t pid));
+};
+
+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_ = new FilePath(temp_path_);
+ ServiceManager::SetLayerOrder(&outer_service_, &inner_service_);
+ chromeos::ClearLog();
+ }
+ void TearDown() {
+ // Removes and deletes temp path.
+ ServiceManager::DeleteTemp();
+ }
+ protected:
+ FilePath temp_path_;
+ FilePath test_path_;
+ MockService outer_service_;
+ MockService inner_service_;
+ MockService single_service_;
+};
+
+TEST_F(ServiceManagerTest, InitializeDirectories) {
+ EXPECT_FALSE(file_util::DirectoryExists(temp_path_));
+ ServiceManager::InitializeDirectories();
+ 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) {
+ EXPECT_FALSE(single_service_.is_running());
+ EXPECT_FALSE(single_service_.was_stopped());
+ single_service_.OnStarted();
+ EXPECT_TRUE(single_service_.is_running());
+ EXPECT_FALSE(single_service_.was_stopped());
+}
+
+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();
+}
« service_manager.cc ('K') | « service_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698