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