qubes_prefs.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.tests.tools
  22. import qubesadmin.tools.qubes_prefs
  23. class TC_00_qubes_prefs(qubesadmin.tests.QubesTestCase):
  24. def test_000_list(self):
  25. self.app.expected_calls[
  26. ('dom0', 'admin.property.List', None, None)] = \
  27. b'0\x00prop1\nprop2\n'
  28. self.app.expected_calls[
  29. ('dom0', 'admin.property.Get', 'prop1', None)] = \
  30. b'0\x00default=True type=str value1'
  31. self.app.expected_calls[
  32. ('dom0', 'admin.property.Get', 'prop2', None)] = \
  33. b'0\x00default=False type=str value2'
  34. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  35. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([], app=self.app))
  36. self.assertEqual(stdout.getvalue(),
  37. 'prop1 D value1\n'
  38. 'prop2 - value2\n')
  39. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  40. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([
  41. '--hide-default'], app=self.app))
  42. self.assertEqual(stdout.getvalue(),
  43. 'prop2 - value2\n')
  44. self.assertAllCalled()
  45. def test_002_set_property(self):
  46. self.app.expected_calls[
  47. ('dom0', 'admin.property.Set', 'default_user', b'testuser')]\
  48. = b'0\x00'
  49. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([
  50. 'default_user', 'testuser'], app=self.app))
  51. self.assertAllCalled()
  52. def test_003_invalid_property(self):
  53. self.app.expected_calls[
  54. ('dom0', 'admin.property.Get', 'no_such_property', None)] = \
  55. b'2\x00AttributeError\x00\x00no_such_property\x00'
  56. with self.assertRaises(SystemExit):
  57. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  58. qubesadmin.tools.qubes_prefs.main([
  59. 'no_such_property'], app=self.app)
  60. self.assertIn('no such property: \'no_such_property\'',
  61. stderr.getvalue())
  62. self.assertAllCalled()
  63. def test_004_set_invalid_property(self):
  64. self.app.expected_calls[
  65. ('dom0', 'admin.property.Set', 'no_such_property', b'value')]\
  66. = b'2\x00AttributeError\x00\x00no_such_property\x00'
  67. with self.assertRaises(SystemExit):
  68. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  69. qubesadmin.tools.qubes_prefs.main([
  70. 'no_such_property', 'value'], app=self.app)
  71. self.assertIn('no such property: \'no_such_property\'',
  72. stderr.getvalue())
  73. self.assertAllCalled()