ext.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 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. from unittest import mock
  21. import qubes.ext.core_features
  22. import qubes.ext.windows
  23. import qubes.tests
  24. class TC_00_CoreFeatures(qubes.tests.QubesTestCase):
  25. def setUp(self):
  26. super().setUp()
  27. self.ext = qubes.ext.core_features.CoreFeatures()
  28. self.vm = mock.MagicMock()
  29. self.features = {}
  30. self.vm.configure_mock(**{
  31. 'features.get.side_effect': self.features.get,
  32. 'features.__contains__.side_effect': self.features.__contains__,
  33. 'features.__setitem__.side_effect': self.features.__setitem__,
  34. })
  35. def test_010_notify_tools(self):
  36. del self.vm.template
  37. self.ext.qubes_features_request(self.vm, 'features-request',
  38. untrusted_features={
  39. 'gui': '1',
  40. 'version': '1',
  41. 'default-user': 'user',
  42. 'qrexec': '1'}),
  43. self.assertEqual(self.vm.mock_calls, [
  44. ('features.get', ('qrexec', False), {}),
  45. ('features.__contains__', ('qrexec',), {}),
  46. ('features.__setitem__', ('qrexec', True), {}),
  47. ('features.__contains__', ('gui',), {}),
  48. ('features.__setitem__', ('gui', True), {}),
  49. ('features.get', ('qrexec', False), {}),
  50. ('fire_event', ('template-postinstall',), {})
  51. ])
  52. def test_011_notify_tools_uninstall(self):
  53. del self.vm.template
  54. self.ext.qubes_features_request(self.vm, 'features-request',
  55. untrusted_features={
  56. 'gui': '0',
  57. 'version': '1',
  58. 'default-user': 'user',
  59. 'qrexec': '0'}),
  60. self.assertEqual(self.vm.mock_calls, [
  61. ('features.get', ('qrexec', False), {}),
  62. ('features.__contains__', ('qrexec',), {}),
  63. ('features.__setitem__', ('qrexec', False), {}),
  64. ('features.__contains__', ('gui',), {}),
  65. ('features.__setitem__', ('gui', False), {}),
  66. ('features.get', ('qrexec', False), {}),
  67. ])
  68. def test_012_notify_tools_uninstall2(self):
  69. del self.vm.template
  70. self.ext.qubes_features_request(self.vm, 'features-request',
  71. untrusted_features={
  72. 'version': '1',
  73. 'default-user': 'user',
  74. })
  75. self.assertEqual(self.vm.mock_calls, [
  76. ('features.get', ('qrexec', False), {}),
  77. ('features.get', ('qrexec', False), {}),
  78. ])
  79. def test_013_notify_tools_no_version(self):
  80. del self.vm.template
  81. self.ext.qubes_features_request(self.vm, 'features-request',
  82. untrusted_features={
  83. 'qrexec': '1',
  84. 'gui': '1',
  85. 'default-user': 'user',
  86. })
  87. self.assertEqual(self.vm.mock_calls, [
  88. ('features.get', ('qrexec', False), {}),
  89. ('features.__contains__', ('qrexec',), {}),
  90. ('features.__setitem__', ('qrexec', True), {}),
  91. ('features.__contains__', ('gui',), {}),
  92. ('features.__setitem__', ('gui', True), {}),
  93. ('features.get', ('qrexec', False), {}),
  94. ('fire_event', ('template-postinstall',), {})
  95. ])
  96. def test_015_notify_tools_invalid_value_qrexec(self):
  97. del self.vm.template
  98. self.ext.qubes_features_request(self.vm, 'features-request',
  99. untrusted_features={
  100. 'version': '1',
  101. 'qrexec': 'invalid',
  102. 'gui': '1',
  103. 'default-user': 'user',
  104. })
  105. self.assertEqual(self.vm.mock_calls, [
  106. ('features.get', ('qrexec', False), {}),
  107. ('features.__contains__', ('gui',), {}),
  108. ('features.__setitem__', ('gui', True), {}),
  109. ('features.get', ('qrexec', False), {}),
  110. ])
  111. def test_016_notify_tools_invalid_value_gui(self):
  112. del self.vm.template
  113. self.ext.qubes_features_request(self.vm, 'features-request',
  114. untrusted_features={
  115. 'version': '1',
  116. 'qrexec': '1',
  117. 'gui': 'invalid',
  118. 'default-user': 'user',
  119. })
  120. self.assertEqual(self.vm.mock_calls, [
  121. ('features.get', ('qrexec', False), {}),
  122. ('features.__contains__', ('qrexec',), {}),
  123. ('features.__setitem__', ('qrexec', True), {}),
  124. ('features.get', ('qrexec', False), {}),
  125. ('fire_event', ('template-postinstall',), {})
  126. ])
  127. def test_017_notify_tools_template_based(self):
  128. self.ext.qubes_features_request(self.vm, 'features-request',
  129. untrusted_features={
  130. 'version': '1',
  131. 'qrexec': '1',
  132. 'gui': '1',
  133. 'default-user': 'user',
  134. })
  135. self.assertEqual(self.vm.mock_calls, [
  136. ('template.__bool__', (), {}),
  137. ('log.warning', ('Ignoring qubes.NotifyTools for template-based '
  138. 'VM',), {})
  139. ])
  140. def test_018_notify_tools_already_installed(self):
  141. self.features['qrexec'] = True
  142. self.features['gui'] = True
  143. del self.vm.template
  144. self.ext.qubes_features_request(self.vm, 'features-request',
  145. untrusted_features={
  146. 'gui': '1',
  147. 'version': '1',
  148. 'default-user': 'user',
  149. 'qrexec': '1'}),
  150. self.assertEqual(self.vm.mock_calls, [
  151. ('features.get', ('qrexec', False), {}),
  152. ('features.__contains__', ('qrexec',), {}),
  153. ('features.__contains__', ('gui',), {}),
  154. ])
  155. class TC_10_WindowsFeatures(qubes.tests.QubesTestCase):
  156. def setUp(self):
  157. super().setUp()
  158. self.ext = qubes.ext.windows.WindowsFeatures()
  159. self.vm = mock.MagicMock()
  160. self.features = {}
  161. self.vm.configure_mock(**{
  162. 'features.get.side_effect': self.features.get,
  163. 'features.__contains__.side_effect': self.features.__contains__,
  164. 'features.__setitem__.side_effect': self.features.__setitem__,
  165. })
  166. def test_000_notify_tools_full(self):
  167. del self.vm.template
  168. self.ext.qubes_features_request(self.vm, 'features-request',
  169. untrusted_features={
  170. 'gui': '1',
  171. 'version': '1',
  172. 'default-user': 'user',
  173. 'qrexec': '1',
  174. 'os': 'Windows'})
  175. self.assertEqual(self.vm.mock_calls, [
  176. ('features.__setitem__', ('os', 'Windows'), {}),
  177. ('features.__setitem__', ('rpc-clipboard', True), {}),
  178. ])
  179. def test_001_notify_tools_no_qrexec(self):
  180. del self.vm.template
  181. self.ext.qubes_features_request(self.vm, 'features-request',
  182. untrusted_features={
  183. 'gui': '1',
  184. 'version': '1',
  185. 'default-user': 'user',
  186. 'qrexec': '0',
  187. 'os': 'Windows'})
  188. self.assertEqual(self.vm.mock_calls, [
  189. ('features.__setitem__', ('os', 'Windows'), {}),
  190. ])
  191. def test_002_notify_tools_other_os(self):
  192. del self.vm.template
  193. self.ext.qubes_features_request(self.vm, 'features-request',
  194. untrusted_features={
  195. 'gui': '1',
  196. 'version': '1',
  197. 'default-user': 'user',
  198. 'qrexec': '1',
  199. 'os': 'Linux'})
  200. self.assertEqual(self.vm.mock_calls, [])