utils.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.utils
  22. class TestVMUsage(qubesadmin.tests.QubesTestCase):
  23. def setUp(self):
  24. super(TestVMUsage, self).setUp()
  25. self.app.expected_calls[
  26. ('dom0', 'admin.vm.List', None, None)] = \
  27. b'0\x00vm1 class=AppVM state=Running\n' \
  28. b'template1 class=TemplateVM state=Halted\n' \
  29. b'template2 class=TemplateVM state=Running\n' \
  30. b'vm2 class=AppVM state=Running\n' \
  31. b'sys-net class=AppVM state=Running\n' \
  32. b'sys-firewall class=AppVM state=Running\n'
  33. self.global_properties = ['default_dispvm', 'default_netvm',
  34. 'default_guivm', 'default_audiovm',
  35. 'default_template', 'clockvm', 'updatevm',
  36. 'management_dispvm']
  37. for prop in self.global_properties:
  38. self.app.expected_calls[
  39. ('dom0', 'admin.property.Get', prop, None)] = \
  40. b'0\x00default=True type=vm vm2'
  41. self.vms = ['vm1', 'vm2', 'sys-net', 'sys-firewall',
  42. 'template1', 'template2']
  43. self.vm_properties = ['template', 'netvm', 'guivm', 'audiovm',
  44. 'default_dispvm', 'management_dispvm']
  45. for vm in self.vms:
  46. for prop in self.vm_properties:
  47. if not prop.startswith('template') or \
  48. not vm.startswith('template'):
  49. self.app.expected_calls[
  50. (vm, 'admin.vm.property.Get', prop, None)] = \
  51. b'0\x00default=False type=vm template1'
  52. else:
  53. self.app.expected_calls[
  54. (vm, 'admin.vm.property.Get', prop, None)] = \
  55. b'2\0QubesNoSuchPropertyError\0\0invalid property\0'
  56. def test_00_only_global(self):
  57. result = qubesadmin.utils.vm_dependencies(self.app,
  58. self.app.domains['vm2'])
  59. self.assertListEqual(result,
  60. [(None, prop) for prop in self.global_properties],
  61. "Incorrect global properties listed.")
  62. def test_01_only_vm(self):
  63. result = qubesadmin.utils.vm_dependencies(
  64. self.app, self.app.domains['template1'])
  65. self.assertSetEqual(
  66. set(result),
  67. set([(vm, prop) for vm in self.vms for prop in self.vm_properties
  68. if (not vm.startswith('template')
  69. or not prop.startswith(
  70. 'template')) and vm != 'template1']),
  71. "Incorrect VM properties listed.")
  72. def test_02_empty(self):
  73. result = qubesadmin.utils.vm_dependencies(self.app,
  74. self.app.domains['vm1'])
  75. self.assertListEqual(result, [], "Incorrect use found.")
  76. def test_03_access_error(self):
  77. self.app.expected_calls[
  78. ('dom0', 'admin.property.Get', 'default_dispvm', None)] = b''
  79. result = qubesadmin.utils.vm_dependencies(self.app,
  80. self.app.domains['vm1'])
  81. self.assertListEqual(result, [], "Incorrect use found.")
  82. def test_04_defaults(self):
  83. self.app.expected_calls[
  84. ('vm2', 'admin.vm.property.Get', 'netvm', None)] = \
  85. b'0\x00default=True type=vm sys-net'
  86. self.app.expected_calls[
  87. ('vm1', 'admin.vm.property.Get', 'netvm', None)] = \
  88. b'0\x00default=False type=vm sys-net'
  89. result = qubesadmin.utils.vm_dependencies(self.app,
  90. self.app.domains['sys-net'])
  91. self.assertListEqual(result, [(self.app.domains['vm1'], 'netvm')])
  92. class TestVMExecEncode(qubesadmin.tests.QubesTestCase):
  93. def test_00_encode(self):
  94. self.assertEqual(
  95. qubesadmin.utils.encode_for_vmexec(['ls', '-a']),
  96. 'ls+--a')
  97. self.assertEqual(
  98. qubesadmin.utils.encode_for_vmexec(
  99. ['touch', '/home/user/.profile']),
  100. 'touch+-2Fhome-2Fuser-2F.profile')