net.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # pylint: disable=protected-access
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2014-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2014-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import ipaddress
  22. import unittest
  23. import qubes
  24. import qubes.vm.qubesvm
  25. import qubes.tests
  26. import qubes.tests.vm.qubesvm
  27. class TC_00_NetVMMixin(
  28. qubes.tests.vm.qubesvm.QubesVMTestsMixin, qubes.tests.QubesTestCase):
  29. def setUp(self):
  30. super(TC_00_NetVMMixin, self).setUp()
  31. self.app = qubes.tests.vm.TestApp()
  32. self.app.vmm.offline_mode = True
  33. def setup_netvms(self, vm):
  34. # usage of QubesVM here means that those tests should be after
  35. # testing properties used here
  36. self.netvm1 = qubes.vm.qubesvm.QubesVM(self.app, None, qid=2,
  37. name=qubes.tests.VMPREFIX + 'netvm1',
  38. provides_network=True, netvm=None)
  39. self.netvm2 = qubes.vm.qubesvm.QubesVM(self.app, None, qid=3,
  40. name=qubes.tests.VMPREFIX + 'netvm2',
  41. provides_network=True, netvm=None)
  42. self.nonetvm = qubes.vm.qubesvm.QubesVM(self.app, None, qid=4,
  43. name=qubes.tests.VMPREFIX + 'nonet')
  44. self.app.domains = qubes.app.VMCollection(self.app)
  45. for domain in (vm, self.netvm1, self.netvm2, self.nonetvm):
  46. self.app.domains._dict[domain.qid] = domain
  47. self.app.default_netvm = self.netvm1
  48. self.app.default_fw_netvm = self.netvm1
  49. self.addCleanup(self.cleanup_netvms)
  50. def cleanup_netvms(self):
  51. self.netvm1.close()
  52. self.netvm2.close()
  53. self.nonetvm.close()
  54. try:
  55. self.app.domains.close()
  56. except AttributeError:
  57. pass
  58. del self.netvm1
  59. del self.netvm2
  60. del self.nonetvm
  61. del self.app.default_netvm
  62. del self.app.default_fw_netvm
  63. @qubes.tests.skipUnlessDom0
  64. def test_140_netvm(self):
  65. vm = self.get_vm()
  66. self.setup_netvms(vm)
  67. self.assertPropertyDefaultValue(vm, 'netvm', self.app.default_netvm)
  68. self.assertPropertyValue(vm, 'netvm', self.netvm2, self.netvm2,
  69. self.netvm2.name)
  70. del vm.netvm
  71. self.assertPropertyDefaultValue(vm, 'netvm', self.app.default_netvm)
  72. self.assertPropertyValue(vm, 'netvm', self.netvm2.name, self.netvm2,
  73. self.netvm2.name)
  74. self.assertPropertyValue(vm, 'netvm', None, None, '')
  75. def test_141_netvm_invalid(self):
  76. vm = self.get_vm()
  77. self.setup_netvms(vm)
  78. self.assertPropertyInvalidValue(vm, 'netvm', 'invalid')
  79. self.assertPropertyInvalidValue(vm, 'netvm', 123)
  80. def test_142_netvm_netvm(self):
  81. vm = self.get_vm()
  82. self.setup_netvms(vm)
  83. self.assertPropertyInvalidValue(vm, 'netvm', self.nonetvm)
  84. def test_143_netvm_loopback(self):
  85. vm = self.get_vm()
  86. self.app.domains = {1: vm, vm: vm}
  87. self.addCleanup(self.app.domains.clear)
  88. self.assertPropertyInvalidValue(vm, 'netvm', vm)
  89. def test_144_netvm_loopback2(self):
  90. vm = self.get_vm()
  91. self.setup_netvms(vm)
  92. vm.netvm = None
  93. self.netvm2.netvm = self.netvm1
  94. vm.provides_network = True
  95. self.netvm1.netvm = vm
  96. self.assertPropertyInvalidValue(vm, 'netvm', self.netvm2)
  97. def test_150_ip(self):
  98. vm = self.get_vm()
  99. self.setup_netvms(vm)
  100. self.assertPropertyDefaultValue(vm, 'ip',
  101. ipaddress.IPv4Address('10.137.0.' + str(vm.qid)))
  102. vm.ip = '192.168.1.1'
  103. self.assertEqual(vm.ip, ipaddress.IPv4Address('192.168.1.1'))
  104. def test_151_ip_invalid(self):
  105. vm = self.get_vm()
  106. self.setup_netvms(vm)
  107. self.assertPropertyInvalidValue(vm, 'ip', 'abcd')
  108. self.assertPropertyInvalidValue(vm, 'ip', 'a.b.c.d')
  109. self.assertPropertyInvalidValue(vm, 'ip', '1111.2222.3333.4444')
  110. # TODO: implement and add here: 0.0.0.0, 333.333.333.333
  111. def test_160_ip6(self):
  112. vm = self.get_vm()
  113. self.setup_netvms(vm)
  114. self.assertPropertyDefaultValue(vm, 'ip6', None)
  115. vm.netvm.features['ipv6'] = True
  116. self.assertPropertyDefaultValue(vm, 'ip6',
  117. ipaddress.IPv6Address('{}::a89:{:x}'.format(
  118. qubes.config.qubes_ipv6_prefix, vm.qid)))
  119. vm.ip6 = 'abcd:efff::1'
  120. self.assertEqual(vm.ip6, ipaddress.IPv6Address('abcd:efff::1'))
  121. def test_161_ip6_invalid(self):
  122. vm = self.get_vm()
  123. self.setup_netvms(vm)
  124. vm.netvm.features['ipv6'] = True
  125. self.assertPropertyInvalidValue(vm, 'ip', 'zzzz')
  126. self.assertPropertyInvalidValue(vm, 'ip',
  127. '1:2:3:4:5:6:7:8:0:a:b:c:d:e:f:0')
  128. def test_170_provides_network_netvm(self):
  129. vm = self.get_vm()
  130. vm2 = self.get_vm('test2', qid=3)
  131. self.assertPropertyDefaultValue(vm, 'provides_network', False)
  132. self.assertPropertyInvalidValue(vm2, 'netvm', vm)
  133. self.assertPropertyValue(vm, 'provides_network', True, True, 'True')
  134. self.assertPropertyValue(vm2, 'netvm', vm, vm, 'test-inst-test')
  135. # used by other vm
  136. self.assertPropertyInvalidValue(vm, 'provides_network', False)
  137. self.assertPropertyValue(vm2, 'netvm', None, None, '')
  138. self.assertPropertyValue(vm2, 'netvm', '', None, '')
  139. self.assertPropertyValue(vm, 'provides_network', False, False, 'False')