ext.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- encoding: utf8 -*-
  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 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, see <http://www.gnu.org/licenses/>.
  20. from unittest import mock
  21. import qubes.ext.core_features
  22. import qubes.tests
  23. class TC_00_CoreFeatures(qubes.tests.QubesTestCase):
  24. def setUp(self):
  25. super().setUp()
  26. self.ext = qubes.ext.core_features.CoreFeatures()
  27. self.vm = mock.MagicMock()
  28. self.features = {}
  29. self.vm.configure_mock(**{
  30. 'features.get.side_effect': self.features.get,
  31. 'features.__contains__.side_effect': self.features.__contains__,
  32. 'features.__setitem__.side_effect': self.features.__setitem__,
  33. })
  34. def test_010_notify_tools(self):
  35. del self.vm.template
  36. self.ext.qubes_features_request(self.vm, 'features-request',
  37. untrusted_features={
  38. 'gui': '1',
  39. 'version': '1',
  40. 'default-user': 'user',
  41. 'qrexec': '1'}),
  42. self.assertEqual(self.vm.mock_calls, [
  43. ('features.get', ('qrexec', False), {}),
  44. ('features.__contains__', ('qrexec',), {}),
  45. ('features.__setitem__', ('qrexec', True), {}),
  46. ('features.__contains__', ('gui',), {}),
  47. ('features.__setitem__', ('gui', True), {}),
  48. ('features.get', ('qrexec', False), {}),
  49. ('fire_event', ('template-postinstall',), {})
  50. ])
  51. def test_011_notify_tools_uninstall(self):
  52. del self.vm.template
  53. self.ext.qubes_features_request(self.vm, 'features-request',
  54. untrusted_features={
  55. 'gui': '0',
  56. 'version': '1',
  57. 'default-user': 'user',
  58. 'qrexec': '0'}),
  59. self.assertEqual(self.vm.mock_calls, [
  60. ('features.get', ('qrexec', False), {}),
  61. ('features.__contains__', ('qrexec',), {}),
  62. ('features.__setitem__', ('qrexec', False), {}),
  63. ('features.__contains__', ('gui',), {}),
  64. ('features.__setitem__', ('gui', False), {}),
  65. ('features.get', ('qrexec', False), {}),
  66. ])
  67. def test_012_notify_tools_uninstall2(self):
  68. del self.vm.template
  69. self.ext.qubes_features_request(self.vm, 'features-request',
  70. untrusted_features={
  71. 'version': '1',
  72. 'default-user': 'user',
  73. })
  74. self.assertEqual(self.vm.mock_calls, [
  75. ('features.get', ('qrexec', False), {}),
  76. ('features.get', ('qrexec', False), {}),
  77. ])
  78. def test_013_notify_tools_no_version(self):
  79. del self.vm.template
  80. self.ext.qubes_features_request(self.vm, 'features-request',
  81. untrusted_features={
  82. 'qrexec': '1',
  83. 'gui': '1',
  84. 'default-user': 'user',
  85. })
  86. self.assertEqual(self.vm.mock_calls, [
  87. ('features.get', ('qrexec', False), {}),
  88. ('features.__contains__', ('qrexec',), {}),
  89. ('features.__setitem__', ('qrexec', True), {}),
  90. ('features.__contains__', ('gui',), {}),
  91. ('features.__setitem__', ('gui', True), {}),
  92. ('features.get', ('qrexec', False), {}),
  93. ('fire_event', ('template-postinstall',), {})
  94. ])
  95. def test_015_notify_tools_invalid_value_qrexec(self):
  96. del self.vm.template
  97. self.ext.qubes_features_request(self.vm, 'features-request',
  98. untrusted_features={
  99. 'version': '1',
  100. 'qrexec': 'invalid',
  101. 'gui': '1',
  102. 'default-user': 'user',
  103. })
  104. self.assertEqual(self.vm.mock_calls, [
  105. ('features.get', ('qrexec', False), {}),
  106. ('features.__contains__', ('gui',), {}),
  107. ('features.__setitem__', ('gui', True), {}),
  108. ('features.get', ('qrexec', False), {}),
  109. ])
  110. def test_016_notify_tools_invalid_value_gui(self):
  111. del self.vm.template
  112. self.ext.qubes_features_request(self.vm, 'features-request',
  113. untrusted_features={
  114. 'version': '1',
  115. 'qrexec': '1',
  116. 'gui': 'invalid',
  117. 'default-user': 'user',
  118. })
  119. self.assertEqual(self.vm.mock_calls, [
  120. ('features.get', ('qrexec', False), {}),
  121. ('features.__contains__', ('qrexec',), {}),
  122. ('features.__setitem__', ('qrexec', True), {}),
  123. ('features.get', ('qrexec', False), {}),
  124. ('fire_event', ('template-postinstall',), {})
  125. ])
  126. def test_017_notify_tools_template_based(self):
  127. self.ext.qubes_features_request(self.vm, 'features-request',
  128. untrusted_features={
  129. 'version': '1',
  130. 'qrexec': '1',
  131. 'gui': '1',
  132. 'default-user': 'user',
  133. })
  134. self.assertEqual(self.vm.mock_calls, [
  135. ('template.__bool__', (), {}),
  136. ('log.warning', ('Ignoring qubes.NotifyTools for template-based '
  137. 'VM',), {})
  138. ])
  139. def test_018_notify_tools_already_installed(self):
  140. self.features['qrexec'] = True
  141. self.features['gui'] = True
  142. del self.vm.template
  143. self.ext.qubes_features_request(self.vm, 'features-request',
  144. untrusted_features={
  145. 'gui': '1',
  146. 'version': '1',
  147. 'default-user': 'user',
  148. 'qrexec': '1'}),
  149. self.assertEqual(self.vm.mock_calls, [
  150. ('features.get', ('qrexec', False), {}),
  151. ('features.__contains__', ('qrexec',), {}),
  152. ('features.__contains__', ('gui',), {}),
  153. ])