| OLD | NEW | 
|---|
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH | 
| 3 # | 3 # | 
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify | 
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as | 
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. | 
| 7 # | 7 # | 
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, | 
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. | 
| 12 # | 12 # | 
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License | 
| 14 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
| 15 | 15 | 
| 16 from __future__ import unicode_literals | 16 from __future__ import unicode_literals | 
| 17 | 17 | 
| 18 import pytest | 18 import pytest | 
| 19 | 19 | 
| 20 from abp.filters import ( | 20 from abp.filters import ( | 
| 21     parse_line, parse_filterlist, ParseError, | 21     parse_line, parse_filterlist, ParseError, SelectorType as SelType, | 
| 22     SELECTOR_TYPE as ST, FILTER_ACTION as FA, FILTER_OPTION as OPT, | 22     FilterAction, FilterOption, | 
| 23 ) | 23 ) | 
| 24 from abp.filters.parser import Comment, Metadata, Header | 24 from abp.filters.parser import Comment, Metadata, Header | 
| 25 | 25 | 
| 26 | 26 | 
| 27 def test_parse_empty(): | 27 def test_parse_empty(): | 
| 28     line = parse_line('    ') | 28     line = parse_line('    ') | 
| 29     assert line.type == 'emptyline' | 29     assert line.type == 'emptyline' | 
| 30 | 30 | 
| 31 | 31 | 
| 32 @pytest.mark.parametrize('filter_text, expected', { | 32 @pytest.mark.parametrize('filter_text, expected', { | 
| 33     # Blocking filters with patterns and regexps and blocking exceptions. | 33     # Blocking filters with patterns and regexps and blocking exceptions. | 
| 34     '*asdf*d**dd*': { | 34     '*asdf*d**dd*': { | 
| 35         'selector': {'type': ST.URL_PATTERN, 'value': '*asdf*d**dd*'}, | 35         'selector': {'type': SelType.URL_PATTERN, 'value': '*asdf*d**dd*'}, | 
| 36         'action': FA.BLOCK, | 36         'action': FilterAction.BLOCK, | 
| 37     }, | 37     }, | 
| 38     '@@|*asd|f*d**dd*|': { | 38     '@@|*asd|f*d**dd*|': { | 
| 39         'selector': {'type': ST.URL_PATTERN, 'value': '|*asd|f*d**dd*|'}, | 39         'selector': {'type': SelType.URL_PATTERN, 'value': '|*asd|f*d**dd*|'}, | 
| 40         'action': FA.ALLOW, | 40         'action': FilterAction.ALLOW, | 
| 41     }, | 41     }, | 
| 42     '/ddd|f?a[s]d/': { | 42     '/ddd|f?a[s]d/': { | 
| 43         'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'}, | 43         'selector': {'type': SelType.URL_REGEXP, 'value': 'ddd|f?a[s]d'}, | 
| 44         'action': FA.BLOCK, | 44         'action': FilterAction.BLOCK, | 
| 45     }, | 45     }, | 
| 46     '@@/ddd|f?a[s]d/': { | 46     '@@/ddd|f?a[s]d/': { | 
| 47         'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'}, | 47         'selector': {'type': SelType.URL_REGEXP, 'value': 'ddd|f?a[s]d'}, | 
| 48         'action': FA.ALLOW, | 48         'action': FilterAction.ALLOW, | 
| 49     }, | 49     }, | 
| 50     # Blocking filters with some options. | 50     # Blocking filters with some options. | 
| 51     'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': { | 51     'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': { | 
| 52         'selector': {'type': ST.URL_PATTERN, 'value': 'bla'}, | 52         'selector': {'type': SelType.URL_PATTERN, 'value': 'bla'}, | 
| 53         'action': FA.BLOCK, | 53         'action': FilterAction.BLOCK, | 
| 54         'options': [ | 54         'options': [ | 
| 55             (OPT.MATCH_CASE, True), | 55             (FilterOption.MATCH_CASE, True), | 
| 56             (OPT.SCRIPT, False), | 56             (FilterOption.SCRIPT, False), | 
| 57             (OPT.DOMAIN, [('foo.com', True), ('bar.com', False)]), | 57             (FilterOption.DOMAIN, [('foo.com', True), ('bar.com', False)]), | 
| 58             (OPT.SITEKEY, ['foo']), | 58             (FilterOption.SITEKEY, ['foo']), | 
| 59         ], | 59         ], | 
| 60     }, | 60     }, | 
| 61     '@@http://bla$~script,~other,sitekey=foo|bar': { | 61     '@@http://bla$~script,~other,sitekey=foo|bar': { | 
| 62         'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'}, | 62         'selector': {'type': SelType.URL_PATTERN, 'value': 'http://bla'}, | 
| 63         'action': FA.ALLOW, | 63         'action': FilterAction.ALLOW, | 
| 64         'options': [ | 64         'options': [ | 
| 65             (OPT.SCRIPT, False), | 65             (FilterOption.SCRIPT, False), | 
| 66             (OPT.OTHER, False), | 66             (FilterOption.OTHER, False), | 
| 67             (OPT.SITEKEY, ['foo', 'bar']), | 67             (FilterOption.SITEKEY, ['foo', 'bar']), | 
| 68         ], | 68         ], | 
| 69     }, | 69     }, | 
| 70     "||foo.com^$csp=script-src 'self' * 'unsafe-inline',script,sitekey=foo," | 70     "||foo.com^$csp=script-src 'self' * 'unsafe-inline',script,sitekey=foo," | 
| 71     + 'other,match-case,domain=foo.com': { | 71     + 'other,match-case,domain=foo.com': { | 
| 72         'selector': {'type': ST.URL_PATTERN, 'value': '||foo.com^'}, | 72         'selector': {'type': SelType.URL_PATTERN, 'value': '||foo.com^'}, | 
| 73         'action': FA.BLOCK, | 73         'action': FilterAction.BLOCK, | 
| 74         'options': [ | 74         'options': [ | 
| 75             (OPT.CSP, "script-src 'self' * 'unsafe-inline'"), | 75             (FilterOption.CSP, "script-src 'self' * 'unsafe-inline'"), | 
| 76             ('script', True), | 76             ('script', True), | 
| 77             ('sitekey', ['foo']), | 77             ('sitekey', ['foo']), | 
| 78             ('other', True), | 78             ('other', True), | 
| 79             ('match-case', True), | 79             ('match-case', True), | 
| 80             ('domain', [('foo.com', True)]), | 80             ('domain', [('foo.com', True)]), | 
| 81         ], | 81         ], | 
| 82     }, | 82     }, | 
| 83     '@@bla$script,other,domain=foo.com|~bar.foo.com,csp=c s p': { | 83     '@@bla$script,other,domain=foo.com|~bar.foo.com,csp=c s p': { | 
| 84         'selector': {'type': ST.URL_PATTERN, 'value': 'bla'}, | 84         'selector': {'type': SelType.URL_PATTERN, 'value': 'bla'}, | 
| 85         'action': FA.ALLOW, | 85         'action': FilterAction.ALLOW, | 
| 86         'options': [ | 86         'options': [ | 
| 87             ('script', True), | 87             ('script', True), | 
| 88             ('other', True), | 88             ('other', True), | 
| 89             ('domain', [('foo.com', True), ('bar.foo.com', False)]), | 89             ('domain', [('foo.com', True), ('bar.foo.com', False)]), | 
| 90             ('csp', 'c s p'), | 90             ('csp', 'c s p'), | 
| 91         ], | 91         ], | 
| 92     }, | 92     }, | 
| 93     '||content.server.com/files/*.php$rewrite=$1': { | 93     '||content.server.com/files/*.php$rewrite=$1': { | 
| 94         'selector': {'type': ST.URL_PATTERN, | 94         'selector': {'type': SelType.URL_PATTERN, | 
| 95                      'value': '||content.server.com/files/*.php'}, | 95                      'value': '||content.server.com/files/*.php'}, | 
| 96         'action': FA.BLOCK, | 96         'action': FilterAction.BLOCK, | 
| 97         'options': [ | 97         'options': [ | 
| 98             ('rewrite', '$1'), | 98             ('rewrite', '$1'), | 
| 99         ], | 99         ], | 
| 100     }, | 100     }, | 
| 101     # Element hiding filters and exceptions. | 101     # Element hiding filters and exceptions. | 
| 102     '##ddd': { | 102     '##ddd': { | 
| 103         'selector': {'type': ST.CSS, 'value': 'ddd'}, | 103         'selector': {'type': SelType.CSS, 'value': 'ddd'}, | 
| 104         'action': FA.HIDE, | 104         'action': FilterAction.HIDE, | 
| 105         'options': [], | 105         'options': [], | 
| 106     }, | 106     }, | 
| 107     '#@#body > div:first-child': { | 107     '#@#body > div:first-child': { | 
| 108         'selector': {'type': ST.CSS, 'value': 'body > div:first-child'}, | 108         'selector': {'type': SelType.CSS, 'value': 'body > div:first-child'}, | 
| 109         'action': FA.SHOW, | 109         'action': FilterAction.SHOW, | 
| 110         'options': [], | 110         'options': [], | 
| 111     }, | 111     }, | 
| 112     'foo,~bar##ddd': { | 112     'foo,~bar##ddd': { | 
| 113         'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])], | 113         'options': [(FilterOption.DOMAIN, [('foo', True), ('bar', False)])], | 
| 114     }, | 114     }, | 
| 115     # Element hiding emulation filters (extended CSS). | 115     # Element hiding emulation filters (extended CSS). | 
| 116     'foo,~bar#?#:-abp-properties(abc)': { | 116     'foo,~bar#?#:-abp-properties(abc)': { | 
| 117         'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'}, | 117         'selector': {'type': SelType.XCSS, 'value': ':-abp-properties(abc)'}, | 
| 118         'action': FA.HIDE, | 118         'action': FilterAction.HIDE, | 
| 119         'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])], | 119         'options': [(FilterOption.DOMAIN, [('foo', True), ('bar', False)])], | 
| 120     }, | 120     }, | 
| 121     'foo.com#?#aaa :-abp-properties(abc) bbb': { | 121     'foo.com#?#aaa :-abp-properties(abc) bbb': { | 
| 122         'selector': { | 122         'selector': { | 
| 123             'type': ST.XCSS, | 123             'type': SelType.XCSS, | 
| 124             'value': 'aaa :-abp-properties(abc) bbb', | 124             'value': 'aaa :-abp-properties(abc) bbb', | 
| 125         }, | 125         }, | 
| 126     }, | 126     }, | 
| 127     '#?#:-abp-properties(|background-image: url(data:*))': { | 127     '#?#:-abp-properties(|background-image: url(data:*))': { | 
| 128         'selector': { | 128         'selector': { | 
| 129             'type': ST.XCSS, | 129             'type': SelType.XCSS, | 
| 130             'value': ':-abp-properties(|background-image: url(data:*))', | 130             'value': ':-abp-properties(|background-image: url(data:*))', | 
| 131         }, | 131         }, | 
| 132         'options': [], | 132         'options': [], | 
| 133     }, | 133     }, | 
| 134 }.items()) | 134 }.items()) | 
| 135 def test_parse_filters(filter_text, expected): | 135 def test_parse_filters(filter_text, expected): | 
| 136     """Parametric test for filter parsing.""" | 136     """Parametric test for filter parsing.""" | 
| 137     parsed = parse_line(filter_text) | 137     parsed = parse_line(filter_text) | 
| 138     assert parsed.type == 'filter' | 138     assert parsed.type == 'filter' | 
| 139     assert parsed.text == filter_text | 139     assert parsed.text == filter_text | 
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 218 def test_exception_timing(): | 218 def test_exception_timing(): | 
| 219     result = parse_filterlist(['! good line', '%bad line%']) | 219     result = parse_filterlist(['! good line', '%bad line%']) | 
| 220     assert next(result) == Comment('good line') | 220     assert next(result) == Comment('good line') | 
| 221     with pytest.raises(ParseError): | 221     with pytest.raises(ParseError): | 
| 222         next(result) | 222         next(result) | 
| 223 | 223 | 
| 224 | 224 | 
| 225 def test_parse_line_bytes(): | 225 def test_parse_line_bytes(): | 
| 226     line = parse_line(b'! \xc3\xbc') | 226     line = parse_line(b'! \xc3\xbc') | 
| 227     assert line.text == '\xfc' | 227     assert line.text == '\xfc' | 
| OLD | NEW | 
|---|