ext.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 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.services
  23. import qubes.ext.windows
  24. import qubes.tests
  25. class TC_00_CoreFeatures(qubes.tests.QubesTestCase):
  26. def setUp(self):
  27. super().setUp()
  28. self.ext = qubes.ext.core_features.CoreFeatures()
  29. self.vm = mock.MagicMock()
  30. self.features = {}
  31. self.vm.configure_mock(**{
  32. 'features.get.side_effect': self.features.get,
  33. 'features.__contains__.side_effect': self.features.__contains__,
  34. 'features.__setitem__.side_effect': self.features.__setitem__,
  35. })
  36. def test_010_notify_tools(self):
  37. del self.vm.template
  38. self.loop.run_until_complete(
  39. self.ext.qubes_features_request(self.vm, 'features-request',
  40. untrusted_features={
  41. 'gui': '1',
  42. 'version': '1',
  43. 'default-user': 'user',
  44. 'qrexec': '1',
  45. 'vmexec': '1'}))
  46. self.assertListEqual(self.vm.mock_calls, [
  47. ('features.get', ('qrexec', False), {}),
  48. ('features.__contains__', ('qrexec',), {}),
  49. ('features.__setitem__', ('qrexec', True), {}),
  50. ('features.__contains__', ('gui',), {}),
  51. ('features.__setitem__', ('gui', True), {}),
  52. ('features.__setitem__', ('vmexec', True), {}),
  53. ('features.get', ('qrexec', False), {}),
  54. ('fire_event_async', ('template-postinstall',), {}),
  55. ('fire_event_async().__iter__', (), {}),
  56. ])
  57. def test_011_notify_tools_uninstall(self):
  58. del self.vm.template
  59. self.loop.run_until_complete(
  60. self.ext.qubes_features_request(self.vm, 'features-request',
  61. untrusted_features={
  62. 'gui': '0',
  63. 'version': '1',
  64. 'default-user': 'user',
  65. 'qrexec': '0',
  66. 'vmexec': '0'}))
  67. self.assertListEqual(self.vm.mock_calls, [
  68. ('features.get', ('qrexec', False), {}),
  69. ('features.__contains__', ('qrexec',), {}),
  70. ('features.__setitem__', ('qrexec', False), {}),
  71. ('features.__contains__', ('gui',), {}),
  72. ('features.__setitem__', ('gui', False), {}),
  73. ('features.__setitem__', ('vmexec', False), {}),
  74. ('features.get', ('qrexec', False), {}),
  75. ])
  76. def test_012_notify_tools_uninstall2(self):
  77. del self.vm.template
  78. self.loop.run_until_complete(
  79. self.ext.qubes_features_request(self.vm, 'features-request',
  80. untrusted_features={
  81. 'version': '1',
  82. 'default-user': 'user',
  83. }))
  84. self.assertListEqual(self.vm.mock_calls, [
  85. ('features.get', ('qrexec', False), {}),
  86. ('features.get', ('qrexec', False), {}),
  87. ])
  88. def test_013_notify_tools_no_version(self):
  89. del self.vm.template
  90. self.loop.run_until_complete(
  91. self.ext.qubes_features_request(self.vm, 'features-request',
  92. untrusted_features={
  93. 'qrexec': '1',
  94. 'gui': '1',
  95. 'default-user': 'user',
  96. }))
  97. self.assertListEqual(self.vm.mock_calls, [
  98. ('features.get', ('qrexec', False), {}),
  99. ('features.__contains__', ('qrexec',), {}),
  100. ('features.__setitem__', ('qrexec', True), {}),
  101. ('features.__contains__', ('gui',), {}),
  102. ('features.__setitem__', ('gui', True), {}),
  103. ('features.get', ('qrexec', False), {}),
  104. ('fire_event_async', ('template-postinstall',), {}),
  105. ('fire_event_async().__iter__', (), {}),
  106. ])
  107. def test_015_notify_tools_invalid_value_qrexec(self):
  108. del self.vm.template
  109. self.loop.run_until_complete(
  110. self.ext.qubes_features_request(self.vm, 'features-request',
  111. untrusted_features={
  112. 'version': '1',
  113. 'qrexec': 'invalid',
  114. 'gui': '1',
  115. 'default-user': 'user',
  116. }))
  117. self.assertEqual(self.vm.mock_calls, [
  118. ('features.get', ('qrexec', False), {}),
  119. ('features.__contains__', ('gui',), {}),
  120. ('features.__setitem__', ('gui', True), {}),
  121. ('features.get', ('qrexec', False), {}),
  122. ])
  123. def test_016_notify_tools_invalid_value_gui(self):
  124. del self.vm.template
  125. self.loop.run_until_complete(
  126. self.ext.qubes_features_request(self.vm, 'features-request',
  127. untrusted_features={
  128. 'version': '1',
  129. 'qrexec': '1',
  130. 'gui': 'invalid',
  131. 'default-user': 'user',
  132. }))
  133. self.assertListEqual(self.vm.mock_calls, [
  134. ('features.get', ('qrexec', False), {}),
  135. ('features.__contains__', ('qrexec',), {}),
  136. ('features.__setitem__', ('qrexec', True), {}),
  137. ('features.get', ('qrexec', False), {}),
  138. ('fire_event_async', ('template-postinstall',), {}),
  139. ('fire_event_async().__iter__', (), {}),
  140. ])
  141. def test_017_notify_tools_template_based(self):
  142. self.loop.run_until_complete(
  143. self.ext.qubes_features_request(self.vm, 'features-request',
  144. untrusted_features={
  145. 'version': '1',
  146. 'qrexec': '1',
  147. 'gui': '1',
  148. 'default-user': 'user',
  149. }))
  150. self.assertEqual(self.vm.mock_calls, [
  151. ('template.__bool__', (), {}),
  152. ('log.warning', ('Ignoring qubes.NotifyTools for template-based '
  153. 'VM',), {})
  154. ])
  155. def test_018_notify_tools_already_installed(self):
  156. self.features['qrexec'] = True
  157. self.features['gui'] = True
  158. del self.vm.template
  159. self.loop.run_until_complete(
  160. self.ext.qubes_features_request(self.vm, 'features-request',
  161. untrusted_features={
  162. 'gui': '1',
  163. 'version': '1',
  164. 'default-user': 'user',
  165. 'qrexec': '1'}))
  166. self.assertListEqual(self.vm.mock_calls, [
  167. ('features.get', ('qrexec', False), {}),
  168. ('features.__contains__', ('qrexec',), {}),
  169. ('features.__contains__', ('gui',), {}),
  170. ])
  171. class TC_10_WindowsFeatures(qubes.tests.QubesTestCase):
  172. def setUp(self):
  173. super().setUp()
  174. self.ext = qubes.ext.windows.WindowsFeatures()
  175. self.vm = mock.MagicMock()
  176. self.features = {}
  177. self.vm.configure_mock(**{
  178. 'features.get.side_effect': self.features.get,
  179. 'features.__contains__.side_effect': self.features.__contains__,
  180. 'features.__setitem__.side_effect': self.features.__setitem__,
  181. })
  182. def test_000_notify_tools_full(self):
  183. del self.vm.template
  184. self.ext.qubes_features_request(self.vm, 'features-request',
  185. untrusted_features={
  186. 'gui': '1',
  187. 'version': '1',
  188. 'default-user': 'user',
  189. 'qrexec': '1',
  190. 'os': 'Windows'})
  191. self.assertEqual(self.vm.mock_calls, [
  192. ('features.__setitem__', ('os', 'Windows'), {}),
  193. ('features.__setitem__', ('rpc-clipboard', True), {}),
  194. ])
  195. def test_001_notify_tools_no_qrexec(self):
  196. del self.vm.template
  197. self.ext.qubes_features_request(self.vm, 'features-request',
  198. untrusted_features={
  199. 'gui': '1',
  200. 'version': '1',
  201. 'default-user': 'user',
  202. 'qrexec': '0',
  203. 'os': 'Windows'})
  204. self.assertEqual(self.vm.mock_calls, [
  205. ('features.__setitem__', ('os', 'Windows'), {}),
  206. ])
  207. def test_002_notify_tools_other_os(self):
  208. del self.vm.template
  209. self.ext.qubes_features_request(self.vm, 'features-request',
  210. untrusted_features={
  211. 'gui': '1',
  212. 'version': '1',
  213. 'default-user': 'user',
  214. 'qrexec': '1',
  215. 'os': 'other'})
  216. self.assertEqual(self.vm.mock_calls, [])
  217. class TC_20_Services(qubes.tests.QubesTestCase):
  218. def setUp(self):
  219. super().setUp()
  220. self.ext = qubes.ext.services.ServicesExtension()
  221. self.vm = mock.MagicMock()
  222. self.features = {}
  223. self.vm.configure_mock(**{
  224. 'template': None,
  225. 'maxmem': 1024,
  226. 'is_running.return_value': True,
  227. 'features.get.side_effect': self.features.get,
  228. 'features.items.side_effect': self.features.items,
  229. 'features.__iter__.side_effect': self.features.__iter__,
  230. 'features.__contains__.side_effect': self.features.__contains__,
  231. 'features.__setitem__.side_effect': self.features.__setitem__,
  232. 'features.__delitem__.side_effect': self.features.__delitem__,
  233. })
  234. def test_000_write_to_qdb(self):
  235. self.features['service.test1'] = '1'
  236. self.features['service.test2'] = ''
  237. self.ext.on_domain_qdb_create(self.vm, 'domain-qdb-create')
  238. self.assertEqual(sorted(self.vm.untrusted_qdb.mock_calls), [
  239. ('write', ('/qubes-service/meminfo-writer', '1'), {}),
  240. ('write', ('/qubes-service/test1', '1'), {}),
  241. ('write', ('/qubes-service/test2', '0'), {}),
  242. ])
  243. def test_001_feature_set(self):
  244. self.ext.on_domain_feature_set(self.vm,
  245. 'feature-set:service.test_no_oldvalue',
  246. 'service.test_no_oldvalue', '1')
  247. self.ext.on_domain_feature_set(self.vm,
  248. 'feature-set:service.test_oldvalue',
  249. 'service.test_oldvalue', '1', '')
  250. self.ext.on_domain_feature_set(self.vm,
  251. 'feature-set:service.test_disable',
  252. 'service.test_disable', '', '1')
  253. self.ext.on_domain_feature_set(self.vm,
  254. 'feature-set:service.test_disable_no_oldvalue',
  255. 'service.test_disable_no_oldvalue', '')
  256. self.assertEqual(sorted(self.vm.untrusted_qdb.mock_calls), sorted([
  257. ('write', ('/qubes-service/test_no_oldvalue', '1'), {}),
  258. ('write', ('/qubes-service/test_oldvalue', '1'), {}),
  259. ('write', ('/qubes-service/test_disable', '0'), {}),
  260. ('write', ('/qubes-service/test_disable_no_oldvalue', '0'), {}),
  261. ]))
  262. def test_002_feature_delete(self):
  263. self.ext.on_domain_feature_delete(self.vm,
  264. 'feature-delete:service.test3', 'service.test3')
  265. self.assertEqual(sorted(self.vm.untrusted_qdb.mock_calls), [
  266. ('rm', ('/qubes-service/test3',), {}),
  267. ])
  268. def test_010_supported_services(self):
  269. self.ext.supported_services(self.vm, 'features-request',
  270. untrusted_features={
  271. 'supported-service.test1': '1', # ok
  272. 'supported-service.test2': '0', # ignored
  273. 'supported-service.test3': 'some text', # ignored
  274. 'no-service': '1', # ignored
  275. })
  276. self.assertEqual(self.features, {
  277. 'supported-service.test1': True,
  278. })
  279. def test_011_supported_services_add(self):
  280. self.features['supported-service.test1'] = '1'
  281. self.ext.supported_services(self.vm, 'features-request',
  282. untrusted_features={
  283. 'supported-service.test1': '1', # ok
  284. 'supported-service.test2': '1', # ok
  285. })
  286. # also check if existing one is untouched
  287. self.assertEqual(self.features, {
  288. 'supported-service.test1': '1',
  289. 'supported-service.test2': True,
  290. })
  291. def test_012_supported_services_remove(self):
  292. self.features['supported-service.test1'] = '1'
  293. self.ext.supported_services(self.vm, 'features-request',
  294. untrusted_features={
  295. 'supported-service.test2': '1', # ok
  296. })
  297. self.assertEqual(self.features, {
  298. 'supported-service.test2': True,
  299. })