mime.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. #
  23. import os
  24. from distutils import spawn
  25. import re
  26. import subprocess
  27. import time
  28. import unittest
  29. import itertools
  30. import asyncio
  31. import sys
  32. import qubes.tests
  33. import qubes
  34. @unittest.skipUnless(
  35. spawn.find_executable('xprop') and
  36. spawn.find_executable('xdotool') and
  37. spawn.find_executable('wmctrl'),
  38. "xprop or xdotool or wmctrl not installed")
  39. class TC_50_MimeHandlers:
  40. def setUp(self):
  41. super(TC_50_MimeHandlers, self).setUp()
  42. if self.template.startswith('whonix-gw') or 'minimal' in self.template:
  43. raise unittest.SkipTest(
  44. 'Template {} not supported by this test'.format(self.template))
  45. self.source_vmname = self.make_vm_name('source')
  46. self.source_vm = self.app.add_new_vm("AppVM",
  47. template=self.template,
  48. name=self.source_vmname,
  49. label='red')
  50. self.loop.run_until_complete(self.source_vm.create_on_disk())
  51. self.target_vmname = self.make_vm_name('target')
  52. self.target_vm = self.app.add_new_vm("AppVM",
  53. template=self.template,
  54. name=self.target_vmname,
  55. label='red')
  56. self.loop.run_until_complete(self.target_vm.create_on_disk())
  57. self.target_vm.template_for_dispvms = True
  58. self.source_vm.default_dispvm = self.target_vm
  59. done, not_done = self.loop.run_until_complete(asyncio.gather(
  60. self.source_vm.start(),
  61. self.target_vm.start()))
  62. for result in itertools.chain(done, not_done):
  63. # catch any exceptions
  64. result.result()
  65. def get_window_class(self, winid, dispvm=False):
  66. (vm_winid, _) = subprocess.Popen(
  67. ['xprop', '-id', winid, '_QUBES_VMWINDOWID'],
  68. stdout=subprocess.PIPE
  69. ).communicate()
  70. vm_winid = vm_winid.decode().split("#")[1].strip('\n" ')
  71. if dispvm:
  72. (vmname, _) = subprocess.Popen(
  73. ['xprop', '-id', winid, '_QUBES_VMNAME'],
  74. stdout=subprocess.PIPE
  75. ).communicate()
  76. vmname = vmname.decode().split("=")[1].strip('\n" ')
  77. vm = self.app.domains[vmname]
  78. else:
  79. vm = self.target_vm
  80. window_class = None
  81. while window_class is None:
  82. try:
  83. window_class, _ = self.loop.run_until_complete(
  84. vm.run_for_stdio('xprop -id {} WM_CLASS'.format(vm_winid)))
  85. except subprocess.CalledProcessError as e:
  86. if e.returncode == 127:
  87. self.skipTest('xprop not installed')
  88. self.fail(
  89. "xprop -id {} WM_CLASS failed: {}".format(
  90. vm_winid, e.stderr.decode()))
  91. if b'not found' in window_class:
  92. # WM_CLASS not set yet, wait a little
  93. time.sleep(0.1)
  94. window_class = None
  95. # output: WM_CLASS(STRING) = "gnome-terminal-server", "Gnome-terminal"
  96. try:
  97. window_class = window_class.decode()
  98. window_class = window_class.split("=")[1].split(",")[0].strip('\n" ')
  99. except IndexError:
  100. raise Exception(
  101. "Unexpected output from xprop: '{}'".format(window_class))
  102. return window_class
  103. def open_file_and_check_viewer(self, filename, expected_app_titles,
  104. expected_app_classes, dispvm=False):
  105. if dispvm:
  106. p = self.loop.run_until_complete(self.source_vm.run(
  107. "qvm-open-in-dvm {}".format(filename), stdout=subprocess.PIPE))
  108. vmpattern = "disp[0-9]*"
  109. else:
  110. p = self.loop.run_until_complete(self.source_vm.run(
  111. "qvm-open-in-vm {} {}".format(self.target_vmname, filename),
  112. stdout=subprocess.PIPE))
  113. vmpattern = self.target_vmname
  114. wait_count = 0
  115. winid = None
  116. with self.qrexec_policy('qubes.OpenInVM', self.source_vm.name,
  117. self.target_vmname):
  118. with self.qrexec_policy('qubes.OpenURL', self.source_vm.name,
  119. self.target_vmname):
  120. while True:
  121. search = subprocess.Popen(['xdotool', 'search',
  122. '--onlyvisible', '--class', vmpattern],
  123. stdout=subprocess.PIPE,
  124. stderr=subprocess.DEVNULL)
  125. retcode = search.wait()
  126. if retcode == 0:
  127. winid = search.stdout.read().strip()
  128. # get window title
  129. (window_title, _) = subprocess.Popen(
  130. ['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
  131. communicate()
  132. window_title = window_title.decode('utf8').strip()
  133. # ignore LibreOffice splash screen and window with no title
  134. # set yet
  135. if window_title and \
  136. not window_title.startswith("LibreOffice") and\
  137. not window_title.startswith("NetworkManager") and\
  138. not window_title == 'VMapp command':
  139. break
  140. wait_count += 1
  141. if wait_count > 100:
  142. self.fail("Timeout while waiting for editor window")
  143. self.loop.run_until_complete(asyncio.sleep(0.3))
  144. # get window class
  145. window_class = self.get_window_class(winid, dispvm)
  146. # close the window - we've got the window class, it is no longer needed
  147. subprocess.check_call(['wmctrl', '-i', '-c', winid])
  148. # confirm quit for example for Firefox
  149. self.loop.run_until_complete(asyncio.sleep(1))
  150. subprocess.call(['xdotool', 'search', '--onlyvisible', '--name',
  151. 'Quit', 'windowfocus', 'key', 'Return'])
  152. try:
  153. self.loop.run_until_complete(asyncio.wait_for(p.wait(), 30))
  154. except asyncio.TimeoutError:
  155. self.fail('qvm-open-in-vm did not exited')
  156. self.wait_for_window(window_title, show=False)
  157. def check_matches(obj, patterns):
  158. return any((pat.search(obj) if isinstance(pat, type(re.compile('')))
  159. else pat in obj) for pat in patterns)
  160. if not check_matches(window_title, expected_app_titles) and \
  161. not check_matches(window_class, expected_app_classes):
  162. self.fail("Opening file {} resulted in window '{} ({})', which is "
  163. "none of {!r} ({!r})".format(
  164. filename, window_title, window_class,
  165. expected_app_titles, expected_app_classes))
  166. def prepare_txt(self, filename):
  167. self.loop.run_until_complete(
  168. self.source_vm.run_for_stdio("cat > {}".format(filename),
  169. input=b'This is test\n'))
  170. def prepare_pdf(self, filename):
  171. self.prepare_txt("/tmp/source.txt")
  172. cmd = "convert text:/tmp/source.txt {}".format(filename)
  173. try:
  174. self.loop.run_until_complete(
  175. self.source_vm.run_for_stdio(cmd))
  176. except subprocess.CalledProcessError as e:
  177. self.fail('{} failed: {}'.format(cmd, e.stderr.decode()))
  178. def prepare_doc(self, filename):
  179. self.prepare_txt("/tmp/source.txt")
  180. cmd = "unoconv -f doc -o {} /tmp/source.txt".format(filename)
  181. try:
  182. self.loop.run_until_complete(
  183. self.source_vm.run_for_stdio(cmd))
  184. except subprocess.CalledProcessError as e:
  185. if e.returncode == 127:
  186. self.skipTest("unoconv not installed".format(cmd))
  187. self.skipTest("Failed to run '{}': {}".format(cmd,
  188. e.stderr.decode()))
  189. def prepare_pptx(self, filename):
  190. self.prepare_txt("/tmp/source.txt")
  191. cmd = "unoconv -f pptx -o {} /tmp/source.txt".format(filename)
  192. try:
  193. self.loop.run_until_complete(
  194. self.source_vm.run_for_stdio(cmd))
  195. except subprocess.CalledProcessError as e:
  196. if e.returncode == 127:
  197. self.skipTest("unoconv not installed".format(cmd))
  198. self.skipTest("Failed to run '{}': {}".format(cmd,
  199. e.stderr.decode()))
  200. def prepare_png(self, filename):
  201. self.prepare_txt("/tmp/source.txt")
  202. cmd = "convert text:/tmp/source.txt {}".format(filename)
  203. try:
  204. self.loop.run_until_complete(
  205. self.source_vm.run_for_stdio(cmd))
  206. except subprocess.CalledProcessError as e:
  207. if e.returncode == 127:
  208. self.skipTest("convert not installed".format(cmd))
  209. self.skipTest("Failed to run '{}': {}".format(cmd,
  210. e.stderr.decode()))
  211. def prepare_jpg(self, filename):
  212. self.prepare_txt("/tmp/source.txt")
  213. cmd = "convert text:/tmp/source.txt {}".format(filename)
  214. try:
  215. self.loop.run_until_complete(
  216. self.source_vm.run_for_stdio(cmd))
  217. except subprocess.CalledProcessError as e:
  218. if e.returncode == 127:
  219. self.skipTest("convert not installed".format(cmd))
  220. self.skipTest("Failed to run '{}': {}".format(cmd,
  221. e.stderr.decode()))
  222. def test_000_txt(self):
  223. filename = "/home/user/test_file.txt"
  224. self.prepare_txt(filename)
  225. self.open_file_and_check_viewer(filename, ["vim", "user@"],
  226. ["gedit", "emacs", "libreoffice"])
  227. def test_001_pdf(self):
  228. filename = "/home/user/test_file.pdf"
  229. self.prepare_pdf(filename)
  230. self.open_file_and_check_viewer(filename, [],
  231. ["evince"])
  232. def test_002_doc(self):
  233. filename = "/home/user/test_file.doc"
  234. self.prepare_doc(filename)
  235. self.open_file_and_check_viewer(filename, [],
  236. ["libreoffice", "abiword"])
  237. def test_003_pptx(self):
  238. filename = "/home/user/test_file.pptx"
  239. self.prepare_pptx(filename)
  240. self.open_file_and_check_viewer(filename, [],
  241. ["libreoffice"])
  242. def test_004_png(self):
  243. filename = "/home/user/test_file.png"
  244. self.prepare_png(filename)
  245. self.open_file_and_check_viewer(filename, [],
  246. ["shotwell", "eog", "display"])
  247. def test_005_jpg(self):
  248. filename = "/home/user/test_file.jpg"
  249. self.prepare_jpg(filename)
  250. self.open_file_and_check_viewer(filename, [],
  251. ["shotwell", "eog", "display"])
  252. def test_006_jpeg(self):
  253. filename = "/home/user/test_file.jpeg"
  254. self.prepare_jpg(filename)
  255. self.open_file_and_check_viewer(filename, [],
  256. ["shotwell", "eog", "display"])
  257. def test_010_url(self):
  258. self.open_file_and_check_viewer("https://www.qubes-os.org/", [],
  259. ["Firefox", "Iceweasel", "Navigator"])
  260. def test_100_txt_dispvm(self):
  261. filename = "/home/user/test_file.txt"
  262. self.prepare_txt(filename)
  263. self.open_file_and_check_viewer(filename, ["vim", "user@"],
  264. ["gedit", "emacs", "libreoffice"],
  265. dispvm=True)
  266. def test_101_pdf_dispvm(self):
  267. filename = "/home/user/test_file.pdf"
  268. self.prepare_pdf(filename)
  269. self.open_file_and_check_viewer(filename, [],
  270. ["evince"],
  271. dispvm=True)
  272. def test_102_doc_dispvm(self):
  273. filename = "/home/user/test_file.doc"
  274. self.prepare_doc(filename)
  275. self.open_file_and_check_viewer(filename, [],
  276. ["libreoffice", "abiword"],
  277. dispvm=True)
  278. def test_103_pptx_dispvm(self):
  279. filename = "/home/user/test_file.pptx"
  280. self.prepare_pptx(filename)
  281. self.open_file_and_check_viewer(filename, [],
  282. ["libreoffice"],
  283. dispvm=True)
  284. def test_104_png_dispvm(self):
  285. filename = "/home/user/test_file.png"
  286. self.prepare_png(filename)
  287. self.open_file_and_check_viewer(filename, [],
  288. ["shotwell", "eog", "display"],
  289. dispvm=True)
  290. def test_105_jpg_dispvm(self):
  291. filename = "/home/user/test_file.jpg"
  292. self.prepare_jpg(filename)
  293. self.open_file_and_check_viewer(filename, [],
  294. ["shotwell", "eog", "display"],
  295. dispvm=True)
  296. def test_106_jpeg_dispvm(self):
  297. filename = "/home/user/test_file.jpeg"
  298. self.prepare_jpg(filename)
  299. self.open_file_and_check_viewer(filename, [],
  300. ["shotwell", "eog", "display"],
  301. dispvm=True)
  302. def test_110_url_dispvm(self):
  303. self.open_file_and_check_viewer("https://www.qubes-os.org/", [],
  304. ["Firefox", "Iceweasel", "Navigator"],
  305. dispvm=True)
  306. def create_testcases_for_templates():
  307. return qubes.tests.create_testcases_for_templates('TC_50_MimeHandlers',
  308. TC_50_MimeHandlers, qubes.tests.SystemTestCase,
  309. module=sys.modules[__name__])
  310. def load_tests(loader, tests, pattern):
  311. tests.addTests(loader.loadTestsFromNames(
  312. create_testcases_for_templates()))
  313. return tests
  314. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)