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.

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