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.

538 lines
23 KiB

10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. # Various small unit tests
  10. import io
  11. import json
  12. import xml.etree.ElementTree
  13. from youtube_dl.utils import (
  14. age_restricted,
  15. args_to_str,
  16. clean_html,
  17. DateRange,
  18. detect_exe_version,
  19. encodeFilename,
  20. escape_rfc3986,
  21. escape_url,
  22. find_xpath_attr,
  23. fix_xml_ampersands,
  24. InAdvancePagedList,
  25. intlist_to_bytes,
  26. is_html,
  27. js_to_json,
  28. limit_length,
  29. OnDemandPagedList,
  30. orderedSet,
  31. parse_duration,
  32. parse_filesize,
  33. parse_iso8601,
  34. read_batch_urls,
  35. sanitize_filename,
  36. sanitize_path,
  37. shell_quote,
  38. smuggle_url,
  39. str_to_int,
  40. strip_jsonp,
  41. struct_unpack,
  42. timeconvert,
  43. unescapeHTML,
  44. unified_strdate,
  45. unsmuggle_url,
  46. uppercase_escape,
  47. url_basename,
  48. urlencode_postdata,
  49. version_tuple,
  50. xpath_with_ns,
  51. render_table,
  52. match_str,
  53. )
  54. class TestUtil(unittest.TestCase):
  55. def test_timeconvert(self):
  56. self.assertTrue(timeconvert('') is None)
  57. self.assertTrue(timeconvert('bougrg') is None)
  58. def test_sanitize_filename(self):
  59. self.assertEqual(sanitize_filename('abc'), 'abc')
  60. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  61. self.assertEqual(sanitize_filename('123'), '123')
  62. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  63. 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'))
  68. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  69. aumlaut = 'ä'
  70. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  71. tests = '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430'
  72. self.assertEqual(sanitize_filename(tests), tests)
  73. self.assertEqual(
  74. sanitize_filename('New World record at 0:12:34'),
  75. 'New World record at 0_12_34')
  76. self.assertEqual(sanitize_filename('--gasdgf'), '_-gasdgf')
  77. self.assertEqual(sanitize_filename('--gasdgf', is_id=True), '--gasdgf')
  78. self.assertEqual(sanitize_filename('.gasdgf'), 'gasdgf')
  79. self.assertEqual(sanitize_filename('.gasdgf', is_id=True), '.gasdgf')
  80. forbidden = '"\0\\/'
  81. for fc in forbidden:
  82. for fbc in forbidden:
  83. self.assertTrue(fbc not in sanitize_filename(fc))
  84. def test_sanitize_filename_restricted(self):
  85. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  86. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  87. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  88. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  89. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  90. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  91. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  92. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  93. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  94. tests = 'a\xe4b\u4e2d\u56fd\u7684c'
  95. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  96. self.assertTrue(sanitize_filename('\xf6', restricted=True) != '') # No empty filename
  97. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  98. for fc in forbidden:
  99. for fbc in forbidden:
  100. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  101. # Handle a common case more neatly
  102. self.assertEqual(sanitize_filename('\u5927\u58f0\u5e26 - Song', restricted=True), 'Song')
  103. self.assertEqual(sanitize_filename('\u603b\u7edf: Speech', restricted=True), 'Speech')
  104. # .. but make sure the file name is never empty
  105. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  106. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  107. def test_sanitize_ids(self):
  108. self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  109. self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  110. self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  111. def test_sanitize_path(self):
  112. if sys.platform != 'win32':
  113. return
  114. self.assertEqual(sanitize_path('abc'), 'abc')
  115. self.assertEqual(sanitize_path('abc/def'), 'abc\\def')
  116. self.assertEqual(sanitize_path('abc\\def'), 'abc\\def')
  117. self.assertEqual(sanitize_path('abc|def'), 'abc#def')
  118. self.assertEqual(sanitize_path('<>:"|?*'), '#######')
  119. self.assertEqual(sanitize_path('C:/abc/def'), 'C:\\abc\\def')
  120. self.assertEqual(sanitize_path('C?:/abc/def'), 'C##\\abc\\def')
  121. self.assertEqual(sanitize_path('\\\\?\\UNC\\ComputerName\\abc'), '\\\\?\\UNC\\ComputerName\\abc')
  122. self.assertEqual(sanitize_path('\\\\?\\UNC/ComputerName/abc'), '\\\\?\\UNC\\ComputerName\\abc')
  123. self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
  124. self.assertEqual(sanitize_path('\\\\?\\C:/abc'), '\\\\?\\C:\\abc')
  125. self.assertEqual(sanitize_path('\\\\?\\C:\\ab?c\\de:f'), '\\\\?\\C:\\ab#c\\de#f')
  126. self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
  127. self.assertEqual(
  128. sanitize_path('youtube/%(uploader)s/%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s'),
  129. 'youtube\\%(uploader)s\\%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s')
  130. self.assertEqual(
  131. sanitize_path('youtube/TheWreckingYard ./00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part'),
  132. 'youtube\\TheWreckingYard #\\00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part')
  133. self.assertEqual(sanitize_path('abc/def...'), 'abc\\def..#')
  134. self.assertEqual(sanitize_path('abc.../def'), 'abc..#\\def')
  135. self.assertEqual(sanitize_path('abc.../def...'), 'abc..#\\def..#')
  136. def test_ordered_set(self):
  137. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  138. self.assertEqual(orderedSet([]), [])
  139. self.assertEqual(orderedSet([1]), [1])
  140. # keep the list ordered
  141. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  142. def test_unescape_html(self):
  143. self.assertEqual(unescapeHTML('%20;'), '%20;')
  144. self.assertEqual(
  145. unescapeHTML('&eacute;'), 'é')
  146. def test_daterange(self):
  147. _20century = DateRange("19000101", "20000101")
  148. self.assertFalse("17890714" in _20century)
  149. _ac = DateRange("00010101")
  150. self.assertTrue("19690721" in _ac)
  151. _firstmilenium = DateRange(end="10000101")
  152. self.assertTrue("07110427" in _firstmilenium)
  153. def test_unified_dates(self):
  154. self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
  155. self.assertEqual(unified_strdate('8/7/2009'), '20090708')
  156. self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
  157. self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
  158. self.assertEqual(unified_strdate('1968 12 10'), '19681210')
  159. self.assertEqual(unified_strdate('1968-12-10'), '19681210')
  160. self.assertEqual(unified_strdate('28/01/2014 21:00:00 +0100'), '20140128')
  161. self.assertEqual(
  162. unified_strdate('11/26/2014 11:30:00 AM PST', day_first=False),
  163. '20141126')
  164. self.assertEqual(
  165. unified_strdate('2/2/2015 6:47:40 PM', day_first=False),
  166. '20150202')
  167. def test_find_xpath_attr(self):
  168. testxml = '''<root>
  169. <node/>
  170. <node x="a"/>
  171. <node x="a" y="c" />
  172. <node x="b" y="d" />
  173. </root>'''
  174. doc = xml.etree.ElementTree.fromstring(testxml)
  175. self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
  176. self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
  177. self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
  178. def test_xpath_with_ns(self):
  179. testxml = '''<root xmlns:media="http://example.com/">
  180. <media:song>
  181. <media:author>The Author</media:author>
  182. <url>http://server.com/download.mp3</url>
  183. </media:song>
  184. </root>'''
  185. doc = xml.etree.ElementTree.fromstring(testxml)
  186. find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
  187. self.assertTrue(find('media:song') is not None)
  188. self.assertEqual(find('media:song/media:author').text, 'The Author')
  189. self.assertEqual(find('media:song/url').text, 'http://server.com/download.mp3')
  190. def test_smuggle_url(self):
  191. data = {"ö": "ö", "abc": [3]}
  192. url = 'https://foo.bar/baz?x=y#a'
  193. smug_url = smuggle_url(url, data)
  194. unsmug_url, unsmug_data = unsmuggle_url(smug_url)
  195. self.assertEqual(url, unsmug_url)
  196. self.assertEqual(data, unsmug_data)
  197. res_url, res_data = unsmuggle_url(url)
  198. self.assertEqual(res_url, url)
  199. self.assertEqual(res_data, None)
  200. def test_shell_quote(self):
  201. args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')]
  202. self.assertEqual(shell_quote(args), """ffmpeg -i 'ñ€ß'"'"'.mp4'""")
  203. def test_str_to_int(self):
  204. self.assertEqual(str_to_int('123,456'), 123456)
  205. self.assertEqual(str_to_int('123.456'), 123456)
  206. def test_url_basename(self):
  207. self.assertEqual(url_basename('http://foo.de/'), '')
  208. self.assertEqual(url_basename('http://foo.de/bar/baz'), 'baz')
  209. self.assertEqual(url_basename('http://foo.de/bar/baz?x=y'), 'baz')
  210. self.assertEqual(url_basename('http://foo.de/bar/baz#x=y'), 'baz')
  211. self.assertEqual(url_basename('http://foo.de/bar/baz/'), 'baz')
  212. self.assertEqual(
  213. url_basename('http://media.w3.org/2010/05/sintel/trailer.mp4'),
  214. 'trailer.mp4')
  215. def test_parse_duration(self):
  216. self.assertEqual(parse_duration(None), None)
  217. self.assertEqual(parse_duration(False), None)
  218. self.assertEqual(parse_duration('invalid'), None)
  219. self.assertEqual(parse_duration('1'), 1)
  220. self.assertEqual(parse_duration('1337:12'), 80232)
  221. self.assertEqual(parse_duration('9:12:43'), 33163)
  222. self.assertEqual(parse_duration('12:00'), 720)
  223. self.assertEqual(parse_duration('00:01:01'), 61)
  224. self.assertEqual(parse_duration('x:y'), None)
  225. self.assertEqual(parse_duration('3h11m53s'), 11513)
  226. self.assertEqual(parse_duration('3h 11m 53s'), 11513)
  227. self.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513)
  228. self.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513)
  229. self.assertEqual(parse_duration('62m45s'), 3765)
  230. self.assertEqual(parse_duration('6m59s'), 419)
  231. self.assertEqual(parse_duration('49s'), 49)
  232. self.assertEqual(parse_duration('0h0m0s'), 0)
  233. self.assertEqual(parse_duration('0m0s'), 0)
  234. self.assertEqual(parse_duration('0s'), 0)
  235. self.assertEqual(parse_duration('01:02:03.05'), 3723.05)
  236. self.assertEqual(parse_duration('T30M38S'), 1838)
  237. self.assertEqual(parse_duration('5 s'), 5)
  238. self.assertEqual(parse_duration('3 min'), 180)
  239. self.assertEqual(parse_duration('2.5 hours'), 9000)
  240. self.assertEqual(parse_duration('02:03:04'), 7384)
  241. self.assertEqual(parse_duration('01:02:03:04'), 93784)
  242. self.assertEqual(parse_duration('1 hour 3 minutes'), 3780)
  243. def test_fix_xml_ampersands(self):
  244. self.assertEqual(
  245. fix_xml_ampersands('"&x=y&z=a'), '"&amp;x=y&amp;z=a')
  246. self.assertEqual(
  247. fix_xml_ampersands('"&amp;x=y&wrong;&z=a'),
  248. '"&amp;x=y&amp;wrong;&amp;z=a')
  249. self.assertEqual(
  250. fix_xml_ampersands('&amp;&apos;&gt;&lt;&quot;'),
  251. '&amp;&apos;&gt;&lt;&quot;')
  252. self.assertEqual(
  253. fix_xml_ampersands('&#1234;&#x1abC;'), '&#1234;&#x1abC;')
  254. self.assertEqual(fix_xml_ampersands('&#&#'), '&amp;#&amp;#')
  255. def test_paged_list(self):
  256. def testPL(size, pagesize, sliceargs, expected):
  257. def get_page(pagenum):
  258. firstid = pagenum * pagesize
  259. upto = min(size, pagenum * pagesize + pagesize)
  260. for i in range(firstid, upto):
  261. yield i
  262. pl = OnDemandPagedList(get_page, pagesize)
  263. got = pl.getslice(*sliceargs)
  264. self.assertEqual(got, expected)
  265. iapl = InAdvancePagedList(get_page, size // pagesize + 1, pagesize)
  266. got = iapl.getslice(*sliceargs)
  267. self.assertEqual(got, expected)
  268. testPL(5, 2, (), [0, 1, 2, 3, 4])
  269. testPL(5, 2, (1,), [1, 2, 3, 4])
  270. testPL(5, 2, (2,), [2, 3, 4])
  271. testPL(5, 2, (4,), [4])
  272. testPL(5, 2, (0, 3), [0, 1, 2])
  273. testPL(5, 2, (1, 4), [1, 2, 3])
  274. testPL(5, 2, (2, 99), [2, 3, 4])
  275. testPL(5, 2, (20, 99), [])
  276. def test_struct_unpack(self):
  277. self.assertEqual(struct_unpack('!B', b'\x00'), (0,))
  278. def test_read_batch_urls(self):
  279. f = io.StringIO('''\xef\xbb\xbf foo
  280. bar\r
  281. baz
  282. # More after this line\r
  283. ; or after this
  284. bam''')
  285. self.assertEqual(read_batch_urls(f), ['foo', 'bar', 'baz', 'bam'])
  286. def test_urlencode_postdata(self):
  287. data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'})
  288. self.assertTrue(isinstance(data, bytes))
  289. def test_parse_iso8601(self):
  290. self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266)
  291. self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266)
  292. self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266)
  293. self.assertEqual(parse_iso8601('2014-03-23T22:04:26.1234Z'), 1395612266)
  294. def test_strip_jsonp(self):
  295. stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);')
  296. d = json.loads(stripped)
  297. self.assertEqual(d, [{"id": "532cb", "x": 3}])
  298. stripped = strip_jsonp('parseMetadata({"STATUS":"OK"})\n\n\n//epc')
  299. d = json.loads(stripped)
  300. self.assertEqual(d, {'STATUS': 'OK'})
  301. def test_uppercase_escape(self):
  302. self.assertEqual(uppercase_escape(''), '')
  303. self.assertEqual(uppercase_escape('\\U0001d550'), '𝕐')
  304. def test_limit_length(self):
  305. self.assertEqual(limit_length(None, 12), None)
  306. self.assertEqual(limit_length('foo', 12), 'foo')
  307. self.assertTrue(
  308. limit_length('foo bar baz asd', 12).startswith('foo bar'))
  309. self.assertTrue('...' in limit_length('foo bar baz asd', 12))
  310. def test_escape_rfc3986(self):
  311. reserved = "!*'();:@&=+$,/?#[]"
  312. unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
  313. self.assertEqual(escape_rfc3986(reserved), reserved)
  314. self.assertEqual(escape_rfc3986(unreserved), unreserved)
  315. self.assertEqual(escape_rfc3986('тест'), '%D1%82%D0%B5%D1%81%D1%82')
  316. self.assertEqual(escape_rfc3986('%D1%82%D0%B5%D1%81%D1%82'), '%D1%82%D0%B5%D1%81%D1%82')
  317. self.assertEqual(escape_rfc3986('foo bar'), 'foo%20bar')
  318. self.assertEqual(escape_rfc3986('foo%20bar'), 'foo%20bar')
  319. def test_escape_url(self):
  320. self.assertEqual(
  321. escape_url('http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavré_FD.mp4'),
  322. 'http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavre%CC%81_FD.mp4'
  323. )
  324. self.assertEqual(
  325. 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'),
  326. 'http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erkl%C3%A4rt/Das-Erste/Video?documentId=22673108&bcastId=5290'
  327. )
  328. self.assertEqual(
  329. escape_url('http://тест.рф/фрагмент'),
  330. 'http://тест.рф/%D1%84%D1%80%D0%B0%D0%B3%D0%BC%D0%B5%D0%BD%D1%82'
  331. )
  332. self.assertEqual(
  333. escape_url('http://тест.рф/абв?абв=абв#абв'),
  334. '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'
  335. )
  336. self.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0')
  337. def test_js_to_json_realworld(self):
  338. inp = '''{
  339. 'clip':{'provider':'pseudo'}
  340. }'''
  341. self.assertEqual(js_to_json(inp), '''{
  342. "clip":{"provider":"pseudo"}
  343. }''')
  344. json.loads(js_to_json(inp))
  345. inp = '''{
  346. 'playlist':[{'controls':{'all':null}}]
  347. }'''
  348. self.assertEqual(js_to_json(inp), '''{
  349. "playlist":[{"controls":{"all":null}}]
  350. }''')
  351. inp = '"SAND Number: SAND 2013-7800P\\nPresenter: Tom Russo\\nHabanero Software Training - Xyce Software\\nXyce, Sandia\\u0027s"'
  352. json_code = js_to_json(inp)
  353. self.assertEqual(json.loads(json_code), json.loads(inp))
  354. def test_js_to_json_edgecases(self):
  355. on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}")
  356. self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"})
  357. on = js_to_json('{"abc": true}')
  358. self.assertEqual(json.loads(on), {'abc': True})
  359. # Ignore JavaScript code as well
  360. on = js_to_json('''{
  361. "x": 1,
  362. y: "a",
  363. z: some.code
  364. }''')
  365. d = json.loads(on)
  366. self.assertEqual(d['x'], 1)
  367. self.assertEqual(d['y'], 'a')
  368. def test_clean_html(self):
  369. self.assertEqual(clean_html('a:\nb'), 'a: b')
  370. self.assertEqual(clean_html('a:\n "b"'), 'a: "b"')
  371. def test_intlist_to_bytes(self):
  372. self.assertEqual(
  373. intlist_to_bytes([0, 1, 127, 128, 255]),
  374. b'\x00\x01\x7f\x80\xff')
  375. def test_args_to_str(self):
  376. self.assertEqual(
  377. args_to_str(['foo', 'ba/r', '-baz', '2 be', '']),
  378. 'foo ba/r -baz \'2 be\' \'\''
  379. )
  380. def test_parse_filesize(self):
  381. self.assertEqual(parse_filesize(None), None)
  382. self.assertEqual(parse_filesize(''), None)
  383. self.assertEqual(parse_filesize('91 B'), 91)
  384. self.assertEqual(parse_filesize('foobar'), None)
  385. self.assertEqual(parse_filesize('2 MiB'), 2097152)
  386. self.assertEqual(parse_filesize('5 GB'), 5000000000)
  387. self.assertEqual(parse_filesize('1.2Tb'), 1200000000000)
  388. self.assertEqual(parse_filesize('1,24 KB'), 1240)
  389. def test_version_tuple(self):
  390. self.assertEqual(version_tuple('1'), (1,))
  391. self.assertEqual(version_tuple('10.23.344'), (10, 23, 344))
  392. self.assertEqual(version_tuple('10.1-6'), (10, 1, 6)) # avconv style
  393. def test_detect_exe_version(self):
  394. self.assertEqual(detect_exe_version('''ffmpeg version 1.2.1
  395. built on May 27 2013 08:37:26 with gcc 4.7 (Debian 4.7.3-4)
  396. configuration: --prefix=/usr --extra-'''), '1.2.1')
  397. self.assertEqual(detect_exe_version('''ffmpeg version N-63176-g1fb4685
  398. built on May 15 2014 22:09:06 with gcc 4.8.2 (GCC)'''), 'N-63176-g1fb4685')
  399. self.assertEqual(detect_exe_version('''X server found. dri2 connection failed!
  400. Trying to open render node...
  401. Success at /dev/dri/renderD128.
  402. ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4')
  403. def test_age_restricted(self):
  404. self.assertFalse(age_restricted(None, 10)) # unrestricted content
  405. self.assertFalse(age_restricted(1, None)) # unrestricted policy
  406. self.assertFalse(age_restricted(8, 10))
  407. self.assertTrue(age_restricted(18, 14))
  408. self.assertFalse(age_restricted(18, 18))
  409. def test_is_html(self):
  410. self.assertFalse(is_html(b'\x49\x44\x43<html'))
  411. self.assertTrue(is_html(b'<!DOCTYPE foo>\xaaa'))
  412. self.assertTrue(is_html( # UTF-8 with BOM
  413. b'\xef\xbb\xbf<!DOCTYPE foo>\xaaa'))
  414. self.assertTrue(is_html( # UTF-16-LE
  415. b'\xff\xfe<\x00h\x00t\x00m\x00l\x00>\x00\xe4\x00'
  416. ))
  417. self.assertTrue(is_html( # UTF-16-BE
  418. b'\xfe\xff\x00<\x00h\x00t\x00m\x00l\x00>\x00\xe4'
  419. ))
  420. self.assertTrue(is_html( # UTF-32-BE
  421. b'\x00\x00\xFE\xFF\x00\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4'))
  422. self.assertTrue(is_html( # UTF-32-LE
  423. b'\xFF\xFE\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4\x00\x00\x00'))
  424. def test_render_table(self):
  425. self.assertEqual(
  426. render_table(
  427. ['a', 'bcd'],
  428. [[123, 4], [9999, 51]]),
  429. 'a bcd\n'
  430. '123 4\n'
  431. '9999 51')
  432. def test_match_str(self):
  433. self.assertRaises(ValueError, match_str, 'xy>foobar', {})
  434. self.assertFalse(match_str('xy', {'x': 1200}))
  435. self.assertTrue(match_str('!xy', {'x': 1200}))
  436. self.assertTrue(match_str('x', {'x': 1200}))
  437. self.assertFalse(match_str('!x', {'x': 1200}))
  438. self.assertTrue(match_str('x', {'x': 0}))
  439. self.assertFalse(match_str('x>0', {'x': 0}))
  440. self.assertFalse(match_str('x>0', {}))
  441. self.assertTrue(match_str('x>?0', {}))
  442. self.assertTrue(match_str('x>1K', {'x': 1200}))
  443. self.assertFalse(match_str('x>2K', {'x': 1200}))
  444. self.assertTrue(match_str('x>=1200 & x < 1300', {'x': 1200}))
  445. self.assertFalse(match_str('x>=1100 & x < 1200', {'x': 1200}))
  446. self.assertFalse(match_str('y=a212', {'y': 'foobar42'}))
  447. self.assertTrue(match_str('y=foobar42', {'y': 'foobar42'}))
  448. self.assertFalse(match_str('y!=foobar42', {'y': 'foobar42'}))
  449. self.assertTrue(match_str('y!=foobar2', {'y': 'foobar42'}))
  450. self.assertFalse(match_str(
  451. 'like_count > 100 & dislike_count <? 50 & description',
  452. {'like_count': 90, 'description': 'foo'}))
  453. self.assertTrue(match_str(
  454. 'like_count > 100 & dislike_count <? 50 & description',
  455. {'like_count': 190, 'description': 'foo'}))
  456. self.assertFalse(match_str(
  457. 'like_count > 100 & dislike_count <? 50 & description',
  458. {'like_count': 190, 'dislike_count': 60, 'description': 'foo'}))
  459. self.assertFalse(match_str(
  460. 'like_count > 100 & dislike_count <? 50 & description',
  461. {'like_count': 190, 'dislike_count': 10}))
  462. if __name__ == '__main__':
  463. unittest.main()