"""unit tests for sanitize""" __author__ = "Aaron Swartz " __version__ = "0.33" __license__ = "public domain" _adaptedfrom_ = [ "http://www.iamcal.com/publish/articles/php/processing_html_part_2/", "http://feedparser.org/tests/wellformed/sanitize/", "http://alpha-geek.com/example/crazy_html2.html"] import sanitize def HTML(a, b, baseuri=None, addnofollow=False): try: assert sanitize.HTML(a, baseuri=baseuri, addnofollow=addnofollow) == b except AssertionError: print ' in:', repr(a) print 'out:', repr(sanitize.HTML(a, baseuri=baseuri)) print 'exp:', repr(b) raise ## basics HTML("","") HTML("hello","hello") ## balancing tags HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("hello","hello") HTML("","") ## trailing slashes HTML('','') HTML('','') HTML('','') ## balancing angle brakets HTML('','b>') HTML('','>') HTML('foofoo','b>foo') HTML('>') HTML('b><','b>') HTML('>','>') ## attributes HTML('','') HTML('','') HTML('','') HTML('', '') ## dangerous tags (a small sample) sHTML = lambda x: HTML(x, 'safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe') sHTML('safe description') sHTML('safe description') sHTML('safe description') sHTML('safe description') for x in ['onabort', 'onblur', 'onchange', 'onclick', 'ondblclick', 'onerror', 'onfocus', 'onkeydown', 'onkeypress', 'onkeyup', 'onload', 'onmousedown', 'onmouseout', 'onmouseover', 'onmouseup', 'onreset', 'resize', 'onsubmit', 'onunload']: HTML('' % x, '') HTML('never trust your upstream platypus', 'never trust your upstream platypus') ## ignorables HTML('foo', 'foo') ## non-allowed tags HTML('','') HTML('\r\n\r\n\r\n\r\n\r\nfunction executeMe()\r\n{\r\n\r\n\r\n\r\n\r\n/*