Peter Qiu | 376e404 | 2014-11-13 09:40:28 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 "apmanager/service.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/strings/string_util.h> |
| 10 | #include <base/strings/stringprintf.h> |
| 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | #include <chromeos/process_mock.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | #include "apmanager/mock_config.h" |
| 17 | |
| 18 | using chromeos::ProcessMock; |
| 19 | using ::testing::_; |
| 20 | using ::testing::Mock; |
| 21 | using ::testing::Return; |
| 22 | using ::testing::SetArgPointee; |
| 23 | |
| 24 | namespace { |
| 25 | const int kServiceIdentifier = 1; |
| 26 | const char kHostapdConfig[] = "ssid=test\n"; |
| 27 | } // namespace |
| 28 | |
| 29 | namespace apmanager { |
| 30 | |
| 31 | class ServiceTest : public testing::Test { |
| 32 | public: |
| 33 | ServiceTest() : service_(kServiceIdentifier) {} |
| 34 | |
| 35 | void StartDummyProcess() { |
| 36 | service_.hostapd_process_.reset(new chromeos::ProcessImpl); |
| 37 | service_.hostapd_process_->AddArg("sleep"); |
| 38 | service_.hostapd_process_->AddArg("12345"); |
| 39 | CHECK(service_.hostapd_process_->Start()); |
| 40 | LOG(INFO) << "DummyProcess: " << service_.hostapd_process_->pid(); |
| 41 | } |
| 42 | |
| 43 | void SetConfig(Config* config) { |
| 44 | service_.config_.reset(config); |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | Service service_; |
| 49 | }; |
| 50 | |
| 51 | MATCHER_P(IsServiceErrorStartingWith, message, "") { |
| 52 | return arg != nullptr && |
| 53 | arg->GetDomain() == chromeos::errors::dbus::kDomain && |
| 54 | arg->GetCode() == kServiceError && |
| 55 | EndsWith(arg->GetMessage(), message, false); |
| 56 | } |
| 57 | |
| 58 | TEST_F(ServiceTest, StartWhenServiceAlreadyRunning) { |
| 59 | StartDummyProcess(); |
| 60 | |
| 61 | chromeos::ErrorPtr error; |
| 62 | EXPECT_FALSE(service_.Start(&error)); |
| 63 | EXPECT_THAT(error, IsServiceErrorStartingWith("Service already running")); |
| 64 | } |
| 65 | |
| 66 | TEST_F(ServiceTest, StartWhenConfigFileFailed) { |
| 67 | MockConfig* config = new MockConfig(); |
| 68 | SetConfig(config); |
| 69 | |
| 70 | chromeos::ErrorPtr error; |
| 71 | EXPECT_CALL(*config, GenerateConfigFile(_, _)).WillOnce(Return(false)); |
| 72 | EXPECT_FALSE(service_.Start(&error)); |
| 73 | EXPECT_THAT(error, IsServiceErrorStartingWith( |
| 74 | "Failed to generate config file")); |
| 75 | } |
| 76 | |
| 77 | TEST_F(ServiceTest, StartSuccess) { |
| 78 | MockConfig* config = new MockConfig(); |
| 79 | SetConfig(config); |
| 80 | |
| 81 | std::string config_str(kHostapdConfig); |
| 82 | chromeos::ErrorPtr error; |
| 83 | EXPECT_CALL(*config, GenerateConfigFile(_, _)).WillOnce( |
| 84 | DoAll(SetArgPointee<1>(config_str), Return(true))); |
| 85 | EXPECT_TRUE(service_.Start(&error)); |
| 86 | EXPECT_EQ(nullptr, error); |
| 87 | } |
| 88 | |
| 89 | TEST_F(ServiceTest, StopWhenServiceNotRunning) { |
| 90 | chromeos::ErrorPtr error; |
| 91 | EXPECT_FALSE(service_.Stop(&error)); |
| 92 | EXPECT_THAT(error, IsServiceErrorStartingWith( |
| 93 | "Service is not currently running")); |
| 94 | } |
| 95 | |
| 96 | TEST_F(ServiceTest, StopSuccess) { |
| 97 | StartDummyProcess(); |
| 98 | |
| 99 | chromeos::ErrorPtr error; |
| 100 | EXPECT_TRUE(service_.Stop(&error)); |
| 101 | } |
| 102 | |
| 103 | } // namespace apmanager |