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_provider.h" |
| 6 | |
| 7 | #include <chromeos/dbus/service_constants.h> |
| 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 | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 13 | #include "shill/mock_device_info.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 14 | #include "shill/mock_manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 15 | #include "shill/mock_metrics.h" |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 16 | #include "shill/mock_profile.h" |
| 17 | #include "shill/mock_store.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 18 | #include "shill/mock_vpn_driver.h" |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 19 | #include "shill/mock_vpn_service.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 20 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 21 | using std::string; |
| 22 | using testing::_; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 23 | using testing::DoAll; |
| 24 | using testing::NiceMock; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 25 | using testing::Return; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 26 | using testing::SetArgumentPointee; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 27 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 28 | namespace shill { |
| 29 | |
| 30 | class VPNProviderTest : public testing::Test { |
| 31 | public: |
| 32 | VPNProviderTest() |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 33 | : manager_(&control_, NULL, &metrics_, NULL), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 34 | device_info_(&control_, NULL, &metrics_, &manager_), |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 35 | provider_(&control_, NULL, &metrics_, &manager_) {} |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 36 | |
| 37 | virtual ~VPNProviderTest() {} |
| 38 | |
| 39 | protected: |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 40 | static const char kHost[]; |
| 41 | static const char kName[]; |
| 42 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 43 | string GetServiceFriendlyName(const ServiceRefPtr &service) { |
| 44 | return service->friendly_name(); |
| 45 | } |
| 46 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 47 | NiceMockControl control_; |
| 48 | MockMetrics metrics_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 49 | MockManager manager_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 50 | MockDeviceInfo device_info_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 51 | VPNProvider provider_; |
| 52 | }; |
| 53 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 54 | const char VPNProviderTest::kHost[] = "10.8.0.1"; |
| 55 | const char VPNProviderTest::kName[] = "vpn-name"; |
| 56 | |
| 57 | TEST_F(VPNProviderTest, GetServiceNoType) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 58 | KeyValueStore args; |
| 59 | Error e; |
| 60 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 61 | VPNServiceRefPtr service = provider_.GetService(args, &e); |
| 62 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 63 | EXPECT_FALSE(service); |
| 64 | } |
| 65 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 66 | TEST_F(VPNProviderTest, GetServiceUnsupportedType) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 67 | KeyValueStore args; |
| 68 | Error e; |
| 69 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 70 | args.SetString(flimflam::kProviderTypeProperty, "unknown-vpn-type"); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 71 | args.SetString(flimflam::kProviderHostProperty, kHost); |
| 72 | args.SetString(flimflam::kProviderNameProperty, kName); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 73 | VPNServiceRefPtr service = provider_.GetService(args, &e); |
| 74 | EXPECT_EQ(Error::kNotSupported, e.type()); |
| 75 | EXPECT_FALSE(service); |
| 76 | } |
| 77 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 78 | TEST_F(VPNProviderTest, GetService) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 79 | KeyValueStore args; |
| 80 | Error e; |
| 81 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeVPN); |
| 82 | args.SetString(flimflam::kProviderTypeProperty, flimflam::kProviderOpenVpn); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 83 | args.SetString(flimflam::kProviderHostProperty, kHost); |
| 84 | args.SetString(flimflam::kProviderNameProperty, kName); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 85 | EXPECT_CALL(manager_, device_info()).WillOnce(Return(&device_info_)); |
| 86 | EXPECT_CALL(manager_, RegisterService(_)); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 87 | VPNServiceRefPtr service0 = provider_.GetService(args, &e); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 88 | EXPECT_TRUE(e.IsSuccess()); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 89 | ASSERT_TRUE(service0); |
| 90 | EXPECT_EQ("vpn_10_8_0_1_vpn_name", service0->GetStorageIdentifier()); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 91 | EXPECT_EQ(kName, GetServiceFriendlyName(service0)); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 92 | |
| 93 | // A second call should return the same service. |
| 94 | VPNServiceRefPtr service1 = provider_.GetService(args, &e); |
| 95 | EXPECT_TRUE(e.IsSuccess()); |
| 96 | ASSERT_EQ(service0.get(), service1.get()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 99 | TEST_F(VPNProviderTest, OnDeviceInfoAvailable) { |
| 100 | const string kInterfaceName("tun0"); |
| 101 | const int kInterfaceIndex = 1; |
| 102 | |
| 103 | scoped_ptr<MockVPNDriver> bad_driver(new MockVPNDriver()); |
| 104 | EXPECT_CALL(*bad_driver.get(), ClaimInterface(_, _)) |
| 105 | .Times(2) |
| 106 | .WillRepeatedly(Return(false)); |
| 107 | provider_.services_.push_back( |
| 108 | new VPNService(&control_, NULL, &metrics_, NULL, bad_driver.release())); |
| 109 | |
| 110 | EXPECT_FALSE(provider_.OnDeviceInfoAvailable(kInterfaceName, |
| 111 | kInterfaceIndex)); |
| 112 | |
| 113 | scoped_ptr<MockVPNDriver> good_driver(new MockVPNDriver()); |
| 114 | EXPECT_CALL(*good_driver.get(), ClaimInterface(_, _)) |
| 115 | .WillOnce(Return(true)); |
| 116 | provider_.services_.push_back( |
| 117 | new VPNService(&control_, NULL, &metrics_, NULL, good_driver.release())); |
| 118 | |
| 119 | scoped_ptr<MockVPNDriver> dup_driver(new MockVPNDriver()); |
| 120 | EXPECT_CALL(*dup_driver.get(), ClaimInterface(_, _)) |
| 121 | .Times(0); |
| 122 | provider_.services_.push_back( |
| 123 | new VPNService(&control_, NULL, &metrics_, NULL, dup_driver.release())); |
| 124 | |
| 125 | EXPECT_TRUE(provider_.OnDeviceInfoAvailable(kInterfaceName, kInterfaceIndex)); |
| 126 | provider_.services_.clear(); |
| 127 | } |
| 128 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 129 | TEST_F(VPNProviderTest, RemoveService) { |
| 130 | scoped_refptr<MockVPNService> service0( |
| 131 | new MockVPNService(&control_, NULL, &metrics_, NULL, NULL)); |
| 132 | scoped_refptr<MockVPNService> service1( |
| 133 | new MockVPNService(&control_, NULL, &metrics_, NULL, NULL)); |
| 134 | scoped_refptr<MockVPNService> service2( |
| 135 | new MockVPNService(&control_, NULL, &metrics_, NULL, NULL)); |
| 136 | |
| 137 | provider_.services_.push_back(service0.get()); |
| 138 | provider_.services_.push_back(service1.get()); |
| 139 | provider_.services_.push_back(service2.get()); |
| 140 | |
| 141 | ASSERT_EQ(3, provider_.services_.size()); |
| 142 | |
| 143 | provider_.RemoveService(service1); |
| 144 | |
| 145 | EXPECT_EQ(2, provider_.services_.size()); |
| 146 | EXPECT_EQ(service0, provider_.services_[0]); |
| 147 | EXPECT_EQ(service2, provider_.services_[1]); |
| 148 | |
| 149 | provider_.RemoveService(service2); |
| 150 | |
| 151 | EXPECT_EQ(1, provider_.services_.size()); |
| 152 | EXPECT_EQ(service0, provider_.services_[0]); |
| 153 | |
| 154 | provider_.RemoveService(service0); |
| 155 | EXPECT_EQ(0, provider_.services_.size()); |
| 156 | } |
| 157 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 158 | MATCHER_P(ServiceWithStorageId, storage_id, "") { |
| 159 | return arg->GetStorageIdentifier() == storage_id; |
| 160 | } |
| 161 | |
| 162 | TEST_F(VPNProviderTest, CreateServicesFromProfile) { |
| 163 | scoped_refptr<MockProfile> profile( |
| 164 | new NiceMock<MockProfile>(&control_, &manager_, "")); |
| 165 | NiceMock<MockStore> storage; |
| 166 | EXPECT_CALL(*profile, GetConstStorage()) |
| 167 | .WillRepeatedly(Return(&storage)); |
| 168 | EXPECT_CALL(storage, GetString(_, _, _)) |
| 169 | .WillRepeatedly(Return(false)); |
| 170 | |
| 171 | std::set<string> groups; |
| 172 | const string kNonVPNIdentifier("foo_123_456"); |
| 173 | groups.insert(kNonVPNIdentifier); |
| 174 | const string kVPNIdentifier0("vpn_123_456"); |
| 175 | const string kVPNIdentifier1("vpn_789_012"); |
| 176 | groups.insert(kVPNIdentifier0); |
| 177 | groups.insert(kVPNIdentifier1); |
| 178 | EXPECT_CALL(storage, GetGroupsWithKey(flimflam::kProviderTypeProperty)) |
| 179 | .WillRepeatedly(Return(groups)); |
| 180 | |
| 181 | EXPECT_CALL(*profile, ConfigureService(ServiceWithStorageId(kVPNIdentifier1))) |
| 182 | .WillOnce(Return(true)); |
| 183 | |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 184 | const string kName("name"); |
| 185 | EXPECT_CALL(storage, GetString(_, flimflam::kNameProperty, _)) |
| 186 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kName), Return(true))); |
| 187 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 188 | const string kProviderTypeUnknown("unknown"); |
| 189 | EXPECT_CALL(storage, GetString(kVPNIdentifier0, |
| 190 | flimflam::kProviderTypeProperty, |
| 191 | _)) |
| 192 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kProviderTypeUnknown), |
| 193 | Return(true))); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 194 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 195 | const string kProviderTypeOpenVPN(flimflam::kProviderOpenVpn); |
| 196 | EXPECT_CALL(storage, GetString(kVPNIdentifier1, |
| 197 | flimflam::kProviderTypeProperty, |
| 198 | _)) |
| 199 | .WillRepeatedly(DoAll(SetArgumentPointee<2>(kProviderTypeOpenVPN), |
| 200 | Return(true))); |
| 201 | |
| 202 | EXPECT_CALL(manager_, device_info()) |
| 203 | .WillRepeatedly(Return(reinterpret_cast<DeviceInfo *>(NULL))); |
| 204 | |
| 205 | EXPECT_CALL(manager_, RegisterService(ServiceWithStorageId(kVPNIdentifier1))) |
| 206 | .Times(1); |
| 207 | |
| 208 | provider_.CreateServicesFromProfile(profile); |
| 209 | |
| 210 | // Calling this again should not create any more services (checked by the |
| 211 | // Times(1) above). |
| 212 | provider_.CreateServicesFromProfile(profile); |
| 213 | } |
| 214 | |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 215 | TEST_F(VPNProviderTest, CreateService) { |
| 216 | static const char kName[] = "test-vpn-service"; |
| 217 | static const char kStorageID[] = "test_vpn_storage_id"; |
| 218 | static const char *kTypes[] = { |
| 219 | flimflam::kProviderOpenVpn, |
| 220 | flimflam::kProviderL2tpIpsec, |
| 221 | }; |
| 222 | const size_t kTypesCount = arraysize(kTypes); |
| 223 | EXPECT_CALL(manager_, device_info()) |
| 224 | .Times(kTypesCount) |
| 225 | .WillRepeatedly(Return(&device_info_)); |
| 226 | EXPECT_CALL(manager_, RegisterService(_)).Times(kTypesCount); |
| 227 | for (size_t i = 0; i < kTypesCount; i++) { |
| 228 | Error error; |
| 229 | VPNServiceRefPtr service = |
| 230 | provider_.CreateService(kTypes[i], kName, kStorageID, &error); |
| 231 | ASSERT_TRUE(service) << kTypes[i]; |
| 232 | ASSERT_TRUE(service->driver()) << kTypes[i]; |
| 233 | EXPECT_EQ(kTypes[i], service->driver()->GetProviderType()); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 234 | EXPECT_EQ(kName, GetServiceFriendlyName(service)) << kTypes[i]; |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 235 | EXPECT_EQ(kStorageID, service->GetStorageIdentifier()) << kTypes[i]; |
| 236 | EXPECT_TRUE(error.IsSuccess()) << kTypes[i]; |
| 237 | } |
| 238 | Error error; |
| 239 | VPNServiceRefPtr unknown_service = |
| 240 | provider_.CreateService("unknown-vpn-type", kName, kStorageID, &error); |
| 241 | EXPECT_FALSE(unknown_service); |
| 242 | EXPECT_EQ(Error::kNotSupported, error.type()); |
| 243 | } |
| 244 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 245 | } // namespace shill |