devices.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.devices
  22. class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase):
  23. def setUp(self):
  24. super(TC_00_DeviceCollection, self).setUp()
  25. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\0test-vm class=AppVM state=Running\n' \
  27. b'test-vm2 class=AppVM state=Running\n' \
  28. b'test-vm3 class=AppVM state=Running\n'
  29. self.vm = self.app.domains['test-vm']
  30. def test_000_available(self):
  31. self.app.expected_calls[
  32. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  33. b'0\0dev1\n'
  34. devices = list(self.vm.devices['test'].available())
  35. self.assertEqual(len(devices), 1)
  36. dev = devices[0]
  37. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  38. self.assertEqual(dev.backend_domain, self.vm)
  39. self.assertEqual(dev.ident, 'dev1')
  40. self.assertEqual(dev.description, '')
  41. self.assertEqual(dev.data, {})
  42. self.assertEqual(str(dev), 'test-vm:dev1')
  43. self.assertAllCalled()
  44. def test_001_available_desc(self):
  45. self.app.expected_calls[
  46. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  47. b'0\0dev1 description=This is description\n'
  48. devices = list(self.vm.devices['test'].available())
  49. self.assertEqual(len(devices), 1)
  50. dev = devices[0]
  51. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  52. self.assertEqual(dev.backend_domain, self.vm)
  53. self.assertEqual(dev.ident, 'dev1')
  54. self.assertEqual(dev.description, 'This is description')
  55. self.assertEqual(dev.data, {})
  56. self.assertEqual(str(dev), 'test-vm:dev1')
  57. def test_002_available_options(self):
  58. self.app.expected_calls[
  59. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  60. b'0\0dev1 ro=True other=123 description=This is description\n'
  61. devices = list(self.vm.devices['test'].available())
  62. self.assertEqual(len(devices), 1)
  63. dev = devices[0]
  64. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  65. self.assertEqual(dev.backend_domain, self.vm)
  66. self.assertEqual(dev.ident, 'dev1')
  67. self.assertEqual(dev.description, 'This is description')
  68. self.assertEqual(dev.data, {'ro': 'True', 'other': '123'})
  69. self.assertEqual(str(dev), 'test-vm:dev1')
  70. self.assertAllCalled()
  71. def test_010_getitem(self):
  72. self.app.expected_calls[
  73. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  74. b'0\0dev1 description=This is description\n'
  75. dev = self.vm.devices['test']['dev1']
  76. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  77. self.assertEqual(dev.backend_domain, self.vm)
  78. self.assertEqual(dev.ident, 'dev1')
  79. self.assertEqual(dev.description, 'This is description')
  80. self.assertEqual(dev.data, {})
  81. self.assertEqual(str(dev), 'test-vm:dev1')
  82. self.assertAllCalled()
  83. def test_011_getitem_missing(self):
  84. self.app.expected_calls[
  85. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  86. b'0\0dev1 description=This is description\n'
  87. dev = self.vm.devices['test']['dev2']
  88. self.assertIsInstance(dev, qubesadmin.devices.UnknownDevice)
  89. self.assertEqual(dev.backend_domain, self.vm)
  90. self.assertEqual(dev.ident, 'dev2')
  91. self.assertEqual(dev.description, 'Unknown device')
  92. self.assertEqual(dev.data, {})
  93. self.assertEqual(str(dev), 'test-vm:dev2')
  94. self.assertAllCalled()
  95. def test_020_attach(self):
  96. self.app.expected_calls[
  97. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  98. b'')] = \
  99. b'0\0'
  100. assign = qubesadmin.devices.DeviceAssignment(
  101. self.app.domains['test-vm2'], 'dev1')
  102. self.vm.devices['test'].attach(assign)
  103. self.assertAllCalled()
  104. def test_021_attach_options(self):
  105. self.app.expected_calls[
  106. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  107. b'ro=True something=value')] = b'0\0'
  108. assign = qubesadmin.devices.DeviceAssignment(
  109. self.app.domains['test-vm2'], 'dev1')
  110. assign.options['ro'] = True
  111. assign.options['something'] = 'value'
  112. self.vm.devices['test'].attach(assign)
  113. self.assertAllCalled()
  114. def test_022_attach_persistent(self):
  115. self.app.expected_calls[
  116. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  117. b'persistent=True')] = b'0\0'
  118. assign = qubesadmin.devices.DeviceAssignment(
  119. self.app.domains['test-vm2'], 'dev1')
  120. assign.persistent = True
  121. self.vm.devices['test'].attach(assign)
  122. self.assertAllCalled()
  123. def test_023_attach_persistent_options(self):
  124. self.app.expected_calls[
  125. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  126. b'persistent=True ro=True')] = b'0\0'
  127. assign = qubesadmin.devices.DeviceAssignment(
  128. self.app.domains['test-vm2'], 'dev1')
  129. assign.persistent = True
  130. assign.options['ro'] = True
  131. self.vm.devices['test'].attach(assign)
  132. self.assertAllCalled()
  133. def test_030_detach(self):
  134. self.app.expected_calls[
  135. ('test-vm', 'admin.vm.device.test.Detach', 'test-vm2+dev1',
  136. None)] = b'0\0'
  137. assign = qubesadmin.devices.DeviceAssignment(
  138. self.app.domains['test-vm2'], 'dev1')
  139. self.vm.devices['test'].detach(assign)
  140. self.assertAllCalled()
  141. def test_040_assignments(self):
  142. self.app.expected_calls[
  143. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  144. b'0\0test-vm2+dev1\n' \
  145. b'test-vm3+dev2\n'
  146. self.app.expected_calls[
  147. ('test-vm2', 'admin.vm.device.test.Available', None, None)] = \
  148. b'0\0dev1 description=desc\n'
  149. self.app.expected_calls[
  150. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  151. b'0\0dev2 description=desc\n'
  152. assigns = list(self.vm.devices['test'].assignments())
  153. self.assertEqual(len(assigns), 2)
  154. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  155. self.assertEqual(assigns[0].backend_domain,
  156. self.app.domains['test-vm2'])
  157. self.assertEqual(assigns[0].ident, 'dev1')
  158. self.assertEqual(assigns[0].frontend_domain,
  159. self.app.domains['test-vm'])
  160. self.assertEqual(assigns[0].options, {})
  161. self.assertEqual(assigns[0].devclass, 'test')
  162. self.assertEqual(assigns[0].device,
  163. self.app.domains['test-vm2'].devices['test']['dev1'])
  164. self.assertIsInstance(assigns[1], qubesadmin.devices.DeviceAssignment)
  165. self.assertEqual(assigns[1].backend_domain,
  166. self.app.domains['test-vm3'])
  167. self.assertEqual(assigns[1].ident, 'dev2')
  168. self.assertEqual(assigns[1].frontend_domain,
  169. self.app.domains['test-vm'])
  170. self.assertEqual(assigns[1].options, {})
  171. self.assertEqual(assigns[1].devclass, 'test')
  172. self.assertEqual(assigns[1].device,
  173. self.app.domains['test-vm3'].devices['test']['dev2'])
  174. self.assertAllCalled()
  175. def test_041_assignments_options(self):
  176. self.app.expected_calls[
  177. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  178. b'0\0test-vm2+dev1 ro=True\n' \
  179. b'test-vm3+dev2 ro=False persistent=True\n'
  180. assigns = list(self.vm.devices['test'].assignments())
  181. self.assertEqual(len(assigns), 2)
  182. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  183. self.assertEqual(assigns[0].backend_domain,
  184. self.app.domains['test-vm2'])
  185. self.assertEqual(assigns[0].ident, 'dev1')
  186. self.assertEqual(assigns[0].frontend_domain,
  187. self.app.domains['test-vm'])
  188. self.assertEqual(assigns[0].options, {'ro': 'True'})
  189. self.assertEqual(assigns[0].persistent, False)
  190. self.assertEqual(assigns[0].devclass, 'test')
  191. self.assertIsInstance(assigns[1], qubesadmin.devices.DeviceAssignment)
  192. self.assertEqual(assigns[1].backend_domain,
  193. self.app.domains['test-vm3'])
  194. self.assertEqual(assigns[1].ident, 'dev2')
  195. self.assertEqual(assigns[1].frontend_domain,
  196. self.app.domains['test-vm'])
  197. self.assertEqual(assigns[1].options, {'ro': 'False'})
  198. self.assertEqual(assigns[1].persistent, True)
  199. self.assertEqual(assigns[1].devclass, 'test')
  200. self.assertAllCalled()
  201. def test_041_assignments_persistent(self):
  202. self.app.expected_calls[
  203. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  204. b'0\0test-vm2+dev1\n' \
  205. b'test-vm3+dev2 persistent=True\n'
  206. assigns = list(self.vm.devices['test'].assignments(True))
  207. self.assertEqual(len(assigns), 1)
  208. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  209. self.assertEqual(assigns[0].backend_domain,
  210. self.app.domains['test-vm3'])
  211. self.assertEqual(assigns[0].ident, 'dev2')
  212. self.assertEqual(assigns[0].frontend_domain,
  213. self.app.domains['test-vm'])
  214. self.assertEqual(assigns[0].options, {})
  215. self.assertEqual(assigns[0].persistent, True)
  216. self.assertEqual(assigns[0].devclass, 'test')
  217. self.assertAllCalled()
  218. def test_042_assignments_non_persistent(self):
  219. self.app.expected_calls[
  220. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  221. b'0\0test-vm2+dev1\n' \
  222. b'test-vm3+dev2 persistent=True\n'
  223. assigns = list(self.vm.devices['test'].assignments(False))
  224. self.assertEqual(len(assigns), 1)
  225. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  226. self.assertEqual(assigns[0].backend_domain,
  227. self.app.domains['test-vm2'])
  228. self.assertEqual(assigns[0].ident, 'dev1')
  229. self.assertEqual(assigns[0].frontend_domain,
  230. self.app.domains['test-vm'])
  231. self.assertEqual(assigns[0].options, {})
  232. self.assertEqual(assigns[0].persistent, False)
  233. self.assertAllCalled()
  234. def test_050_persistent(self):
  235. self.app.expected_calls[
  236. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  237. b'0\0test-vm2+dev1\n' \
  238. b'test-vm3+dev2 persistent=True\n'
  239. self.app.expected_calls[
  240. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  241. b'0\0dev2\n'
  242. devs = list(self.vm.devices['test'].persistent())
  243. self.assertEqual(len(devs), 1)
  244. self.assertIsInstance(devs[0], qubesadmin.devices.DeviceInfo)
  245. self.assertEqual(devs[0].backend_domain, self.app.domains['test-vm3'])
  246. self.assertEqual(devs[0].ident, 'dev2')
  247. self.assertAllCalled()
  248. def test_060_attached(self):
  249. self.app.expected_calls[
  250. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  251. b'0\0test-vm2+dev1\n' \
  252. b'test-vm3+dev2 persistent=True\n'
  253. self.app.expected_calls[
  254. ('test-vm2', 'admin.vm.device.test.Available', None, None)] = \
  255. b'0\0dev1\n'
  256. self.app.expected_calls[
  257. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  258. b'0\0dev2\n'
  259. devs = list(self.vm.devices['test'].attached())
  260. self.assertEqual(len(devs), 2)
  261. self.assertIsInstance(devs[0], qubesadmin.devices.DeviceInfo)
  262. self.assertEqual(devs[0].backend_domain, self.app.domains['test-vm2'])
  263. self.assertEqual(devs[0].ident, 'dev1')
  264. self.assertIsInstance(devs[1], qubesadmin.devices.DeviceInfo)
  265. self.assertEqual(devs[1].backend_domain, self.app.domains['test-vm3'])
  266. self.assertEqual(devs[1].ident, 'dev2')
  267. self.assertAllCalled()
  268. def test_070_update_persistent(self):
  269. self.app.expected_calls[
  270. ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
  271. b'True')] = b'0\0'
  272. dev = qubesadmin.devices.DeviceInfo(
  273. self.app.domains['test-vm2'], 'test', 'dev1')
  274. self.vm.devices['test'].update_persistent(dev, True)
  275. self.assertAllCalled()
  276. def test_071_update_persistent_false(self):
  277. self.app.expected_calls[
  278. ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
  279. b'False')] = b'0\0'
  280. dev = qubesadmin.devices.DeviceInfo(
  281. self.app.domains['test-vm2'], 'test', 'dev1')
  282. self.vm.devices['test'].update_persistent(dev, False)
  283. self.assertAllCalled()
  284. def test_072_list(self):
  285. self.app.expected_calls[
  286. ('dom0', 'admin.deviceclass.List', None, None)] = \
  287. b'0\x00block\nmic\nusb\n'
  288. seen = set()
  289. for devclass in self.app.domains['test-vm'].devices:
  290. self.assertNotIn(devclass, seen)
  291. seen.add(devclass)
  292. self.assertEqual(seen, {'block', 'mic', 'usb'})