4 # Allow direct execution
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 # Various small unit tests
14 import xml.etree.ElementTree
16 #from youtube_dl.utils import htmlentity_transform
17 from youtube_dl.utils import (
44 if sys.version_info < (3, 0):
45 _compat_str = lambda b: b.decode('unicode-escape')
47 _compat_str = lambda s: s
50 class TestUtil(unittest.TestCase):
51 def test_timeconvert(self):
52 self.assertTrue(timeconvert('') is None)
53 self.assertTrue(timeconvert('bougrg') is None)
55 def test_sanitize_filename(self):
56 self.assertEqual(sanitize_filename('abc'), 'abc')
57 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
59 self.assertEqual(sanitize_filename('123'), '123')
61 self.assertEqual('abc_de', sanitize_filename('abc/de'))
62 self.assertFalse('/' in sanitize_filename('abc/de///'))
64 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
65 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
66 self.assertEqual('yes no', sanitize_filename('yes? no'))
67 self.assertEqual('this - that', sanitize_filename('this: that'))
69 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
70 aumlaut = _compat_str('\xe4')
71 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
72 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
73 self.assertEqual(sanitize_filename(tests), tests)
78 self.assertTrue(fbc not in sanitize_filename(fc))
80 def test_sanitize_filename_restricted(self):
81 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
82 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
84 self.assertEqual(sanitize_filename('123', restricted=True), '123')
86 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
87 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
89 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
90 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
91 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
92 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
94 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
95 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
96 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
98 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
100 for fbc in forbidden:
101 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
103 # Handle a common case more neatly
104 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
105 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
106 # .. but make sure the file name is never empty
107 self.assertTrue(sanitize_filename('-', restricted=True) != '')
108 self.assertTrue(sanitize_filename(':', restricted=True) != '')
110 def test_sanitize_ids(self):
111 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
112 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
113 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
115 def test_ordered_set(self):
116 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
117 self.assertEqual(orderedSet([]), [])
118 self.assertEqual(orderedSet([1]), [1])
119 #keep the list ordered
120 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
122 def test_unescape_html(self):
123 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
125 def test_daterange(self):
126 _20century = DateRange("19000101","20000101")
127 self.assertFalse("17890714" in _20century)
128 _ac = DateRange("00010101")
129 self.assertTrue("19690721" in _ac)
130 _firstmilenium = DateRange(end="10000101")
131 self.assertTrue("07110427" in _firstmilenium)
133 def test_unified_dates(self):
134 self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
135 self.assertEqual(unified_strdate('8/7/2009'), '20090708')
136 self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
137 self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
138 self.assertEqual(unified_strdate('1968-12-10'), '19681210')
140 def test_find_xpath_attr(self):
147 doc = xml.etree.ElementTree.fromstring(testxml)
149 self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
150 self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
151 self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
153 def test_meta_parser(self):
156 <meta name="description" content="foo & bar">
157 <meta content='Plato' name='author'/>
160 get_meta = lambda name: get_meta_content(name, testhtml)
161 self.assertEqual(get_meta('description'), u'foo & bar')
162 self.assertEqual(get_meta('author'), 'Plato')
164 def test_xpath_with_ns(self):
165 testxml = u'''<root xmlns:media="http://example.com/">
167 <media:author>The Author</media:author>
168 <url>http://server.com/download.mp3</url>
171 doc = xml.etree.ElementTree.fromstring(testxml)
172 find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
173 self.assertTrue(find('media:song') is not None)
174 self.assertEqual(find('media:song/media:author').text, u'The Author')
175 self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
177 def test_smuggle_url(self):
178 data = {u"ö": u"ö", u"abc": [3]}
179 url = 'https://foo.bar/baz?x=y#a'
180 smug_url = smuggle_url(url, data)
181 unsmug_url, unsmug_data = unsmuggle_url(smug_url)
182 self.assertEqual(url, unsmug_url)
183 self.assertEqual(data, unsmug_data)
185 res_url, res_data = unsmuggle_url(url)
186 self.assertEqual(res_url, url)
187 self.assertEqual(res_data, None)
189 def test_shell_quote(self):
190 args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
191 self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
193 def test_str_to_int(self):
194 self.assertEqual(str_to_int('123,456'), 123456)
195 self.assertEqual(str_to_int('123.456'), 123456)
197 def test_url_basename(self):
198 self.assertEqual(url_basename(u'http://foo.de/'), u'')
199 self.assertEqual(url_basename(u'http://foo.de/bar/baz'), u'baz')
200 self.assertEqual(url_basename(u'http://foo.de/bar/baz?x=y'), u'baz')
201 self.assertEqual(url_basename(u'http://foo.de/bar/baz#x=y'), u'baz')
202 self.assertEqual(url_basename(u'http://foo.de/bar/baz/'), u'baz')
204 url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
207 def test_parse_duration(self):
208 self.assertEqual(parse_duration(None), None)
209 self.assertEqual(parse_duration('1'), 1)
210 self.assertEqual(parse_duration('1337:12'), 80232)
211 self.assertEqual(parse_duration('9:12:43'), 33163)
212 self.assertEqual(parse_duration('12:00'), 720)
213 self.assertEqual(parse_duration('00:01:01'), 61)
214 self.assertEqual(parse_duration('x:y'), None)
215 self.assertEqual(parse_duration('3h11m53s'), 11513)
216 self.assertEqual(parse_duration('62m45s'), 3765)
217 self.assertEqual(parse_duration('6m59s'), 419)
218 self.assertEqual(parse_duration('49s'), 49)
219 self.assertEqual(parse_duration('0h0m0s'), 0)
220 self.assertEqual(parse_duration('0m0s'), 0)
221 self.assertEqual(parse_duration('0s'), 0)
223 def test_fix_xml_ampersands(self):
225 fix_xml_ampersands('"&x=y&z=a'), '"&x=y&z=a')
227 fix_xml_ampersands('"&x=y&wrong;&z=a'),
228 '"&x=y&wrong;&z=a')
230 fix_xml_ampersands('&'><"'),
231 '&'><"')
233 fix_xml_ampersands('Ӓ᪼'), 'Ӓ᪼')
234 self.assertEqual(fix_xml_ampersands('&#&#'), '&#&#')
236 def test_paged_list(self):
237 def testPL(size, pagesize, sliceargs, expected):
238 def get_page(pagenum):
239 firstid = pagenum * pagesize
240 upto = min(size, pagenum * pagesize + pagesize)
241 for i in range(firstid, upto):
244 pl = PagedList(get_page, pagesize)
245 got = pl.getslice(*sliceargs)
246 self.assertEqual(got, expected)
248 testPL(5, 2, (), [0, 1, 2, 3, 4])
249 testPL(5, 2, (1,), [1, 2, 3, 4])
250 testPL(5, 2, (2,), [2, 3, 4])
251 testPL(5, 2, (4,), [4])
252 testPL(5, 2, (0, 3), [0, 1, 2])
253 testPL(5, 2, (1, 4), [1, 2, 3])
254 testPL(5, 2, (2, 99), [2, 3, 4])
255 testPL(5, 2, (20, 99), [])
257 def test_struct_unpack(self):
258 self.assertEqual(struct_unpack(u'!B', b'\x00'), (0,))
260 def test_read_batch_urls(self):
261 f = io.StringIO(u'''\xef\xbb\xbf foo
264 # More after this line\r
267 self.assertEqual(read_batch_urls(f), [u'foo', u'bar', u'baz', u'bam'])
269 def test_urlencode_postdata(self):
270 data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'})
271 self.assertTrue(isinstance(data, bytes))
273 def test_parse_iso8601(self):
274 self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266)
275 self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266)
276 self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266)
278 def test_strip_jsonp(self):
279 stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);')
280 d = json.loads(stripped)
281 self.assertEqual(d, [{"id": "532cb", "x": 3}])
283 def test_uppercase_escape(self):
284 self.assertEqual(uppercase_escape(u'aä'), u'aä')
285 self.assertEqual(uppercase_escape(u'\\U0001d550'), u'𝕐')
287 if __name__ == '__main__':