errors.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 qubesmgmt.exc
  21. import qubesmgmt.tests
  22. import unittest
  23. class TC_00_Errors(qubesmgmt.tests.QubesTestCase):
  24. def test_000_exception(self):
  25. self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
  26. b'2\x00QubesException\x00\x00An error occurred\x00'
  27. with self.assertRaises(qubesmgmt.exc.QubesException) as context:
  28. vms = list(self.app.domains)
  29. self.assertEqual(str(context.exception), 'An error occurred')
  30. def test_001_exception_with_fields(self):
  31. self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
  32. b'2\x00QubesException\x00\x00' \
  33. b'An error occurred: %s, %s\x00string\x00other\x00'
  34. with self.assertRaises(qubesmgmt.exc.QubesException) as context:
  35. vms = list(self.app.domains)
  36. self.assertEqual(str(context.exception),
  37. 'An error occurred: string, other')
  38. @unittest.expectedFailure
  39. def test_002_exception_with_numbers(self):
  40. self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
  41. b'2\x00QubesException\x00\x00' \
  42. b'An error occurred: %d, %d\x001\x002\x00'
  43. try:
  44. with self.assertRaises(qubesmgmt.exc.QubesException) as context:
  45. vms = list(self.app.domains)
  46. except TypeError as e:
  47. self.fail('TypeError: {!s}'.format(e))
  48. self.assertEqual(str(context.exception), 'An error occurred: 1 2')
  49. def test_010_empty(self):
  50. self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = b''\
  51. # FIXME: change to appropriate exception when defined
  52. with self.assertRaises(qubesmgmt.exc.QubesException) as context:
  53. vms = list(self.app.domains)
  54. self.assertEqual(str(context.exception), 'Invalid response format')