• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013 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
5import unittest
6
7from lansim import tuntap
8
9
10class TunTapTest(unittest.TestCase):
11    """Unit tests for the TunTap class."""
12
13    def testCreateTapDevice(self):
14        """Tests creation of a TAP device and its attributes."""
15        tap = tuntap.TunTap(tuntap.IFF_TAP, name="faketap%d")
16
17        self.assertEqual(tap.mode, tuntap.IFF_TAP)
18
19        # Interface name respects the provided format.
20        self.assertTrue(hasattr(tap, 'name'))
21        self.assertTrue(tap.name.startswith('faketap'))
22
23        # MTU is set for the interface.
24        self.assertTrue(hasattr(tap, 'mtu'))
25        self.assertTrue(tap.mtu)
26
27
28    def testCreateTunDevice(self):
29        """Tests creation of a TAP device and its attributes."""
30        tun = tuntap.TunTap(tuntap.IFF_TUN, name="faketun%d")
31        self.assertEqual(tun.mode, tuntap.IFF_TUN)
32
33
34    def testTapDeviceHWAddr(self):
35        """Tests that we can get and set the HW address of a TAP device."""
36        tap = tuntap.TunTap(tuntap.IFF_TAP, name="faketap%d")
37        family, addr = tap.get_hwaddr()
38        self.assertEqual(family, 1) # Ethernet address
39
40        # Select a different hwaddr.
41        addr = addr[:-2] + ('11' if addr[-2:] != '11' else '22')
42
43        new_family, new_addr = tap.set_hwaddr(addr)
44        self.assertEqual(new_family, 1)
45        self.assertEqual(new_addr, addr)
46
47        new_family, new_addr = tap.get_hwaddr()
48        self.assertEqual(new_family, 1)
49        self.assertEqual(new_addr, addr)
50
51
52    def testTapDeviceUpDown(self):
53        """Tests if it is possible to bring up and down the interface."""
54        tap = tuntap.TunTap(tuntap.IFF_TAP, name="faketap%d")
55        # Set the IP address to a safe value:
56        tap.set_addr('169.254.10.1')
57        self.assertEqual(tap.addr, '169.254.10.1')
58        tap.set_addr('0.0.0.0')
59
60        self.assertFalse(tap.is_up())
61        tap.up()
62        self.assertTrue(tap.is_up())
63        # Checks that calling up() twice is harmless.
64        tap.up()
65        self.assertTrue(tap.is_up())
66        tap.down()
67        self.assertFalse(tap.is_up())
68
69
70if __name__ == '__main__':
71    unittest.main()
72
73