Package VMBuilder :: Package tests :: Module network_tests
[frames] | no frames]

Source Code for Module VMBuilder.tests.network_tests

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2009 Canonical Ltd. 
 4  #     
 5  #    See AUTHORS for list of contributors 
 6  # 
 7  #    This program is free software: you can redistribute it and/or modify 
 8  #    it under the terms of the GNU General Public License version 3, as 
 9  #    published by the Free Software Foundation. 
10  # 
11  #    This program is distributed in the hope that it will be useful, 
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #    GNU General Public License for more details. 
15  # 
16  #    You should have received a copy of the GNU General Public License 
17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  import unittest 
19   
20  from VMBuilder.exception import VMBuilderUserError 
21  from VMBuilder.plugins import network 
22   
23 -class TestNetworkPlugin(unittest.TestCase):
24 - def test_validate_mac(self):
25 valid_macs = ['11:22:33:44:55:66', 26 'ff:ff:ff:ff:ff:ff', 27 '00:00:00:00:00:00'] 28 invalid_macs = ['g1:22:33:44:55:66', 29 '11:ff:ff:ff:ff:ff:ff', 30 'ffffffffffff'] 31 for mac in valid_macs: 32 self.assertTrue(network.validate_mac(mac), '%s was not considered a valid MAC address' % mac) 33 for mac in invalid_macs: 34 self.assertFalse(network.validate_mac(mac), '%s was not considered an invalid MAC address' % mac)
35
37 valid_ips = ['192.168.1.1', 38 '1.1.1.1', 39 '10.0.0.1', 40 '255.255.255.255'] 41 42 invalid_ips = ['this is not a valid IP', 43 '256.1.1.1'] 44 45 for ip in valid_ips: 46 self.assertTrue(network.dotted_to_numeric_ip(ip), '%s was considered a valid IP address' % ip) 47 for ip in invalid_ips: 48 self.assertRaises(VMBuilderUserError, network.dotted_to_numeric_ip, ip)
49
50 - def test_guess_mask_from_ip(self):
51 known_correct_values = [('10.0.0.1', 0xFF), 52 ('127.0.0.1', 0xFF), 53 ('172.17.0.1', 0xFFFF), 54 ('192.168.1.1', 0xFFFFFF)] 55 56 for ip, nummask in known_correct_values: 57 numip = network.dotted_to_numeric_ip(ip) 58 self.assertEqual(network.guess_mask_from_ip(numip), nummask, "Incorrect netmask guessed") 59 60 self.assertRaises(VMBuilderUserError, network.guess_mask_from_ip, network.dotted_to_numeric_ip('230.0.0.0'))
61
63 known_correct_values = [(('192.168.1.1', '255.255.255.0'), '192.168.1.0'), 64 (('192.168.1.1', '255.255.0.0'), '192.168.0.0'), 65 (('192.168.1.1', '255.0.0.0'), '192.0.0.0'), 66 (('192.168.1.1', '255.242.255.0'), '192.160.1.0'), 67 (('192.168.1.1', '0.255.255.0'), '0.168.1.0')] 68 69 for ((ip, netmask), expected_network) in known_correct_values: 70 numip = network.dotted_to_numeric_ip(ip) 71 numnetmask = network.dotted_to_numeric_ip(netmask) 72 self.assertEqual(network.calculate_net_address_from_ip_and_netmask(numip, numnetmask), 73 network.dotted_to_numeric_ip(expected_network))
74
76 known_correct_values = [(('192.168.1.0', '255.255.255.0'), '192.168.1.255'), 77 (('192.168.0.0', '255.255.0.0'), '192.168.255.255'), 78 (('192.0.0.0', '255.0.0.0'), '192.255.255.255'), 79 (('192.160.1.0', '255.242.255.0'), '192.173.1.255'), 80 (('0.168.1.0', '0.255.255.0'), '255.168.1.255')] 81 82 for ((ip, netmask), expected_bcast) in known_correct_values: 83 numip = network.dotted_to_numeric_ip(ip) 84 numnetmask = network.dotted_to_numeric_ip(netmask) 85 guessed_broadcast = network.calculate_broadcast_address_from_ip_and_netmask(numip, numnetmask) 86 self.assertEqual(guessed_broadcast, 87 network.dotted_to_numeric_ip(expected_bcast), 88 "%s %s made %s, but expected %s" % (ip, 89 netmask, 90 network.numeric_to_dotted_ip(guessed_broadcast), 91 expected_bcast))
92
93 - def test_ip_conversion(self):
94 for x in xrange(256*256): 95 self.assertEqual(x*x, network.dotted_to_numeric_ip(network.numeric_to_dotted_ip(x*x)))
96