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.

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