| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 SelectorType as St, FilterAction as Fa, FilterOption as Opt, | 22 FilterAction, FilterOption, |
|
rhowell
2019/01/12 01:25:31
I wasn't sure what best practices were for this na
Vasily Kuznetsov
2019/01/12 19:23:38
Yeah, I agree about the oddness. Maybe just the fi
rhowell
2019/01/12 21:18:34
That produces another flake8 error: `N814 camelcas
Vasily Kuznetsov
2019/01/14 14:14:35
I see. Pep8-naming is stricter than I expected. Th
| |
| 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' |
| LEFT | RIGHT |