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.

240 lines
9.4 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. DateRange,
  13. encodeFilename,
  14. find_xpath_attr,
  15. fix_xml_ampersands,
  16. get_meta_content,
  17. orderedSet,
  18. PagedList,
  19. parse_duration,
  20. sanitize_filename,
  21. shell_quote,
  22. smuggle_url,
  23. str_to_int,
  24. timeconvert,
  25. unescapeHTML,
  26. unified_strdate,
  27. unsmuggle_url,
  28. url_basename,
  29. xpath_with_ns,
  30. )
  31. if sys.version_info < (3, 0):
  32. _compat_str = lambda b: b.decode('unicode-escape')
  33. else:
  34. _compat_str = lambda s: s
  35. class TestUtil(unittest.TestCase):
  36. def test_timeconvert(self):
  37. self.assertTrue(timeconvert('') is None)
  38. self.assertTrue(timeconvert('bougrg') is None)
  39. def test_sanitize_filename(self):
  40. self.assertEqual(sanitize_filename('abc'), 'abc')
  41. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  42. self.assertEqual(sanitize_filename('123'), '123')
  43. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  44. self.assertFalse('/' in sanitize_filename('abc/de///'))
  45. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
  46. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
  47. self.assertEqual('yes no', sanitize_filename('yes? no'))
  48. self.assertEqual('this - that', sanitize_filename('this: that'))
  49. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  50. aumlaut = _compat_str('\xe4')
  51. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  52. tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
  53. self.assertEqual(sanitize_filename(tests), tests)
  54. forbidden = '"\0\\/'
  55. for fc in forbidden:
  56. for fbc in forbidden:
  57. self.assertTrue(fbc not in sanitize_filename(fc))
  58. def test_sanitize_filename_restricted(self):
  59. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  60. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  61. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  62. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  63. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  64. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  65. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  66. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  67. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  68. tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
  69. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  70. self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
  71. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  72. for fc in forbidden:
  73. for fbc in forbidden:
  74. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  75. # Handle a common case more neatly
  76. self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
  77. self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
  78. # .. but make sure the file name is never empty
  79. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  80. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  81. def test_sanitize_ids(self):
  82. self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  83. self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  84. self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  85. def test_ordered_set(self):
  86. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  87. self.assertEqual(orderedSet([]), [])
  88. self.assertEqual(orderedSet([1]), [1])
  89. #keep the list ordered
  90. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  91. def test_unescape_html(self):
  92. self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
  93. def test_daterange(self):
  94. _20century = DateRange("19000101","20000101")
  95. self.assertFalse("17890714" in _20century)
  96. _ac = DateRange("00010101")
  97. self.assertTrue("19690721" in _ac)
  98. _firstmilenium = DateRange(end="10000101")
  99. self.assertTrue("07110427" in _firstmilenium)
  100. def test_unified_dates(self):
  101. self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
  102. self.assertEqual(unified_strdate('8/7/2009'), '20090708')
  103. self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
  104. self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
  105. def test_find_xpath_attr(self):
  106. testxml = u'''<root>
  107. <node/>
  108. <node x="a"/>
  109. <node x="a" y="c" />
  110. <node x="b" y="d" />
  111. </root>'''
  112. doc = xml.etree.ElementTree.fromstring(testxml)
  113. self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
  114. self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
  115. self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
  116. def test_meta_parser(self):
  117. testhtml = u'''
  118. <head>
  119. <meta name="description" content="foo &amp; bar">
  120. <meta content='Plato' name='author'/>
  121. </head>
  122. '''
  123. get_meta = lambda name: get_meta_content(name, testhtml)
  124. self.assertEqual(get_meta('description'), u'foo & bar')
  125. self.assertEqual(get_meta('author'), 'Plato')
  126. def test_xpath_with_ns(self):
  127. testxml = u'''<root xmlns:media="http://example.com/">
  128. <media:song>
  129. <media:author>The Author</media:author>
  130. <url>http://server.com/download.mp3</url>
  131. </media:song>
  132. </root>'''
  133. doc = xml.etree.ElementTree.fromstring(testxml)
  134. find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
  135. self.assertTrue(find('media:song') is not None)
  136. self.assertEqual(find('media:song/media:author').text, u'The Author')
  137. self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
  138. def test_smuggle_url(self):
  139. data = {u"ö": u"ö", u"abc": [3]}
  140. url = 'https://foo.bar/baz?x=y#a'
  141. smug_url = smuggle_url(url, data)
  142. unsmug_url, unsmug_data = unsmuggle_url(smug_url)
  143. self.assertEqual(url, unsmug_url)
  144. self.assertEqual(data, unsmug_data)
  145. res_url, res_data = unsmuggle_url(url)
  146. self.assertEqual(res_url, url)
  147. self.assertEqual(res_data, None)
  148. def test_shell_quote(self):
  149. args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
  150. self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
  151. def test_str_to_int(self):
  152. self.assertEqual(str_to_int('123,456'), 123456)
  153. self.assertEqual(str_to_int('123.456'), 123456)
  154. def test_url_basename(self):
  155. self.assertEqual(url_basename(u'http://foo.de/'), u'')
  156. self.assertEqual(url_basename(u'http://foo.de/bar/baz'), u'baz')
  157. self.assertEqual(url_basename(u'http://foo.de/bar/baz?x=y'), u'baz')
  158. self.assertEqual(url_basename(u'http://foo.de/bar/baz#x=y'), u'baz')
  159. self.assertEqual(url_basename(u'http://foo.de/bar/baz/'), u'baz')
  160. self.assertEqual(
  161. url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
  162. u'trailer.mp4')
  163. def test_parse_duration(self):
  164. self.assertEqual(parse_duration(None), None)
  165. self.assertEqual(parse_duration('1'), 1)
  166. self.assertEqual(parse_duration('1337:12'), 80232)
  167. self.assertEqual(parse_duration('9:12:43'), 33163)
  168. self.assertEqual(parse_duration('x:y'), None)
  169. def test_fix_xml_ampersands(self):
  170. self.assertEqual(
  171. fix_xml_ampersands('"&x=y&z=a'), '"&amp;x=y&amp;z=a')
  172. self.assertEqual(
  173. fix_xml_ampersands('"&amp;x=y&wrong;&z=a'),
  174. '"&amp;x=y&amp;wrong;&amp;z=a')
  175. self.assertEqual(
  176. fix_xml_ampersands('&amp;&apos;&gt;&lt;&quot;'),
  177. '&amp;&apos;&gt;&lt;&quot;')
  178. self.assertEqual(
  179. fix_xml_ampersands('&#1234;&#x1abC;'), '&#1234;&#x1abC;')
  180. self.assertEqual(fix_xml_ampersands('&#&#'), '&amp;#&amp;#')
  181. def test_paged_list(self):
  182. def testPL(size, pagesize, sliceargs, expected):
  183. def get_page(pagenum):
  184. firstid = pagenum * pagesize
  185. upto = min(size, pagenum * pagesize + pagesize)
  186. for i in range(firstid, upto):
  187. yield i
  188. pl = PagedList(get_page, pagesize)
  189. got = pl.getslice(*sliceargs)
  190. self.assertEqual(got, expected)
  191. testPL(5, 2, (), [0, 1, 2, 3, 4])
  192. testPL(5, 2, (1,), [1, 2, 3, 4])
  193. testPL(5, 2, (2,), [2, 3, 4])
  194. testPL(5, 2, (4,), [4])
  195. testPL(5, 2, (0, 3), [0, 1, 2])
  196. testPL(5, 2, (1, 4), [1, 2, 3])
  197. testPL(5, 2, (2, 99), [2, 3, 4])
  198. testPL(5, 2, (20, 99), [])
  199. if __name__ == '__main__':
  200. unittest.main()