qubesvm.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. # pylint: disable=protected-access
  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 base64
  22. import os
  23. import unittest
  24. import uuid
  25. import datetime
  26. import lxml.etree
  27. import unittest.mock
  28. import shutil
  29. import qubes
  30. import qubes.exc
  31. import qubes.config
  32. import qubes.vm
  33. import qubes.vm.qubesvm
  34. import qubes.tests
  35. import qubes.tests.vm
  36. class TestApp(object):
  37. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  38. def __init__(self):
  39. self.domains = {}
  40. class TestProp(object):
  41. # pylint: disable=too-few-public-methods
  42. __name__ = 'testprop'
  43. class TestDeviceCollection(object):
  44. def __init__(self):
  45. self._list = []
  46. def persistent(self):
  47. return self._list
  48. class TestQubesDB(object):
  49. def __init__(self):
  50. self.data = {}
  51. def write(self, path, value):
  52. self.data[path] = value
  53. def rm(self, path):
  54. if path.endswith('/'):
  55. for key in [x for x in self.data if x.startswith(path)]:
  56. del self.data[key]
  57. else:
  58. self.data.pop(path, None)
  59. class TestVM(object):
  60. # pylint: disable=too-few-public-methods
  61. app = TestApp()
  62. def __init__(self, **kwargs):
  63. self.running = False
  64. self.installed_by_rpm = False
  65. for k, v in kwargs.items():
  66. setattr(self, k, v)
  67. self.devices = {'pci': TestDeviceCollection()}
  68. def is_running(self):
  69. return self.running
  70. class TC_00_setters(qubes.tests.QubesTestCase):
  71. def setUp(self):
  72. super().setUp()
  73. self.vm = TestVM()
  74. self.prop = TestProp()
  75. def test_000_setter_qid(self):
  76. self.assertEqual(
  77. qubes.vm._setter_qid(self.vm, self.prop, 5), 5)
  78. def test_001_setter_qid_lt_0(self):
  79. with self.assertRaises(ValueError):
  80. qubes.vm._setter_qid(self.vm, self.prop, -1)
  81. def test_002_setter_qid_gt_max(self):
  82. with self.assertRaises(ValueError):
  83. qubes.vm._setter_qid(self.vm,
  84. self.prop, qubes.config.max_qid + 5)
  85. @unittest.skip('test not implemented')
  86. def test_020_setter_kernel(self):
  87. pass
  88. def test_030_setter_label_object(self):
  89. label = TestApp.labels[1]
  90. self.assertIs(label,
  91. qubes.vm.setter_label(self.vm, self.prop, label))
  92. def test_031_setter_label_getitem(self):
  93. label = TestApp.labels[1]
  94. self.assertIs(label,
  95. qubes.vm.setter_label(self.vm, self.prop, 'label-1'))
  96. # there is no check for self.app.get_label()
  97. def test_040_setter_virt_mode(self):
  98. self.assertEqual(
  99. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'hvm'),
  100. 'hvm')
  101. self.assertEqual(
  102. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'HVM'),
  103. 'hvm')
  104. self.assertEqual(
  105. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'PV'),
  106. 'pv')
  107. self.assertEqual(
  108. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'pvh'),
  109. 'pvh')
  110. self.vm.devices['pci']._list.append(object())
  111. with self.assertRaises(ValueError):
  112. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'pvh')
  113. with self.assertRaises(ValueError):
  114. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'True')
  115. class QubesVMTestsMixin(object):
  116. property_no_default = object()
  117. def setUp(self):
  118. super(QubesVMTestsMixin, self).setUp()
  119. self.app = qubes.tests.vm.TestApp()
  120. self.app.vmm.offline_mode = True
  121. self.app.default_kernel = None
  122. # when full test run is called, extensions are loaded by earlier
  123. # tests, but if just this test class is run, load them manually here,
  124. # to have the same behaviour
  125. qubes.ext.get_extensions()
  126. def tearDown(self):
  127. try:
  128. self.app.domains.close()
  129. except AttributeError:
  130. pass
  131. super(QubesVMTestsMixin, self).tearDown()
  132. def get_vm(self, name='test', cls=qubes.vm.qubesvm.QubesVM, **kwargs):
  133. vm = cls(self.app, None,
  134. qid=kwargs.pop('qid', 1), name=qubes.tests.VMPREFIX + name,
  135. **kwargs)
  136. self.app.domains[vm.qid] = vm
  137. self.app.domains[vm.uuid] = vm
  138. self.app.domains[vm] = vm
  139. self.addCleanup(vm.close)
  140. return vm
  141. def assertPropertyValue(self, vm, prop_name, set_value, expected_value,
  142. expected_xml_content=None):
  143. # FIXME: any better exception list? or maybe all of that should be a
  144. # single exception?
  145. with self.assertNotRaises((ValueError, TypeError, KeyError)):
  146. setattr(vm, prop_name, set_value)
  147. self.assertEqual(getattr(vm, prop_name), expected_value)
  148. if expected_xml_content is not None:
  149. xml = vm.__xml__()
  150. prop_xml = xml.xpath(
  151. './properties/property[@name=\'{}\']'.format(prop_name))
  152. self.assertEqual(len(prop_xml), 1, "Property not found in XML")
  153. self.assertEqual(prop_xml[0].text, expected_xml_content)
  154. def assertPropertyInvalidValue(self, vm, prop_name, set_value):
  155. orig_value_set = True
  156. orig_value = None
  157. try:
  158. orig_value = getattr(vm, prop_name)
  159. except AttributeError:
  160. orig_value_set = False
  161. # FIXME: any better exception list? or maybe all of that should be a
  162. # single exception?
  163. with self.assertRaises((ValueError, TypeError, KeyError)):
  164. setattr(vm, prop_name, set_value)
  165. if orig_value_set:
  166. self.assertEqual(getattr(vm, prop_name), orig_value)
  167. else:
  168. with self.assertRaises(AttributeError):
  169. getattr(vm, prop_name)
  170. def assertPropertyDefaultValue(self, vm, prop_name,
  171. expected_default=property_no_default):
  172. if expected_default is self.property_no_default:
  173. with self.assertRaises(AttributeError):
  174. getattr(vm, prop_name)
  175. else:
  176. with self.assertNotRaises(AttributeError):
  177. self.assertEqual(getattr(vm, prop_name), expected_default)
  178. xml = vm.__xml__()
  179. prop_xml = xml.xpath(
  180. './properties/property[@name=\'{}\']'.format(prop_name))
  181. self.assertEqual(len(prop_xml), 0, "Property still found in XML")
  182. def _test_generic_bool_property(self, vm, prop_name, default=False):
  183. self.assertPropertyDefaultValue(vm, prop_name, default)
  184. self.assertPropertyValue(vm, prop_name, False, False, 'False')
  185. self.assertPropertyValue(vm, prop_name, True, True, 'True')
  186. delattr(vm, prop_name)
  187. self.assertPropertyDefaultValue(vm, prop_name, default)
  188. self.assertPropertyValue(vm, prop_name, 'True', True, 'True')
  189. self.assertPropertyValue(vm, prop_name, 'False', False, 'False')
  190. self.assertPropertyInvalidValue(vm, prop_name, 'xxx')
  191. self.assertPropertyValue(vm, prop_name, 123, True)
  192. self.assertPropertyInvalidValue(vm, prop_name, '')
  193. class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
  194. def test_000_init(self):
  195. self.get_vm()
  196. def test_001_init_no_qid_or_name(self):
  197. with self.assertRaises(AssertionError):
  198. qubes.vm.qubesvm.QubesVM(self.app, None,
  199. name=qubes.tests.VMPREFIX + 'test')
  200. with self.assertRaises(AssertionError):
  201. qubes.vm.qubesvm.QubesVM(self.app, None,
  202. qid=1)
  203. def test_003_init_fire_domain_init(self):
  204. class TestVM2(qubes.vm.qubesvm.QubesVM):
  205. event_fired = False
  206. @qubes.events.handler('domain-init')
  207. def on_domain_init(self, event): # pylint: disable=unused-argument
  208. self.__class__.event_fired = True
  209. TestVM2(self.app, None, qid=1, name=qubes.tests.VMPREFIX + 'test')
  210. self.assertTrue(TestVM2.event_fired)
  211. def test_004_uuid_autogen(self):
  212. vm = self.get_vm()
  213. self.assertTrue(hasattr(vm, 'uuid'))
  214. def test_100_qid(self):
  215. vm = self.get_vm()
  216. self.assertIsInstance(vm.qid, int)
  217. with self.assertRaises(AttributeError):
  218. vm.qid = 2
  219. def test_110_name(self):
  220. vm = self.get_vm()
  221. self.assertIsInstance(vm.name, str)
  222. def test_120_uuid(self):
  223. my_uuid = uuid.uuid4()
  224. vm = self.get_vm(uuid=my_uuid)
  225. self.assertIsInstance(vm.uuid, uuid.UUID)
  226. self.assertIs(vm.uuid, my_uuid)
  227. with self.assertRaises(AttributeError):
  228. vm.uuid = uuid.uuid4()
  229. @unittest.skip('TODO: how to not fail on making an icon symlink here?')
  230. def test_130_label(self):
  231. vm = self.get_vm()
  232. self.assertPropertyDefaultValue(vm, 'label')
  233. self.assertPropertyValue(vm, 'label', self.app.labels[1],
  234. self.app.labels[1], 'label-1')
  235. del vm.label
  236. self.assertPropertyDefaultValue(vm, 'label')
  237. self.assertPropertyValue(vm, 'label', 'red',
  238. self.app.labels[1], 'label-1')
  239. self.assertPropertyValue(vm, 'label', 'label-1',
  240. self.app.labels[1], 'label-1')
  241. def test_131_label_invalid(self):
  242. vm = self.get_vm()
  243. self.assertPropertyInvalidValue(vm, 'label', 'invalid')
  244. self.assertPropertyInvalidValue(vm, 'label', 123)
  245. def test_160_memory(self):
  246. vm = self.get_vm()
  247. self.assertPropertyDefaultValue(vm, 'memory', 400)
  248. self.assertPropertyValue(vm, 'memory', 500, 500, '500')
  249. del vm.memory
  250. self.assertPropertyDefaultValue(vm, 'memory', 400)
  251. self.assertPropertyValue(vm, 'memory', '500', 500, '500')
  252. def test_161_memory_invalid(self):
  253. vm = self.get_vm()
  254. self.assertPropertyInvalidValue(vm, 'memory', -100)
  255. self.assertPropertyInvalidValue(vm, 'memory', '-100')
  256. self.assertPropertyInvalidValue(vm, 'memory', '')
  257. # TODO: higher than maxmem
  258. # TODO: human readable setter (500M, 4G)?
  259. def test_170_maxmem(self):
  260. vm = self.get_vm()
  261. self.assertPropertyDefaultValue(vm, 'maxmem',
  262. self.app.host.memory_total / 1024 / 2)
  263. self.assertPropertyValue(vm, 'maxmem', 500, 500, '500')
  264. del vm.maxmem
  265. self.assertPropertyDefaultValue(vm, 'maxmem',
  266. self.app.host.memory_total / 1024 / 2)
  267. self.assertPropertyValue(vm, 'maxmem', '500', 500, '500')
  268. def test_171_maxmem_invalid(self):
  269. vm = self.get_vm()
  270. self.assertPropertyInvalidValue(vm, 'maxmem', -100)
  271. self.assertPropertyInvalidValue(vm, 'maxmem', '-100')
  272. self.assertPropertyInvalidValue(vm, 'maxmem', '')
  273. # TODO: lower than memory
  274. # TODO: human readable setter (500M, 4G)?
  275. def test_190_vcpus(self):
  276. vm = self.get_vm()
  277. self.assertPropertyDefaultValue(vm, 'vcpus', 2)
  278. self.assertPropertyValue(vm, 'vcpus', 3, 3, '3')
  279. del vm.vcpus
  280. self.assertPropertyDefaultValue(vm, 'vcpus', 2)
  281. self.assertPropertyValue(vm, 'vcpus', '3', 3, '3')
  282. def test_191_vcpus_invalid(self):
  283. vm = self.get_vm()
  284. self.assertPropertyInvalidValue(vm, 'vcpus', 0)
  285. self.assertPropertyInvalidValue(vm, 'vcpus', -2)
  286. self.assertPropertyInvalidValue(vm, 'vcpus', '-2')
  287. self.assertPropertyInvalidValue(vm, 'vcpus', '')
  288. def test_200_debug(self):
  289. vm = self.get_vm()
  290. self._test_generic_bool_property(vm, 'debug', False)
  291. def test_210_installed_by_rpm(self):
  292. vm = self.get_vm()
  293. self._test_generic_bool_property(vm, 'installed_by_rpm', False)
  294. def test_220_include_in_backups(self):
  295. vm = self.get_vm()
  296. self._test_generic_bool_property(vm, 'include_in_backups', True)
  297. @qubes.tests.skipUnlessDom0
  298. def test_250_kernel(self):
  299. kernels = os.listdir(os.path.join(
  300. qubes.config.qubes_base_dir,
  301. qubes.config.system_path['qubes_kernels_base_dir']))
  302. if not len(kernels):
  303. self.skipTest('Needs at least one kernel installed')
  304. self.app.default_kernel = kernels[0]
  305. vm = self.get_vm()
  306. self.assertPropertyDefaultValue(vm, 'kernel', kernels[0])
  307. self.assertPropertyValue(vm, 'kernel', kernels[-1], kernels[-1],
  308. kernels[-1])
  309. del vm.kernel
  310. self.assertPropertyDefaultValue(vm, 'kernel', kernels[0])
  311. @qubes.tests.skipUnlessDom0
  312. def test_251_kernel_invalid(self):
  313. vm = self.get_vm()
  314. self.assertPropertyInvalidValue(vm, 'kernel', 123)
  315. self.assertPropertyInvalidValue(vm, 'kernel', 'invalid')
  316. def test_252_kernel_empty(self):
  317. vm = self.get_vm()
  318. self.assertPropertyValue(vm, 'kernel', '', '', '')
  319. self.assertPropertyValue(vm, 'kernel', None, '', '')
  320. def test_260_kernelopts(self):
  321. vm = self.get_vm()
  322. self.assertPropertyDefaultValue(vm, 'kernelopts',
  323. qubes.config.defaults['kernelopts'])
  324. self.assertPropertyValue(vm, 'kernelopts', 'some options',
  325. 'some options', 'some options')
  326. del vm.kernelopts
  327. self.assertPropertyDefaultValue(vm, 'kernelopts',
  328. qubes.config.defaults['kernelopts'])
  329. self.assertPropertyValue(vm, 'kernelopts', '',
  330. '', '')
  331. # TODO?
  332. # self.assertPropertyInvalidValue(vm, 'kernelopts', None),
  333. @unittest.skip('test not implemented')
  334. def test_261_kernelopts_pcidevs(self):
  335. vm = self.get_vm()
  336. # how to do that here? use dummy DeviceManager/DeviceCollection?
  337. # Disable events?
  338. vm.devices['pci'].attach('something')
  339. self.assertPropertyDefaultValue(vm, 'kernelopts',
  340. qubes.config.defaults['kernelopts_pcidevs'])
  341. def test_270_qrexec_timeout(self):
  342. vm = self.get_vm()
  343. self.assertPropertyDefaultValue(vm, 'qrexec_timeout', 60)
  344. self.assertPropertyValue(vm, 'qrexec_timeout', 3, 3, '3')
  345. del vm.qrexec_timeout
  346. self.assertPropertyDefaultValue(vm, 'qrexec_timeout', 60)
  347. self.assertPropertyValue(vm, 'qrexec_timeout', '3', 3, '3')
  348. def test_271_qrexec_timeout_invalid(self):
  349. vm = self.get_vm()
  350. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', -2)
  351. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', '-2')
  352. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', '')
  353. def test_280_autostart(self):
  354. vm = self.get_vm()
  355. # FIXME any better idea to not involve systemctl call at this stage?
  356. vm.events_enabled = False
  357. self._test_generic_bool_property(vm, 'autostart', False)
  358. @qubes.tests.skipUnlessDom0
  359. def test_281_autostart_systemd(self):
  360. vm = self.get_vm()
  361. self.assertFalse(os.path.exists(
  362. '/etc/systemd/system/multi-user.target.wants/'
  363. 'qubes-vm@{}.service'.format(vm.name)),
  364. "systemd service enabled before setting autostart")
  365. vm.autostart = True
  366. self.assertTrue(os.path.exists(
  367. '/etc/systemd/system/multi-user.target.wants/'
  368. 'qubes-vm@{}.service'.format(vm.name)),
  369. "systemd service not enabled by autostart=True")
  370. vm.autostart = False
  371. self.assertFalse(os.path.exists(
  372. '/etc/systemd/system/multi-user.target.wants/'
  373. 'qubes-vm@{}.service'.format(vm.name)),
  374. "systemd service not disabled by autostart=False")
  375. vm.autostart = True
  376. del vm.autostart
  377. self.assertFalse(os.path.exists(
  378. '/etc/systemd/system/multi-user.target.wants/'
  379. 'qubes-vm@{}.service'.format(vm.name)),
  380. "systemd service not disabled by resetting autostart")
  381. @unittest.skip('TODO')
  382. def test_320_seamless_gui_mode(self):
  383. vm = self.get_vm()
  384. self._test_generic_bool_property(vm, 'seamless_gui_mode')
  385. # TODO: reject setting to True when guiagent_installed is false
  386. def test_330_mac(self):
  387. vm = self.get_vm()
  388. # TODO: calculate proper default here
  389. default_mac = vm.mac
  390. self.assertIsNotNone(default_mac)
  391. self.assertPropertyDefaultValue(vm, 'mac', default_mac)
  392. self.assertPropertyValue(vm, 'mac', '00:11:22:33:44:55',
  393. '00:11:22:33:44:55', '00:11:22:33:44:55')
  394. del vm.mac
  395. self.assertPropertyDefaultValue(vm, 'mac', default_mac)
  396. def test_331_mac_invalid(self):
  397. vm = self.get_vm()
  398. self.assertPropertyInvalidValue(vm, 'mac', 123)
  399. self.assertPropertyInvalidValue(vm, 'mac', 'invalid')
  400. self.assertPropertyInvalidValue(vm, 'mac', '00:11:22:33:44:55:66')
  401. def test_340_default_user(self):
  402. vm = self.get_vm()
  403. self.assertPropertyDefaultValue(vm, 'default_user', 'user')
  404. self.assertPropertyValue(vm, 'default_user', 'someuser', 'someuser',
  405. 'someuser')
  406. del vm.default_user
  407. self.assertPropertyDefaultValue(vm, 'default_user', 'user')
  408. self.assertPropertyValue(vm, 'default_user', 123, '123', '123')
  409. vm.default_user = 'user'
  410. # TODO: check propagation for template-based VMs
  411. @unittest.skip('TODO')
  412. def test_350_timezone(self):
  413. vm = self.get_vm()
  414. self.assertPropertyDefaultValue(vm, 'timezone', 'localtime')
  415. self.assertPropertyValue(vm, 'timezone', 0, 0, '0')
  416. del vm.timezone
  417. self.assertPropertyDefaultValue(vm, 'timezone', 'localtime')
  418. self.assertPropertyValue(vm, 'timezone', '0', 0, '0')
  419. self.assertPropertyValue(vm, 'timezone', -3600, -3600, '-3600')
  420. self.assertPropertyValue(vm, 'timezone', 7200, 7200, '7200')
  421. @unittest.skip('TODO')
  422. def test_350_timezone_invalid(self):
  423. vm = self.get_vm()
  424. self.assertPropertyInvalidValue(vm, 'timezone', 'xxx')
  425. @unittest.skip('TODO')
  426. def test_360_drive(self):
  427. vm = self.get_vm()
  428. self.assertPropertyDefaultValue(vm, 'drive', None)
  429. # self.execute_tests('drive', [
  430. # ('hd:dom0:/tmp/drive.img', 'hd:dom0:/tmp/drive.img', True),
  431. # ('hd:/tmp/drive.img', 'hd:dom0:/tmp/drive.img', True),
  432. # ('cdrom:dom0:/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  433. # ('cdrom:/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  434. # ('/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  435. # ('hd:drive.img', '', False),
  436. # ('drive.img', '', False),
  437. # ])
  438. def test_400_backup_timestamp(self):
  439. vm = self.get_vm()
  440. timestamp = datetime.datetime(2016, 1, 1, 12, 14, 2)
  441. timestamp_str = timestamp.strftime('%s')
  442. self.assertPropertyDefaultValue(vm, 'backup_timestamp', None)
  443. self.assertPropertyValue(vm, 'backup_timestamp', int(timestamp_str),
  444. int(timestamp_str), timestamp_str)
  445. del vm.backup_timestamp
  446. self.assertPropertyDefaultValue(vm, 'backup_timestamp', None)
  447. self.assertPropertyValue(vm, 'backup_timestamp', timestamp_str,
  448. int(timestamp_str))
  449. def test_401_backup_timestamp_invalid(self):
  450. vm = self.get_vm()
  451. self.assertPropertyInvalidValue(vm, 'backup_timestamp', 'xxx')
  452. self.assertPropertyInvalidValue(vm, 'backup_timestamp', None)
  453. def test_500_property_migrate_virt_mode(self):
  454. xml_template = '''
  455. <domain class="QubesVM" id="domain-1">
  456. <properties>
  457. <property name="qid">1</property>
  458. <property name="name">testvm</property>
  459. <property name="label" ref="label-1" />
  460. <property name="hvm">{hvm_value}</property>
  461. </properties>
  462. </domain>
  463. '''
  464. xml = lxml.etree.XML(xml_template.format(hvm_value='True'))
  465. vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
  466. self.assertEqual(vm.virt_mode, 'hvm')
  467. with self.assertRaises(AttributeError):
  468. vm.hvm
  469. xml = lxml.etree.XML(xml_template.format(hvm_value='False'))
  470. vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
  471. self.assertEqual(vm.virt_mode, 'pv')
  472. with self.assertRaises(AttributeError):
  473. vm.hvm
  474. def test_600_libvirt_xml_pv(self):
  475. expected = '''<domain type="xen">
  476. <name>test-inst-test</name>
  477. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  478. <memory unit="MiB">500</memory>
  479. <currentMemory unit="MiB">400</currentMemory>
  480. <vcpu placement="static">2</vcpu>
  481. <os>
  482. <type arch="x86_64" machine="xenpv">linux</type>
  483. <kernel>/tmp/kernel/vmlinuz</kernel>
  484. <initrd>/tmp/kernel/initramfs</initrd>
  485. <cmdline>root=/dev/mapper/dmroot ro nomodeset console=hvc0 rd_NO_PLYMOUTH rd.plymouth.enable=0 plymouth.enable=0 nopat</cmdline>
  486. </os>
  487. <features>
  488. </features>
  489. <clock offset='utc' adjustment='reset'>
  490. <timer name="tsc" mode="native"/>
  491. </clock>
  492. <on_poweroff>destroy</on_poweroff>
  493. <on_reboot>destroy</on_reboot>
  494. <on_crash>destroy</on_crash>
  495. <devices>
  496. <disk type="block" device="disk">
  497. <driver name="phy" />
  498. <source dev="/tmp/kernel/modules.img" />
  499. <target dev="xvdd" />
  500. <backenddomain name="dom0" />
  501. </disk>
  502. <console type="pty">
  503. <target type="xen" port="0"/>
  504. </console>
  505. </devices>
  506. </domain>
  507. '''
  508. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  509. vm = self.get_vm(uuid=my_uuid)
  510. vm.netvm = None
  511. vm.virt_mode = 'pv'
  512. with unittest.mock.patch('qubes.config.qubes_base_dir',
  513. '/tmp/qubes-test'):
  514. kernel_dir = '/tmp/qubes-test/vm-kernels/dummy'
  515. os.makedirs(kernel_dir, exist_ok=True)
  516. open(os.path.join(kernel_dir, 'vmlinuz'), 'w').close()
  517. open(os.path.join(kernel_dir, 'initramfs'), 'w').close()
  518. self.addCleanup(shutil.rmtree, '/tmp/qubes-test')
  519. vm.kernel = 'dummy'
  520. # tests for storage are later
  521. vm.volumes['kernel'] = unittest.mock.Mock(**{
  522. 'kernels_dir': '/tmp/kernel',
  523. 'block_device.return_value.domain': 'dom0',
  524. 'block_device.return_value.script': None,
  525. 'block_device.return_value.path': '/tmp/kernel/modules.img',
  526. 'block_device.return_value.devtype': 'disk',
  527. 'block_device.return_value.name': 'kernel',
  528. })
  529. libvirt_xml = vm.create_config_file()
  530. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  531. lxml.etree.XML(expected))
  532. def test_600_libvirt_xml_hvm(self):
  533. expected = '''<domain type="xen">
  534. <name>test-inst-test</name>
  535. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  536. <memory unit="MiB">500</memory>
  537. <currentMemory unit="MiB">400</currentMemory>
  538. <vcpu placement="static">2</vcpu>
  539. <cpu mode='host-passthrough'>
  540. <!-- disable nested HVM -->
  541. <feature name='vmx' policy='disable'/>
  542. <feature name='svm' policy='disable'/>
  543. <!-- disable SMAP inside VM, because of Linux bug -->
  544. <feature name='smap' policy='disable'/>
  545. </cpu>
  546. <os>
  547. <type arch="x86_64" machine="xenfv">hvm</type>
  548. <!--
  549. For the libxl backend libvirt switches between OVMF (UEFI)
  550. and SeaBIOS based on the loader type. This has nothing to
  551. do with the hvmloader binary.
  552. -->
  553. <loader type="rom">hvmloader</loader>
  554. <boot dev="cdrom" />
  555. <boot dev="hd" />
  556. </os>
  557. <features>
  558. <pae/>
  559. <acpi/>
  560. <apic/>
  561. <viridian/>
  562. </features>
  563. <clock offset="variable" adjustment="0" basis="localtime" />
  564. <on_poweroff>destroy</on_poweroff>
  565. <on_reboot>destroy</on_reboot>
  566. <on_crash>destroy</on_crash>
  567. <devices>
  568. <!-- server_ip is the address of stubdomain. It hosts it's own DNS server. -->
  569. <emulator type="stubdom-linux" />
  570. <input type="tablet" bus="usb"/>
  571. <video>
  572. <model type="vga"/>
  573. </video>
  574. <graphics type="qubes"/>
  575. </devices>
  576. </domain>
  577. '''
  578. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  579. vm = self.get_vm(uuid=my_uuid)
  580. vm.netvm = None
  581. vm.virt_mode = 'hvm'
  582. libvirt_xml = vm.create_config_file()
  583. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  584. lxml.etree.XML(expected))
  585. def test_600_libvirt_xml_hvm_dom0_kernel(self):
  586. expected = '''<domain type="xen">
  587. <name>test-inst-test</name>
  588. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  589. <memory unit="MiB">500</memory>
  590. <currentMemory unit="MiB">400</currentMemory>
  591. <vcpu placement="static">2</vcpu>
  592. <cpu mode='host-passthrough'>
  593. <!-- disable nested HVM -->
  594. <feature name='vmx' policy='disable'/>
  595. <feature name='svm' policy='disable'/>
  596. <!-- disable SMAP inside VM, because of Linux bug -->
  597. <feature name='smap' policy='disable'/>
  598. </cpu>
  599. <os>
  600. <type arch="x86_64" machine="xenfv">hvm</type>
  601. <!--
  602. For the libxl backend libvirt switches between OVMF (UEFI)
  603. and SeaBIOS based on the loader type. This has nothing to
  604. do with the hvmloader binary.
  605. -->
  606. <loader type="rom">hvmloader</loader>
  607. <boot dev="cdrom" />
  608. <boot dev="hd" />
  609. <cmdline>root=/dev/mapper/dmroot ro nomodeset console=hvc0 rd_NO_PLYMOUTH rd.plymouth.enable=0 plymouth.enable=0 nopat</cmdline>
  610. </os>
  611. <features>
  612. <pae/>
  613. <acpi/>
  614. <apic/>
  615. <viridian/>
  616. </features>
  617. <clock offset="variable" adjustment="0" basis="localtime" />
  618. <on_poweroff>destroy</on_poweroff>
  619. <on_reboot>destroy</on_reboot>
  620. <on_crash>destroy</on_crash>
  621. <devices>
  622. <!-- server_ip is the address of stubdomain. It hosts it's own DNS server. -->
  623. <emulator type="stubdom-linux" />
  624. <input type="tablet" bus="usb"/>
  625. <video>
  626. <model type="vga"/>
  627. </video>
  628. <graphics type="qubes"/>
  629. </devices>
  630. </domain>
  631. '''
  632. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  633. vm = self.get_vm(uuid=my_uuid)
  634. vm.netvm = None
  635. vm.virt_mode = 'hvm'
  636. with unittest.mock.patch('qubes.config.qubes_base_dir',
  637. '/tmp/qubes-test'):
  638. kernel_dir = '/tmp/qubes-test/vm-kernels/dummy'
  639. os.makedirs(kernel_dir, exist_ok=True)
  640. open(os.path.join(kernel_dir, 'vmlinuz'), 'w').close()
  641. open(os.path.join(kernel_dir, 'initramfs'), 'w').close()
  642. self.addCleanup(shutil.rmtree, '/tmp/qubes-test')
  643. vm.kernel = 'dummy'
  644. libvirt_xml = vm.create_config_file()
  645. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  646. lxml.etree.XML(expected))
  647. def test_600_libvirt_xml_pvh(self):
  648. expected = '''<domain type="xen">
  649. <name>test-inst-test</name>
  650. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  651. <memory unit="MiB">500</memory>
  652. <currentMemory unit="MiB">400</currentMemory>
  653. <vcpu placement="static">2</vcpu>
  654. <cpu mode='host-passthrough'>
  655. <!-- disable nested HVM -->
  656. <feature name='vmx' policy='disable'/>
  657. <feature name='svm' policy='disable'/>
  658. <!-- disable SMAP inside VM, because of Linux bug -->
  659. <feature name='smap' policy='disable'/>
  660. </cpu>
  661. <os>
  662. <type arch="x86_64" machine="xenfv">pvh</type>
  663. <kernel>/tmp/kernel/vmlinuz</kernel>
  664. <initrd>/tmp/kernel/initramfs</initrd>
  665. <cmdline>root=/dev/mapper/dmroot ro nomodeset console=hvc0 rd_NO_PLYMOUTH rd.plymouth.enable=0 plymouth.enable=0 nopat</cmdline>
  666. </os>
  667. <features>
  668. <pae/>
  669. <acpi/>
  670. <apic/>
  671. <viridian/>
  672. </features>
  673. <clock offset='utc' adjustment='reset'>
  674. <timer name="tsc" mode="native"/>
  675. </clock>
  676. <on_poweroff>destroy</on_poweroff>
  677. <on_reboot>destroy</on_reboot>
  678. <on_crash>destroy</on_crash>
  679. <devices>
  680. <disk type="block" device="disk">
  681. <driver name="phy" />
  682. <source dev="/tmp/kernel/modules.img" />
  683. <target dev="xvdd" />
  684. <backenddomain name="dom0" />
  685. </disk>
  686. <console type="pty">
  687. <target type="xen" port="0"/>
  688. </console>
  689. </devices>
  690. </domain>
  691. '''
  692. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  693. vm = self.get_vm(uuid=my_uuid)
  694. vm.netvm = None
  695. vm.virt_mode = 'pvh'
  696. with unittest.mock.patch('qubes.config.qubes_base_dir',
  697. '/tmp/qubes-test'):
  698. kernel_dir = '/tmp/qubes-test/vm-kernels/dummy'
  699. os.makedirs(kernel_dir, exist_ok=True)
  700. open(os.path.join(kernel_dir, 'vmlinuz'), 'w').close()
  701. open(os.path.join(kernel_dir, 'initramfs'), 'w').close()
  702. self.addCleanup(shutil.rmtree, '/tmp/qubes-test')
  703. vm.kernel = 'dummy'
  704. # tests for storage are later
  705. vm.volumes['kernel'] = unittest.mock.Mock(**{
  706. 'kernels_dir': '/tmp/kernel',
  707. 'block_device.return_value.domain': 'dom0',
  708. 'block_device.return_value.script': None,
  709. 'block_device.return_value.path': '/tmp/kernel/modules.img',
  710. 'block_device.return_value.devtype': 'disk',
  711. 'block_device.return_value.name': 'kernel',
  712. })
  713. libvirt_xml = vm.create_config_file()
  714. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  715. lxml.etree.XML(expected))
  716. def test_610_libvirt_xml_network(self):
  717. expected = '''<domain type="xen">
  718. <name>test-inst-test</name>
  719. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  720. <memory unit="MiB">500</memory>
  721. <currentMemory unit="MiB">400</currentMemory>
  722. <vcpu placement="static">2</vcpu>
  723. <cpu mode='host-passthrough'>
  724. <!-- disable nested HVM -->
  725. <feature name='vmx' policy='disable'/>
  726. <feature name='svm' policy='disable'/>
  727. <!-- disable SMAP inside VM, because of Linux bug -->
  728. <feature name='smap' policy='disable'/>
  729. </cpu>
  730. <os>
  731. <type arch="x86_64" machine="xenfv">hvm</type>
  732. <!--
  733. For the libxl backend libvirt switches between OVMF (UEFI)
  734. and SeaBIOS based on the loader type. This has nothing to
  735. do with the hvmloader binary.
  736. -->
  737. <loader type="rom">hvmloader</loader>
  738. <boot dev="cdrom" />
  739. <boot dev="hd" />
  740. </os>
  741. <features>
  742. <pae/>
  743. <acpi/>
  744. <apic/>
  745. <viridian/>
  746. </features>
  747. <clock offset="variable" adjustment="0" basis="localtime" />
  748. <on_poweroff>destroy</on_poweroff>
  749. <on_reboot>destroy</on_reboot>
  750. <on_crash>destroy</on_crash>
  751. <devices>
  752. <interface type="ethernet">
  753. <mac address="00:16:3E:5E:6C:00" />
  754. <ip address="10.137.0.1" />
  755. {extra_ip}
  756. <backenddomain name="test-inst-netvm" />
  757. <script path="vif-route-qubes" />
  758. </interface>
  759. <!-- server_ip is the address of stubdomain. It hosts it's own DNS server. -->
  760. <emulator type="stubdom-linux" />
  761. <input type="tablet" bus="usb"/>
  762. <video>
  763. <model type="vga"/>
  764. </video>
  765. <graphics type="qubes"/>
  766. </devices>
  767. </domain>
  768. '''
  769. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  770. netvm = self.get_vm(qid=2, name='netvm', provides_network=True)
  771. vm = self.get_vm(uuid=my_uuid)
  772. vm.netvm = netvm
  773. vm.virt_mode = 'hvm'
  774. with self.subTest('ipv4_only'):
  775. libvirt_xml = vm.create_config_file()
  776. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  777. lxml.etree.XML(expected.format(extra_ip='')))
  778. with self.subTest('ipv6'):
  779. netvm.features['ipv6'] = True
  780. libvirt_xml = vm.create_config_file()
  781. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  782. lxml.etree.XML(expected.format(
  783. extra_ip='<ip address="{}::a89:1" family=\'ipv6\'/>'.format(
  784. qubes.config.qubes_ipv6_prefix.replace(':0000', '')))))
  785. @unittest.mock.patch('qubes.utils.get_timezone')
  786. @unittest.mock.patch('qubes.utils.urandom')
  787. @unittest.mock.patch('qubes.vm.qubesvm.QubesVM.untrusted_qdb')
  788. def test_620_qdb_standalone(self, mock_qubesdb, mock_urandom,
  789. mock_timezone):
  790. mock_urandom.return_value = b'A' * 64
  791. mock_timezone.return_value = 'UTC'
  792. vm = self.get_vm(cls=qubes.vm.standalonevm.StandaloneVM)
  793. vm.netvm = None
  794. vm.events_enabled = True
  795. test_qubesdb = TestQubesDB()
  796. mock_qubesdb.write.side_effect = test_qubesdb.write
  797. mock_qubesdb.rm.side_effect = test_qubesdb.rm
  798. vm.create_qdb_entries()
  799. self.maxDiff = None
  800. iptables_header = (
  801. '# Generated by Qubes Core on {}\n'
  802. '*filter\n'
  803. ':INPUT DROP [0:0]\n'
  804. ':FORWARD DROP [0:0]\n'
  805. ':OUTPUT ACCEPT [0:0]\n'
  806. '-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n'
  807. '-A INPUT -m conntrack --ctstate '
  808. 'RELATED,ESTABLISHED -j ACCEPT\n'
  809. '-A INPUT -p icmp -j ACCEPT\n'
  810. '-A INPUT -i lo -j ACCEPT\n'
  811. '-A INPUT -j REJECT --reject-with '
  812. 'icmp-host-prohibited\n'
  813. '-A FORWARD -m conntrack --ctstate '
  814. 'RELATED,ESTABLISHED -j ACCEPT\n'
  815. '-A FORWARD -i vif+ -o vif+ -j DROP\n'
  816. 'COMMIT\n'.format(datetime.datetime.now().ctime()))
  817. self.assertEqual(test_qubesdb.data, {
  818. '/name': 'test-inst-test',
  819. '/type': 'StandaloneVM',
  820. '/default-user': 'user',
  821. '/qubes-vm-type': 'AppVM',
  822. '/qubes-debug-mode': '0',
  823. '/qubes-base-template': '',
  824. '/qubes-timezone': 'UTC',
  825. '/qubes-random-seed': base64.b64encode(b'A' * 64),
  826. '/qubes-vm-persistence': 'full',
  827. '/qubes-vm-updateable': 'True',
  828. '/qubes-block-devices': '',
  829. '/qubes-usb-devices': '',
  830. '/qubes-iptables': 'reload',
  831. '/qubes-iptables-error': '',
  832. '/qubes-iptables-header': iptables_header,
  833. '/qubes-service/qubes-update-check': '0',
  834. })
  835. @unittest.mock.patch('qubes.utils.get_timezone')
  836. @unittest.mock.patch('qubes.utils.urandom')
  837. @unittest.mock.patch('qubes.vm.qubesvm.QubesVM.untrusted_qdb')
  838. def test_621_qdb_vm_with_network(self, mock_qubesdb, mock_urandom,
  839. mock_timezone):
  840. mock_urandom.return_value = b'A' * 64
  841. mock_timezone.return_value = 'UTC'
  842. template = self.get_vm(cls=qubes.vm.templatevm.TemplateVM, name='template')
  843. template.netvm = None
  844. netvm = self.get_vm(cls=qubes.vm.appvm.AppVM, template=template,
  845. name='netvm', qid=2, provides_network=True)
  846. vm = self.get_vm(cls=qubes.vm.appvm.AppVM, template=template,
  847. name='appvm', qid=3)
  848. vm.netvm = netvm
  849. vm.kernel = None
  850. # pretend the VM is running...
  851. vm._qubesprop_xid = 3
  852. netvm.kernel = None
  853. test_qubesdb = TestQubesDB()
  854. mock_qubesdb.write.side_effect = test_qubesdb.write
  855. mock_qubesdb.rm.side_effect = test_qubesdb.rm
  856. self.maxDiff = None
  857. iptables_header = (
  858. '# Generated by Qubes Core on {}\n'
  859. '*filter\n'
  860. ':INPUT DROP [0:0]\n'
  861. ':FORWARD DROP [0:0]\n'
  862. ':OUTPUT ACCEPT [0:0]\n'
  863. '-A INPUT -i vif+ -p udp -m udp --dport 68 -j DROP\n'
  864. '-A INPUT -m conntrack --ctstate '
  865. 'RELATED,ESTABLISHED -j ACCEPT\n'
  866. '-A INPUT -p icmp -j ACCEPT\n'
  867. '-A INPUT -i lo -j ACCEPT\n'
  868. '-A INPUT -j REJECT --reject-with '
  869. 'icmp-host-prohibited\n'
  870. '-A FORWARD -m conntrack --ctstate '
  871. 'RELATED,ESTABLISHED -j ACCEPT\n'
  872. '-A FORWARD -i vif+ -o vif+ -j DROP\n'
  873. 'COMMIT\n'.format(datetime.datetime.now().ctime()))
  874. expected = {
  875. '/name': 'test-inst-appvm',
  876. '/type': 'AppVM',
  877. '/default-user': 'user',
  878. '/qubes-vm-type': 'AppVM',
  879. '/qubes-debug-mode': '0',
  880. '/qubes-base-template': 'test-inst-template',
  881. '/qubes-timezone': 'UTC',
  882. '/qubes-random-seed': base64.b64encode(b'A' * 64),
  883. '/qubes-vm-persistence': 'rw-only',
  884. '/qubes-vm-updateable': 'False',
  885. '/qubes-block-devices': '',
  886. '/qubes-usb-devices': '',
  887. '/qubes-iptables': 'reload',
  888. '/qubes-iptables-error': '',
  889. '/qubes-iptables-header': iptables_header,
  890. '/qubes-service/qubes-update-check': '0',
  891. '/qubes-ip': '10.137.0.3',
  892. '/qubes-netmask': '255.255.255.255',
  893. '/qubes-gateway': '10.137.0.2',
  894. '/qubes-primary-dns': '10.139.1.1',
  895. '/qubes-secondary-dns': '10.139.1.2',
  896. }
  897. with self.subTest('ipv4'):
  898. vm.create_qdb_entries()
  899. self.assertEqual(test_qubesdb.data, expected)
  900. test_qubesdb.data.clear()
  901. with self.subTest('ipv6'):
  902. netvm.features['ipv6'] = True
  903. expected['/qubes-ip6'] = \
  904. qubes.config.qubes_ipv6_prefix.replace(':0000', '') + \
  905. '::a89:3'
  906. expected['/qubes-gateway6'] = 'fe80::fcff:ffff:feff:ffff'
  907. vm.create_qdb_entries()
  908. self.assertEqual(test_qubesdb.data, expected)
  909. test_qubesdb.data.clear()
  910. with self.subTest('ipv6_just_appvm'):
  911. del netvm.features['ipv6']
  912. vm.features['ipv6'] = True
  913. expected['/qubes-ip6'] = \
  914. qubes.config.qubes_ipv6_prefix.replace(':0000', '') + \
  915. '::a89:3'
  916. del expected['/qubes-gateway6']
  917. vm.create_qdb_entries()
  918. self.assertEqual(test_qubesdb.data, expected)
  919. test_qubesdb.data.clear()
  920. with self.subTest('proxy_ipv4'):
  921. del vm.features['ipv6']
  922. expected['/name'] = 'test-inst-netvm'
  923. expected['/qubes-vm-type'] = 'NetVM'
  924. del expected['/qubes-ip']
  925. del expected['/qubes-gateway']
  926. del expected['/qubes-netmask']
  927. del expected['/qubes-ip6']
  928. del expected['/qubes-primary-dns']
  929. del expected['/qubes-secondary-dns']
  930. expected['/qubes-netvm-primary-dns'] = '10.139.1.1'
  931. expected['/qubes-netvm-secondary-dns'] = '10.139.1.2'
  932. expected['/qubes-netvm-network'] = '10.137.0.2'
  933. expected['/qubes-netvm-gateway'] = '10.137.0.2'
  934. expected['/qubes-netvm-netmask'] = '255.255.255.255'
  935. expected['/qubes-iptables-domainrules/3'] = \
  936. '*filter\n' \
  937. '-A FORWARD -s 10.137.0.3 -j ACCEPT\n' \
  938. '-A FORWARD -s 10.137.0.3 -j DROP\n' \
  939. 'COMMIT\n'
  940. expected['/mapped-ip/10.137.0.3/visible-ip'] = '10.137.0.3'
  941. expected['/mapped-ip/10.137.0.3/visible-gateway'] = '10.137.0.2'
  942. expected['/qubes-firewall/10.137.0.3'] = ''
  943. expected['/qubes-firewall/10.137.0.3/0000'] = 'action=accept'
  944. expected['/qubes-firewall/10.137.0.3/policy'] = 'drop'
  945. with unittest.mock.patch('qubes.vm.qubesvm.QubesVM.is_running',
  946. lambda _: True):
  947. netvm.create_qdb_entries()
  948. self.assertEqual(test_qubesdb.data, expected)
  949. test_qubesdb.data.clear()
  950. with self.subTest('proxy_ipv6'):
  951. netvm.features['ipv6'] = True
  952. expected['/qubes-netvm-gateway6'] = 'fe80::fcff:ffff:feff:ffff'
  953. ip6 = qubes.config.qubes_ipv6_prefix.replace(
  954. ':0000', '') + '::a89:3'
  955. expected['/qubes-firewall/' + ip6] = ''
  956. expected['/qubes-firewall/' + ip6 + '/0000'] = 'action=accept'
  957. expected['/qubes-firewall/' + ip6 + '/policy'] = 'drop'
  958. with unittest.mock.patch('qubes.vm.qubesvm.QubesVM.is_running',
  959. lambda _: True):
  960. netvm.create_qdb_entries()
  961. self.assertEqual(test_qubesdb.data, expected)