mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 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 | |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 7 | #include <sys/socket.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 8 | #include <linux/if.h> // NOLINT - Needs typedefs from sys/socket.h. |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 9 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 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" |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 16 | #include "shill/mock_store.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 17 | #include "shill/net/mock_rtnl_handler.h" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 18 | #include "shill/nice_mock_control.h" |
| 19 | #include "shill/technology.h" |
| 20 | |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 21 | using testing::_; |
| 22 | using testing::StrictMock; |
| 23 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 24 | namespace shill { |
| 25 | |
mukesh agrawal | a8f7c49 | 2013-06-13 18:31:14 -0700 | [diff] [blame] | 26 | namespace { |
| 27 | const char kTestDeviceName[] = "tun0"; |
| 28 | const int kTestInterfaceIndex = 5; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 29 | } // namespace |
mukesh agrawal | a8f7c49 | 2013-06-13 18:31:14 -0700 | [diff] [blame] | 30 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 31 | class 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 agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 46 | virtual void SetUp() { |
| 47 | device_->rtnl_handler_ = &rtnl_handler_; |
| 48 | } |
| 49 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 50 | protected: |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 51 | NiceMockControl control_; |
| 52 | EventDispatcher dispatcher_; |
| 53 | MockMetrics metrics_; |
| 54 | MockGLib glib_; |
| 55 | MockManager manager_; |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 56 | StrictMock<MockRTNLHandler> rtnl_handler_; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 57 | |
| 58 | VirtualDeviceRefPtr device_; |
| 59 | }; |
| 60 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 61 | TEST_F(VirtualDeviceTest, technology) { |
| 62 | EXPECT_EQ(Technology::kVPN, device_->technology()); |
| 63 | EXPECT_NE(Technology::kEthernet, device_->technology()); |
| 64 | } |
| 65 | |
mukesh agrawal | 4a16558 | 2013-08-02 15:58:21 -0700 | [diff] [blame] | 66 | TEST_F(VirtualDeviceTest, Load) { |
| 67 | StrictMock<MockStore> storage; |
| 68 | EXPECT_CALL(storage, ContainsGroup(_)).Times(0); |
| 69 | EXPECT_TRUE(device_->Load(&storage)); |
| 70 | } |
| 71 | |
| 72 | TEST_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 | |
| 78 | TEST_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 | |
| 85 | TEST_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 agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 93 | } // namespace shill |