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.

181 lines
7.0 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. # Various small unit tests
  9. import xml.etree.ElementTree
  10. #from youtube_dl.utils import htmlentity_transform
  11. from youtube_dl.utils import (
  12. timeconvert,
  13. sanitize_filename,
  14. unescapeHTML,
  15. orderedSet,
  16. DateRange,
  17. unified_strdate,
  18. find_xpath_attr,
  19. get_meta_content,
  20. xpath_with_ns,
  21. smuggle_url,
  22. unsmuggle_url,
  23. shell_quote,
  24. encodeFilename,
  25. )
  26. if sys.version_info < (3, 0):
  27. _compat_str = lambda b: b.decode('unicode-escape')
  28. else:
  29. _compat_str = lambda s: s
  30. class TestUtil(unittest.TestCase):
  31. def test_timeconvert(self):
  32. self.assertTrue(timeconvert('') is None)
  33. self.assertTrue(timeconvert('bougrg') is None)
  34. def test_sanitize_filename(self):
  35. self.assertEqual(sanitize_filename('abc'), 'abc')
  36. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  37. self.assertEqual(sanitize_filename('123'), '123')
  38. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  39. self.assertFalse('/' in sanitize_filename('abc/de///'))
  40. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
  41. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
  42. self.assertEqual('yes no', sanitize_filename('yes? no'))
  43. self.assertEqual('this - that', sanitize_filename('this: that'))
  44. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  45. aumlaut = _compat_str('\xe4')
  46. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  47. tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
  48. self.assertEqual(sanitize_filename(tests), tests)
  49. forbidden = '"\0\\/'
  50. for fc in forbidden:
  51. for fbc in forbidden:
  52. self.assertTrue(fbc not in sanitize_filename(fc))
  53. def test_sanitize_filename_restricted(self):
  54. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  55. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  56. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  57. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  58. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  59. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  60. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  61. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  62. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  63. tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
  64. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  65. self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
  66. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  67. for fc in forbidden:
  68. for fbc in forbidden:
  69. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  70. # Handle a common case more neatly
  71. self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
  72. self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
  73. # .. but make sure the file name is never empty
  74. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  75. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  76. def test_sanitize_ids(self):
  77. self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  78. self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  79. self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  80. def test_ordered_set(self):
  81. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  82. self.assertEqual(orderedSet([]), [])
  83. self.assertEqual(orderedSet([1]), [1])
  84. #keep the list ordered
  85. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  86. def test_unescape_html(self):
  87. self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
  88. def test_daterange(self):
  89. _20century = DateRange("19000101","20000101")
  90. self.assertFalse("17890714" in _20century)
  91. _ac = DateRange("00010101")
  92. self.assertTrue("19690721" in _ac)
  93. _firstmilenium = DateRange(end="10000101")
  94. self.assertTrue("07110427" in _firstmilenium)
  95. def test_unified_dates(self):
  96. self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
  97. self.assertEqual(unified_strdate('8/7/2009'), '20090708')
  98. self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
  99. self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
  100. def test_find_xpath_attr(self):
  101. testxml = u'''<root>
  102. <node/>
  103. <node x="a"/>
  104. <node x="a" y="c" />
  105. <node x="b" y="d" />
  106. </root>'''
  107. doc = xml.etree.ElementTree.fromstring(testxml)
  108. self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
  109. self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
  110. self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
  111. def test_meta_parser(self):
  112. testhtml = u'''
  113. <head>
  114. <meta name="description" content="foo &amp; bar">
  115. <meta content='Plato' name='author'/>
  116. </head>
  117. '''
  118. get_meta = lambda name: get_meta_content(name, testhtml)
  119. self.assertEqual(get_meta('description'), u'foo & bar')
  120. self.assertEqual(get_meta('author'), 'Plato')
  121. def test_xpath_with_ns(self):
  122. testxml = u'''<root xmlns:media="http://example.com/">
  123. <media:song>
  124. <media:author>The Author</media:author>
  125. <url>http://server.com/download.mp3</url>
  126. </media:song>
  127. </root>'''
  128. doc = xml.etree.ElementTree.fromstring(testxml)
  129. find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
  130. self.assertTrue(find('media:song') is not None)
  131. self.assertEqual(find('media:song/media:author').text, u'The Author')
  132. self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
  133. def test_smuggle_url(self):
  134. data = {u"ö": u"ö", u"abc": [3]}
  135. url = 'https://foo.bar/baz?x=y#a'
  136. smug_url = smuggle_url(url, data)
  137. unsmug_url, unsmug_data = unsmuggle_url(smug_url)
  138. self.assertEqual(url, unsmug_url)
  139. self.assertEqual(data, unsmug_data)
  140. res_url, res_data = unsmuggle_url(url)
  141. self.assertEqual(res_url, url)
  142. self.assertEqual(res_data, None)
  143. def test_shell_quote(self):
  144. args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
  145. self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
  146. if __name__ == '__main__':
  147. unittest.main()