]> jfr.im git - yt-dlp.git/blame - test/test_youtube_signature.py
Fix YouTubeDL test
[yt-dlp.git] / test / test_youtube_signature.py
CommitLineData
e0df6211
PH
1#!/usr/bin/env python
2
44a5f171
PH
3# Allow direct execution
4import os
e0df6211
PH
5import sys
6import unittest
44a5f171 7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
e0df6211 8
44a5f171
PH
9from test.helper import global_setup
10global_setup()
11
12
13import io
14import re
15import string
e0df6211
PH
16
17from youtube_dl.extractor import YoutubeIE
18from youtube_dl.utils import compat_str, compat_urlretrieve
19
20_TESTS = [
21 (
22 u'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
23 u'js',
24 86,
25 u'>=<;:/.-[+*)(\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBA\\yxwvutsrqponmlkjihgfedcba987654321',
26 ),
27 (
28 u'https://s.ytimg.com/yts/jsbin/html5player-vfldJ8xgI.js',
29 u'js',
30 85,
31 u'3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@',
32 ),
33 (
34 u'https://s.ytimg.com/yts/swfbin/watch_as3-vflg5GhxU.swf',
35 u'swf',
36 82,
95dbd2f9 37 u':/.-,+*)=\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBAzyxw>utsrqponmlkjihgfedcba987654321'
e0df6211
PH
38 ),
39]
40
41
42class TestSignature(unittest.TestCase):
43 def setUp(self):
44 TEST_DIR = os.path.dirname(os.path.abspath(__file__))
45 self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
46 if not os.path.exists(self.TESTDATA_DIR):
47 os.mkdir(self.TESTDATA_DIR)
48
49
45f4a76d 50def make_tfunc(url, stype, sig_length, expected_sig):
e0df6211
PH
51 basename = url.rpartition('/')[2]
52 m = re.match(r'.*-([a-zA-Z0-9_-]+)\.[a-z]+$', basename)
53 assert m, '%r should follow URL format' % basename
54 test_id = m.group(1)
55
56 def test_func(self):
57 fn = os.path.join(self.TESTDATA_DIR, basename)
58
59 if not os.path.exists(fn):
60 compat_urlretrieve(url, fn)
61
62 ie = YoutubeIE()
63 if stype == 'js':
64 with io.open(fn, encoding='utf-8') as testf:
65 jscode = testf.read()
66 func = ie._parse_sig_js(jscode)
67 else:
68 assert stype == 'swf'
69 with open(fn, 'rb') as testf:
70 swfcode = testf.read()
71 func = ie._parse_sig_swf(swfcode)
72 src_sig = compat_str(string.printable[:sig_length])
73 got_sig = func(src_sig)
74 self.assertEqual(got_sig, expected_sig)
75
76 test_func.__name__ = str('test_signature_' + stype + '_' + test_id)
77 setattr(TestSignature, test_func.__name__, test_func)
78
79for test_spec in _TESTS:
45f4a76d 80 make_tfunc(*test_spec)
e0df6211
PH
81
82
83if __name__ == '__main__':
84 unittest.main()