devices.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015-2016 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 qubes.devices
  23. import qubes.tests
  24. class TestDevice(qubes.devices.DeviceInfo):
  25. # pylint: disable=too-few-public-methods
  26. pass
  27. class TestVMCollection(dict):
  28. def __iter__(self):
  29. return iter(set(self.values()))
  30. class TestApp(object):
  31. # pylint: disable=too-few-public-methods
  32. def __init__(self):
  33. self.domains = TestVMCollection()
  34. class TestVM(qubes.tests.TestEmitter):
  35. def __init__(self, app, name, *args, **kwargs):
  36. super(TestVM, self).__init__(*args, **kwargs)
  37. self.app = app
  38. self.name = name
  39. self.device = TestDevice(self, 'testdev', 'Description')
  40. self.events_enabled = True
  41. self.devices = {
  42. 'testclass': qubes.devices.DeviceCollection(self, 'testclass')
  43. }
  44. self.app.domains[name] = self
  45. self.app.domains[self] = self
  46. self.running = False
  47. def __str__(self):
  48. return self.name
  49. @qubes.events.handler('device-list-attached:testclass')
  50. def dev_testclass_list_attached(self, event, persistent = False):
  51. for vm in self.app.domains:
  52. if vm.device.frontend_domain == self:
  53. yield (vm.device, {})
  54. @qubes.events.handler('device-list:testclass')
  55. def dev_testclass_list(self, event):
  56. yield self.device
  57. def is_halted(self):
  58. return not self.running
  59. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  60. def setUp(self):
  61. super().setUp()
  62. self.app = TestApp()
  63. self.emitter = TestVM(self.app, 'vm')
  64. self.app.domains['vm'] = self.emitter
  65. self.device = self.emitter.device
  66. self.collection = self.emitter.devices['testclass']
  67. self.assignment = qubes.devices.DeviceAssignment(
  68. backend_domain=self.device.backend_domain,
  69. ident=self.device.ident,
  70. persistent=True
  71. )
  72. def test_000_init(self):
  73. self.assertFalse(self.collection._set)
  74. def test_001_attach(self):
  75. self.collection.attach(self.assignment)
  76. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  77. self.assertEventFired(self.emitter, 'device-attach:testclass')
  78. self.assertEventNotFired(self.emitter, 'device-pre-detach:testclass')
  79. self.assertEventNotFired(self.emitter, 'device-detach:testclass')
  80. def test_002_detach(self):
  81. self.collection.attach(self.assignment)
  82. self.collection.detach(self.assignment)
  83. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  84. self.assertEventFired(self.emitter, 'device-attach:testclass')
  85. self.assertEventFired(self.emitter, 'device-pre-detach:testclass')
  86. self.assertEventFired(self.emitter, 'device-detach:testclass')
  87. def test_010_empty_detach(self):
  88. with self.assertRaises(LookupError):
  89. self.collection.detach(self.assignment)
  90. def test_011_double_attach(self):
  91. self.collection.attach(self.assignment)
  92. with self.assertRaises(qubes.devices.DeviceAlreadyAttached):
  93. self.collection.attach(self.assignment)
  94. def test_012_double_detach(self):
  95. self.collection.attach(self.assignment)
  96. self.collection.detach(self.assignment)
  97. with self.assertRaises(qubes.devices.DeviceNotAttached):
  98. self.collection.detach(self.assignment)
  99. def test_013_list_attached_persistent(self):
  100. self.assertEqual(set([]), set(self.collection.persistent()))
  101. self.collection.attach(self.assignment)
  102. self.assertEventFired(self.emitter, 'device-list-attached:testclass')
  103. self.assertEqual({self.device}, set(self.collection.persistent()))
  104. self.assertEqual({self.device},
  105. set(self.collection.persistent()))
  106. self.assertEqual(set([]),
  107. set(self.collection.attached()))
  108. def test_014_list_attached_non_persistent(self):
  109. self.assignment.persistent = False
  110. self.emitter.running = True
  111. self.collection.attach(self.assignment)
  112. # device-attach event not implemented, so manipulate object manually
  113. self.device.frontend_domain = self.emitter
  114. self.assertEqual({self.device},
  115. set(self.collection.attached()))
  116. self.assertEqual(set([]),
  117. set(self.collection.persistent()))
  118. self.assertEqual({self.device},
  119. set(self.collection.attached()))
  120. self.assertEventFired(self.emitter, 'device-list-attached:testclass')
  121. def test_015_list_available(self):
  122. self.assertEqual({self.device}, set(self.collection))
  123. self.assertEventFired(self.emitter, 'device-list:testclass')
  124. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  125. def setUp(self):
  126. super().setUp()
  127. self.app = TestApp()
  128. self.emitter = TestVM(self.app, 'vm')
  129. self.manager = qubes.devices.DeviceManager(self.emitter)
  130. def test_000_init(self):
  131. self.assertEqual(self.manager, {})
  132. def test_001_missing(self):
  133. device = TestDevice(self.emitter.app.domains['vm'], 'testdev')
  134. assignment = qubes.devices.DeviceAssignment(
  135. backend_domain=device.backend_domain,
  136. ident=device.ident,
  137. persistent=True)
  138. self.manager['testclass'].attach(assignment)
  139. self.assertEventFired(self.emitter, 'device-attach:testclass')