4 # Copyright (c) 2012 INRIA
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # this program; if not, write to the Free Software Foundation, Inc., 51
18 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
27 from vsys import fd_tuntap, vif_up, vif_down, vroute, IFF_TAP, IFF_TUN
30 class TestVsys(unittest.TestCase):
32 self._network = "192.168.2.0"
34 self._ip = "192.168.2.2"
35 self._remote_net = "192.168.2.4"
37 def _create_vif(self, if_type, no_pi = False):
38 ####### create virtual device
39 (fd, if_name) = fd_tuntap(if_type, no_pi)
40 self.assertTrue(fd > 0)
42 ###### configure virtual device
43 vif_up(if_name, self._ip, self._prefix)
45 # wait for prcocess to see the new configuration...
48 ###### test ip responds to pings
49 p = subprocess.Popen(["ping", "-qc3", self._ip], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
50 out, err = p.communicate()
54 expected = """PING %(ip)s (%(ip)s) 56(84) bytes of data.
56 --- %(ip)s ping statistics ---
57 3 packets transmitted, 3 received, 0%% packet loss""" % {'ip': self._ip}
59 self.assertTrue(out.startswith(expected), out)
62 vroute ("add", self._remote_net, self._prefix, self._ip, if_name)
64 # wait for prcocess to see the new configuration...
68 p = subprocess.Popen(["ip", "r"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
69 out, err = p.communicate()
72 self.assertTrue(out.find(self._remote_net) >= 0 )
75 vroute ("del", self._remote_net, self._prefix, self._ip, if_name)
77 # wait for prcocess to see the new configuration...
81 p = subprocess.Popen(["ip", "r"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
82 out, err = p.communicate()
85 self.assertTrue(out.find(self._remote_net) < 0 )
87 ##### delete interface
90 # wait for prcocess to see the new configuration...
93 ###### test ip NOT responds to pings
94 p = subprocess.Popen(["ping", "-qc3", self._ip], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
95 out, err = p.communicate()
99 expected = """PING %(ip)s (%(ip)s) 56(84) bytes of data.
101 --- %(ip)s ping statistics ---
102 3 packets transmitted, 0 received, 100%% packet loss""" % {'ip': self._ip}
104 self.assertTrue(out.startswith(expected), out)
107 def test_create_tun(self):
108 self._create_vif(IFF_TUN)
110 def test_create_tap(self):
111 self._create_vif(IFF_TAP)
113 def test_create_tun_no_pi(self):
114 self._create_vif(IFF_TUN, no_pi = True)
116 def test_create_tap_no_pi(self):
117 self._create_vif(IFF_TAP, no_pi = True)
121 if __name__ == '__main__':