app.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@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. #
  21. import os
  22. import unittest.mock as mock
  23. import lxml.etree
  24. import qubes
  25. import qubes.events
  26. import qubes.tests
  27. import qubes.tests.init
  28. import qubes.tests.storage_reflink
  29. class TestApp(qubes.tests.TestEmitter):
  30. pass
  31. class TC_20_QubesHost(qubes.tests.QubesTestCase):
  32. sample_xc_domain_getinfo = [
  33. {'paused': 0, 'cpu_time': 243951379111104, 'ssidref': 0,
  34. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  35. 'mem_kb': 3733212, 'domid': 0, 'max_vcpu_id': 7,
  36. 'crashed': 0, 'running': 1, 'maxmem_kb': 3734236,
  37. 'shutdown': 0, 'online_vcpus': 8,
  38. 'handle': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  39. 'cpupool': 0, 'blocked': 0},
  40. {'paused': 0, 'cpu_time': 2849496569205, 'ssidref': 0,
  41. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  42. 'mem_kb': 303916, 'domid': 1, 'max_vcpu_id': 0,
  43. 'crashed': 0, 'running': 0, 'maxmem_kb': 308224,
  44. 'shutdown': 0, 'online_vcpus': 1,
  45. 'handle': [116, 174, 229, 207, 17, 1, 79, 39, 191, 37, 41,
  46. 186, 205, 158, 219, 8],
  47. 'cpupool': 0, 'blocked': 1},
  48. {'paused': 0, 'cpu_time': 249658663079978, 'ssidref': 0,
  49. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  50. 'mem_kb': 3782668, 'domid': 11, 'max_vcpu_id': 7,
  51. 'crashed': 0, 'running': 0, 'maxmem_kb': 3783692,
  52. 'shutdown': 0, 'online_vcpus': 8,
  53. 'handle': [169, 95, 55, 127, 140, 94, 79, 220, 186, 210,
  54. 117, 5, 148, 11, 185, 206],
  55. 'cpupool': 0, 'blocked': 1}]
  56. def setUp(self):
  57. super(TC_20_QubesHost, self).setUp()
  58. self.app = TestApp()
  59. self.app.vmm = mock.Mock()
  60. self.qubes_host = qubes.app.QubesHost(self.app)
  61. def test_000_get_vm_stats_single(self):
  62. self.app.vmm.configure_mock(**{
  63. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  64. })
  65. info_time, info = self.qubes_host.get_vm_stats()
  66. self.assertEqual(self.app.vmm.mock_calls, [
  67. ('xc.domain_getinfo', (0, 1024), {}),
  68. ])
  69. self.assertIsNotNone(info_time)
  70. expected_info = {
  71. 0: {
  72. 'cpu_time': 243951379111104,
  73. 'cpu_usage': 0,
  74. 'cpu_usage_raw': 0,
  75. 'memory_kb': 3733212,
  76. },
  77. 1: {
  78. 'cpu_time': 2849496569205,
  79. 'cpu_usage': 0,
  80. 'cpu_usage_raw': 0,
  81. 'memory_kb': 303916,
  82. },
  83. 11: {
  84. 'cpu_time': 249658663079978,
  85. 'cpu_usage': 0,
  86. 'cpu_usage_raw': 0,
  87. 'memory_kb': 3782668,
  88. },
  89. }
  90. self.assertEqual(info, expected_info)
  91. def test_001_get_vm_stats_twice(self):
  92. self.app.vmm.configure_mock(**{
  93. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  94. })
  95. prev_time, prev_info = self.qubes_host.get_vm_stats()
  96. prev_time -= 1
  97. prev_info[0]['cpu_time'] -= 8*10**8 # 0.8s
  98. prev_info[1]['cpu_time'] -= 10**9 # 1s
  99. prev_info[11]['cpu_time'] -= 10**9 # 1s
  100. info_time, info = self.qubes_host.get_vm_stats(prev_time, prev_info)
  101. self.assertIsNotNone(info_time)
  102. expected_info = {
  103. 0: {
  104. 'cpu_time': 243951379111104,
  105. 'cpu_usage': 9,
  106. 'cpu_usage_raw': 79,
  107. 'memory_kb': 3733212,
  108. },
  109. 1: {
  110. 'cpu_time': 2849496569205,
  111. 'cpu_usage': 99,
  112. 'cpu_usage_raw': 99,
  113. 'memory_kb': 303916,
  114. },
  115. 11: {
  116. 'cpu_time': 249658663079978,
  117. 'cpu_usage': 12,
  118. 'cpu_usage_raw': 99,
  119. 'memory_kb': 3782668,
  120. },
  121. }
  122. self.assertEqual(info, expected_info)
  123. self.assertEqual(self.app.vmm.mock_calls, [
  124. ('xc.domain_getinfo', (0, 1024), {}),
  125. ('xc.domain_getinfo', (0, 1024), {}),
  126. ])
  127. def test_002_get_vm_stats_one_vm(self):
  128. self.app.vmm.configure_mock(**{
  129. 'xc.domain_getinfo.return_value': [self.sample_xc_domain_getinfo[1]]
  130. })
  131. vm = mock.Mock
  132. vm.xid = 1
  133. vm.name = 'somevm'
  134. info_time, info = self.qubes_host.get_vm_stats(only_vm=vm)
  135. self.assertIsNotNone(info_time)
  136. self.assertEqual(self.app.vmm.mock_calls, [
  137. ('xc.domain_getinfo', (1, 1), {}),
  138. ])
  139. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  140. def setUp(self):
  141. super().setUp()
  142. self.app = TestApp()
  143. self.vms = qubes.app.VMCollection(self.app)
  144. self.testvm1 = qubes.tests.init.TestVM(
  145. None, None, qid=1, name='testvm1')
  146. self.testvm2 = qubes.tests.init.TestVM(
  147. None, None, qid=2, name='testvm2')
  148. self.addCleanup(self.cleanup_vmcollection)
  149. def cleanup_vmcollection(self):
  150. self.testvm1.close()
  151. self.testvm2.close()
  152. self.vms.close()
  153. del self.testvm1
  154. del self.testvm2
  155. del self.vms
  156. del self.app
  157. def test_000_contains(self):
  158. self.vms._dict = {1: self.testvm1}
  159. self.assertIn(1, self.vms)
  160. self.assertIn('testvm1', self.vms)
  161. self.assertIn(self.testvm1, self.vms)
  162. self.assertNotIn(2, self.vms)
  163. self.assertNotIn('testvm2', self.vms)
  164. self.assertNotIn(self.testvm2, self.vms)
  165. def test_001_getitem(self):
  166. self.vms._dict = {1: self.testvm1}
  167. self.assertIs(self.vms[1], self.testvm1)
  168. self.assertIs(self.vms['testvm1'], self.testvm1)
  169. self.assertIs(self.vms[self.testvm1], self.testvm1)
  170. def test_002_add(self):
  171. self.vms.add(self.testvm1)
  172. self.assertIn(1, self.vms)
  173. self.assertEventFired(self.app, 'domain-add',
  174. kwargs={'vm': self.testvm1})
  175. with self.assertRaises(TypeError):
  176. self.vms.add(object())
  177. testvm_qid_collision = qubes.tests.init.TestVM(
  178. None, None, name='testvm2', qid=1)
  179. testvm_name_collision = qubes.tests.init.TestVM(
  180. None, None, name='testvm1', qid=2)
  181. with self.assertRaises(ValueError):
  182. self.vms.add(testvm_qid_collision)
  183. with self.assertRaises(ValueError):
  184. self.vms.add(testvm_name_collision)
  185. def test_003_qids(self):
  186. self.vms.add(self.testvm1)
  187. self.vms.add(self.testvm2)
  188. self.assertCountEqual(self.vms.qids(), [1, 2])
  189. self.assertCountEqual(self.vms.keys(), [1, 2])
  190. def test_004_names(self):
  191. self.vms.add(self.testvm1)
  192. self.vms.add(self.testvm2)
  193. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  194. def test_005_vms(self):
  195. self.vms.add(self.testvm1)
  196. self.vms.add(self.testvm2)
  197. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  198. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  199. def test_006_items(self):
  200. self.vms.add(self.testvm1)
  201. self.vms.add(self.testvm2)
  202. self.assertCountEqual(self.vms.items(),
  203. [(1, self.testvm1), (2, self.testvm2)])
  204. def test_007_len(self):
  205. self.vms.add(self.testvm1)
  206. self.vms.add(self.testvm2)
  207. self.assertEqual(len(self.vms), 2)
  208. def test_008_delitem(self):
  209. self.vms.add(self.testvm1)
  210. self.vms.add(self.testvm2)
  211. del self.vms['testvm2']
  212. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  213. self.assertEventFired(self.app, 'domain-delete',
  214. kwargs={'vm': self.testvm2})
  215. def test_100_get_new_unused_qid(self):
  216. self.vms.add(self.testvm1)
  217. self.vms.add(self.testvm2)
  218. self.vms.get_new_unused_qid()
  219. # def test_200_get_vms_based_on(self):
  220. # pass
  221. # def test_201_get_vms_connected_to(self):
  222. # pass
  223. class TC_80_QubesInitialPools(qubes.tests.QubesTestCase):
  224. def setUp(self):
  225. super().setUp()
  226. self.app = qubes.Qubes('/tmp/qubestest.xml', load=False,
  227. offline_mode=True)
  228. self.test_dir = '/var/tmp/test-varlibqubes'
  229. self.test_patch = mock.patch.dict(
  230. qubes.config.defaults['pool_configs']['varlibqubes'],
  231. {'dir_path': self.test_dir})
  232. self.test_patch.start()
  233. def tearDown(self):
  234. self.test_patch.stop()
  235. self.app.close()
  236. del self.app
  237. def get_driver(self, fs_type, accessible):
  238. qubes.tests.storage_reflink.mkdir_fs(self.test_dir, fs_type,
  239. accessible=accessible, cleanup_via=self.addCleanup)
  240. self.app.load_initial_values()
  241. varlibqubes = self.app.pools['varlibqubes']
  242. self.assertEqual(varlibqubes.dir_path, self.test_dir)
  243. return varlibqubes.driver
  244. def test_100_varlibqubes_btrfs_accessible(self):
  245. self.assertEqual(self.get_driver('btrfs', True), 'file-reflink')
  246. def test_101_varlibqubes_btrfs_inaccessible(self):
  247. self.assertEqual(self.get_driver('btrfs', False), 'file')
  248. def test_102_varlibqubes_ext4_accessible(self):
  249. self.assertEqual(self.get_driver('ext4', True), 'file')
  250. def test_103_varlibqubes_ext4_inaccessible(self):
  251. self.assertEqual(self.get_driver('ext4', False), 'file')
  252. class TC_89_QubesEmpty(qubes.tests.QubesTestCase):
  253. def tearDown(self):
  254. try:
  255. os.unlink('/tmp/qubestest.xml')
  256. except:
  257. pass
  258. try:
  259. self.app.close()
  260. del self.app
  261. except AttributeError:
  262. pass
  263. super().tearDown()
  264. @qubes.tests.skipUnlessDom0
  265. def test_000_init_empty(self):
  266. # pylint: disable=no-self-use,unused-variable,bare-except
  267. try:
  268. os.unlink('/tmp/qubestest.xml')
  269. except FileNotFoundError:
  270. pass
  271. qubes.Qubes.create_empty_store('/tmp/qubestest.xml').close()
  272. def test_100_property_migrate_default_fw_netvm(self):
  273. xml_template = '''<?xml version="1.0" encoding="utf-8" ?>
  274. <qubes version="3.0">
  275. <properties>
  276. <property name="default_netvm">{default_netvm}</property>
  277. <property name="default_fw_netvm">{default_fw_netvm}</property>
  278. </properties>
  279. <labels>
  280. <label id="label-1" color="#cc0000">red</label>
  281. </labels>
  282. <pools>
  283. <pool driver="file" dir_path="/tmp/qubes-test" name="default"/>
  284. </pools>
  285. <domains>
  286. <domain class="StandaloneVM" id="domain-1">
  287. <properties>
  288. <property name="qid">1</property>
  289. <property name="name">sys-net</property>
  290. <property name="provides_network">True</property>
  291. <property name="label" ref="label-1" />
  292. <property name="netvm"></property>
  293. <property name="uuid">2fcfc1f4-b2fe-4361-931a-c5294b35edfa</property>
  294. </properties>
  295. <features/>
  296. <devices class="pci"/>
  297. </domain>
  298. <domain class="StandaloneVM" id="domain-2">
  299. <properties>
  300. <property name="qid">2</property>
  301. <property name="name">sys-firewall</property>
  302. <property name="provides_network">True</property>
  303. <property name="label" ref="label-1" />
  304. <property name="uuid">9a6d9689-25f7-48c9-a15f-8205d6c5b7c6</property>
  305. </properties>
  306. </domain>
  307. <domain class="StandaloneVM" id="domain-3">
  308. <properties>
  309. <property name="qid">3</property>
  310. <property name="name">appvm</property>
  311. <property name="label" ref="label-1" />
  312. <property name="uuid">1d6aab41-3262-400a-b3d3-21aae8fdbec8</property>
  313. </properties>
  314. </domain>
  315. </domains>
  316. </qubes>
  317. '''
  318. with self.subTest('default_setup'):
  319. with open('/tmp/qubestest.xml', 'w') as xml_file:
  320. xml_file.write(xml_template.format(
  321. default_netvm='sys-firewall',
  322. default_fw_netvm='sys-net'))
  323. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  324. self.assertEqual(
  325. self.app.domains['sys-net'].netvm, None)
  326. self.assertEqual(
  327. self.app.domains['sys-firewall'].netvm, self.app.domains['sys-net'])
  328. # property is no longer "default"
  329. self.assertFalse(
  330. self.app.domains['sys-firewall'].property_is_default('netvm'))
  331. # verify that appvm.netvm is unaffected
  332. self.assertTrue(
  333. self.app.domains['appvm'].property_is_default('netvm'))
  334. self.assertEqual(
  335. self.app.domains['appvm'].netvm,
  336. self.app.domains['sys-firewall'])
  337. with self.assertRaises(AttributeError):
  338. self.app.default_fw_netvm
  339. self.app.close()
  340. del self.app
  341. with self.subTest('same'):
  342. with open('/tmp/qubestest.xml', 'w') as xml_file:
  343. xml_file.write(xml_template.format(
  344. default_netvm='sys-net',
  345. default_fw_netvm='sys-net'))
  346. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  347. self.assertEqual(
  348. self.app.domains['sys-net'].netvm, None)
  349. self.assertEqual(
  350. self.app.domains['sys-firewall'].netvm,
  351. self.app.domains['sys-net'])
  352. self.assertTrue(
  353. self.app.domains['sys-firewall'].property_is_default('netvm'))
  354. # verify that appvm.netvm is unaffected
  355. self.assertTrue(
  356. self.app.domains['appvm'].property_is_default('netvm'))
  357. self.assertEqual(
  358. self.app.domains['appvm'].netvm,
  359. self.app.domains['sys-net'])
  360. with self.assertRaises(AttributeError):
  361. self.app.default_fw_netvm
  362. with self.subTest('loop'):
  363. with open('/tmp/qubestest.xml', 'w') as xml_file:
  364. xml_file.write(xml_template.format(
  365. default_netvm='sys-firewall',
  366. default_fw_netvm='sys-firewall'))
  367. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  368. self.assertEqual(
  369. self.app.domains['sys-net'].netvm, None)
  370. # this was netvm loop, better set to none, to not crash qubesd
  371. self.assertEqual(
  372. self.app.domains['sys-firewall'].netvm, None)
  373. self.assertFalse(
  374. self.app.domains['sys-firewall'].property_is_default('netvm'))
  375. # verify that appvm.netvm is unaffected
  376. self.assertTrue(
  377. self.app.domains['appvm'].property_is_default('netvm'))
  378. self.assertEqual(
  379. self.app.domains['appvm'].netvm,
  380. self.app.domains['sys-firewall'])
  381. with self.assertRaises(AttributeError):
  382. self.app.default_fw_netvm
  383. class TC_90_Qubes(qubes.tests.QubesTestCase):
  384. def tearDown(self):
  385. try:
  386. os.unlink('/tmp/qubestest.xml')
  387. except:
  388. pass
  389. super().tearDown()
  390. def setUp(self):
  391. super(TC_90_Qubes, self).setUp()
  392. self.app = qubes.Qubes('/tmp/qubestest.xml', load=False,
  393. offline_mode=True)
  394. self.addCleanup(self.cleanup_qubes)
  395. self.app.load_initial_values()
  396. self.template = self.app.add_new_vm('TemplateVM', name='test-template',
  397. label='green')
  398. def cleanup_qubes(self):
  399. self.app.close()
  400. del self.app
  401. try:
  402. del self.template
  403. except AttributeError:
  404. pass
  405. def test_100_clockvm(self):
  406. appvm = self.app.add_new_vm('AppVM', name='test-vm', template=self.template,
  407. label='red')
  408. self.assertIsNone(self.app.clockvm)
  409. self.assertNotIn('service.clocksync', appvm.features)
  410. self.assertNotIn('service.clocksync', self.template.features)
  411. self.app.clockvm = appvm
  412. self.assertIn('service.clocksync', appvm.features)
  413. self.assertTrue(appvm.features['service.clocksync'])
  414. self.app.clockvm = self.template
  415. self.assertNotIn('service.clocksync', appvm.features)
  416. self.assertIn('service.clocksync', self.template.features)
  417. self.assertTrue(self.template.features['service.clocksync'])
  418. def test_110_netvm_loop(self):
  419. '''Netvm loop through default_netvm'''
  420. netvm = self.app.add_new_vm('AppVM', name='test-net',
  421. template=self.template, label='red')
  422. try:
  423. self.app.default_netvm = None
  424. netvm.netvm = qubes.property.DEFAULT
  425. with self.assertRaises(ValueError):
  426. self.app.default_netvm = netvm
  427. finally:
  428. del netvm
  429. def test_111_netvm_loop(self):
  430. '''Netvm loop through default_netvm'''
  431. netvm = self.app.add_new_vm('AppVM', name='test-net',
  432. template=self.template, label='red')
  433. try:
  434. netvm.netvm = None
  435. self.app.default_netvm = netvm
  436. with self.assertRaises(ValueError):
  437. netvm.netvm = qubes.property.DEFAULT
  438. finally:
  439. del netvm
  440. def test_200_remove_template(self):
  441. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  442. template=self.template,
  443. label='red')
  444. with mock.patch.object(self.app, 'vmm'):
  445. with self.assertRaises(qubes.exc.QubesException):
  446. del self.app.domains[self.template]
  447. def test_201_remove_netvm(self):
  448. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  449. template=self.template, provides_network=True,
  450. label='red')
  451. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  452. template=self.template,
  453. label='red')
  454. appvm.netvm = netvm
  455. with mock.patch.object(self.app, 'vmm'):
  456. with self.assertRaises(qubes.exc.QubesVMInUseError):
  457. del self.app.domains[netvm]
  458. def test_202_remove_default_netvm(self):
  459. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  460. template=self.template, provides_network=True,
  461. label='red')
  462. netvm.netvm = None
  463. self.app.default_netvm = netvm
  464. with mock.patch.object(self.app, 'vmm'):
  465. with self.assertRaises(qubes.exc.QubesVMInUseError):
  466. del self.app.domains[netvm]
  467. def test_203_remove_default_dispvm(self):
  468. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  469. template=self.template,
  470. label='red')
  471. self.app.default_dispvm = appvm
  472. with mock.patch.object(self.app, 'vmm'):
  473. with self.assertRaises(qubes.exc.QubesVMInUseError):
  474. del self.app.domains[appvm]
  475. def test_204_remove_appvm_dispvm(self):
  476. dispvm = self.app.add_new_vm('AppVM', name='test-appvm',
  477. template=self.template,
  478. label='red')
  479. appvm = self.app.add_new_vm('AppVM', name='test-appvm2',
  480. template=self.template, default_dispvm=dispvm,
  481. label='red')
  482. with mock.patch.object(self.app, 'vmm'):
  483. with self.assertRaises(qubes.exc.QubesVMInUseError):
  484. del self.app.domains[dispvm]
  485. def test_205_remove_appvm_dispvm(self):
  486. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  487. template=self.template, template_for_dispvms=True,
  488. label='red')
  489. dispvm = self.app.add_new_vm('DispVM', name='test-dispvm',
  490. template=appvm,
  491. label='red')
  492. with mock.patch.object(self.app, 'vmm'):
  493. with self.assertRaises(qubes.exc.QubesVMInUseError):
  494. del self.app.domains[appvm]
  495. @qubes.tests.skipUnlessGit
  496. def test_900_example_xml_in_doc(self):
  497. self.assertXMLIsValid(
  498. lxml.etree.parse(open(
  499. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  500. 'qubes.rng')