This file is indexed.

/usr/share/pyshared/cssutils/tests/test_encutils/__init__.py is in python-cssutils 0.9.10-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""
tests for encutils.py
"""
import httplib
from StringIO import StringIO
import sys
import unittest

PY2x = sys.version_info < (3,0)

try:
    import cssutils.encutils as encutils
except ImportError:
    import encutils

# helper log
log = encutils.buildlog(stream=StringIO())

class AutoEncodingTestCase(unittest.TestCase):

    def _fakeRes(self, content):
        "build a fake HTTP response"
        class FakeRes:
            def __init__(self, content):
                if PY2x:
                    fp = StringIO(content)
                    self._info = httplib.HTTPMessage(fp)
                else:
                    self._info = httplib.HTTPMessage()
                    # Adjust to testdata.
                    l = content.split(':')
                    if len(l) > 1:
                        # Get the type by just
                        # using the data at the end.
                        t = l[-1].strip()
                        self._info.set_type(t)

            def info(self):
                return self._info

            def read(self):
                return content

        return FakeRes(content)

    def test_getTextTypeByMediaType(self):
        "encutils._getTextTypeByMediaType"
        tests = {
            'application/xml': encutils._XML_APPLICATION_TYPE,
            'application/xml-dtd': encutils._XML_APPLICATION_TYPE,
            'application/xml-external-parsed-entity': encutils._XML_APPLICATION_TYPE,
            'application/xhtml+xml': encutils._XML_APPLICATION_TYPE,
            'text/xml': encutils._XML_TEXT_TYPE,
            'text/xml-external-parsed-entity': encutils._XML_TEXT_TYPE,
            'text/xhtml+xml': encutils._XML_TEXT_TYPE,
            'text/html': encutils._HTML_TEXT_TYPE,
            'text/css': encutils._TEXT_UTF8,
            'text/plain': encutils._TEXT_TYPE,
            'x/x': encutils._OTHER_TYPE,
            'ANYTHING': encutils._OTHER_TYPE
            }
        for test, exp in tests.items():
            self.assertEqual(
                exp, encutils._getTextTypeByMediaType(test, log=log))

    def test_getTextType(self):
        "encutils._getTextType"
        tests = {
            u'\x00\x00\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'\xFF\xFE\x00\x00<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'\xFF\xFE<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'\xef\xbb\xbf<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
            u'\x00\x00\xFE\xFFanything': encutils._OTHER_TYPE,
            u'\xFF\xFE\x00\x00anything': encutils._OTHER_TYPE,
            u'\xFE\xFFanything': encutils._OTHER_TYPE,
            u'\xFF\xFEanything': encutils._OTHER_TYPE,
            u'\xef\xbb\xbfanything': encutils._OTHER_TYPE,
            u'x/x': encutils._OTHER_TYPE,
            u'ANYTHING': encutils._OTHER_TYPE
            }
        for test, exp in tests.items():
            self.assertEqual(
                exp, encutils._getTextType(test, log=log))

    def test_encodingByMediaType(self):
        "encutils.encodingByMediaType"
        tests = {
            'application/xml': 'utf-8',
            'application/xml-dtd': 'utf-8',
            'application/xml-external-parsed-entity': 'utf-8',
            'application/ANYTHING+xml': 'utf-8',
            '  application/xml  ': 'utf-8',
            'text/xml': 'ascii',
            'text/xml-external-parsed-entity': 'ascii',
            'text/ANYTHING+xml': 'ascii',
            'text/html': 'iso-8859-1',
            'text/css': 'utf-8',
            'text/plain': 'iso-8859-1',
            'ANYTHING': None
            }
        for test, exp in tests.items():
            self.assertEqual(exp,
                             encutils.encodingByMediaType(test, log=log))

    def test_getMetaInfo(self):
        "encutils.getMetaInfo"
        tests = {
            """<meta tp-equiv='Content-Type' content='text/html; charset=ascii'>""":
                (None, None),
            """<meta http-equiv='ontent-Type' content='text/html; charset=ascii'>""":
                (None, None),

            """<meta http-equiv='Content-Type' content='text/html'>""":
                ('text/html', None),

            """<meta content='text/html' http-equiv='Content-Type'>""":
                ('text/html', None),
            """<meta content='text/html;charset=ascii' http-equiv='Content-Type'>""":
                ('text/html', 'ascii'),

            """<meta http-equiv='Content-Type' content='text/html ;charset=ascii'>""":
                ('text/html', 'ascii'),
            """<meta content='text/html;charset=iso-8859-1' http-equiv='Content-Type'>""":
                ('text/html', 'iso-8859-1'),
            """<meta http-equiv="Content-Type" content="text/html;charset = ascii">""":
                ('text/html', 'ascii'),

            """<meta http-equiv="Content-Type" content="text/html;charset=ascii;x=2">""":
                ('text/html', 'ascii'),
            """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii">""":
                ('text/html', 'ascii'),
            """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii;y=2">""":
                ('text/html', 'ascii'),

            """<meta http-equiv='Content-Type' content="text/html;charset=ascii">""":
                ('text/html', 'ascii'),
            """<meta http-equiv='Content-Type' content='text/html;charset=ascii'  />""":
                ('text/html', 'ascii'),
            """<meta http-equiv = " Content-Type" content = " text/html;charset=ascii " >""":
                ('text/html', 'ascii'),
            """<meta http-equiv = " \n Content-Type " content = "  \t text/html   ;  charset=ascii " >""":
                ('text/html', 'ascii'),

            """<meta content="text/html;charset=ascii" http-equiv="Content-Type">""":
                ('text/html', 'ascii'),
            """<meta content="text/html;charset=ascii" http-equiv="cONTENT-type">""":
                ('text/html', 'ascii'),
            """raises exception: </ >""":
                (None, None),
            """<meta content="text/html;charset=ascii" http-equiv="cONTENT-type">
                </ >""":
                ('text/html', 'ascii'),
            """</ >
                <meta content="text/html;charset=ascii" http-equiv="cONTENT-type">""":
                ('text/html', 'ascii'),
            # py 2.7.3 fixed HTMLParser so:  (None, None)
            """<meta content="text/html" http-equiv="cONTENT-type">
                </ >
                <meta content="text/html;charset=ascii" http-equiv="cONTENT-type">""":
                ('text/html', None)
            }
        for test, exp in tests.items():
            self.assertEqual(exp, encutils.getMetaInfo(test, log=log))

    def test_detectXMLEncoding(self):
        "encutils.detectXMLEncoding"
        tests = {
            # BOM
            ('utf_32_be'): u'\x00\x00\xFE\xFFanything',
            ('utf_32_le'): u'\xFF\xFE\x00\x00anything',
            ('utf_16_be'): u'\xFE\xFFanything',
            ('utf_16_le'): u'\xFF\xFEanything',
            ('utf-8'): u'\xef\xbb\xbfanything',
            # encoding=
            ('ascii'): '<?xml version="1.0" encoding="ascii" ?>',
            ('ascii'): "<?xml version='1.0' encoding='ascii' ?>",
            ('iso-8859-1'): "<?xml version='1.0' encoding='iso-8859-1' ?>",
            # default
            ('utf-8'): '<?xml version="1.0" ?>',
            ('utf-8'): '<?xml version="1.0"?><x encoding="ascii"/>'
            }
        for exp, test in tests.items():
            self.assertEqual(exp, encutils.detectXMLEncoding(test, log=log))

    def test_tryEncodings(self):
        "encutils.tryEncodings"
        try:
            import chardet
            tests = [
                ('ascii', u'abc'.encode('ascii')),
                ('windows-1252', u'€'.encode('windows-1252')),
                ('ascii', u'1'.encode('utf-8'))
                ]
        except ImportError:
            tests = [
                ('ascii', u'abc'.encode('ascii')),
                ('windows-1252', u'€'.encode('windows-1252')),
                ('iso-8859-1', u'äöüß'.encode('iso-8859-1')),
                ('iso-8859-1', u'äöüß'.encode('windows-1252')),
                #('utf-8', u'\u1111'.encode('utf-8'))
                ]
        for exp, test in tests:
            self.assertEqual(exp, encutils.tryEncodings(test))


    def test_getEncodingInfo(self):
        "encutils.getEncodingInfo"
        # (expectedencoding, expectedmismatch): (httpheader, filecontent)
        tests = [

            # --- application/xhtml+xml ---

            # header default and XML default
            (('utf-8', False), (
                '''Content-Type: application/xhtml+xml''',
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml"/>
                    </example>''')),
            # XML default
            (('utf-8', False), (
                None,
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml"/>
                    </example>''')),
            # meta is ignored!
            (('utf-8', False), (
                '''Content-Type: application/xhtml+xml''',
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml;charset=iso_M"/>
                    </example>''')),

            # header enc and XML default
            (('iso-h', True), (
                '''Content-Type: application/xhtml+xml;charset=iso-H''',
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml"/>
                    </example>''')),

            # mismatch header and XML explicit, header wins
            (('iso-h', True), (
                '''Content-Type: application/xhtml+xml;charset=iso-H''',
                '''<?xml version="1.0" encoding="iso-X" ?>
                    <example/>''')),

            # header == XML, meta ignored!
            (('iso-h', False), (
                '''Content-Type: application/xhtml+xml;charset=iso-H''',
                '''<?xml version="1.0" encoding="iso-h" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml;charset=iso_M"/>
                    </example>''')),

            # XML only, meta ignored!
            (('iso-x', False), (
                '''Content-Type: application/xhtml+xml''',
                '''<?xml version="1.0" encoding="iso-X" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="application/xhtml+xml;charset=iso_M"/>
                    </example>''')),


            # no text or not enough text:
            (('iso-h', False), ('Content-Type: application/xml;charset=iso-h',
                             '1')),
            (('utf-8', False), ('Content-Type: application/xml',
                                None)),
            ((None, False), ('Content-Type: application/xml',
                             '1')),


            # --- text/xml ---

            # default enc
            (('ascii', False), (
                '''Content-Type: text/xml''',
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="text/xml"/>
                    </example>''')),
            # default as XML ignored and meta completely ignored
            (('ascii', False), (
                '''Content-Type: text/xml''',
                '''<?xml version="1.0" encoding="iso-X" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="text/xml;charset=iso_M"/>
                    </example>''')),
            (('ascii', False), ('Content-Type: text/xml',
                                '1')),
            (('ascii', False), ('Content-Type: text/xml',
                                None)),

            # header enc
            (('iso-h', False), (
                '''Content-Type: text/xml;charset=iso-H''',
                '''<?xml version="1.0" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="text/xml"/>
                    </example>''')),

            # header only, XML and meta ignored!
            (('iso-h', False), (
                '''Content-Type: text/xml;charset=iso-H''',
                '''<?xml version="1.0" encoding="iso-X" ?>
                    <example/>''')),
            (('iso-h', False), (
                '''Content-Type: text/xml;charset=iso-H''',
                '''<?xml version="1.0"  encoding="iso-h" ?>
                    <example>
                        <meta http-equiv="Content-Type"
                            content="text/xml;charset=iso_M"/>
                    </example>''')),


            # --- text/html ---

            # default enc
            (('iso-8859-1', False), ('Content-Type: text/html;',
                                     '''<meta http-equiv="Content-Type"
                                        content="text/html">''')),
            (('iso-8859-1', False), ('Content-Type: text/html;',
                                     None)),

            # header enc
            (('iso-h', False), ('Content-Type: text/html;charset=iso-H',
                                '''<meta http-equiv="Content-Type"
                                    content="text/html">''')),
            # meta enc
            (('iso-m', False), ('Content-Type: text/html',
                                '''<meta http-equiv="Content-Type"
                                    content="text/html;charset=iso-m">''')),

            # mismatch header and meta, header wins
            (('iso-h', True), ('Content-Type: text/html;charset=iso-H',
                               '''<meta http-equiv="Content-Type"
                                    content="text/html;charset=iso-m">''')),

            # no header:
            ((None, False), (None,
                             '''<meta http-equiv="Content-Type"
                                content="text/html;charset=iso-m">''')),
            # no encoding at all
            ((None, False), (None,
                             '''<meta http-equiv="Content-Type"
                                content="text/html">''')),


            ((None, False), (None,
                             '''text''')),


            # --- no header ---

            ((None, False), (None, '')),
            (('iso-8859-1', False), ('''NoContentType''',
                                     '''OnlyText''')),
            (('iso-8859-1', False), ('Content-Type: text/html;',
                                     None)),
            (('iso-8859-1', False), ('Content-Type: text/html;',
                                     '1')),

            # XML
            (('utf-8', False), (None,
                                '''<?xml version=''')),
            (('iso-x', False), (None,
                                '''<?xml version="1.0" encoding="iso-X"?>''')),
            # meta ignored
            (('utf-8', False), (None,
                                '''<?xml version="1.0" ?>
                                    <html><meta http-equiv="Content-Type"
                                    content="text/html;charset=iso-m"></html>''')),

            (('utf-8', False), ('Content-Type: text/css;',
                                '1')),
            (('iso-h', False), ('Content-Type: text/css;charset=iso-h',
                                '1')),
            # only header is used by encutils
            (('utf-8', False), ('Content-Type: text/css',
                                '@charset "ascii";')),

        ]
        for exp, test in tests:
            header, text = test
            if header:
                res = encutils.getEncodingInfo(self._fakeRes(header), text)
            else:
                res = encutils.getEncodingInfo(text=text)

            res = (res.encoding, res.mismatch)
            self.assertEqual(exp, res)


if __name__ == '__main__':
    unittest.main()