3 # Copyright 2007-2009 Wincent Colaiuta. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
18 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 # POSSIBILITY OF SUCH DAMAGE.
26 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
29 describe Wikitext, 'sanitizing a link target' do
30 it 'should complain if passed nil' do
31 lambda { Wikitext::Parser.sanitize_link_target(nil) }.should raise_error
34 it 'should complain if passed <' do
35 lambda { Wikitext::Parser.sanitize_link_target('<') }.should raise_error(RangeError, /</)
38 it 'should complain if passed >' do
39 lambda { Wikitext::Parser.sanitize_link_target('>') }.should raise_error(RangeError, />/)
42 it 'should do nothing on zero-length input' do
43 Wikitext::Parser.sanitize_link_target('').should == ''
46 it 'should do nothing to embedded spaces' do
47 Wikitext::Parser.sanitize_link_target('hello world').should == 'hello world'
50 it 'should eat leading spaces' do
51 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
52 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
53 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
54 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
55 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
56 Wikitext::Parser.sanitize_link_target(' hello world').should == 'hello world'
59 it 'should eat trailing spaces' do
60 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world'
61 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world'
62 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world'
63 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world'
64 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world' # was a crasher
65 Wikitext::Parser.sanitize_link_target('hello world ').should == 'hello world' # was a crasher
67 # same but with lots of entities to force a reallocation (we were crashing under reallocation)
68 expected = '""""""""""'
69 Wikitext::Parser.sanitize_link_target('"""""""""" ').should == expected
72 it 'should eat leading and trailing spaces combined' do
73 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
74 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
75 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
76 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
77 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
78 Wikitext::Parser.sanitize_link_target(' hello world ').should == 'hello world'
81 it 'should return nothing for input consisting entirely of spaces' do
82 Wikitext::Parser.sanitize_link_target(' ').should == ''
83 Wikitext::Parser.sanitize_link_target(' ').should == ''
84 Wikitext::Parser.sanitize_link_target(' ').should == ''
85 Wikitext::Parser.sanitize_link_target(' ').should == ''
86 Wikitext::Parser.sanitize_link_target(' ').should == ''
87 Wikitext::Parser.sanitize_link_target(' ').should == ''
90 it 'should convert double quotes into named entities' do
91 Wikitext::Parser.sanitize_link_target('hello "world"').should == 'hello "world"'
94 it 'should convert ampersands into named entities' do
95 Wikitext::Parser.sanitize_link_target('hello & goodbye').should == 'hello & goodbye'
98 it 'should convert non-ASCII hexadecimal entities' do
99 Wikitext::Parser.sanitize_link_target('cañon').should == 'cañon'
102 it 'should handle mixed scenarios (ampersands, double-quotes and non-ASCII)' do
103 Wikitext::Parser.sanitize_link_target('foo, "bar" & baz €').should == 'foo, "bar" & baz €'
106 # here we're exercising the _Wikitext_utf8_to_utf32 function
107 describe 'with invalidly encoded input' do
108 it 'should raise an exception for missing second byte' do
110 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::TWO_BYTES_MISSING_SECOND_BYTE)
111 }.should raise_error(Wikitext::Parser::Error, /truncated/)
113 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::TWO_BYTES_MISSING_SECOND_BYTE)
114 }.should raise_error(Wikitext::Parser::Error, /truncated/)
117 it 'should raise an exception for malformed second byte' do
119 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::TWO_BYTES_MALFORMED_SECOND_BYTE)
120 }.should raise_error(Wikitext::Parser::Error, /malformed/)
122 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::TWO_BYTES_MALFORMED_SECOND_BYTE)
123 }.should raise_error(Wikitext::Parser::Error, /malformed/)
126 it 'should raise an exception for overlong sequence' do
128 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::OVERLONG)
129 }.should raise_error(Wikitext::Parser::Error, /overlong/)
131 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::OVERLONG)
132 }.should raise_error(Wikitext::Parser::Error, /overlong/)
136 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::OVERLONG_ALT)
137 }.should raise_error(Wikitext::Parser::Error, /overlong/)
139 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::OVERLONG_ALT)
140 }.should raise_error(Wikitext::Parser::Error, /overlong/)
143 it 'should raise an exception for missing second byte in three-byte sequence' do
145 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::THREE_BYTES_MISSING_SECOND_BYTE)
146 }.should raise_error(Wikitext::Parser::Error, /truncated/)
148 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::THREE_BYTES_MISSING_SECOND_BYTE)
149 }.should raise_error(Wikitext::Parser::Error, /truncated/)
152 it 'should raise an exception for missing third byte in three-byte sequence' do
154 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::THREE_BYTES_MISSING_THIRD_BYTE)
155 }.should raise_error(Wikitext::Parser::Error, /truncated/)
157 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::THREE_BYTES_MISSING_THIRD_BYTE)
158 }.should raise_error(Wikitext::Parser::Error, /truncated/)
161 it 'should raise an exception for malformed second byte in three-byte sequence' do
163 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::THREE_BYTES_MALFORMED_SECOND_BYTE)
164 }.should raise_error(Wikitext::Parser::Error, /malformed/)
166 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::THREE_BYTES_MALFORMED_SECOND_BYTE)
167 }.should raise_error(Wikitext::Parser::Error, /malformed/)
170 it 'should raise an exception for malformed third byte in three-byte sequence' do
172 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::THREE_BYTES_MALFORMED_THIRD_BYTE)
173 }.should raise_error(Wikitext::Parser::Error, /malformed/)
175 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::THREE_BYTES_MALFORMED_THIRD_BYTE)
176 }.should raise_error(Wikitext::Parser::Error, /malformed/)
179 it 'should raise an exception for missing second byte in four-byte sequence' do
181 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_MISSING_SECOND_BYTE)
182 }.should raise_error(Wikitext::Parser::Error, /truncated/)
184 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_MISSING_SECOND_BYTE)
185 }.should raise_error(Wikitext::Parser::Error, /truncated/)
188 it 'should raise an exception for missing third byte in four-byte sequence' do
190 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_MISSING_THIRD_BYTE)
191 }.should raise_error(Wikitext::Parser::Error, /truncated/)
193 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_MISSING_THIRD_BYTE)
194 }.should raise_error(Wikitext::Parser::Error, /truncated/)
197 it 'should raise an exception for missing fourth byte in four-byte sequence' do
199 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_MISSING_FOURTH_BYTE)
200 }.should raise_error(Wikitext::Parser::Error, /truncated/)
202 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_MISSING_FOURTH_BYTE)
203 }.should raise_error(Wikitext::Parser::Error, /truncated/)
206 it 'should raise an exception for illegal first byte in four-byte sequence' do
208 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE)
209 }.should raise_error(Wikitext::Parser::Error, /overlong/)
211 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE)
212 }.should raise_error(Wikitext::Parser::Error, /overlong/)
215 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE_ALT)
216 }.should raise_error(Wikitext::Parser::Error, /overlong/)
218 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE_ALT)
219 }.should raise_error(Wikitext::Parser::Error, /overlong/)
222 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE_ALT2)
223 }.should raise_error(Wikitext::Parser::Error, /overlong/)
225 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::FOUR_BYTES_ILLEGAL_FIRST_BYTE_ALT2)
226 }.should raise_error(Wikitext::Parser::Error, /overlong/)
229 it 'should raise an exception for unexpected bytes' do
231 Wikitext::Parser.sanitize_link_target(UTF8::Invalid::UNEXPECTED_BYTE)
232 }.should raise_error(Wikitext::Parser::Error, /unexpected/)
234 Wikitext::Parser.sanitize_link_target('good text' + UTF8::Invalid::UNEXPECTED_BYTE)
235 }.should raise_error(Wikitext::Parser::Error, /unexpected/)