init.py 4.4 KB

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