init.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  8. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import lxml.etree
  25. import qubes
  26. import qubes.events
  27. import qubes.vm
  28. import qubes.tests
  29. class TestApp(object):
  30. def __init__(self):
  31. super(TestApp, self).__init__()
  32. self.domains = {}
  33. class TestVM(qubes.vm.BaseVM):
  34. qid = qubes.property('qid', type=int)
  35. name = qubes.property('name')
  36. testprop = qubes.property('testprop')
  37. testlabel = qubes.property('testlabel')
  38. defaultprop = qubes.property('defaultprop', default='defaultvalue')
  39. class TC_10_BaseVM(qubes.tests.QubesTestCase):
  40. def setUp(self):
  41. self.xml = lxml.etree.XML('''
  42. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  43. <labels>
  44. <label id="label-1" color="#cc0000">red</label>
  45. </labels>
  46. <domains>
  47. <domain id="domain-1" class="TestVM">
  48. <properties>
  49. <property name="qid">1</property>
  50. <property name="name">domain1</property>
  51. <property name="testprop">testvalue</property>
  52. <property name="testlabel" ref="label-1" />
  53. </properties>
  54. <tags>
  55. <tag name="testtag">tagvalue</tag>
  56. </tags>
  57. <features>
  58. <feature name="testfeature_empty"></feature>
  59. <feature name="testfeature_aqq">aqq</feature>
  60. </features>
  61. <devices class="pci">
  62. <device backend-domain="domain1" id="00:11.22"/>
  63. </devices>
  64. <devices class="usb" />
  65. <devices class="audio-in" />
  66. <devices class="firewire" />
  67. <devices class="i2c" />
  68. <devices class="isa" />
  69. </domain>
  70. </domains>
  71. </qubes>
  72. ''')
  73. def test_000_load(self):
  74. node = self.xml.xpath('//domain')[0]
  75. vm = TestVM(TestApp(), node)
  76. vm.app.domains['domain1'] = vm
  77. vm.load_properties(load_stage=None)
  78. vm.load_extras()
  79. self.assertEqual(vm.qid, 1)
  80. self.assertEqual(vm.testprop, 'testvalue')
  81. self.assertEqual(vm.testprop, 'testvalue')
  82. self.assertEqual(vm.testlabel, 'label-1')
  83. self.assertEqual(vm.defaultprop, 'defaultvalue')
  84. self.assertEqual(vm.tags, {'testtag': 'tagvalue'})
  85. self.assertEqual(vm.features, {
  86. 'testfeature_empty': '',
  87. 'testfeature_aqq': 'aqq',
  88. })
  89. self.assertItemsEqual(vm.devices.keys(), ('pci',))
  90. self.assertItemsEqual(list(vm.devices['pci'].attached(persistent=True)),
  91. [qubes.devices.PCIDevice(vm, '00:11.22')])
  92. self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
  93. def test_001_nxproperty(self):
  94. xml = lxml.etree.XML('''
  95. <qubes version="3">
  96. <domains>
  97. <domain id="domain-1" class="TestVM">
  98. <properties>
  99. <property name="qid">1</property>
  100. <property name="name">domain1</property>
  101. <property name="nxproperty">nxvalue</property>
  102. </properties>
  103. </domain>
  104. </domains>
  105. </qubes>
  106. ''')
  107. node = xml.xpath('//domain')[0]
  108. with self.assertRaises(TypeError):
  109. TestVM(None, node)
  110. def test_002_save_nxproperty(self):
  111. vm = TestVM(None, None, qid=1, name='testvm')
  112. vm.nxproperty = 'value'
  113. xml = vm.__xml__()
  114. self.assertNotIn('nxproperty', xml)