-
-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathtest_encoding.py
More file actions
196 lines (180 loc) · 4.65 KB
/
Copy pathtest_encoding.py
File metadata and controls
196 lines (180 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import codecs
import glob
import os
import pytest
import feedparser
from .helpers import (
fail_unless_eval,
get_file_contents,
get_http_test_data,
get_test_data,
)
encoding_files = glob.glob(os.path.join(".", "tests", "encoding", "*.xml"))
local_files = [file for file in encoding_files if "http" not in file]
http_files = [file for file in encoding_files if "http" in file]
def test_doctype_replacement():
"""
Ensure that non-ASCII-compatible encodings don't hide disallowed ENTITY declarations
"""
doc = """<?xml version="1.0" encoding="utf-16be"?>
<!DOCTYPE feed [
<!ENTITY exponential1 "bogus ">
<!ENTITY exponential2 "&exponential1;&exponential1;">
<!ENTITY exponential3 "&exponential2;&exponential2;">
]>
<feed><title type="html">&exponential3;</title></feed>"""
doc = codecs.BOM_UTF16_BE + doc.encode("utf-16be")
result = feedparser.parse(doc)
assert result["feed"]["title"] == "&exponential3"
def test_gb2312_converted_to_gb18030_in_xml_encoding():
# \u55de was chosen because it exists in gb18030 but not gb2312
feed = """<?xml version="1.0" encoding="gb2312"?>
<feed><title>\u55de</title></feed>"""
result = feedparser.parse(
feed.encode("gb18030"), response_headers={"Content-Type": "text/xml"}
)
assert result.encoding == "gb18030"
@pytest.mark.parametrize("file", local_files)
def test_local_encoding_file(file):
data, text = get_file_contents(file)
description, eval_string, skip_unless = get_test_data(file, text)
assert eval(skip_unless)
fail_unless_eval(file, eval_string)
@pytest.mark.parametrize("file", http_files)
def test_http_encoding_file(file):
data, text = get_file_contents(file)
url, description, eval_string, skip_unless = get_http_test_data(file, data, text)
fail_unless_eval(url, eval_string)
@pytest.mark.parametrize(
"encoding",
(
"437",
"850",
"852",
"855",
"857",
"860",
"861",
"862",
"863",
"865",
"866",
"cp037",
"cp1125",
"cp1250",
"cp1251",
"cp1252",
"cp1253",
"cp1254",
"cp1255",
"cp1256",
"cp1257",
"cp1258",
"cp437",
"cp500",
"cp737",
"cp775",
"cp850",
"cp852",
"cp855",
"cp856",
"cp857",
"cp860",
"cp861",
"cp862",
"cp863",
"cp864",
"cp865",
"cp866",
"cp874",
"cp875",
"cp_is",
"csibm037",
"csibm500",
"csibm855",
"csibm857",
"csibm860",
"csibm861",
"csibm863",
"csibm864",
"csibm865",
"csibm866",
"cskoi8r",
"cspc775baltic",
"cspc850multilingual",
"cspc862latinhebrew",
"cspc8codepage437",
"cspcp852",
"ebcdic-cp-be",
"ebcdic-cp-ca",
"ebcdic-cp-ch",
"ebcdic-cp-nl",
"ebcdic-cp-us",
"ebcdic-cp-wt",
"ebcdic_cp_be",
"ebcdic_cp_ca",
"ebcdic_cp_ch",
"ebcdic_cp_nl",
"ebcdic_cp_us",
"ebcdic_cp_wt",
"ibm037",
"ibm039",
"ibm1140",
"ibm437",
"ibm500",
"ibm775",
"ibm850",
"ibm852",
"ibm855",
"ibm857",
"ibm860",
"ibm861",
"ibm862",
"ibm863",
"ibm864",
"ibm865",
"ibm866",
"koi8-r",
"koi8-t",
"koi8-u",
"mac-cyrillic",
"maccentraleurope",
"maccyrillic",
"macgreek",
"maciceland",
"macintosh",
"maclatin2",
"macroman",
"macturkish",
"windows-1250",
"windows-1251",
"windows-1252",
"windows-1253",
"windows-1254",
"windows-1255",
"windows-1256",
"windows-1257",
"windows-1258",
"windows_1250",
"windows_1251",
"windows_1252",
"windows_1253",
"windows_1254",
"windows_1255",
"windows_1256",
"windows_1257",
"windows_1258",
),
)
def test_encoding(encoding):
"""The XML encoding declaration must be found and honored in binary content."""
expected = b"\x80".decode(encoding)
data = f"""
<?xml version="1.0" encoding="{encoding}"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>{expected}</title>
</feed>
"""
result = feedparser.parse(data.strip().encode(encoding))
assert result["encoding"] == encoding
assert result["feed"]["title"] == expected