test_cookies.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import unittest
  2. from datetime import datetime, timezone
  3. from yt_dlp import cookies
  4. from yt_dlp.cookies import (
  5. LinuxChromeCookieDecryptor,
  6. MacChromeCookieDecryptor,
  7. WindowsChromeCookieDecryptor,
  8. _get_linux_desktop_environment,
  9. _LinuxDesktopEnvironment,
  10. parse_safari_cookies,
  11. pbkdf2_sha1,
  12. )
  13. class Logger:
  14. def debug(self, message, *args, **kwargs):
  15. print(f'[verbose] {message}')
  16. def info(self, message, *args, **kwargs):
  17. print(message)
  18. def warning(self, message, *args, **kwargs):
  19. self.error(message)
  20. def error(self, message, *args, **kwargs):
  21. raise Exception(message)
  22. class MonkeyPatch:
  23. def __init__(self, module, temporary_values):
  24. self._module = module
  25. self._temporary_values = temporary_values
  26. self._backup_values = {}
  27. def __enter__(self):
  28. for name, temp_value in self._temporary_values.items():
  29. self._backup_values[name] = getattr(self._module, name)
  30. setattr(self._module, name, temp_value)
  31. def __exit__(self, exc_type, exc_val, exc_tb):
  32. for name, backup_value in self._backup_values.items():
  33. setattr(self._module, name, backup_value)
  34. class TestCookies(unittest.TestCase):
  35. def test_get_desktop_environment(self):
  36. """ based on https://chromium.googlesource.com/chromium/src/+/refs/heads/main/base/nix/xdg_util_unittest.cc """
  37. test_cases = [
  38. ({}, _LinuxDesktopEnvironment.OTHER),
  39. ({'DESKTOP_SESSION': 'gnome'}, _LinuxDesktopEnvironment.GNOME),
  40. ({'DESKTOP_SESSION': 'mate'}, _LinuxDesktopEnvironment.GNOME),
  41. ({'DESKTOP_SESSION': 'kde4'}, _LinuxDesktopEnvironment.KDE),
  42. ({'DESKTOP_SESSION': 'kde'}, _LinuxDesktopEnvironment.KDE),
  43. ({'DESKTOP_SESSION': 'xfce'}, _LinuxDesktopEnvironment.XFCE),
  44. ({'GNOME_DESKTOP_SESSION_ID': 1}, _LinuxDesktopEnvironment.GNOME),
  45. ({'KDE_FULL_SESSION': 1}, _LinuxDesktopEnvironment.KDE),
  46. ({'XDG_CURRENT_DESKTOP': 'X-Cinnamon'}, _LinuxDesktopEnvironment.CINNAMON),
  47. ({'XDG_CURRENT_DESKTOP': 'GNOME'}, _LinuxDesktopEnvironment.GNOME),
  48. ({'XDG_CURRENT_DESKTOP': 'GNOME:GNOME-Classic'}, _LinuxDesktopEnvironment.GNOME),
  49. ({'XDG_CURRENT_DESKTOP': 'GNOME : GNOME-Classic'}, _LinuxDesktopEnvironment.GNOME),
  50. ({'XDG_CURRENT_DESKTOP': 'Unity', 'DESKTOP_SESSION': 'gnome-fallback'}, _LinuxDesktopEnvironment.GNOME),
  51. ({'XDG_CURRENT_DESKTOP': 'KDE', 'KDE_SESSION_VERSION': '5'}, _LinuxDesktopEnvironment.KDE),
  52. ({'XDG_CURRENT_DESKTOP': 'KDE'}, _LinuxDesktopEnvironment.KDE),
  53. ({'XDG_CURRENT_DESKTOP': 'Pantheon'}, _LinuxDesktopEnvironment.PANTHEON),
  54. ({'XDG_CURRENT_DESKTOP': 'Unity'}, _LinuxDesktopEnvironment.UNITY),
  55. ({'XDG_CURRENT_DESKTOP': 'Unity:Unity7'}, _LinuxDesktopEnvironment.UNITY),
  56. ({'XDG_CURRENT_DESKTOP': 'Unity:Unity8'}, _LinuxDesktopEnvironment.UNITY),
  57. ]
  58. for env, expected_desktop_environment in test_cases:
  59. self.assertEqual(_get_linux_desktop_environment(env), expected_desktop_environment)
  60. def test_chrome_cookie_decryptor_linux_derive_key(self):
  61. key = LinuxChromeCookieDecryptor.derive_key(b'abc')
  62. self.assertEqual(key, b'7\xa1\xec\xd4m\xfcA\xc7\xb19Z\xd0\x19\xdcM\x17')
  63. def test_chrome_cookie_decryptor_mac_derive_key(self):
  64. key = MacChromeCookieDecryptor.derive_key(b'abc')
  65. self.assertEqual(key, b'Y\xe2\xc0\xd0P\xf6\xf4\xe1l\xc1\x8cQ\xcb|\xcdY')
  66. def test_chrome_cookie_decryptor_linux_v10(self):
  67. with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b''}):
  68. encrypted_value = b'v10\xccW%\xcd\xe6\xe6\x9fM" \xa7\xb0\xca\xe4\x07\xd6'
  69. value = 'USD'
  70. decryptor = LinuxChromeCookieDecryptor('Chrome', Logger())
  71. self.assertEqual(decryptor.decrypt(encrypted_value), value)
  72. def test_chrome_cookie_decryptor_linux_v11(self):
  73. with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b''}):
  74. encrypted_value = b'v11#\x81\x10>`w\x8f)\xc0\xb2\xc1\r\xf4\x1al\xdd\x93\xfd\xf8\xf8N\xf2\xa9\x83\xf1\xe9o\x0elVQd'
  75. value = 'tz=Europe.London'
  76. decryptor = LinuxChromeCookieDecryptor('Chrome', Logger())
  77. self.assertEqual(decryptor.decrypt(encrypted_value), value)
  78. def test_chrome_cookie_decryptor_windows_v10(self):
  79. with MonkeyPatch(cookies, {
  80. '_get_windows_v10_key': lambda *args, **kwargs: b'Y\xef\xad\xad\xeerp\xf0Y\xe6\x9b\x12\xc2<z\x16]\n\xbb\xb8\xcb\xd7\x9bA\xc3\x14e\x99{\xd6\xf4&'
  81. }):
  82. encrypted_value = b'v10T\xb8\xf3\xb8\x01\xa7TtcV\xfc\x88\xb8\xb8\xef\x05\xb5\xfd\x18\xc90\x009\xab\xb1\x893\x85)\x87\xe1\xa9-\xa3\xad='
  83. value = '32101439'
  84. decryptor = WindowsChromeCookieDecryptor('', Logger())
  85. self.assertEqual(decryptor.decrypt(encrypted_value), value)
  86. def test_chrome_cookie_decryptor_mac_v10(self):
  87. with MonkeyPatch(cookies, {'_get_mac_keyring_password': lambda *args, **kwargs: b'6eIDUdtKAacvlHwBVwvg/Q=='}):
  88. encrypted_value = b'v10\xb3\xbe\xad\xa1[\x9fC\xa1\x98\xe0\x9a\x01\xd9\xcf\xbfc'
  89. value = '2021-06-01-22'
  90. decryptor = MacChromeCookieDecryptor('', Logger())
  91. self.assertEqual(decryptor.decrypt(encrypted_value), value)
  92. def test_safari_cookie_parsing(self):
  93. cookies = \
  94. b'cook\x00\x00\x00\x01\x00\x00\x00i\x00\x00\x01\x00\x01\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00Y' \
  95. b'\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x008\x00\x00\x00B\x00\x00\x00F\x00\x00\x00H' \
  96. b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x03\xa5>\xc3A\x00\x00\x80\xc3\x07:\xc3A' \
  97. b'localhost\x00foo\x00/\x00test%20%3Bcookie\x00\x00\x00\x054\x07\x17 \x05\x00\x00\x00Kbplist00\xd1\x01' \
  98. b'\x02_\x10\x18NSHTTPCookieAcceptPolicy\x10\x02\x08\x0b&\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00' \
  99. b'\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00('
  100. jar = parse_safari_cookies(cookies)
  101. self.assertEqual(len(jar), 1)
  102. cookie = list(jar)[0]
  103. self.assertEqual(cookie.domain, 'localhost')
  104. self.assertEqual(cookie.port, None)
  105. self.assertEqual(cookie.path, '/')
  106. self.assertEqual(cookie.name, 'foo')
  107. self.assertEqual(cookie.value, 'test%20%3Bcookie')
  108. self.assertFalse(cookie.secure)
  109. expected_expiration = datetime(2021, 6, 18, 21, 39, 19, tzinfo=timezone.utc)
  110. self.assertEqual(cookie.expires, int(expected_expiration.timestamp()))
  111. def test_pbkdf2_sha1(self):
  112. key = pbkdf2_sha1(b'peanuts', b' ' * 16, 1, 16)
  113. self.assertEqual(key, b'g\xe1\x8e\x0fQ\x1c\x9b\xf3\xc9`!\xaa\x90\xd9\xd34')