]>
Commit | Line | Data |
---|---|---|
1 | import unittest | |
2 | from datetime import datetime, timezone | |
3 | ||
4 | from yt_dlp import cookies | |
5 | from yt_dlp.cookies import ( | |
6 | LinuxChromeCookieDecryptor, | |
7 | MacChromeCookieDecryptor, | |
8 | WindowsChromeCookieDecryptor, | |
9 | parse_safari_cookies, | |
10 | pbkdf2_sha1, | |
11 | ) | |
12 | ||
13 | ||
14 | class Logger: | |
15 | def debug(self, message): | |
16 | print(f'[verbose] {message}') | |
17 | ||
18 | def info(self, message): | |
19 | print(message) | |
20 | ||
21 | def warning(self, message, only_once=False): | |
22 | self.error(message) | |
23 | ||
24 | def error(self, message): | |
25 | raise Exception(message) | |
26 | ||
27 | ||
28 | class MonkeyPatch: | |
29 | def __init__(self, module, temporary_values): | |
30 | self._module = module | |
31 | self._temporary_values = temporary_values | |
32 | self._backup_values = {} | |
33 | ||
34 | def __enter__(self): | |
35 | for name, temp_value in self._temporary_values.items(): | |
36 | self._backup_values[name] = getattr(self._module, name) | |
37 | setattr(self._module, name, temp_value) | |
38 | ||
39 | def __exit__(self, exc_type, exc_val, exc_tb): | |
40 | for name, backup_value in self._backup_values.items(): | |
41 | setattr(self._module, name, backup_value) | |
42 | ||
43 | ||
44 | class TestCookies(unittest.TestCase): | |
45 | def test_chrome_cookie_decryptor_linux_derive_key(self): | |
46 | key = LinuxChromeCookieDecryptor.derive_key(b'abc') | |
47 | self.assertEqual(key, b'7\xa1\xec\xd4m\xfcA\xc7\xb19Z\xd0\x19\xdcM\x17') | |
48 | ||
49 | def test_chrome_cookie_decryptor_mac_derive_key(self): | |
50 | key = MacChromeCookieDecryptor.derive_key(b'abc') | |
51 | self.assertEqual(key, b'Y\xe2\xc0\xd0P\xf6\xf4\xe1l\xc1\x8cQ\xcb|\xcdY') | |
52 | ||
53 | def test_chrome_cookie_decryptor_linux_v10(self): | |
54 | with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b''}): | |
55 | encrypted_value = b'v10\xccW%\xcd\xe6\xe6\x9fM" \xa7\xb0\xca\xe4\x07\xd6' | |
56 | value = 'USD' | |
57 | decryptor = LinuxChromeCookieDecryptor('Chrome', Logger()) | |
58 | self.assertEqual(decryptor.decrypt(encrypted_value), value) | |
59 | ||
60 | def test_chrome_cookie_decryptor_linux_v11(self): | |
61 | with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b'', | |
62 | 'KEYRING_AVAILABLE': True}): | |
63 | 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' | |
64 | value = 'tz=Europe.London' | |
65 | decryptor = LinuxChromeCookieDecryptor('Chrome', Logger()) | |
66 | self.assertEqual(decryptor.decrypt(encrypted_value), value) | |
67 | ||
68 | def test_chrome_cookie_decryptor_windows_v10(self): | |
69 | with MonkeyPatch(cookies, { | |
70 | '_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&' | |
71 | }): | |
72 | 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=' | |
73 | value = '32101439' | |
74 | decryptor = WindowsChromeCookieDecryptor('', Logger()) | |
75 | self.assertEqual(decryptor.decrypt(encrypted_value), value) | |
76 | ||
77 | def test_chrome_cookie_decryptor_mac_v10(self): | |
78 | with MonkeyPatch(cookies, {'_get_mac_keyring_password': lambda *args, **kwargs: b'6eIDUdtKAacvlHwBVwvg/Q=='}): | |
79 | encrypted_value = b'v10\xb3\xbe\xad\xa1[\x9fC\xa1\x98\xe0\x9a\x01\xd9\xcf\xbfc' | |
80 | value = '2021-06-01-22' | |
81 | decryptor = MacChromeCookieDecryptor('', Logger()) | |
82 | self.assertEqual(decryptor.decrypt(encrypted_value), value) | |
83 | ||
84 | def test_safari_cookie_parsing(self): | |
85 | cookies = \ | |
86 | 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' \ | |
87 | b'\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x008\x00\x00\x00B\x00\x00\x00F\x00\x00\x00H' \ | |
88 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x03\xa5>\xc3A\x00\x00\x80\xc3\x07:\xc3A' \ | |
89 | b'localhost\x00foo\x00/\x00test%20%3Bcookie\x00\x00\x00\x054\x07\x17 \x05\x00\x00\x00Kbplist00\xd1\x01' \ | |
90 | b'\x02_\x10\x18NSHTTPCookieAcceptPolicy\x10\x02\x08\x0b&\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00' \ | |
91 | b'\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00(' | |
92 | ||
93 | jar = parse_safari_cookies(cookies) | |
94 | self.assertEqual(len(jar), 1) | |
95 | cookie = list(jar)[0] | |
96 | self.assertEqual(cookie.domain, 'localhost') | |
97 | self.assertEqual(cookie.port, None) | |
98 | self.assertEqual(cookie.path, '/') | |
99 | self.assertEqual(cookie.name, 'foo') | |
100 | self.assertEqual(cookie.value, 'test%20%3Bcookie') | |
101 | self.assertFalse(cookie.secure) | |
102 | expected_expiration = datetime(2021, 6, 18, 21, 39, 19, tzinfo=timezone.utc) | |
103 | self.assertEqual(cookie.expires, int(expected_expiration.timestamp())) | |
104 | ||
105 | def test_pbkdf2_sha1(self): | |
106 | key = pbkdf2_sha1(b'peanuts', b' ' * 16, 1, 16) | |
107 | self.assertEqual(key, b'g\xe1\x8e\x0fQ\x1c\x9b\xf3\xc9`!\xaa\x90\xd9\xd34') |