Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/vpn_service.h" |
| 6 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 7 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include "shill/error.h" |
| 11 | #include "shill/nice_mock_control.h" |
| 12 | #include "shill/mock_adaptors.h" |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 13 | #include "shill/mock_connection.h" |
| 14 | #include "shill/mock_device_info.h" |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 15 | #include "shill/mock_manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 16 | #include "shill/mock_metrics.h" |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 17 | #include "shill/mock_sockets.h" |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 18 | #include "shill/mock_store.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 19 | #include "shill/mock_vpn_driver.h" |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 20 | #include "shill/mock_vpn_provider.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 21 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 22 | using std::string; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 23 | using testing::_; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 24 | using testing::NiceMock; |
| 25 | using testing::Return; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 26 | using testing::ReturnRef; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 27 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 28 | namespace shill { |
| 29 | |
| 30 | class VPNServiceTest : public testing::Test { |
| 31 | public: |
| 32 | VPNServiceTest() |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 33 | : interface_name_("test-interface"), |
| 34 | driver_(new MockVPNDriver()), |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 35 | manager_(&control_, NULL, NULL, NULL), |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 36 | device_info_(&control_, NULL, NULL, NULL), |
| 37 | connection_(new NiceMock<MockConnection>(&device_info_)), |
| 38 | sockets_(new MockSockets()), |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 39 | service_(new VPNService(&control_, NULL, &metrics_, &manager_, |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 40 | driver_)) { |
| 41 | service_->sockets_.reset(sockets_); // Passes ownership. |
| 42 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 43 | |
| 44 | virtual ~VPNServiceTest() {} |
| 45 | |
| 46 | protected: |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 47 | virtual void SetUp() { |
| 48 | ON_CALL(*connection_, interface_name()) |
| 49 | .WillByDefault(ReturnRef(interface_name_)); |
| 50 | } |
| 51 | |
| 52 | virtual void TearDown() { |
| 53 | EXPECT_CALL(device_info_, FlushAddresses(0)); |
| 54 | } |
| 55 | |
| 56 | void SetServiceState(Service::ConnectState state) { |
| 57 | service_->state_ = state; |
| 58 | } |
| 59 | |
| 60 | std::string interface_name_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 61 | MockVPNDriver *driver_; // Owned by |service_|. |
| 62 | NiceMockControl control_; |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 63 | MockManager manager_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 64 | MockMetrics metrics_; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 65 | MockDeviceInfo device_info_; |
| 66 | scoped_refptr<NiceMock<MockConnection> > connection_; |
| 67 | MockSockets *sockets_; // Owned by |service_|. |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 68 | VPNServiceRefPtr service_; |
| 69 | }; |
| 70 | |
| 71 | TEST_F(VPNServiceTest, Connect) { |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 72 | EXPECT_TRUE(service_->connectable()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 73 | Error error; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 74 | EXPECT_CALL(*driver_, Connect(_, &error)); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 75 | service_->Connect(&error); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 76 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 79 | TEST_F(VPNServiceTest, ConnectAlreadyConnected) { |
| 80 | Error error; |
| 81 | EXPECT_CALL(*driver_, Connect(_, _)).Times(0); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 82 | SetServiceState(Service::kStateOnline); |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 83 | service_->Connect(&error); |
| 84 | EXPECT_EQ(Error::kAlreadyConnected, error.type()); |
| 85 | error.Reset(); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 86 | SetServiceState(Service::kStateConfiguring); |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 87 | service_->Connect(&error); |
| 88 | EXPECT_EQ(Error::kAlreadyConnected, error.type()); |
| 89 | } |
| 90 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 91 | TEST_F(VPNServiceTest, Disconnect) { |
| 92 | Error error; |
| 93 | EXPECT_CALL(*driver_, Disconnect()); |
| 94 | service_->Disconnect(&error); |
| 95 | EXPECT_TRUE(error.IsSuccess()); |
| 96 | } |
| 97 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 98 | TEST_F(VPNServiceTest, CreateStorageIdentifierNoHost) { |
| 99 | KeyValueStore args; |
| 100 | Error error; |
| 101 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
| 102 | EXPECT_EQ("", VPNService::CreateStorageIdentifier(args, &error)); |
| 103 | EXPECT_EQ(Error::kInvalidProperty, error.type()); |
| 104 | } |
| 105 | |
| 106 | TEST_F(VPNServiceTest, CreateStorageIdentifierNoName) { |
| 107 | KeyValueStore args; |
| 108 | Error error; |
| 109 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 110 | EXPECT_EQ("", VPNService::CreateStorageIdentifier(args, &error)); |
| 111 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 112 | } |
| 113 | |
| 114 | TEST_F(VPNServiceTest, CreateStorageIdentifier) { |
| 115 | KeyValueStore args; |
| 116 | Error error; |
| 117 | args.SetString(flimflam::kProviderNameProperty, "vpn-name"); |
| 118 | args.SetString(flimflam::kProviderHostProperty, "10.8.0.1"); |
| 119 | EXPECT_EQ("vpn_10_8_0_1_vpn_name", |
| 120 | VPNService::CreateStorageIdentifier(args, &error)); |
| 121 | EXPECT_TRUE(error.IsSuccess()); |
| 122 | } |
| 123 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 124 | TEST_F(VPNServiceTest, GetStorageIdentifier) { |
| 125 | EXPECT_EQ("", service_->GetStorageIdentifier()); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 126 | service_->set_storage_id("foo"); |
| 127 | EXPECT_EQ("foo", service_->GetStorageIdentifier()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(VPNServiceTest, GetDeviceRpcId) { |
| 131 | Error error; |
| 132 | EXPECT_EQ("/", service_->GetDeviceRpcId(&error)); |
| 133 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 134 | } |
| 135 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 136 | TEST_F(VPNServiceTest, Load) { |
| 137 | NiceMock<MockStore> storage; |
| 138 | static const char kStorageID[] = "storage-id"; |
| 139 | service_->set_storage_id(kStorageID); |
| 140 | EXPECT_CALL(storage, ContainsGroup(kStorageID)).WillOnce(Return(true)); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 141 | EXPECT_CALL(*driver_, Load(&storage, kStorageID)) |
| 142 | .WillOnce(Return(true)); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 143 | EXPECT_TRUE(service_->Load(&storage)); |
| 144 | } |
| 145 | |
| 146 | TEST_F(VPNServiceTest, Save) { |
| 147 | NiceMock<MockStore> storage; |
| 148 | static const char kStorageID[] = "storage-id"; |
| 149 | service_->set_storage_id(kStorageID); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 150 | EXPECT_CALL(*driver_, Save(&storage, kStorageID, false)) |
| 151 | .WillOnce(Return(true)); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 152 | EXPECT_TRUE(service_->Save(&storage)); |
| 153 | } |
| 154 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 155 | TEST_F(VPNServiceTest, SaveCredentials) { |
| 156 | NiceMock<MockStore> storage; |
| 157 | static const char kStorageID[] = "storage-id"; |
| 158 | service_->set_storage_id(kStorageID); |
| 159 | service_->set_save_credentials(true); |
| 160 | EXPECT_CALL(*driver_, Save(&storage, kStorageID, true)) |
| 161 | .WillOnce(Return(true)); |
| 162 | EXPECT_TRUE(service_->Save(&storage)); |
| 163 | } |
| 164 | |
| 165 | TEST_F(VPNServiceTest, Unload) { |
| 166 | service_->set_auto_connect(true); |
| 167 | service_->set_save_credentials(true); |
| 168 | EXPECT_CALL(*driver_, Disconnect()); |
| 169 | EXPECT_CALL(*driver_, UnloadCredentials()); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 170 | MockVPNProvider provider; |
| 171 | EXPECT_CALL(manager_, vpn_provider()).WillRepeatedly(Return(&provider)); |
| 172 | provider.services_.push_back(service_); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 173 | service_->Unload(); |
| 174 | EXPECT_FALSE(service_->auto_connect()); |
| 175 | EXPECT_FALSE(service_->save_credentials()); |
Paul Stewart | 8c116a9 | 2012-05-02 18:30:03 -0700 | [diff] [blame] | 176 | EXPECT_TRUE(provider.services_.empty()); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 179 | TEST_F(VPNServiceTest, InitPropertyStore) { |
| 180 | EXPECT_CALL(*driver_, InitPropertyStore(service_->mutable_store())); |
| 181 | service_->InitDriverPropertyStore(); |
| 182 | } |
| 183 | |
Darin Petkov | 1d0080a | 2012-04-30 17:10:36 +0200 | [diff] [blame] | 184 | TEST_F(VPNServiceTest, MakeFavorite) { |
| 185 | EXPECT_FALSE(service_->favorite()); |
| 186 | EXPECT_FALSE(service_->auto_connect()); |
| 187 | service_->MakeFavorite(); |
| 188 | EXPECT_TRUE(service_->favorite()); |
| 189 | EXPECT_FALSE(service_->auto_connect()); |
| 190 | } |
| 191 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 192 | TEST_F(VPNServiceTest, SetConnection) { |
| 193 | EXPECT_FALSE(service_->connection_binder_.get()); |
| 194 | EXPECT_FALSE(service_->connection()); |
| 195 | EXPECT_CALL(*sockets_, Socket(_, _, _)).WillOnce(Return(-1)); |
| 196 | service_->SetConnection(connection_); |
| 197 | ASSERT_TRUE(service_->connection_binder_.get()); |
| 198 | EXPECT_EQ(connection_.get(), |
| 199 | service_->connection_binder_->connection().get()); |
| 200 | EXPECT_EQ(connection_.get(), service_->connection().get()); |
| 201 | EXPECT_CALL(*driver_, OnConnectionDisconnected()).Times(0); |
| 202 | } |
| 203 | |
| 204 | TEST_F(VPNServiceTest, OnConnectionDisconnected) { |
| 205 | EXPECT_CALL(*sockets_, Socket(_, _, _)).WillOnce(Return(-1)); |
| 206 | service_->SetConnection(connection_); |
| 207 | EXPECT_CALL(*driver_, OnConnectionDisconnected()).Times(1); |
| 208 | connection_->OnLowerDisconnect(); |
| 209 | } |
| 210 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 211 | } // namespace shill |