devices.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access,pointless-statement
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  8. # Copyright (C) 2015-2016 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 qubes.devices
  25. import qubes.tests
  26. class TestDevice(qubes.devices.DeviceInfo):
  27. pass
  28. class TestVMCollection(dict):
  29. def __iter__(self):
  30. return iter(set(self.values()))
  31. class TestApp(object):
  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. def __str__(self):
  47. return self.name
  48. @qubes.events.handler('device-list-attached:testclass')
  49. def dev_testclass_list_attached(self, event, persistent):
  50. for vm in self.app.domains:
  51. if vm.device.frontend_domain == self:
  52. yield vm.device
  53. @qubes.events.handler('device-list:testclass')
  54. def dev_testclass_list(self, event):
  55. yield self.device
  56. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  57. def setUp(self):
  58. self.app = TestApp()
  59. self.emitter = TestVM(self.app, 'vm')
  60. self.app.domains['vm'] = self.emitter
  61. self.device = self.emitter.device
  62. self.collection = self.emitter.devices['testclass']
  63. def test_000_init(self):
  64. self.assertFalse(self.collection._set)
  65. def test_001_attach(self):
  66. self.collection.attach(self.device)
  67. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  68. self.assertEventFired(self.emitter, 'device-attach:testclass')
  69. self.assertEventNotFired(self.emitter, 'device-pre-detach:testclass')
  70. self.assertEventNotFired(self.emitter, 'device-detach:testclass')
  71. def test_002_detach(self):
  72. self.collection.attach(self.device)
  73. self.collection.detach(self.device)
  74. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  75. self.assertEventFired(self.emitter, 'device-attach:testclass')
  76. self.assertEventFired(self.emitter, 'device-pre-detach:testclass')
  77. self.assertEventFired(self.emitter, 'device-detach:testclass')
  78. def test_010_empty_detach(self):
  79. with self.assertRaises(LookupError):
  80. self.collection.detach(self.device)
  81. def test_011_double_attach(self):
  82. self.collection.attach(self.device)
  83. with self.assertRaises(LookupError):
  84. self.collection.attach(self.device)
  85. def test_012_double_detach(self):
  86. self.collection.attach(self.device)
  87. self.collection.detach(self.device)
  88. with self.assertRaises(LookupError):
  89. self.collection.detach(self.device)
  90. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  91. def setUp(self):
  92. self.app = TestApp()
  93. self.emitter = TestVM(self.app, 'vm')
  94. self.manager = qubes.devices.DeviceManager(self.emitter)
  95. def test_000_init(self):
  96. self.assertEqual(self.manager, {})
  97. def test_001_missing(self):
  98. device = TestDevice(self.emitter.app.domains['vm'], 'testdev')
  99. self.manager['testclass'].attach(device)
  100. self.assertEventFired(self.emitter, 'device-attach:testclass')