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.

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