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