aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/EncoderTest.php
blob: 6084c39f36506e9cbed9833f78bc71414d51507c (plain) (blame)
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
<?php

class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness
{

    protected $_entity_lookup;

    function setUp() {
        $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
        parent::setUp();
    }

    function assertCleanUTF8($string, $expect = null) {
        if ($expect === null) $expect = $string;
        $this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string), $expect, 'iconv: %s');
        $this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string, true), $expect, 'PHP: %s');
    }

    function test_cleanUTF8() {
        $this->assertCleanUTF8('Normal string.');
        $this->assertCleanUTF8("Test\tAllowed\nControl\rCharacters");
        $this->assertCleanUTF8("null byte: \0", 'null byte: ');
        $this->assertCleanUTF8("\1\2\3\4\5\6\7", '');
        $this->assertCleanUTF8("\x7F", ''); // one byte invalid SGML char
        $this->assertCleanUTF8("\xC2\x80", ''); // two byte invalid SGML
        $this->assertCleanUTF8("\xF3\xBF\xBF\xBF"); // valid four byte
        $this->assertCleanUTF8("\xDF\xFF", ''); // malformed UTF8
        // invalid codepoints
        $this->assertCleanUTF8("\xED\xB0\x80", '');
    }

    function test_convertToUTF8_noConvert() {
        // UTF-8 means that we don't touch it
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToUTF8("\xF6", $this->config, $this->context),
            "\xF6", // this is invalid
            'Expected identical [Binary: F6]'
        );
    }

    function test_convertToUTF8_spuriousEncoding() {
        $this->config->set('Core.Encoding', 'utf99');
        $this->expectError('Invalid encoding utf99');
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToUTF8("\xF6", $this->config, $this->context),
            ''
        );
    }

    function test_convertToUTF8_iso8859_1() {
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToUTF8("\xF6", $this->config, $this->context),
            "\xC3\xB6"
        );
    }

    function test_convertToUTF8_withoutIconv() {
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->config->set('Test.ForceNoIconv', true);
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToUTF8("\xF6", $this->config, $this->context),
            "\xC3\xB6"
        );

    }

    function getZhongWen() {
        return "\xE4\xB8\xAD\xE6\x96\x87 (Chinese)";
    }

    function test_convertFromUTF8_utf8() {
        // UTF-8 means that we don't touch it
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $this->config, $this->context),
            "\xC3\xB6"
        );
    }

    function test_convertFromUTF8_iso8859_1() {
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $this->config, $this->context),
            "\xF6",
            'Expected identical [Binary: F6]'
        );
    }

    function test_convertFromUTF8_iconvNoChars() {
        if (!function_exists('iconv')) return;
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8($this->getZhongWen(), $this->config, $this->context),
            " (Chinese)"
        );
    }

    function test_convertFromUTF8_phpNormal() {
        // Plain PHP implementation has slightly different behavior
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->config->set('Test.ForceNoIconv', true);
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $this->config, $this->context),
            "\xF6",
            'Expected identical [Binary: F6]'
        );
    }

    function test_convertFromUTF8_phpNoChars() {
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->config->set('Test.ForceNoIconv', true);
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8($this->getZhongWen(), $this->config, $this->context),
            "?? (Chinese)"
        );
    }

    function test_convertFromUTF8_withProtection() {
        // Preserve the characters!
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->config->set('Core.EscapeNonASCIICharacters', true);
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8($this->getZhongWen(), $this->config, $this->context),
            "&#20013;&#25991; (Chinese)"
        );
    }

    function test_convertFromUTF8_withProtectionButUtf8() {
        // Preserve the characters!
        $this->config->set('Core.EscapeNonASCIICharacters', true);
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8($this->getZhongWen(), $this->config, $this->context),
            "&#20013;&#25991; (Chinese)"
        );
    }

    function test_convertToASCIIDumbLossless() {

        // Uppercase thorn letter
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToASCIIDumbLossless("\xC3\x9Eorn"),
            "&#222;orn"
        );

        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToASCIIDumbLossless("an"),
            "an"
        );

        // test up to four bytes
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToASCIIDumbLossless("\xF3\xA0\x80\xA0"),
            "&#917536;"
        );

    }

    function assertASCIISupportCheck($enc, $ret) {
        $test = HTMLPurifier_Encoder::testEncodingSupportsASCII($enc, true);
        if ($test === false) return;
        $this->assertIdentical(
            HTMLPurifier_Encoder::testEncodingSupportsASCII($enc),
            $ret
        );
        $this->assertIdentical(
            HTMLPurifier_Encoder::testEncodingSupportsASCII($enc, true),
            $ret
        );
    }

    function test_testEncodingSupportsASCII() {
        $this->assertASCIISupportCheck('Shift_JIS', array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~'));
        $this->assertASCIISupportCheck('JOHAB', array("\xE2\x82\xA9" => '\\'));
        $this->assertASCIISupportCheck('ISO-8859-1', array());
        $this->assertASCIISupportCheck('dontexist', array()); // canary
    }

    function testShiftJIS() {
        if (!function_exists('iconv')) return;
        $this->config->set('Core.Encoding', 'Shift_JIS');
        // This actually looks like a Yen, but we're going to treat it differently
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8('\\~', $this->config, $this->context),
            '\\~'
        );
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertToUTF8('\\~', $this->config, $this->context),
            '\\~'
        );
    }

    function testIconvTruncateBug() {
        if (!function_exists('iconv')) return;
        if (HTMLPurifier_Encoder::testIconvTruncateBug() !== HTMLPurifier_Encoder::ICONV_TRUNCATES) return;
        $this->config->set('Core.Encoding', 'ISO-8859-1');
        $this->assertIdentical(
            HTMLPurifier_Encoder::convertFromUTF8("\xE4\xB8\xAD" . str_repeat('a', 10000), $this->config, $this->context),
            str_repeat('a', 10000)
        );
    }

    function testIconvChunking() {
        if (!function_exists('iconv')) return;
        if (HTMLPurifier_Encoder::testIconvTruncateBug() !== HTMLPurifier_Encoder::ICONV_TRUNCATES) return;
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "a\xF3\xA0\x80\xA0b", 4), 'ab');
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aa\xE4\xB8\xADb", 4), 'aab');
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aaa\xCE\xB1b", 4), 'aaab');
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aaaa\xF3\xA0\x80\xA0b", 4), 'aaaab');
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aaaa\xE4\xB8\xADb", 4), 'aaaab');
        $this->assertIdentical(HTMLPurifier_Encoder::iconv('utf-8', 'iso-8859-1//IGNORE', "aaaa\xCE\xB1b", 4), 'aaaab');
    }

}

// vim: et sw=4 sts=4