| 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 """Functional tests for testing rPython integration.""" | 16 """Functional tests for testing rPython integration.""" | 
| 17 from __future__ import unicode_literals |  | 
| 18 | 17 | 
| 19 from collections import namedtuple | 18 from collections import namedtuple | 
| 20 import pytest | 19 import pytest | 
| 21 import sys |  | 
| 22 | 20 | 
| 23 from abp.filters.rpy import tuple2dict, line2dict, lines2dicts | 21 from abp.filters.rpy import tuple2dict, line2dict, lines2dicts | 
| 24 | 22 | 
| 25 | 23 | 
| 26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') | 24 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') | 
| 27 | 25 | 
| 28 _TEST_EXAMPLES = { | 26 _TEST_EXAMPLES = { | 
| 29     'header': { | 27     'header': { | 
| 30         'in': b'[Adblock Plus 2.0]', | 28         'in': '[Adblock Plus 2.0]', | 
| 31         'out': { | 29         'out': { | 
| 32             b'type': b'Header', | 30             'type': 'Header', | 
| 33             b'version': b'Adblock Plus 2.0', | 31             'version': 'Adblock Plus 2.0', | 
| 34         }, | 32         }, | 
| 35     }, | 33     }, | 
| 36     'metadata': { | 34     'metadata': { | 
| 37         'in': b'! Title: Example list', | 35         'in': '! Title: Example list', | 
| 38         'out': { | 36         'out': { | 
| 39             b'type': b'Metadata', | 37             'type': 'Metadata', | 
| 40             b'key': b'Title', | 38             'key': 'Title', | 
| 41             b'value': b'Example list', | 39             'value': 'Example list', | 
| 42         }, | 40         }, | 
| 43     }, | 41     }, | 
| 44     'comment': { | 42     'comment': { | 
| 45         'in': b'! Comment', | 43         'in': '! Comment', | 
| 46         'out': { | 44         'out': { | 
| 47             b'type': b'Comment', | 45             'type': 'Comment', | 
| 48             b'text': b'Comment', | 46             'text': 'Comment', | 
| 49         }, | 47         }, | 
| 50     }, | 48     }, | 
| 51     'empty': { | 49     'empty': { | 
| 52         'in': b'', | 50         'in': '', | 
| 53         'out': { | 51         'out': { | 
| 54             b'type': b'EmptyLine', | 52             'type': 'EmptyLine', | 
| 55         }, | 53         }, | 
| 56     }, | 54     }, | 
| 57     'include': { | 55     'include': { | 
| 58         'in': b'%include www.test.py/filtelist.txt%', | 56         'in': '%include www.test.py/filtelist.txt%', | 
| 59         'out': { | 57         'out': { | 
| 60             b'type': b'Include', | 58             'type': 'Include', | 
| 61             b'target': b'www.test.py/filtelist.txt', | 59             'target': 'www.test.py/filtelist.txt', | 
| 62         }, | 60         }, | 
| 63     }, | 61     }, | 
| 64     'filter_single': { | 62     'filter_single': { | 
| 65         'in': b'foo.com##div#ad1', | 63         'in': 'foo.com##div#ad1', | 
| 66         'out': { | 64         'out': { | 
| 67             b'type': b'Filter', | 65             'type': 'Filter', | 
| 68             b'text': b'foo.com##div#ad1', | 66             'text': 'foo.com##div#ad1', | 
| 69             b'selector': {b'type': b'css', b'value': b'div#ad1'}, | 67             'selector': {'type': 'css', 'value': 'div#ad1'}, | 
| 70             b'action': b'hide', | 68             'action': 'hide', | 
| 71             b'options': {b'domain': {b'foo.com': True}}, | 69             'options': {'domain': {'foo.com': True}}, | 
| 72         }, | 70         }, | 
| 73     }, | 71     }, | 
| 74     'filter_with_%': { | 72     'filter_with_%': { | 
| 75         'in': b'%22banner%*%22idzone%', | 73         'in': '%22banner%*%22idzone%', | 
| 76         'out': { | 74         'out': { | 
| 77             b'type': b'Filter', | 75             'type': 'Filter', | 
| 78             b'text': b'%22banner%*%22idzone%', | 76             'text': '%22banner%*%22idzone%', | 
| 79             b'selector': {b'type': b'url-pattern', | 77             'selector': {'type': 'url-pattern', | 
| 80                           b'value': b'%22banner%*%22idzone%'}, | 78                          'value': '%22banner%*%22idzone%'}, | 
| 81             b'action': b'block', | 79             'action': 'block', | 
| 82             b'options': {}, | 80             'options': {}, | 
| 83         }, | 81         }, | 
| 84     }, | 82     }, | 
| 85     'filter_multiple': { | 83     'filter_multiple': { | 
| 86         'in': b'foo.com,bar.com##div#ad1', | 84         'in': 'foo.com,bar.com##div#ad1', | 
| 87         'out': { | 85         'out': { | 
| 88             b'type': b'Filter', | 86             'type': 'Filter', | 
| 89             b'text': b'foo.com,bar.com##div#ad1', | 87             'text': 'foo.com,bar.com##div#ad1', | 
| 90             b'selector': {b'type': b'css', b'value': b'div#ad1'}, | 88             'selector': {'type': 'css', 'value': 'div#ad1'}, | 
| 91             b'action': b'hide', | 89             'action': 'hide', | 
| 92             b'options': {b'domain': {b'foo.com': True, b'bar.com': True}}, | 90             'options': {'domain': {'foo.com': True, 'bar.com': True}}, | 
| 93         }, | 91         }, | 
| 94     }, | 92     }, | 
| 95     'filter_with_sitekey_list': { | 93     'filter_with_sitekey_list': { | 
| 96         'in': b'@@bla$ping,domain=foo.com|~bar.foo.com,sitekey=foo|bar', | 94         'in': '@@bla$ping,domain=foo.com|~bar.foo.com,sitekey=foo|bar', | 
| 97         'out': { | 95         'out': { | 
| 98             b'text': | 96             'text': | 
| 99                 b'@@bla$ping,domain=foo.com|~bar.foo.com,sitekey=foo|bar', | 97                 '@@bla$ping,domain=foo.com|~bar.foo.com,sitekey=foo|bar', | 
| 100                 b'selector': {b'value': b'bla', b'type': b'url-pattern'}, | 98                 'selector': {'value': 'bla', 'type': 'url-pattern'}, | 
| 101                 b'action': b'allow', | 99                 'action': 'allow', | 
| 102                 b'options': { | 100                 'options': { | 
| 103                     b'ping': True, | 101                     'ping': True, | 
| 104                     b'domain': {b'foo.com': True, | 102                     'domain': {'foo.com': True, | 
| 105                                 b'bar.foo.com': False}, | 103                                'bar.foo.com': False}, | 
| 106                     b'sitekey': [b'foo', b'bar']}, | 104                     'sitekey': ['foo', 'bar']}, | 
| 107                 b'type': b'Filter', | 105                 'type': 'Filter', | 
| 108         }, | 106         }, | 
| 109     }, | 107     }, | 
| 110 } | 108 } | 
| 111 | 109 | 
| 112 | 110 | 
| 113 def check_data_utf8(data): |  | 
| 114     """Check if all the strings in a dict are encoded as unicode. |  | 
| 115 |  | 
| 116     Parameters |  | 
| 117     ---------- |  | 
| 118     data: dict |  | 
| 119         The dictionary to be checked |  | 
| 120 |  | 
| 121     Raises |  | 
| 122     ------- |  | 
| 123     AssertionError |  | 
| 124         If any of the strings encountered are not unicode |  | 
| 125 |  | 
| 126     """ |  | 
| 127     if isinstance(data, dict): |  | 
| 128         for key, value in data.items(): |  | 
| 129             check_data_utf8(key) |  | 
| 130             check_data_utf8(value) |  | 
| 131     elif isinstance(data, (list, tuple)): |  | 
| 132         for item in data: |  | 
| 133             check_data_utf8(item) |  | 
| 134     elif isinstance(data, type('')): |  | 
| 135         raise AssertionError('{} is str. Expected bytes.'.format(data)) |  | 
| 136 |  | 
| 137 |  | 
| 138 @pytest.mark.parametrize('foo,bar', [ | 111 @pytest.mark.parametrize('foo,bar', [ | 
| 139     ('test_foo', 1), | 112     ('test_foo', 1), | 
| 140     ({'foofoo': 'test', 'foobar': 2}, [1, 2, 3]), | 113     ({'foofoo': 'test', 'foobar': 2}, [1, 2, 3]), | 
| 141     ((1,), [('a', True), ('b', False)]), | 114     ((1,), [('a', True), ('b', False)]), | 
| 142 ]) | 115 ]) | 
| 143 def test_tuple2dict(foo, bar): | 116 def test_tuple2dict(foo, bar): | 
| 144     """Test that dicts are produced correctly from a named tuple.""" | 117     """Test that dicts are produced correctly from a named tuple.""" | 
| 145     data = _SAMPLE_TUPLE(foo=foo, bar=bar) | 118     data = _SAMPLE_TUPLE(foo=foo, bar=bar) | 
| 146     exp = {'foo': foo, 'bar': bar, 'type': 'tuple'} | 119     exp = {'foo': foo, 'bar': bar, 'type': 'tuple'} | 
| 147 | 120 | 
| 148     result = tuple2dict(data) | 121     result = tuple2dict(data) | 
| 149 | 122 | 
| 150     assert exp == result | 123     assert exp == result | 
| 151 | 124 | 
| 152 | 125 | 
| 153 @pytest.mark.skipif(sys.version.startswith('3.'), reason='Redundant on py3+.') |  | 
| 154 @pytest.mark.parametrize('line_type', _TEST_EXAMPLES.keys()) |  | 
| 155 def test_line2dict_encoding(line_type): |  | 
| 156     """Test that the resulting object has all strings encoded as utf-8. |  | 
| 157 |  | 
| 158     These tests will only be run on Python2.*. On Python3.*, these test |  | 
| 159     cases are covered by test_line2dict() below. |  | 
| 160     """ |  | 
| 161     data = line2dict(_TEST_EXAMPLES[line_type]['in']) |  | 
| 162     check_data_utf8(data) |  | 
| 163 |  | 
| 164 |  | 
| 165 @pytest.mark.parametrize('line_type', list(_TEST_EXAMPLES.keys())) | 126 @pytest.mark.parametrize('line_type', list(_TEST_EXAMPLES.keys())) | 
| 166 def test_line2dict_format(line_type): | 127 def test_line2dict_format(line_type): | 
| 167     """Test that the API result has the appropriate format. | 128     """Test that the API result has the appropriate format. | 
| 168 | 129 | 
| 169     Checks for both keys and datatypes. | 130     Checks for both keys and datatypes. | 
| 170     """ | 131     """ | 
| 171     position = 'start' if line_type in {'header', 'metadata'} else 'body' | 132     position = 'start' if line_type in {'header', 'metadata'} else 'body' | 
| 172     data = line2dict(_TEST_EXAMPLES[line_type]['in'], position) | 133     data = line2dict(_TEST_EXAMPLES[line_type]['in'], position) | 
| 173 | 134 | 
| 174     assert data == _TEST_EXAMPLES[line_type]['out'] | 135     assert data == _TEST_EXAMPLES[line_type]['out'] | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 185 | 146 | 
| 186     assert lines2dicts(ins, 'start') == outs | 147     assert lines2dicts(ins, 'start') == outs | 
| 187 | 148 | 
| 188 | 149 | 
| 189 def test_lines2dicts_default(): | 150 def test_lines2dicts_default(): | 
| 190     """Test that the API returns the correct result in the appropriate format. | 151     """Test that the API returns the correct result in the appropriate format. | 
| 191 | 152 | 
| 192     By default, lines2dicts() does not correctly parse headers and metadata. | 153     By default, lines2dicts() does not correctly parse headers and metadata. | 
| 193     """ | 154     """ | 
| 194     tests = [t for t in _TEST_EXAMPLES.values() | 155     tests = [t for t in _TEST_EXAMPLES.values() | 
| 195              if t['out'][b'type'] not in {b'Header', b'Metadata'}] | 156              if t['out']['type'] not in {'Header', 'Metadata'}] | 
| 196     ins = [ex['in'] for ex in tests] | 157     ins = [ex['in'] for ex in tests] | 
| 197     outs = [ex['out'] for ex in tests] | 158     outs = [ex['out'] for ex in tests] | 
| 198 | 159 | 
| 199     assert lines2dicts(ins) == outs | 160     assert lines2dicts(ins) == outs | 
| OLD | NEW | 
|---|