Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 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/virtual_device.h"
     18 
     19 #include <sys/socket.h>
     20 #include <linux/if.h>  // NOLINT - Needs typedefs from sys/socket.h.
     21 
     22 #include <gtest/gtest.h>
     23 
     24 #include "shill/event_dispatcher.h"
     25 #include "shill/mock_manager.h"
     26 #include "shill/mock_metrics.h"
     27 #include "shill/mock_store.h"
     28 #include "shill/net/mock_rtnl_handler.h"
     29 #include "shill/nice_mock_control.h"
     30 #include "shill/technology.h"
     31 
     32 using testing::_;
     33 using testing::StrictMock;
     34 
     35 namespace shill {
     36 
     37 namespace {
     38 const char kTestDeviceName[] = "tun0";
     39 const int kTestInterfaceIndex = 5;
     40 }  // namespace
     41 
     42 class VirtualDeviceTest : public testing::Test {
     43  public:
     44   VirtualDeviceTest()
     45       : metrics_(&dispatcher_),
     46         manager_(&control_, &dispatcher_, &metrics_),
     47         device_(new VirtualDevice(&control_,
     48                                   &dispatcher_,
     49                                   &metrics_,
     50                                   &manager_,
     51                                   kTestDeviceName,
     52                                   kTestInterfaceIndex,
     53                                   Technology::kVPN)) {}
     54 
     55   virtual ~VirtualDeviceTest() {}
     56 
     57   virtual void SetUp() {
     58     device_->rtnl_handler_ = &rtnl_handler_;
     59   }
     60 
     61  protected:
     62   NiceMockControl control_;
     63   EventDispatcher dispatcher_;
     64   MockMetrics metrics_;
     65   MockManager manager_;
     66   StrictMock<MockRTNLHandler> rtnl_handler_;
     67 
     68   VirtualDeviceRefPtr device_;
     69 };
     70 
     71 TEST_F(VirtualDeviceTest, technology) {
     72   EXPECT_EQ(Technology::kVPN, device_->technology());
     73   EXPECT_NE(Technology::kEthernet, device_->technology());
     74 }
     75 
     76 TEST_F(VirtualDeviceTest, Load) {
     77   StrictMock<MockStore> storage;
     78   EXPECT_CALL(storage, ContainsGroup(_)).Times(0);
     79   EXPECT_TRUE(device_->Load(&storage));
     80 }
     81 
     82 TEST_F(VirtualDeviceTest, Save) {
     83   StrictMock<MockStore> storage;
     84   EXPECT_CALL(storage, SetBool(_, _, _)).Times(0);  // Or any type, really.
     85   EXPECT_TRUE(device_->Save(&storage));
     86 }
     87 
     88 TEST_F(VirtualDeviceTest, Start) {
     89   Error error(Error::kOperationInitiated);
     90   EXPECT_CALL(rtnl_handler_, SetInterfaceFlags(_, IFF_UP, IFF_UP));
     91   device_->Start(&error, EnabledStateChangedCallback());
     92   EXPECT_TRUE(error.IsSuccess());
     93 }
     94 
     95 TEST_F(VirtualDeviceTest, Stop) {
     96   Error error(Error::kOperationInitiated);
     97   device_->Stop(&error, EnabledStateChangedCallback());
     98   EXPECT_TRUE(error.IsSuccess());
     99 }
    100 
    101 // TODO(quiche): Add test for UpdateIPConfig. crbug.com/266404
    102 
    103 }  // namespace shill
    104