rpcconfirmation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/usr/bin/python
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 boring-stuff <boring-stuff@users.noreply.github.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import sys
  22. import unittest
  23. from qubespolicy.tests.gtkhelpers import GtkTestCase, FocusStealingHelperMock
  24. from qubespolicy.tests.gtkhelpers import mock_domains_info, mock_whitelist
  25. from qubespolicy.gtkhelpers import VMListModeler
  26. from qubespolicy.rpcconfirmation import RPCConfirmationWindow
  27. class MockRPCConfirmationWindow(RPCConfirmationWindow):
  28. def _new_vm_list_modeler(self):
  29. return VMListModeler(mock_domains_info)
  30. def _new_focus_stealing_helper(self):
  31. return FocusStealingHelperMock(
  32. self._rpc_window,
  33. self._rpc_ok_button,
  34. self._focus_stealing_seconds)
  35. def __init__(self, source, rpc_operation, whitelist,
  36. target=None, focus_stealing_seconds=1):
  37. self._focus_stealing_seconds = focus_stealing_seconds
  38. RPCConfirmationWindow.__init__(
  39. self, mock_domains_info, source, rpc_operation, whitelist,
  40. target)
  41. def is_error_visible(self):
  42. return self._error_bar.get_visible()
  43. def get_shown_domains(self):
  44. model = self._rpc_combo_box.get_model()
  45. model_iter = model.get_iter_first()
  46. domains = []
  47. while model_iter is not None:
  48. domain_name = model.get_value(model_iter, 1)
  49. domains += [domain_name]
  50. model_iter = model.iter_next(model_iter)
  51. return domains
  52. class RPCConfirmationWindowTestBase(MockRPCConfirmationWindow, GtkTestCase):
  53. def __init__(self, test_method, source_name="test-source",
  54. rpc_operation="test.Operation", whitelist=mock_whitelist,
  55. target_name=None):
  56. GtkTestCase.__init__(self, test_method)
  57. self.test_source_name = source_name
  58. self.test_rpc_operation = rpc_operation
  59. self.test_target_name = target_name
  60. self._test_time = 0.1
  61. self.test_called_close = False
  62. self.test_called_show = False
  63. self.test_clicked_ok = False
  64. self.test_clicked_cancel = False
  65. MockRPCConfirmationWindow.__init__(self,
  66. self.test_source_name,
  67. self.test_rpc_operation,
  68. whitelist,
  69. self.test_target_name,
  70. focus_stealing_seconds=self._test_time)
  71. def _can_perform_action(self):
  72. return True
  73. def _close(self):
  74. self.test_called_close = True
  75. def _show(self):
  76. self.test_called_show = True
  77. def _clicked_ok(self, button):
  78. MockRPCConfirmationWindow._clicked_ok(self, button)
  79. self.test_clicked_ok = True
  80. def _clicked_cancel(self, button):
  81. MockRPCConfirmationWindow._clicked_cancel(self, button)
  82. self.test_clicked_cancel = True
  83. def test_has_linked_the_fields(self):
  84. self.assertIsNotNone(self._rpc_window)
  85. self.assertIsNotNone(self._rpc_ok_button)
  86. self.assertIsNotNone(self._rpc_cancel_button)
  87. self.assertIsNotNone(self._rpc_label)
  88. self.assertIsNotNone(self._source_entry)
  89. self.assertIsNotNone(self._rpc_combo_box)
  90. self.assertIsNotNone(self._error_bar)
  91. self.assertIsNotNone(self._error_message)
  92. def test_is_showing_source(self):
  93. self.assertTrue(self.test_source_name in self._source_entry.get_text())
  94. def test_is_showing_operation(self):
  95. self.assertTrue(self.test_rpc_operation in self._rpc_label.get_text())
  96. def test_escape_and_format_rpc_text(self):
  97. self.assertEquals("qubes.<b>Test</b>",
  98. self._escape_and_format_rpc_text("qubes.Test"))
  99. self.assertEquals("custom.<b>Domain</b>",
  100. self._escape_and_format_rpc_text("custom.Domain"))
  101. self.assertEquals("<b>nodomain</b>",
  102. self._escape_and_format_rpc_text("nodomain"))
  103. self.assertEquals("domain.<b>Sub.Operation</b>",
  104. self._escape_and_format_rpc_text("domain.Sub.Operation"))
  105. self.assertEquals("<b></b>",
  106. self._escape_and_format_rpc_text(""))
  107. self.assertEquals("<b>.</b>",
  108. self._escape_and_format_rpc_text("."))
  109. self.assertEquals("inject.<b>&lt;script&gt;</b>",
  110. self._escape_and_format_rpc_text("inject.<script>"))
  111. self.assertEquals("&lt;script&gt;.<b>inject</b>",
  112. self._escape_and_format_rpc_text("<script>.inject"))
  113. def test_lifecycle_open_select_ok(self):
  114. self._lifecycle_start(select_target=True)
  115. self._lifecycle_click(click_type="ok")
  116. def test_lifecycle_open_select_cancel(self):
  117. self._lifecycle_start(select_target=True)
  118. self._lifecycle_click(click_type="cancel")
  119. def test_lifecycle_open_select_exit(self):
  120. self._lifecycle_start(select_target=True)
  121. self._lifecycle_click(click_type="exit")
  122. def test_lifecycle_open_cancel(self):
  123. self._lifecycle_start(select_target=False)
  124. self._lifecycle_click(click_type="cancel")
  125. def test_lifecycle_open_exit(self):
  126. self._lifecycle_start(select_target=False)
  127. self._lifecycle_click(click_type="exit")
  128. def _lifecycle_click(self, click_type):
  129. if click_type == "ok":
  130. self._rpc_ok_button.clicked()
  131. self.assertTrue(self.test_clicked_ok)
  132. self.assertFalse(self.test_clicked_cancel)
  133. self.assertTrue(self._confirmed)
  134. self.assertIsNotNone(self._target_name)
  135. elif click_type == "cancel":
  136. self._rpc_cancel_button.clicked()
  137. self.assertFalse(self.test_clicked_ok)
  138. self.assertTrue(self.test_clicked_cancel)
  139. self.assertFalse(self._confirmed)
  140. elif click_type == "exit":
  141. self._close()
  142. self.assertFalse(self.test_clicked_ok)
  143. self.assertFalse(self.test_clicked_cancel)
  144. self.assertIsNone(self._confirmed)
  145. self.assertTrue(self.test_called_close)
  146. def _lifecycle_start(self, select_target):
  147. self.assertFalse(self.test_called_close)
  148. self.assertFalse(self.test_called_show)
  149. self.assert_initial_state(False)
  150. self.assertTrue(isinstance(self._focus_helper, FocusStealingHelperMock))
  151. # Need the following because of pylint's complaints
  152. if isinstance(self._focus_helper, FocusStealingHelperMock):
  153. FocusStealingHelperMock.simulate_focus(self._focus_helper)
  154. self.flush_gtk_events(self._test_time*2)
  155. self.assert_initial_state(True)
  156. try:
  157. # We expect the call to exit immediately, since no window is opened
  158. self.confirm_rpc()
  159. except Exception:
  160. pass
  161. self.assertFalse(self.test_called_close)
  162. self.assertTrue(self.test_called_show)
  163. self.assert_initial_state(True)
  164. if select_target:
  165. self._rpc_combo_box.set_active(1)
  166. self.assertTrue(self._rpc_ok_button.get_sensitive())
  167. self.assertIsNotNone(self._target_name)
  168. self.assertFalse(self.test_called_close)
  169. self.assertTrue(self.test_called_show)
  170. self.assertFalse(self.test_clicked_ok)
  171. self.assertFalse(self.test_clicked_cancel)
  172. self.assertFalse(self._confirmed)
  173. def assert_initial_state(self, after_focus_timer):
  174. self.assertIsNone(self._target_name)
  175. self.assertFalse(self.test_clicked_ok)
  176. self.assertFalse(self.test_clicked_cancel)
  177. self.assertFalse(self._confirmed)
  178. self.assertFalse(self._rpc_ok_button.get_sensitive())
  179. self.assertFalse(self._error_bar.get_visible())
  180. if after_focus_timer:
  181. self.assertTrue(self._focus_helper.can_perform_action())
  182. else:
  183. self.assertFalse(self._focus_helper.can_perform_action())
  184. class RPCConfirmationWindowTestWithTarget(RPCConfirmationWindowTestBase):
  185. def __init__(self, test_method):
  186. RPCConfirmationWindowTestBase.__init__(self, test_method,
  187. source_name="test-source", rpc_operation="test.Operation",
  188. target_name="test-target")
  189. def test_lifecycle_open_ok(self):
  190. self._lifecycle_start(select_target=False)
  191. self._lifecycle_click(click_type="ok")
  192. def assert_initial_state(self, after_focus_timer):
  193. self.assertIsNotNone(self._target_name)
  194. self.assertFalse(self.test_clicked_ok)
  195. self.assertFalse(self.test_clicked_cancel)
  196. self.assertFalse(self._confirmed)
  197. if after_focus_timer:
  198. self.assertTrue(self._rpc_ok_button.get_sensitive())
  199. self.assertTrue(self._focus_helper.can_perform_action())
  200. else:
  201. self.assertFalse(self._rpc_ok_button.get_sensitive())
  202. self.assertFalse(self._focus_helper.can_perform_action())
  203. def _lifecycle_click(self, click_type):
  204. RPCConfirmationWindowTestBase._lifecycle_click(self, click_type)
  205. self.assertIsNotNone(self._target_name)
  206. class RPCConfirmationWindowTestWithTargetInvalid(unittest.TestCase):
  207. def __init__(self, *args, **kwargs):
  208. unittest.TestCase.__init__(self, *args, **kwargs)
  209. def test_unknown(self):
  210. self.assert_raises_error(True, "test-source", "test-wrong-target")
  211. def test_empty(self):
  212. self.assert_raises_error(True, "test-source", "")
  213. def test_equals_source(self):
  214. self.assert_raises_error(True, "test-source", "test-source")
  215. def assert_raises_error(self, expect, source, target):
  216. rpcWindow = MockRPCConfirmationWindow(source, "test.Operation",
  217. mock_whitelist, target=target)
  218. self.assertEquals(expect, rpcWindow.is_error_visible())
  219. class RPCConfirmationWindowTestWhitelist(unittest.TestCase):
  220. def __init__(self, *args, **kwargs):
  221. unittest.TestCase.__init__(self, *args, **kwargs)
  222. def test_no_domains(self):
  223. self._assert_whitelist([], [])
  224. def test_all_red_domains(self):
  225. self._assert_whitelist(["test-red1", "test-red2", "test-red3"],
  226. ["test-red1", "test-red2", "test-red3"])
  227. def test_all_red_domains_plus_nonexistent(self):
  228. self._assert_whitelist(
  229. ["test-red1", "test-red2", "test-red3",
  230. "test-blue1", "test-blue2", "test-blue3"],
  231. ["test-red1", "test-red2", "test-red3"])
  232. def test_all_allowed_domains(self):
  233. self._assert_whitelist(
  234. ["test-red1", "test-red2", "test-red3",
  235. "test-target", "$dispvm:test-disp6", "test-source", "dom0"],
  236. ["test-red1", "test-red2", "test-red3",
  237. "test-target", "Disposable VM (test-disp6)", "test-source",
  238. "dom0"])
  239. def _assert_whitelist(self, whitelist, expected):
  240. rpcWindow = MockRPCConfirmationWindow(
  241. "test-source", "test.Operation", whitelist)
  242. domains = rpcWindow.get_shown_domains()
  243. self.assertCountEqual(domains, expected)
  244. if __name__ == '__main__':
  245. test = False
  246. window = False
  247. if len(sys.argv) == 1 or sys.argv[1] == '-t':
  248. test = True
  249. elif sys.argv[1] == '-w':
  250. window = True
  251. else:
  252. print("Usage: " + __file__ + " [-t|-w]")
  253. if window:
  254. print(MockRPCConfirmationWindow("test-source",
  255. "qubes.Filecopy",
  256. mock_whitelist,
  257. "test-red1").confirm_rpc())
  258. elif test:
  259. unittest.main(argv=[sys.argv[0]])