You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB

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