init.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 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, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import lxml.etree
  23. import qubes
  24. import qubes.events
  25. import qubes.vm
  26. import qubes.tests
  27. class TestVMM(object):
  28. def __init__(self):
  29. super(TestVMM, self).__init__()
  30. self.offline_mode = True
  31. class TestApp(object):
  32. def __init__(self):
  33. super(TestApp, self).__init__()
  34. self.domains = {}
  35. self.vmm = TestVMM()
  36. class TestVM(qubes.vm.BaseVM):
  37. qid = qubes.property('qid', type=int)
  38. name = qubes.property('name')
  39. testprop = qubes.property('testprop')
  40. testlabel = qubes.property('testlabel')
  41. defaultprop = qubes.property('defaultprop', default='defaultvalue')
  42. class TC_10_BaseVM(qubes.tests.QubesTestCase):
  43. def setUp(self):
  44. self.xml = lxml.etree.XML('''
  45. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  46. <labels>
  47. <label id="label-1" color="#cc0000">red</label>
  48. </labels>
  49. <domains>
  50. <domain id="domain-1" class="TestVM">
  51. <properties>
  52. <property name="qid">1</property>
  53. <property name="name">domain1</property>
  54. <property name="testprop">testvalue</property>
  55. <property name="testlabel" ref="label-1" />
  56. </properties>
  57. <tags>
  58. <tag name="testtag">tagvalue</tag>
  59. </tags>
  60. <features>
  61. <feature name="testfeature_empty"></feature>
  62. <feature name="testfeature_aqq">aqq</feature>
  63. </features>
  64. <devices class="pci">
  65. <device backend-domain="domain1" id="00:11.22"/>
  66. </devices>
  67. <devices class="usb" />
  68. <devices class="audio-in" />
  69. <devices class="firewire" />
  70. <devices class="i2c" />
  71. <devices class="isa" />
  72. </domain>
  73. </domains>
  74. </qubes>
  75. ''')
  76. def test_000_load(self):
  77. node = self.xml.xpath('//domain')[0]
  78. vm = TestVM(TestApp(), node)
  79. vm.app.domains['domain1'] = vm
  80. vm.load_properties(load_stage=None)
  81. vm.load_extras()
  82. self.assertEqual(vm.qid, 1)
  83. self.assertEqual(vm.testprop, 'testvalue')
  84. self.assertEqual(vm.testprop, 'testvalue')
  85. self.assertEqual(vm.testlabel, 'label-1')
  86. self.assertEqual(vm.defaultprop, 'defaultvalue')
  87. self.assertEqual(vm.tags, {'testtag': 'tagvalue'})
  88. self.assertEqual(vm.features, {
  89. 'testfeature_empty': '',
  90. 'testfeature_aqq': 'aqq',
  91. })
  92. self.assertCountEqual(vm.devices.keys(), ('pci',))
  93. self.assertCountEqual(list(vm.devices['pci'].persistent()),
  94. [qubes.ext.pci.PCIDevice(vm, '00:11.22')])
  95. self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
  96. def test_001_nxproperty(self):
  97. xml = lxml.etree.XML('''
  98. <qubes version="3">
  99. <domains>
  100. <domain id="domain-1" class="TestVM">
  101. <properties>
  102. <property name="qid">1</property>
  103. <property name="name">domain1</property>
  104. <property name="nxproperty">nxvalue</property>
  105. </properties>
  106. </domain>
  107. </domains>
  108. </qubes>
  109. ''')
  110. node = xml.xpath('//domain')[0]
  111. with self.assertRaises(TypeError):
  112. TestVM(None, node)
  113. def test_002_save_nxproperty(self):
  114. vm = TestVM(None, None, qid=1, name='testvm')
  115. vm.nxproperty = 'value'
  116. xml = vm.__xml__()
  117. self.assertNotIn('nxproperty', xml)