api_misc.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. import asyncio
  21. from unittest import mock
  22. import qubes.tests
  23. import qubes.api.misc
  24. class TC_00_API_Misc(qubes.tests.QubesTestCase):
  25. def setUp(self):
  26. super(TC_00_API_Misc, self).setUp()
  27. self.src = mock.NonCallableMagicMock()
  28. self.app = mock.NonCallableMock()
  29. self.dest = mock.NonCallableMock()
  30. self.dest.name = 'dom0'
  31. self.app.configure_mock(domains={
  32. 'dom0': self.dest,
  33. 'test-vm': self.src,
  34. })
  35. def configure_qdb(self, entries):
  36. self.src.configure_mock(**{
  37. 'qdb.read.side_effect': (lambda path: entries.get(path, None)),
  38. 'qdb.list.side_effect': (lambda path: sorted(entries.keys())),
  39. })
  40. def call_mgmt_func(self, method, arg=b'', payload=b''):
  41. mgmt_obj = qubes.api.misc.QubesMiscAPI(self.app,
  42. b'test-vm', method, b'dom0', arg)
  43. loop = asyncio.get_event_loop()
  44. response = loop.run_until_complete(
  45. mgmt_obj.execute(untrusted_payload=payload))
  46. return response
  47. def test_000_features_request(self):
  48. qdb_entries = {
  49. '/features-request/feature1': b'1',
  50. '/features-request/feature2': b'',
  51. '/features-request/feature3': b'other',
  52. }
  53. self.configure_qdb(qdb_entries)
  54. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  55. self.assertIsNone(response)
  56. self.assertEqual(self.app.mock_calls, [
  57. mock.call.save()
  58. ])
  59. self.assertEqual(self.src.mock_calls, [
  60. mock.call.qdb.list('/features-request/'),
  61. mock.call.qdb.read('/features-request/feature1'),
  62. mock.call.qdb.read('/features-request/feature2'),
  63. mock.call.qdb.read('/features-request/feature3'),
  64. mock.call.fire_event('features-request', untrusted_features={
  65. 'feature1': '1', 'feature2': '', 'feature3': 'other'})
  66. ])
  67. def test_001_features_request_empty(self):
  68. self.configure_qdb({})
  69. response = self.call_mgmt_func(b'qubes.FeaturesRequest')
  70. self.assertIsNone(response)
  71. self.assertEqual(self.app.mock_calls, [
  72. mock.call.save()
  73. ])
  74. self.assertEqual(self.src.mock_calls, [
  75. mock.call.qdb.list('/features-request/'),
  76. mock.call.fire_event('features-request', untrusted_features={})
  77. ])
  78. def test_002_features_request_invalid1(self):
  79. qdb_entries = {
  80. '/features-request/feature1': b'test spaces',
  81. }
  82. self.configure_qdb(qdb_entries)
  83. with self.assertRaises(AssertionError):
  84. self.call_mgmt_func(b'qubes.FeaturesRequest')
  85. self.assertEqual(self.app.mock_calls, [])
  86. self.assertEqual(self.src.mock_calls, [
  87. mock.call.qdb.list('/features-request/'),
  88. mock.call.qdb.read('/features-request/feature1'),
  89. ])
  90. def test_003_features_request_invalid2(self):
  91. qdb_entries = {
  92. '/features-request/feature1': b'\xfe\x01',
  93. }
  94. self.configure_qdb(qdb_entries)
  95. with self.assertRaises(UnicodeDecodeError):
  96. self.call_mgmt_func(b'qubes.FeaturesRequest')
  97. self.assertEqual(self.app.mock_calls, [])
  98. self.assertEqual(self.src.mock_calls, [
  99. mock.call.qdb.list('/features-request/'),
  100. mock.call.qdb.read('/features-request/feature1'),
  101. ])
  102. def test_010_notify_tools(self):
  103. qdb_entries = {
  104. '/qubes-tools/version': b'1',
  105. '/qubes-tools/qrexec': b'1',
  106. '/qubes-tools/gui': b'1',
  107. '/qubes-tools/os': b'Linux',
  108. '/qubes-tools/default-user': b'user',
  109. }
  110. self.configure_qdb(qdb_entries)
  111. del self.src.template
  112. self.src.configure_mock(**{'features.get.return_value': False})
  113. response = self.call_mgmt_func(b'qubes.NotifyTools')
  114. self.assertIsNone(response)
  115. self.assertEqual(self.app.mock_calls, [
  116. mock.call.save()
  117. ])
  118. self.assertEqual(self.src.mock_calls, [
  119. mock.call.qdb.read('/qubes-tools/version'),
  120. mock.call.qdb.read('/qubes-tools/qrexec'),
  121. mock.call.qdb.read('/qubes-tools/gui'),
  122. mock.call.features.get('qrexec', False),
  123. mock.call.features.__setitem__('qrexec', True),
  124. mock.call.features.__setitem__('gui', True),
  125. mock.call.fire_event('template-postinstall')
  126. ])
  127. def test_011_notify_tools_uninstall(self):
  128. qdb_entries = {
  129. '/qubes-tools/version': b'1',
  130. '/qubes-tools/qrexec': b'0',
  131. '/qubes-tools/gui': b'0',
  132. '/qubes-tools/os': b'Linux',
  133. '/qubes-tools/default-user': b'user',
  134. }
  135. self.configure_qdb(qdb_entries)
  136. del self.src.template
  137. self.src.configure_mock(**{'features.get.return_value': True})
  138. response = self.call_mgmt_func(b'qubes.NotifyTools')
  139. self.assertIsNone(response)
  140. self.assertEqual(self.app.mock_calls, [
  141. mock.call.save()
  142. ])
  143. self.assertEqual(self.src.mock_calls, [
  144. mock.call.qdb.read('/qubes-tools/version'),
  145. mock.call.qdb.read('/qubes-tools/qrexec'),
  146. mock.call.qdb.read('/qubes-tools/gui'),
  147. mock.call.features.get('qrexec', False),
  148. mock.call.features.__setitem__('qrexec', False),
  149. mock.call.features.__setitem__('gui', False),
  150. ])
  151. def test_012_notify_tools_uninstall2(self):
  152. qdb_entries = {
  153. '/qubes-tools/version': b'1',
  154. '/qubes-tools/os': b'Linux',
  155. '/qubes-tools/default-user': b'user',
  156. }
  157. self.configure_qdb(qdb_entries)
  158. del self.src.template
  159. self.src.configure_mock(**{'features.get.return_value': True})
  160. response = self.call_mgmt_func(b'qubes.NotifyTools')
  161. self.assertIsNone(response)
  162. self.assertEqual(self.app.mock_calls, [
  163. mock.call.save()
  164. ])
  165. self.assertEqual(self.src.mock_calls, [
  166. mock.call.qdb.read('/qubes-tools/version'),
  167. mock.call.qdb.read('/qubes-tools/qrexec'),
  168. mock.call.qdb.read('/qubes-tools/gui'),
  169. mock.call.features.get('qrexec', False),
  170. mock.call.features.__setitem__('qrexec', False),
  171. mock.call.features.__setitem__('gui', False),
  172. ])
  173. def test_013_notify_tools_no_version(self):
  174. qdb_entries = {
  175. '/qubes-tools/qrexec': b'0',
  176. '/qubes-tools/gui': b'0',
  177. '/qubes-tools/os': b'Linux',
  178. '/qubes-tools/default-user': b'user',
  179. }
  180. self.configure_qdb(qdb_entries)
  181. del self.src.template
  182. self.src.configure_mock(**{'features.get.return_value': True})
  183. response = self.call_mgmt_func(b'qubes.NotifyTools')
  184. self.assertIsNone(response)
  185. self.assertEqual(self.app.mock_calls, [])
  186. self.assertEqual(self.src.mock_calls, [
  187. mock.call.qdb.read('/qubes-tools/version'),
  188. mock.call.qdb.read('/qubes-tools/qrexec'),
  189. mock.call.qdb.read('/qubes-tools/gui'),
  190. ])
  191. def test_014_notify_tools_invalid_version(self):
  192. qdb_entries = {
  193. '/qubes-tools/version': b'this is invalid',
  194. '/qubes-tools/qrexec': b'0',
  195. '/qubes-tools/gui': b'0',
  196. '/qubes-tools/os': b'Linux',
  197. '/qubes-tools/default-user': b'user',
  198. }
  199. self.configure_qdb(qdb_entries)
  200. del self.src.template
  201. self.src.configure_mock(**{'features.get.return_value': True})
  202. with self.assertRaises(ValueError):
  203. self.call_mgmt_func(b'qubes.NotifyTools')
  204. self.assertEqual(self.app.mock_calls, [])
  205. self.assertEqual(self.src.mock_calls, [
  206. mock.call.qdb.read('/qubes-tools/version'),
  207. mock.call.qdb.read('/qubes-tools/qrexec'),
  208. mock.call.qdb.read('/qubes-tools/gui'),
  209. ])
  210. def test_015_notify_tools_invalid_value_qrexec(self):
  211. qdb_entries = {
  212. '/qubes-tools/version': b'1',
  213. '/qubes-tools/qrexec': b'invalid',
  214. '/qubes-tools/gui': b'0',
  215. '/qubes-tools/os': b'Linux',
  216. '/qubes-tools/default-user': b'user',
  217. }
  218. self.configure_qdb(qdb_entries)
  219. del self.src.template
  220. self.src.configure_mock(**{'features.get.return_value': True})
  221. with self.assertRaises(ValueError):
  222. self.call_mgmt_func(b'qubes.NotifyTools')
  223. self.assertEqual(self.app.mock_calls, [])
  224. self.assertEqual(self.src.mock_calls, [
  225. mock.call.qdb.read('/qubes-tools/version'),
  226. mock.call.qdb.read('/qubes-tools/qrexec'),
  227. mock.call.qdb.read('/qubes-tools/gui'),
  228. ])
  229. def test_016_notify_tools_invalid_value_gui(self):
  230. qdb_entries = {
  231. '/qubes-tools/version': b'1',
  232. '/qubes-tools/qrexec': b'1',
  233. '/qubes-tools/gui': b'invalid',
  234. '/qubes-tools/os': b'Linux',
  235. '/qubes-tools/default-user': b'user',
  236. }
  237. self.configure_qdb(qdb_entries)
  238. del self.src.template
  239. self.src.configure_mock(**{'features.get.return_value': True})
  240. with self.assertRaises(ValueError):
  241. self.call_mgmt_func(b'qubes.NotifyTools')
  242. self.assertEqual(self.app.mock_calls, [])
  243. self.assertEqual(self.src.mock_calls, [
  244. mock.call.qdb.read('/qubes-tools/version'),
  245. mock.call.qdb.read('/qubes-tools/qrexec'),
  246. mock.call.qdb.read('/qubes-tools/gui'),
  247. ])
  248. def test_017_notify_tools_template_based(self):
  249. qdb_entries = {
  250. '/qubes-tools/version': b'1',
  251. '/qubes-tools/qrexec': b'1',
  252. '/qubes-tools/gui': b'invalid',
  253. '/qubes-tools/os': b'Linux',
  254. '/qubes-tools/default-user': b'user',
  255. }
  256. self.configure_qdb(qdb_entries)
  257. self.src.configure_mock(**{'features.get.return_value': True})
  258. response = self.call_mgmt_func(b'qubes.NotifyTools')
  259. self.assertIsNone(response)
  260. self.assertEqual(self.app.mock_calls, [])
  261. self.assertEqual(self.src.mock_calls, [
  262. mock.call.template.__bool__(),
  263. mock.call.log.warning(
  264. 'Ignoring qubes.NotifyTools for template-based VM')
  265. ])
  266. def test_018_notify_tools_already_installed(self):
  267. qdb_entries = {
  268. '/qubes-tools/version': b'1',
  269. '/qubes-tools/qrexec': b'1',
  270. '/qubes-tools/gui': b'1',
  271. '/qubes-tools/os': b'Linux',
  272. '/qubes-tools/default-user': b'user',
  273. }
  274. self.configure_qdb(qdb_entries)
  275. del self.src.template
  276. self.src.configure_mock(**{'features.get.return_value': True})
  277. response = self.call_mgmt_func(b'qubes.NotifyTools')
  278. self.assertIsNone(response)
  279. self.assertEqual(self.app.mock_calls, [
  280. mock.call.save()
  281. ])
  282. self.assertEqual(self.src.mock_calls, [
  283. mock.call.qdb.read('/qubes-tools/version'),
  284. mock.call.qdb.read('/qubes-tools/qrexec'),
  285. mock.call.qdb.read('/qubes-tools/gui'),
  286. mock.call.features.get('qrexec', False),
  287. mock.call.features.__setitem__('qrexec', True),
  288. mock.call.features.__setitem__('gui', True),
  289. ])
  290. def test_020_notify_updates_standalone(self):
  291. del self.src.template
  292. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  293. self.assertIsNone(response)
  294. self.assertEqual(self.src.mock_calls, [
  295. mock.call.updateable.__bool__(),
  296. ])
  297. self.assertEqual(self.src.updates_available, True)
  298. self.assertEqual(self.app.mock_calls, [
  299. mock.call.save()
  300. ])
  301. def test_021_notify_updates_standalone2(self):
  302. del self.src.template
  303. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  304. self.assertIsNone(response)
  305. self.assertEqual(self.src.mock_calls, [
  306. mock.call.updateable.__bool__(),
  307. ])
  308. self.assertEqual(self.src.updates_available, False)
  309. self.assertEqual(self.app.mock_calls, [
  310. mock.call.save()
  311. ])
  312. def test_022_notify_updates_invalid(self):
  313. del self.src.template
  314. with self.assertRaises(AssertionError):
  315. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'')
  316. self.assertEqual(self.src.mock_calls, [])
  317. # not set property returns Mock()
  318. self.assertIsInstance(self.src.updates_available, mock.Mock)
  319. self.assertEqual(self.app.mock_calls, [])
  320. def test_023_notify_updates_invalid2(self):
  321. del self.src.template
  322. with self.assertRaises(AssertionError):
  323. self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'no updates')
  324. self.assertEqual(self.src.mock_calls, [])
  325. # not set property returns Mock()
  326. self.assertIsInstance(self.src.updates_available, mock.Mock)
  327. self.assertEqual(self.app.mock_calls, [])
  328. def test_024_notify_updates_template_based_no_updates(self):
  329. '''No updates on template-based VM, should not reset state'''
  330. self.src.updateable = False
  331. self.src.template.is_running.return_value = False
  332. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n')
  333. self.assertIsNone(response)
  334. self.assertEqual(self.src.mock_calls, [
  335. mock.call.template.is_running(),
  336. ])
  337. # not set property returns Mock()
  338. self.assertIsInstance(self.src.template.updates_available, mock.Mock)
  339. self.assertIsInstance(self.src.updates_available, mock.Mock)
  340. self.assertEqual(self.app.mock_calls, [])
  341. def test_025_notify_updates_template_based(self):
  342. '''Some updates on template-based VM, should save flag'''
  343. self.src.updateable = False
  344. self.src.template.is_running.return_value = False
  345. self.src.storage.outdated_volumes = []
  346. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  347. self.assertIsNone(response)
  348. self.assertEqual(self.src.mock_calls, [
  349. mock.call.template.is_running(),
  350. ])
  351. # not set property returns Mock()
  352. self.assertIsInstance(self.src.updates_available, mock.Mock)
  353. self.assertEqual(self.src.template.updates_available, True)
  354. self.assertEqual(self.app.mock_calls, [
  355. mock.call.save()
  356. ])
  357. def test_026_notify_updates_template_based_outdated(self):
  358. self.src.updateable = False
  359. self.src.template.is_running.return_value = False
  360. self.src.storage.outdated_volumes = ['root']
  361. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  362. self.assertIsNone(response)
  363. self.assertEqual(self.src.mock_calls, [
  364. mock.call.template.is_running(),
  365. ])
  366. # not set property returns Mock()
  367. self.assertIsInstance(self.src.updates_available, mock.Mock)
  368. self.assertIsInstance(self.src.template.updates_available, mock.Mock)
  369. self.assertEqual(self.app.mock_calls, [])
  370. def test_027_notify_updates_template_based_template_running(self):
  371. self.src.updateable = False
  372. self.src.template.is_running.return_value = True
  373. self.src.storage.outdated_volumes = []
  374. response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n')
  375. self.assertIsNone(response)
  376. self.assertEqual(self.src.mock_calls, [
  377. mock.call.template.is_running(),
  378. ])
  379. # not set property returns Mock()
  380. self.assertIsInstance(self.src.template.updates_available, mock.Mock)
  381. self.assertIsInstance(self.src.updates_available, mock.Mock)
  382. self.assertEqual(self.app.mock_calls, [])