test_unicode.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Notes about unicode handling in psutil
  6. ======================================.
  7. Starting from version 5.3.0 psutil adds unicode support, see:
  8. https://github.com/giampaolo/psutil/issues/1040
  9. The notes below apply to *any* API returning a string such as
  10. process exe(), cwd() or username():
  11. * all strings are encoded by using the OS filesystem encoding
  12. (sys.getfilesystemencoding()) which varies depending on the platform
  13. (e.g. "UTF-8" on macOS, "mbcs" on Win)
  14. * no API call is supposed to crash with UnicodeDecodeError
  15. * instead, in case of badly encoded data returned by the OS, the
  16. following error handlers are used to replace the corrupted characters in
  17. the string:
  18. * sys.getfilesystemencodeerrors() or "surrogatescape" on POSIX and
  19. "replace" on Windows.
  20. For a detailed explanation of how psutil handles unicode see #1040.
  21. Tests
  22. =====
  23. List of APIs returning or dealing with a string:
  24. ('not tested' means they are not tested to deal with non-ASCII strings):
  25. * Process.cmdline()
  26. * Process.cwd()
  27. * Process.environ()
  28. * Process.exe()
  29. * Process.memory_maps()
  30. * Process.name()
  31. * Process.net_connections('unix')
  32. * Process.open_files()
  33. * Process.username() (not tested)
  34. * disk_io_counters() (not tested)
  35. * disk_partitions() (not tested)
  36. * disk_usage(str)
  37. * net_connections('unix')
  38. * net_if_addrs() (not tested)
  39. * net_if_stats() (not tested)
  40. * net_io_counters() (not tested)
  41. * sensors_fans() (not tested)
  42. * sensors_temperatures() (not tested)
  43. * users() (not tested)
  44. * WindowsService.binpath() (not tested)
  45. * WindowsService.description() (not tested)
  46. * WindowsService.display_name() (not tested)
  47. * WindowsService.name() (not tested)
  48. * WindowsService.status() (not tested)
  49. * WindowsService.username() (not tested)
  50. In here we create a unicode path with a funky non-ASCII name and (where
  51. possible) make psutil return it back (e.g. on name(), exe(), open_files(),
  52. etc.) and make sure that:
  53. * psutil never crashes with UnicodeDecodeError
  54. * the returned path matches
  55. """
  56. import os
  57. import shutil
  58. import warnings
  59. from contextlib import closing
  60. import psutil
  61. from psutil import BSD
  62. from psutil import MACOS
  63. from psutil import NETBSD
  64. from psutil import OPENBSD
  65. from psutil import POSIX
  66. from psutil import WINDOWS
  67. from psutil.tests import ASCII_FS
  68. from psutil.tests import CI_TESTING
  69. from psutil.tests import HAS_ENVIRON
  70. from psutil.tests import HAS_MEMORY_MAPS
  71. from psutil.tests import HAS_NET_CONNECTIONS_UNIX
  72. from psutil.tests import INVALID_UNICODE_SUFFIX
  73. from psutil.tests import PYPY
  74. from psutil.tests import TESTFN_PREFIX
  75. from psutil.tests import UNICODE_SUFFIX
  76. from psutil.tests import PsutilTestCase
  77. from psutil.tests import bind_unix_socket
  78. from psutil.tests import chdir
  79. from psutil.tests import copyload_shared_lib
  80. from psutil.tests import create_py_exe
  81. from psutil.tests import get_testfn
  82. from psutil.tests import pytest
  83. from psutil.tests import safe_mkdir
  84. from psutil.tests import safe_rmpath
  85. from psutil.tests import skip_on_access_denied
  86. from psutil.tests import spawn_subproc
  87. from psutil.tests import terminate
  88. def try_unicode(suffix):
  89. """Return True if both the fs and the subprocess module can
  90. deal with a unicode file name.
  91. """
  92. sproc = None
  93. testfn = get_testfn(suffix=suffix)
  94. try:
  95. safe_rmpath(testfn)
  96. create_py_exe(testfn)
  97. sproc = spawn_subproc(cmd=[testfn])
  98. shutil.copyfile(testfn, testfn + '-2')
  99. safe_rmpath(testfn + '-2')
  100. except (UnicodeEncodeError, OSError):
  101. return False
  102. else:
  103. return True
  104. finally:
  105. if sproc is not None:
  106. terminate(sproc)
  107. safe_rmpath(testfn)
  108. # ===================================================================
  109. # FS APIs
  110. # ===================================================================
  111. class BaseUnicodeTest(PsutilTestCase):
  112. funky_suffix = None
  113. @classmethod
  114. def setUpClass(cls):
  115. super().setUpClass()
  116. cls.skip_tests = False
  117. cls.funky_name = None
  118. if cls.funky_suffix is not None:
  119. if not try_unicode(cls.funky_suffix):
  120. cls.skip_tests = True
  121. else:
  122. cls.funky_name = get_testfn(suffix=cls.funky_suffix)
  123. create_py_exe(cls.funky_name)
  124. def setUp(self):
  125. super().setUp()
  126. if self.skip_tests:
  127. return pytest.skip("can't handle unicode str")
  128. @pytest.mark.xdist_group(name="serial")
  129. @pytest.mark.skipif(ASCII_FS, reason="ASCII fs")
  130. class TestFSAPIs(BaseUnicodeTest):
  131. """Test FS APIs with a funky, valid, UTF8 path name."""
  132. funky_suffix = UNICODE_SUFFIX
  133. def expect_exact_path_match(self):
  134. with warnings.catch_warnings():
  135. warnings.simplefilter("ignore")
  136. return self.funky_name in os.listdir(".")
  137. # ---
  138. def test_proc_exe(self):
  139. cmd = [
  140. self.funky_name,
  141. "-c",
  142. "import time; [time.sleep(0.1) for x in range(100)]",
  143. ]
  144. subp = self.spawn_subproc(cmd)
  145. p = psutil.Process(subp.pid)
  146. exe = p.exe()
  147. assert isinstance(exe, str)
  148. if self.expect_exact_path_match():
  149. assert os.path.normcase(exe) == os.path.normcase(self.funky_name)
  150. def test_proc_name(self):
  151. cmd = [
  152. self.funky_name,
  153. "-c",
  154. "import time; [time.sleep(0.1) for x in range(100)]",
  155. ]
  156. subp = self.spawn_subproc(cmd)
  157. name = psutil.Process(subp.pid).name()
  158. assert isinstance(name, str)
  159. if self.expect_exact_path_match():
  160. assert name == os.path.basename(self.funky_name)
  161. def test_proc_cmdline(self):
  162. cmd = [
  163. self.funky_name,
  164. "-c",
  165. "import time; [time.sleep(0.1) for x in range(100)]",
  166. ]
  167. subp = self.spawn_subproc(cmd)
  168. p = psutil.Process(subp.pid)
  169. cmdline = p.cmdline()
  170. for part in cmdline:
  171. assert isinstance(part, str)
  172. if self.expect_exact_path_match():
  173. assert cmdline == cmd
  174. def test_proc_cwd(self):
  175. dname = self.funky_name + "2"
  176. self.addCleanup(safe_rmpath, dname)
  177. safe_mkdir(dname)
  178. with chdir(dname):
  179. p = psutil.Process()
  180. cwd = p.cwd()
  181. assert isinstance(p.cwd(), str)
  182. if self.expect_exact_path_match():
  183. assert cwd == dname
  184. @pytest.mark.skipif(PYPY and WINDOWS, reason="fails on PYPY + WINDOWS")
  185. @pytest.mark.skipif(
  186. NETBSD or OPENBSD, reason="broken on NETBSD or OPENBSD"
  187. )
  188. def test_proc_open_files(self):
  189. p = psutil.Process()
  190. start = set(p.open_files())
  191. with open(self.funky_name, 'rb'):
  192. new = set(p.open_files())
  193. path = (new - start).pop().path
  194. assert isinstance(path, str)
  195. if BSD and not path:
  196. # XXX - see https://github.com/giampaolo/psutil/issues/595
  197. return pytest.skip("open_files on BSD is broken")
  198. if self.expect_exact_path_match():
  199. assert os.path.normcase(path) == os.path.normcase(self.funky_name)
  200. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  201. @pytest.mark.skipif(
  202. not HAS_NET_CONNECTIONS_UNIX, reason="can't list UNIX sockets"
  203. )
  204. def test_proc_net_connections(self):
  205. name = self.get_testfn(suffix=self.funky_suffix)
  206. sock = bind_unix_socket(name)
  207. with closing(sock):
  208. conn = psutil.Process().net_connections('unix')[0]
  209. assert isinstance(conn.laddr, str)
  210. if not conn.laddr and MACOS and CI_TESTING:
  211. return pytest.skip("unreliable on OSX")
  212. assert conn.laddr == name
  213. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  214. @pytest.mark.skipif(
  215. not HAS_NET_CONNECTIONS_UNIX, reason="can't list UNIX sockets"
  216. )
  217. @skip_on_access_denied()
  218. def test_net_connections(self):
  219. def find_sock(cons):
  220. for conn in cons:
  221. if os.path.basename(conn.laddr).startswith(TESTFN_PREFIX):
  222. return conn
  223. raise ValueError("connection not found")
  224. name = self.get_testfn(suffix=self.funky_suffix)
  225. sock = bind_unix_socket(name)
  226. with closing(sock):
  227. cons = psutil.net_connections(kind='unix')
  228. conn = find_sock(cons)
  229. assert isinstance(conn.laddr, str)
  230. assert conn.laddr == name
  231. def test_disk_usage(self):
  232. dname = self.funky_name + "2"
  233. self.addCleanup(safe_rmpath, dname)
  234. safe_mkdir(dname)
  235. psutil.disk_usage(dname)
  236. @pytest.mark.skipif(not HAS_MEMORY_MAPS, reason="not supported")
  237. def test_memory_maps(self):
  238. with copyload_shared_lib(suffix=self.funky_suffix) as funky_path:
  239. def normpath(p):
  240. return os.path.realpath(os.path.normcase(p))
  241. libpaths = [
  242. normpath(x.path) for x in psutil.Process().memory_maps()
  243. ]
  244. # ...just to have a clearer msg in case of failure
  245. libpaths = [x for x in libpaths if TESTFN_PREFIX in x]
  246. assert normpath(funky_path) in libpaths
  247. for path in libpaths:
  248. assert isinstance(path, str)
  249. @pytest.mark.skipif(CI_TESTING, reason="unreliable on CI")
  250. class TestFSAPIsWithInvalidPath(TestFSAPIs):
  251. """Test FS APIs with a funky, invalid path name."""
  252. funky_suffix = INVALID_UNICODE_SUFFIX
  253. def expect_exact_path_match(self):
  254. return not MACOS
  255. # ===================================================================
  256. # Non fs APIs
  257. # ===================================================================
  258. class TestNonFSAPIS(BaseUnicodeTest):
  259. """Unicode tests for non fs-related APIs."""
  260. funky_suffix = UNICODE_SUFFIX
  261. @pytest.mark.skipif(not HAS_ENVIRON, reason="not supported")
  262. @pytest.mark.skipif(PYPY and WINDOWS, reason="segfaults on PYPY + WINDOWS")
  263. def test_proc_environ(self):
  264. # Note: differently from others, this test does not deal
  265. # with fs paths.
  266. env = os.environ.copy()
  267. env['FUNNY_ARG'] = self.funky_suffix
  268. sproc = self.spawn_subproc(env=env)
  269. p = psutil.Process(sproc.pid)
  270. env = p.environ()
  271. for k, v in env.items():
  272. assert isinstance(k, str)
  273. assert isinstance(v, str)
  274. assert env['FUNNY_ARG'] == self.funky_suffix