third_party_vpn_driver_unittest.cc revision c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5
1// 2// Copyright (C) 2014 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16 17#include "shill/vpn/third_party_vpn_driver.h" 18 19#include <gtest/gtest.h> 20 21#include "shill/mock_adaptors.h" 22#include "shill/mock_device_info.h" 23#include "shill/mock_event_dispatcher.h" 24#include "shill/mock_file_io.h" 25#include "shill/mock_glib.h" 26#include "shill/mock_manager.h" 27#include "shill/mock_metrics.h" 28#include "shill/mock_service.h" 29#include "shill/mock_store.h" 30#include "shill/mock_virtual_device.h" 31#include "shill/nice_mock_control.h" 32#include "shill/vpn/mock_vpn_service.h" 33 34using testing::_; 35using testing::Mock; 36using testing::NiceMock; 37using testing::Return; 38using testing::SetArgumentPointee; 39 40namespace shill { 41 42class ThirdPartyVpnDriverTest : public testing::Test { 43 public: 44 ThirdPartyVpnDriverTest() 45 : device_info_(&control_, &dispatcher_, &metrics_, &manager_), 46 metrics_(&dispatcher_), 47 manager_(&control_, &dispatcher_, &metrics_, &glib_), 48 driver_(new ThirdPartyVpnDriver(&control_, &dispatcher_, &metrics_, 49 &manager_, &device_info_)), 50 adaptor_interface_(new ThirdPartyVpnMockAdaptor()), 51 service_(new MockVPNService(&control_, &dispatcher_, &metrics_, 52 &manager_, driver_)), 53 device_(new MockVirtualDevice(&control_, &dispatcher_, &metrics_, 54 &manager_, kInterfaceName, 55 kInterfaceIndex, Technology::kVPN)) {} 56 57 virtual ~ThirdPartyVpnDriverTest() {} 58 59 virtual void SetUp() { 60 driver_->adaptor_interface_.reset(adaptor_interface_); 61 driver_->file_io_ = &mock_file_io_; 62 } 63 64 virtual void TearDown() { 65 driver_->device_ = nullptr; 66 driver_->service_ = nullptr; 67 driver_->file_io_ = nullptr; 68 } 69 70 protected: 71 static const char kConfigName[]; 72 static const char kInterfaceName[]; 73 static const int kInterfaceIndex; 74 75 NiceMockControl control_; 76 NiceMock<MockDeviceInfo> device_info_; 77 MockEventDispatcher dispatcher_; 78 MockMetrics metrics_; 79 MockFileIO mock_file_io_; 80 MockGLib glib_; 81 MockManager manager_; 82 ThirdPartyVpnDriver* driver_; // Owned by |service_| 83 ThirdPartyVpnMockAdaptor* adaptor_interface_; // Owned by |driver_| 84 scoped_refptr<MockVPNService> service_; 85 scoped_refptr<MockVirtualDevice> device_; 86}; 87 88const char ThirdPartyVpnDriverTest::kConfigName[] = "default-1"; 89const char ThirdPartyVpnDriverTest::kInterfaceName[] = "tun0"; 90const int ThirdPartyVpnDriverTest::kInterfaceIndex = 123; 91 92TEST_F(ThirdPartyVpnDriverTest, ConnectAndDisconnect) { 93 const std::string interface = kInterfaceName; 94 IOHandler* io_handler = new IOHandler(); // Owned by |driver_| 95 int fd = 1; 96 97 EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)).Times(1); 98 EXPECT_CALL(device_info_, CreateTunnelInterface(_)) 99 .WillOnce(DoAll(SetArgumentPointee<0>(interface), Return(true))); 100 Error error; 101 driver_->Connect(service_, &error); 102 EXPECT_TRUE(error.IsSuccess()); 103 EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_); 104 EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); 105 106 EXPECT_CALL(device_info_, OpenTunnelInterface(interface)) 107 .WillOnce(Return(fd)); 108 EXPECT_CALL(dispatcher_, CreateInputHandler(fd, _, _)) 109 .WillOnce(Return(io_handler)); 110 EXPECT_CALL(*adaptor_interface_, EmitPlatformMessage(static_cast<uint32_t>( 111 ThirdPartyVpnDriver::kConnected))); 112 EXPECT_FALSE(driver_->ClaimInterface("eth1", kInterfaceIndex)); 113 EXPECT_TRUE(driver_->ClaimInterface(interface, kInterfaceIndex)); 114 EXPECT_EQ(driver_->active_client_, driver_); 115 EXPECT_TRUE(driver_->parameters_expected_); 116 EXPECT_EQ(driver_->io_handler_.get(), io_handler); 117 ASSERT_TRUE(driver_->device_); 118 EXPECT_EQ(kInterfaceIndex, driver_->device_->interface_index()); 119 120 EXPECT_CALL(*service_, SetState(Service::kStateIdle)).Times(1); 121 EXPECT_CALL(*adaptor_interface_, EmitPlatformMessage(static_cast<uint32_t>( 122 ThirdPartyVpnDriver::kDisconnected))); 123 EXPECT_CALL(mock_file_io_, Close(fd)); 124 driver_->Disconnect(); 125 EXPECT_EQ(driver_->io_handler_.get(), nullptr); 126} 127 128TEST_F(ThirdPartyVpnDriverTest, SendPacket) { 129 int fd = 1; 130 std::string error; 131 std::vector<uint8_t> ip_packet(5, 0); 132 driver_->SendPacket(ip_packet, &error); 133 EXPECT_EQ(error, "Unexpected call"); 134 135 error.clear(); 136 ThirdPartyVpnDriver::active_client_ = driver_; 137 driver_->SendPacket(ip_packet, &error); 138 EXPECT_EQ(error, "Device not open"); 139 140 driver_->tun_fd_ = fd; 141 error.clear(); 142 EXPECT_CALL(mock_file_io_, Write(fd, ip_packet.data(), ip_packet.size())) 143 .WillOnce(Return(ip_packet.size() - 1)); 144 EXPECT_CALL( 145 *adaptor_interface_, 146 EmitPlatformMessage(static_cast<uint32_t>(ThirdPartyVpnDriver::kError))); 147 driver_->SendPacket(ip_packet, &error); 148 EXPECT_EQ(error, "Partial write"); 149 150 error.clear(); 151 EXPECT_CALL(mock_file_io_, Write(fd, ip_packet.data(), ip_packet.size())) 152 .WillOnce(Return(ip_packet.size())); 153 driver_->SendPacket(ip_packet, &error); 154 EXPECT_TRUE(error.empty()); 155 156 driver_->tun_fd_ = -1; 157 158 EXPECT_CALL(*adaptor_interface_, EmitPlatformMessage(static_cast<uint32_t>( 159 ThirdPartyVpnDriver::kDisconnected))); 160} 161 162TEST_F(ThirdPartyVpnDriverTest, UpdateConnectionState) { 163 std::string error; 164 driver_->UpdateConnectionState(Service::kStateConfiguring, &error); 165 EXPECT_EQ(error, "Unexpected call"); 166 167 error.clear(); 168 ThirdPartyVpnDriver::active_client_ = driver_; 169 driver_->UpdateConnectionState(Service::kStateConfiguring, &error); 170 EXPECT_EQ(error, "Invalid argument"); 171 172 error.clear(); 173 driver_->service_ = service_; 174 EXPECT_CALL(*service_, SetState(_)).Times(0); 175 driver_->UpdateConnectionState(Service::kStateOnline, &error); 176 EXPECT_TRUE(error.empty()); 177 Mock::VerifyAndClearExpectations(service_.get()); 178 179 EXPECT_CALL(*service_, SetState(Service::kStateFailure)).Times(1); 180 EXPECT_CALL(*adaptor_interface_, EmitPlatformMessage(static_cast<uint32_t>( 181 ThirdPartyVpnDriver::kDisconnected))) 182 .Times(1); 183 driver_->UpdateConnectionState(Service::kStateFailure, &error); 184 EXPECT_TRUE(error.empty()); 185 Mock::VerifyAndClearExpectations(service_.get()); 186 Mock::VerifyAndClearExpectations(adaptor_interface_); 187} 188 189TEST_F(ThirdPartyVpnDriverTest, SetParameters) { 190 std::map<std::string, std::string> parameters; 191 std::string error; 192 std::string warning; 193 driver_->SetParameters(parameters, &error, &warning); 194 EXPECT_EQ(error, "Unexpected call"); 195 196 error.clear(); 197 ThirdPartyVpnDriver::active_client_ = driver_; 198 driver_->parameters_expected_ = true; 199 driver_->SetParameters(parameters, &error, &warning); 200 EXPECT_EQ(error, 201 "address is missing;subnet_prefix is missing;" 202 "dns_servers is missing;" 203 "exclusion_list is missing;inclusion_list is missing;"); 204 EXPECT_TRUE(warning.empty()); 205 206 error.clear(); 207 parameters["address"] = "1234.1.1.1"; 208 driver_->SetParameters(parameters, &error, &warning); 209 EXPECT_EQ(error, 210 "address is not a valid IP;subnet_prefix is missing;" 211 "dns_servers is missing;" 212 "exclusion_list is missing;inclusion_list is missing;"); 213 EXPECT_TRUE(warning.empty()); 214 215 error.clear(); 216 parameters["address"] = "123.211.21.18"; 217 driver_->SetParameters(parameters, &error, &warning); 218 EXPECT_EQ(error, 219 "subnet_prefix is missing;dns_servers is missing;" 220 "exclusion_list is missing;inclusion_list is missing;"); 221 EXPECT_TRUE(warning.empty()); 222 223 error.clear(); 224 parameters["subnet_prefix"] = "123"; 225 driver_->SetParameters(parameters, &error, &warning); 226 EXPECT_EQ(error, 227 "subnet_prefix not in expected range;dns_servers is missing;" 228 "exclusion_list is missing;inclusion_list is missing;"); 229 EXPECT_TRUE(warning.empty()); 230 231 error.clear(); 232 parameters["subnet_prefix"] = "12"; 233 driver_->SetParameters(parameters, &error, &warning); 234 EXPECT_EQ(error, "dns_servers is missing;" 235 "exclusion_list is missing;inclusion_list is missing;"); 236 EXPECT_TRUE(warning.empty()); 237 238 error.clear(); 239 parameters["dns_servers"] = "12 123123 43902374"; 240 driver_->SetParameters(parameters, &error, &warning); 241 EXPECT_EQ(error, "dns_servers has no valid values or is empty;" 242 "exclusion_list is missing;inclusion_list is missing;"); 243 EXPECT_EQ(warning, "12 for dns_servers is invalid;" 244 "123123 for dns_servers is invalid;" 245 "43902374 for dns_servers is invalid;"); 246 247 driver_->device_ = 248 new MockVirtualDevice(&control_, &dispatcher_, &metrics_, &manager_, 249 kInterfaceName, kInterfaceIndex, Technology::kVPN); 250 error.clear(); 251 warning.clear(); 252 parameters["exclusion_list"] = 253 "400.400.400.400/12 1.1.1.1/44 1.1.1.1/-1 " 254 "123.211.21.0/23 123.211.21.1/23 123.211.21.0/25 " 255 "1.1.1.1.1/12 1.1.1/13"; 256 parameters["dns_servers"] = ""; 257 driver_->SetParameters(parameters, &error, &warning); 258 EXPECT_EQ(error, "dns_servers has no valid values or is empty;" 259 "inclusion_list is missing;"); 260 EXPECT_EQ(warning, 261 "400.400.400.400/12 for exclusion_list is invalid;" 262 "1.1.1.1/44 for exclusion_list is invalid;" 263 "1.1.1.1/-1 for exclusion_list is invalid;" 264 "Duplicate entry for 123.211.21.1/23 in exclusion_list found;" 265 "1.1.1.1.1/12 for exclusion_list is invalid;" 266 "1.1.1/13 for exclusion_list is invalid;"); 267 268 error.clear(); 269 warning.clear(); 270 parameters["exclusion_list"] = "0.0.0.0/0 123.211.21.29/31 123.211.21.1/24"; 271 parameters["inclusion_list"] = 272 "400.400.400.400/12 1.1.1.1/44 1.1.1.1/-1 " 273 "123.211.22.0/24 123.211.22.1/24 " 274 "1.1.1.1.1/12 1.1.1/13 123.211.21.0/24"; 275 driver_->SetParameters(parameters, &error, &warning); 276 EXPECT_EQ(error, "dns_servers has no valid values or is empty;"); 277 EXPECT_EQ(warning, 278 "400.400.400.400/12 for inclusion_list is invalid;" 279 "1.1.1.1/44 for inclusion_list is invalid;" 280 "1.1.1.1/-1 for inclusion_list is invalid;" 281 "Duplicate entry for 123.211.22.1/24 in inclusion_list found;" 282 "1.1.1.1.1/12 for inclusion_list is invalid;" 283 "1.1.1/13 for inclusion_list is invalid;" 284 "Duplicate entry for 123.211.21.0/24 in inclusion_list found;"); 285 286 error.clear(); 287 warning.clear(); 288 parameters["dns_servers"] = "123.211.21.18 123.211.21.19"; 289 parameters["inclusion_list"] = "123.211.61.29/7 123.211.42.29/17"; 290 driver_->SetParameters(parameters, &error, &warning); 291 EXPECT_EQ(driver_->ip_properties_.exclusion_list.size(), 3); 292 EXPECT_EQ(driver_->ip_properties_.exclusion_list[0], "123.211.21.29/31"); 293 EXPECT_EQ(driver_->ip_properties_.exclusion_list[1], "0.0.0.0/0"); 294 EXPECT_EQ(driver_->ip_properties_.exclusion_list[2], "123.211.21.1/24"); 295 EXPECT_EQ(driver_->ip_properties_.routes.size(), 2); 296 EXPECT_EQ(driver_->ip_properties_.routes[0].host, "123.211.61.29"); 297 EXPECT_EQ(driver_->ip_properties_.routes[1].host, "123.211.42.29"); 298 EXPECT_EQ(driver_->ip_properties_.routes[0].netmask, "254.0.0.0"); 299 EXPECT_EQ(driver_->ip_properties_.routes[1].netmask, "255.255.128.0"); 300 EXPECT_EQ(driver_->ip_properties_.routes[0].gateway, parameters["address"]); 301 EXPECT_EQ(driver_->ip_properties_.routes[1].gateway, parameters["address"]); 302 EXPECT_TRUE(error.empty()); 303 EXPECT_TRUE(warning.empty()); 304 EXPECT_FALSE(driver_->parameters_expected_); 305 driver_->device_ = nullptr; 306} 307 308} // namespace shill 309