]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python | |
2 | # coding: utf-8 | |
3 | ||
4 | from __future__ import unicode_literals | |
5 | ||
6 | # Allow direct execution | |
7 | import os | |
8 | import sys | |
9 | import unittest | |
10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
11 | ||
12 | ||
13 | # Various small unit tests | |
14 | import io | |
15 | import json | |
16 | import xml.etree.ElementTree | |
17 | ||
18 | from youtube_dl.utils import ( | |
19 | clean_html, | |
20 | DateRange, | |
21 | encodeFilename, | |
22 | find_xpath_attr, | |
23 | fix_xml_ampersands, | |
24 | orderedSet, | |
25 | OnDemandPagedList, | |
26 | InAdvancePagedList, | |
27 | parse_duration, | |
28 | read_batch_urls, | |
29 | sanitize_filename, | |
30 | shell_quote, | |
31 | smuggle_url, | |
32 | str_to_int, | |
33 | struct_unpack, | |
34 | timeconvert, | |
35 | unescapeHTML, | |
36 | unified_strdate, | |
37 | unsmuggle_url, | |
38 | url_basename, | |
39 | urlencode_postdata, | |
40 | xpath_with_ns, | |
41 | parse_iso8601, | |
42 | strip_jsonp, | |
43 | uppercase_escape, | |
44 | limit_length, | |
45 | escape_rfc3986, | |
46 | escape_url, | |
47 | js_to_json, | |
48 | get_filesystem_encoding, | |
49 | ) | |
50 | ||
51 | ||
52 | class TestUtil(unittest.TestCase): | |
53 | def test_timeconvert(self): | |
54 | self.assertTrue(timeconvert('') is None) | |
55 | self.assertTrue(timeconvert('bougrg') is None) | |
56 | ||
57 | def test_sanitize_filename(self): | |
58 | self.assertEqual(sanitize_filename('abc'), 'abc') | |
59 | self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e') | |
60 | ||
61 | self.assertEqual(sanitize_filename('123'), '123') | |
62 | ||
63 | self.assertEqual('abc_de', sanitize_filename('abc/de')) | |
64 | self.assertFalse('/' in sanitize_filename('abc/de///')) | |
65 | ||
66 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de')) | |
67 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|')) | |
68 | self.assertEqual('yes no', sanitize_filename('yes? no')) | |
69 | self.assertEqual('this - that', sanitize_filename('this: that')) | |
70 | ||
71 | self.assertEqual(sanitize_filename('AT&T'), 'AT&T') | |
72 | aumlaut = 'ä' | |
73 | self.assertEqual(sanitize_filename(aumlaut), aumlaut) | |
74 | tests = '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430' | |
75 | self.assertEqual(sanitize_filename(tests), tests) | |
76 | ||
77 | forbidden = '"\0\\/' | |
78 | for fc in forbidden: | |
79 | for fbc in forbidden: | |
80 | self.assertTrue(fbc not in sanitize_filename(fc)) | |
81 | ||
82 | def test_sanitize_filename_restricted(self): | |
83 | self.assertEqual(sanitize_filename('abc', restricted=True), 'abc') | |
84 | self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e') | |
85 | ||
86 | self.assertEqual(sanitize_filename('123', restricted=True), '123') | |
87 | ||
88 | self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True)) | |
89 | self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True)) | |
90 | ||
91 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True)) | |
92 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True)) | |
93 | self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True)) | |
94 | self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True)) | |
95 | ||
96 | tests = 'a\xe4b\u4e2d\u56fd\u7684c' | |
97 | self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c') | |
98 | self.assertTrue(sanitize_filename('\xf6', restricted=True) != '') # No empty filename | |
99 | ||
100 | forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#' | |
101 | for fc in forbidden: | |
102 | for fbc in forbidden: | |
103 | self.assertTrue(fbc not in sanitize_filename(fc, restricted=True)) | |
104 | ||
105 | # Handle a common case more neatly | |
106 | self.assertEqual(sanitize_filename('\u5927\u58f0\u5e26 - Song', restricted=True), 'Song') | |
107 | self.assertEqual(sanitize_filename('\u603b\u7edf: Speech', restricted=True), 'Speech') | |
108 | # .. but make sure the file name is never empty | |
109 | self.assertTrue(sanitize_filename('-', restricted=True) != '') | |
110 | self.assertTrue(sanitize_filename(':', restricted=True) != '') | |
111 | ||
112 | def test_sanitize_ids(self): | |
113 | self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw') | |
114 | self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw') | |
115 | self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI') | |
116 | ||
117 | def test_ordered_set(self): | |
118 | self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7]) | |
119 | self.assertEqual(orderedSet([]), []) | |
120 | self.assertEqual(orderedSet([1]), [1]) | |
121 | #keep the list ordered | |
122 | self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1]) | |
123 | ||
124 | def test_unescape_html(self): | |
125 | self.assertEqual(unescapeHTML('%20;'), '%20;') | |
126 | self.assertEqual( | |
127 | unescapeHTML('é'), 'é') | |
128 | ||
129 | def test_daterange(self): | |
130 | _20century = DateRange("19000101","20000101") | |
131 | self.assertFalse("17890714" in _20century) | |
132 | _ac = DateRange("00010101") | |
133 | self.assertTrue("19690721" in _ac) | |
134 | _firstmilenium = DateRange(end="10000101") | |
135 | self.assertTrue("07110427" in _firstmilenium) | |
136 | ||
137 | def test_unified_dates(self): | |
138 | self.assertEqual(unified_strdate('December 21, 2010'), '20101221') | |
139 | self.assertEqual(unified_strdate('8/7/2009'), '20090708') | |
140 | self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214') | |
141 | self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011') | |
142 | self.assertEqual(unified_strdate('1968-12-10'), '19681210') | |
143 | self.assertEqual(unified_strdate('28/01/2014 21:00:00 +0100'), '20140128') | |
144 | ||
145 | def test_find_xpath_attr(self): | |
146 | testxml = '''<root> | |
147 | <node/> | |
148 | <node x="a"/> | |
149 | <node x="a" y="c" /> | |
150 | <node x="b" y="d" /> | |
151 | </root>''' | |
152 | doc = xml.etree.ElementTree.fromstring(testxml) | |
153 | ||
154 | self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None) | |
155 | self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1]) | |
156 | self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2]) | |
157 | ||
158 | def test_xpath_with_ns(self): | |
159 | testxml = '''<root xmlns:media="http://example.com/"> | |
160 | <media:song> | |
161 | <media:author>The Author</media:author> | |
162 | <url>http://server.com/download.mp3</url> | |
163 | </media:song> | |
164 | </root>''' | |
165 | doc = xml.etree.ElementTree.fromstring(testxml) | |
166 | find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'})) | |
167 | self.assertTrue(find('media:song') is not None) | |
168 | self.assertEqual(find('media:song/media:author').text, 'The Author') | |
169 | self.assertEqual(find('media:song/url').text, 'http://server.com/download.mp3') | |
170 | ||
171 | def test_smuggle_url(self): | |
172 | data = {u"ö": u"ö", u"abc": [3]} | |
173 | url = 'https://foo.bar/baz?x=y#a' | |
174 | smug_url = smuggle_url(url, data) | |
175 | unsmug_url, unsmug_data = unsmuggle_url(smug_url) | |
176 | self.assertEqual(url, unsmug_url) | |
177 | self.assertEqual(data, unsmug_data) | |
178 | ||
179 | res_url, res_data = unsmuggle_url(url) | |
180 | self.assertEqual(res_url, url) | |
181 | self.assertEqual(res_data, None) | |
182 | ||
183 | def test_shell_quote(self): | |
184 | args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')] | |
185 | self.assertEqual(shell_quote(args), """ffmpeg -i 'ñ€ß'"'"'.mp4'""") | |
186 | ||
187 | def test_str_to_int(self): | |
188 | self.assertEqual(str_to_int('123,456'), 123456) | |
189 | self.assertEqual(str_to_int('123.456'), 123456) | |
190 | ||
191 | def test_url_basename(self): | |
192 | self.assertEqual(url_basename('http://foo.de/'), '') | |
193 | self.assertEqual(url_basename('http://foo.de/bar/baz'), 'baz') | |
194 | self.assertEqual(url_basename('http://foo.de/bar/baz?x=y'), 'baz') | |
195 | self.assertEqual(url_basename('http://foo.de/bar/baz#x=y'), 'baz') | |
196 | self.assertEqual(url_basename('http://foo.de/bar/baz/'), 'baz') | |
197 | self.assertEqual( | |
198 | url_basename('http://media.w3.org/2010/05/sintel/trailer.mp4'), | |
199 | 'trailer.mp4') | |
200 | ||
201 | def test_parse_duration(self): | |
202 | self.assertEqual(parse_duration(None), None) | |
203 | self.assertEqual(parse_duration('1'), 1) | |
204 | self.assertEqual(parse_duration('1337:12'), 80232) | |
205 | self.assertEqual(parse_duration('9:12:43'), 33163) | |
206 | self.assertEqual(parse_duration('12:00'), 720) | |
207 | self.assertEqual(parse_duration('00:01:01'), 61) | |
208 | self.assertEqual(parse_duration('x:y'), None) | |
209 | self.assertEqual(parse_duration('3h11m53s'), 11513) | |
210 | self.assertEqual(parse_duration('3h 11m 53s'), 11513) | |
211 | self.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513) | |
212 | self.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513) | |
213 | self.assertEqual(parse_duration('62m45s'), 3765) | |
214 | self.assertEqual(parse_duration('6m59s'), 419) | |
215 | self.assertEqual(parse_duration('49s'), 49) | |
216 | self.assertEqual(parse_duration('0h0m0s'), 0) | |
217 | self.assertEqual(parse_duration('0m0s'), 0) | |
218 | self.assertEqual(parse_duration('0s'), 0) | |
219 | self.assertEqual(parse_duration('01:02:03.05'), 3723.05) | |
220 | ||
221 | def test_fix_xml_ampersands(self): | |
222 | self.assertEqual( | |
223 | fix_xml_ampersands('"&x=y&z=a'), '"&x=y&z=a') | |
224 | self.assertEqual( | |
225 | fix_xml_ampersands('"&x=y&wrong;&z=a'), | |
226 | '"&x=y&wrong;&z=a') | |
227 | self.assertEqual( | |
228 | fix_xml_ampersands('&'><"'), | |
229 | '&'><"') | |
230 | self.assertEqual( | |
231 | fix_xml_ampersands('Ӓ᪼'), 'Ӓ᪼') | |
232 | self.assertEqual(fix_xml_ampersands('&#&#'), '&#&#') | |
233 | ||
234 | def test_paged_list(self): | |
235 | def testPL(size, pagesize, sliceargs, expected): | |
236 | def get_page(pagenum): | |
237 | firstid = pagenum * pagesize | |
238 | upto = min(size, pagenum * pagesize + pagesize) | |
239 | for i in range(firstid, upto): | |
240 | yield i | |
241 | ||
242 | pl = OnDemandPagedList(get_page, pagesize) | |
243 | got = pl.getslice(*sliceargs) | |
244 | self.assertEqual(got, expected) | |
245 | ||
246 | iapl = InAdvancePagedList(get_page, size // pagesize + 1, pagesize) | |
247 | got = iapl.getslice(*sliceargs) | |
248 | self.assertEqual(got, expected) | |
249 | ||
250 | testPL(5, 2, (), [0, 1, 2, 3, 4]) | |
251 | testPL(5, 2, (1,), [1, 2, 3, 4]) | |
252 | testPL(5, 2, (2,), [2, 3, 4]) | |
253 | testPL(5, 2, (4,), [4]) | |
254 | testPL(5, 2, (0, 3), [0, 1, 2]) | |
255 | testPL(5, 2, (1, 4), [1, 2, 3]) | |
256 | testPL(5, 2, (2, 99), [2, 3, 4]) | |
257 | testPL(5, 2, (20, 99), []) | |
258 | ||
259 | def test_struct_unpack(self): | |
260 | self.assertEqual(struct_unpack('!B', b'\x00'), (0,)) | |
261 | ||
262 | def test_read_batch_urls(self): | |
263 | f = io.StringIO('''\xef\xbb\xbf foo | |
264 | bar\r | |
265 | baz | |
266 | # More after this line\r | |
267 | ; or after this | |
268 | bam''') | |
269 | self.assertEqual(read_batch_urls(f), ['foo', 'bar', 'baz', 'bam']) | |
270 | ||
271 | def test_urlencode_postdata(self): | |
272 | data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'}) | |
273 | self.assertTrue(isinstance(data, bytes)) | |
274 | ||
275 | def test_parse_iso8601(self): | |
276 | self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266) | |
277 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266) | |
278 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266) | |
279 | self.assertEqual(parse_iso8601('2014-03-23T22:04:26.1234Z'), 1395612266) | |
280 | ||
281 | def test_strip_jsonp(self): | |
282 | stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);') | |
283 | d = json.loads(stripped) | |
284 | self.assertEqual(d, [{"id": "532cb", "x": 3}]) | |
285 | ||
286 | def test_uppercase_escape(self): | |
287 | self.assertEqual(uppercase_escape('aä'), 'aä') | |
288 | self.assertEqual(uppercase_escape('\\U0001d550'), '𝕐') | |
289 | ||
290 | def test_limit_length(self): | |
291 | self.assertEqual(limit_length(None, 12), None) | |
292 | self.assertEqual(limit_length('foo', 12), 'foo') | |
293 | self.assertTrue( | |
294 | limit_length('foo bar baz asd', 12).startswith('foo bar')) | |
295 | self.assertTrue('...' in limit_length('foo bar baz asd', 12)) | |
296 | ||
297 | def test_escape_rfc3986(self): | |
298 | reserved = "!*'();:@&=+$,/?#[]" | |
299 | unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~' | |
300 | self.assertEqual(escape_rfc3986(reserved), reserved) | |
301 | self.assertEqual(escape_rfc3986(unreserved), unreserved) | |
302 | self.assertEqual(escape_rfc3986('тест'), '%D1%82%D0%B5%D1%81%D1%82') | |
303 | self.assertEqual(escape_rfc3986('%D1%82%D0%B5%D1%81%D1%82'), '%D1%82%D0%B5%D1%81%D1%82') | |
304 | self.assertEqual(escape_rfc3986('foo bar'), 'foo%20bar') | |
305 | self.assertEqual(escape_rfc3986('foo%20bar'), 'foo%20bar') | |
306 | ||
307 | def test_escape_url(self): | |
308 | self.assertEqual( | |
309 | escape_url('http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavré_FD.mp4'), | |
310 | 'http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavre%CC%81_FD.mp4' | |
311 | ) | |
312 | self.assertEqual( | |
313 | escape_url('http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erklärt/Das-Erste/Video?documentId=22673108&bcastId=5290'), | |
314 | 'http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erkl%C3%A4rt/Das-Erste/Video?documentId=22673108&bcastId=5290' | |
315 | ) | |
316 | self.assertEqual( | |
317 | escape_url('http://тест.рф/фрагмент'), | |
318 | 'http://тест.рф/%D1%84%D1%80%D0%B0%D0%B3%D0%BC%D0%B5%D0%BD%D1%82' | |
319 | ) | |
320 | self.assertEqual( | |
321 | escape_url('http://тест.рф/абв?абв=абв#абв'), | |
322 | 'http://тест.рф/%D0%B0%D0%B1%D0%B2?%D0%B0%D0%B1%D0%B2=%D0%B0%D0%B1%D0%B2#%D0%B0%D0%B1%D0%B2' | |
323 | ) | |
324 | self.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0') | |
325 | ||
326 | def test_js_to_json_realworld(self): | |
327 | inp = '''{ | |
328 | 'clip':{'provider':'pseudo'} | |
329 | }''' | |
330 | self.assertEqual(js_to_json(inp), '''{ | |
331 | "clip":{"provider":"pseudo"} | |
332 | }''') | |
333 | json.loads(js_to_json(inp)) | |
334 | ||
335 | inp = '''{ | |
336 | 'playlist':[{'controls':{'all':null}}] | |
337 | }''' | |
338 | self.assertEqual(js_to_json(inp), '''{ | |
339 | "playlist":[{"controls":{"all":null}}] | |
340 | }''') | |
341 | ||
342 | def test_js_to_json_edgecases(self): | |
343 | on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}") | |
344 | self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"}) | |
345 | ||
346 | on = js_to_json('{"abc": true}') | |
347 | self.assertEqual(json.loads(on), {'abc': True}) | |
348 | ||
349 | def test_clean_html(self): | |
350 | self.assertEqual(clean_html('a:\nb'), 'a: b') | |
351 | self.assertEqual(clean_html('a:\n "b"'), 'a: "b"') | |
352 | ||
353 | if __name__ == '__main__': | |
354 | unittest.main() |