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.

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