[stem/master] Decode cookie path with filesystem encoding first

commit 1f90729003490c9d12c7af95041f8de56f9cf766 Author: Damian Johnson <atagar@torproject.org> Date: Tue Aug 25 16:22:16 2020 -0700 Decode cookie path with filesystem encoding first Oops, I iterated over a set to deduplicate if our filesystem encoding is utf-8 or latin-1. However, there's no harm in decoding twice and doing so inconsistently broke our unit test because set order is non-deterministic (so we sometimes decoded our unicode path as latin-1). ====================================================================== FAIL: test_unicode_cookie ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python3.7/unittest/mock.py", line 1204, in patched return func(*args, **keywargs) File "/srv/jenkins-workspace/workspace/stem-tor-ci/test/unit/response/protocolinfo.py", line 160, in test_unicode_cookie self.assertEqual(EXPECTED_UNICODE_PATH, control_message.cookie_path) AssertionError: '/home/user/文档/tor-browser_en-US/Browser/TorBrowser/D[23 chars]okie' != '/home/user/æ\x96\x87æ¡£/tor-browser_en-US/Browser/To[33 chars]okie' - /home/user/文档/tor-browser_en-US/Browser/TorBrowser/Data/Tor/control_auth_cookie ? ^^ + /home/user/文档/tor-browser_en-US/Browser/TorBrowser/Data/Tor/control_auth_cookie ? ^^^^^^ --- stem/response/protocolinfo.py | 7 +++---- test/unit/response/protocolinfo.py | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/stem/response/protocolinfo.py b/stem/response/protocolinfo.py index 65cb2d8c..ca3a6203 100644 --- a/stem/response/protocolinfo.py +++ b/stem/response/protocolinfo.py @@ -110,12 +110,11 @@ class ProtocolInfoResponse(stem.response.ControlMessage): if line.is_next_mapping('COOKIEFILE', True, True): path = line._pop_mapping_bytes(True, True)[1] - # re-encode this path from our filesystem's encoding (with common - # fallbacks) to unicode + # fall back if our filesystem encoding doesn't work - for encoding in set([sys.getfilesystemencoding(), 'utf-8', 'latin-1']): + for encoding in [sys.getfilesystemencoding(), 'utf-8', 'latin-1']: try: - self.cookie_path = stem.util.str_tools._to_unicode(path.decode(encoding)) + self.cookie_path = path.decode(encoding) break except ValueError: pass diff --git a/test/unit/response/protocolinfo.py b/test/unit/response/protocolinfo.py index 69c54413..349507d1 100644 --- a/test/unit/response/protocolinfo.py +++ b/test/unit/response/protocolinfo.py @@ -150,7 +150,7 @@ class TestProtocolInfoResponse(unittest.TestCase): self.assertEqual((), control_message.unknown_auth_methods) self.assertEqual(None, control_message.cookie_path) - @patch('sys.getfilesystemencoding', Mock(return_value = 'UTF-8')) + @patch('sys.getfilesystemencoding', Mock(return_value = 'utf-8')) def test_unicode_cookie(self): """ Checks an authentication cookie with a unicode path. @@ -159,7 +159,7 @@ class TestProtocolInfoResponse(unittest.TestCase): control_message = ControlMessage.from_str(UNICODE_COOKIE_PATH, 'PROTOCOLINFO', normalize = True) self.assertEqual(EXPECTED_UNICODE_PATH, control_message.cookie_path) - @patch('sys.getfilesystemencoding', Mock(return_value = 'UTF-8')) + @patch('sys.getfilesystemencoding', Mock(return_value = 'utf-8')) def test_latin_cookie_path(self): """ Parse a latin-1 path when our filesystem is configured for unicode.
participants (1)
-
atagar@torproject.org