blob: 942480409438ed52a77d42840717ae4c8c8feba4 [file] [log] [blame]
mukesh agrawal9da07772013-05-15 14:15:17 -07001// 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
mukesh agrawal4a165582013-08-02 15:58:21 -07007#include <sys/socket.h>
Alex Vakulenkoa41ab512014-07-23 14:24:23 -07008#include <linux/if.h> // NOLINT - Needs typedefs from sys/socket.h.
mukesh agrawal4a165582013-08-02 15:58:21 -07009
mukesh agrawal9da07772013-05-15 14:15:17 -070010#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"
mukesh agrawal4a165582013-08-02 15:58:21 -070016#include "shill/mock_store.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/mock_rtnl_handler.h"
mukesh agrawal9da07772013-05-15 14:15:17 -070018#include "shill/nice_mock_control.h"
19#include "shill/technology.h"
20
mukesh agrawal4a165582013-08-02 15:58:21 -070021using testing::_;
22using testing::StrictMock;
23
mukesh agrawal9da07772013-05-15 14:15:17 -070024namespace shill {
25
mukesh agrawala8f7c492013-06-13 18:31:14 -070026namespace {
27const char kTestDeviceName[] = "tun0";
28const int kTestInterfaceIndex = 5;
Alex Vakulenko8a532292014-06-16 17:18:44 -070029} // namespace
mukesh agrawala8f7c492013-06-13 18:31:14 -070030
mukesh agrawal9da07772013-05-15 14:15:17 -070031class 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
mukesh agrawal4a165582013-08-02 15:58:21 -070046 virtual void SetUp() {
47 device_->rtnl_handler_ = &rtnl_handler_;
48 }
49
mukesh agrawal9da07772013-05-15 14:15:17 -070050 protected:
mukesh agrawal9da07772013-05-15 14:15:17 -070051 NiceMockControl control_;
52 EventDispatcher dispatcher_;
53 MockMetrics metrics_;
54 MockGLib glib_;
55 MockManager manager_;
mukesh agrawal4a165582013-08-02 15:58:21 -070056 StrictMock<MockRTNLHandler> rtnl_handler_;
mukesh agrawal9da07772013-05-15 14:15:17 -070057
58 VirtualDeviceRefPtr device_;
59};
60
mukesh agrawal9da07772013-05-15 14:15:17 -070061TEST_F(VirtualDeviceTest, technology) {
62 EXPECT_EQ(Technology::kVPN, device_->technology());
63 EXPECT_NE(Technology::kEthernet, device_->technology());
64}
65
mukesh agrawal4a165582013-08-02 15:58:21 -070066TEST_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
mukesh agrawal9da07772013-05-15 14:15:17 -070093} // namespace shill