net.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  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)
  39. self.netvm2 = qubes.vm.qubesvm.QubesVM(self.app, None, qid=3,
  40. name=qubes.tests.VMPREFIX + 'netvm2',
  41. provides_network=True)
  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. self.app.domains.close()
  55. del self.netvm1
  56. del self.netvm2
  57. del self.nonetvm
  58. del self.app.default_netvm
  59. del self.app.default_fw_netvm
  60. @qubes.tests.skipUnlessDom0
  61. def test_140_netvm(self):
  62. vm = self.get_vm()
  63. self.setup_netvms(vm)
  64. self.assertPropertyDefaultValue(vm, 'netvm', self.app.default_netvm)
  65. self.assertPropertyValue(vm, 'netvm', self.netvm2, self.netvm2,
  66. self.netvm2.name)
  67. del vm.netvm
  68. self.assertPropertyDefaultValue(vm, 'netvm', self.app.default_netvm)
  69. self.assertPropertyValue(vm, 'netvm', self.netvm2.name, self.netvm2,
  70. self.netvm2.name)
  71. self.assertPropertyValue(vm, 'netvm', None, None, '')
  72. def test_141_netvm_invalid(self):
  73. vm = self.get_vm()
  74. self.setup_netvms(vm)
  75. self.assertPropertyInvalidValue(vm, 'netvm', 'invalid')
  76. self.assertPropertyInvalidValue(vm, 'netvm', 123)
  77. def test_142_netvm_netvm(self):
  78. vm = self.get_vm()
  79. self.setup_netvms(vm)
  80. self.assertPropertyInvalidValue(vm, 'netvm', self.nonetvm)
  81. def test_143_netvm_loopback(self):
  82. vm = self.get_vm()
  83. self.app.domains = {1: vm, vm: vm}
  84. self.addCleanup(self.app.domains.clear)
  85. self.assertPropertyInvalidValue(vm, 'netvm', vm)
  86. def test_150_ip(self):
  87. vm = self.get_vm()
  88. self.setup_netvms(vm)
  89. self.assertPropertyDefaultValue(vm, 'ip', '10.137.0.' + str(vm.qid))
  90. vm.ip = '192.168.1.1'
  91. self.assertEqual(vm.ip, '192.168.1.1')
  92. def test_151_ip_invalid(self):
  93. vm = self.get_vm()
  94. self.setup_netvms(vm)
  95. self.assertPropertyInvalidValue(vm, 'ip', 'abcd')
  96. self.assertPropertyInvalidValue(vm, 'ip', 'a.b.c.d')
  97. self.assertPropertyInvalidValue(vm, 'ip', '1111.2222.3333.4444')
  98. # TODO: implement and add here: 0.0.0.0, 333.333.333.333