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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Unicode;
using Xunit;
namespace System.Text.Encodings.Web.Tests
{
public class UnicodeEncoderBaseTests
{
[Fact]
public void Ctor_WithCustomFilters()
{
// Arrange
var filter = new TextEncoderSettings();
filter.AllowCharacters('a', 'b');
filter.AllowCharacters('\0', '&', '\uFFFF', 'd');
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(filter);
// Act & assert
Assert.Equal("a", encoder.Encode("a"));
Assert.Equal("b", encoder.Encode("b"));
Assert.Equal("[U+0063]", encoder.Encode("c"));
Assert.Equal("d", encoder.Encode("d"));
Assert.Equal("[U+0000]", encoder.Encode("\0")); // we still always encode control chars
Assert.Equal("[U+0026]", encoder.Encode("&")); // we still always encode HTML-special chars
Assert.Equal("[U+FFFF]", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars
}
[Fact]
public void Ctor_WithUnicodeRanges()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(new TextEncoderSettings(UnicodeRanges.Latin1Supplement, UnicodeRanges.MiscellaneousSymbols));
// Act & assert
Assert.Equal("[U+0061]", encoder.Encode("a"));
Assert.Equal("\u00E9", encoder.Encode("\u00E9" /* LATIN SMALL LETTER E WITH ACUTE */));
Assert.Equal("\u2601", encoder.Encode("\u2601" /* CLOUD */));
}
[Fact]
public void Encode_AllRangesAllowed_StillEncodesForbiddenChars_Simple()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
const string input = "Hello <>&\'\"+ there!";
const string expected = "Hello [U+003C][U+003E][U+0026][U+0027][U+0022][U+002B] there!";
// Act & assert
Assert.Equal(expected, encoder.Encode(input));
}
[Fact]
public void Encode_AllRangesAllowed_StillEncodesForbiddenChars_Extended()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
// Act & assert - BMP chars
for (int i = 0; i <= 0xFFFF; i++)
{
string input = new string((char)i, 1);
string expected;
if (IsSurrogateCodePoint(i))
{
expected = "\uFFFD"; // unpaired surrogate -> Unicode replacement char
}
else
{
bool mustEncode = false;
switch (i)
{
case '<':
case '>':
case '&':
case '\"':
case '\'':
case '+':
mustEncode = true;
break;
}
if (i <= 0x001F || (0x007F <= i && i <= 0x9F))
{
mustEncode = true; // control char
}
else if (!UnicodeTestHelpers.IsCharacterDefined((char)i))
{
mustEncode = true; // undefined (or otherwise disallowed) char
}
if (mustEncode)
{
expected = string.Format(CultureInfo.InvariantCulture, "[U+{0:X4}]", i);
}
else
{
expected = input; // no encoding
}
}
string retVal = encoder.Encode(input);
Assert.Equal(expected, retVal);
}
// Act & assert - astral chars
for (int i = 0x10000; i <= 0x10FFFF; i++)
{
string input = char.ConvertFromUtf32(i);
string expected = string.Format(CultureInfo.InvariantCulture, "[U+{0:X}]", i);
string retVal = encoder.Encode(input);
Assert.Equal(expected, retVal);
}
}
[Fact]
public void Encode_BadSurrogates_ReturnsUnicodeReplacementChar()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All); // allow all codepoints
// "a<unpaired leading>b<unpaired trailing>c<trailing before leading>d<unpaired trailing><valid>e<high at end of string>"
const string input = "a\uD800b\uDFFFc\uDFFF\uD800d\uDFFF\uD800\uDFFFe\uD800";
const string expected = "a\uFFFDb\uFFFDc\uFFFD\uFFFDd\uFFFD[U+103FF]e\uFFFD";
// Act
string retVal = encoder.Encode(input);
// Assert
Assert.Equal(expected, retVal);
}
[Fact]
public void Encode_EmptyStringInput_ReturnsEmptyString()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
// Act & assert
Assert.Equal("", encoder.Encode(""));
}
[Fact]
public void Encode_InputDoesNotRequireEncoding_ReturnsOriginalStringInstance()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
string input = "Hello, there!";
// Act & assert
Assert.Same(input, encoder.Encode(input));
}
[Fact]
public void Encode_NullInput_ReturnsNull()
{
// Arrange
UnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
// Act & assert
Assert.Null(encoder.Encode(null));
}
[Fact]
public void Encode_WithCharsRequiringEncodingAtBeginning()
{
Assert.Equal("[U+0026]Hello, there!", new CustomUnicodeEncoderBase(UnicodeRanges.All).Encode("&Hello, there!"));
}
[Fact]
public void Encode_WithCharsRequiringEncodingAtEnd()
{
Assert.Equal("Hello, there![U+0026]", new CustomUnicodeEncoderBase(UnicodeRanges.All).Encode("Hello, there!&"));
}
[Fact]
public void Encode_WithCharsRequiringEncodingInMiddle()
{
Assert.Equal("Hello, [U+0026]there!", new CustomUnicodeEncoderBase(UnicodeRanges.All).Encode("Hello, &there!"));
}
[Fact]
public void Encode_WithCharsRequiringEncodingInterspersed()
{
Assert.Equal("Hello, [U+003C]there[U+003E]!", new CustomUnicodeEncoderBase(UnicodeRanges.All).Encode("Hello, <there>!"));
}
[Fact]
public void Encode_CharArray_ParameterChecking_NegativeTestCases()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
// Act & assert
Assert.Throws<ArgumentNullException>(() => encoder.Encode((char[])null, 0, 0, new StringWriter()));
Assert.Throws<ArgumentNullException>(() => encoder.Encode("abc".ToCharArray(), 0, 3, null));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc".ToCharArray(), -1, 2, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc".ToCharArray(), 2, 2, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc".ToCharArray(), 4, 0, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc".ToCharArray(), 2, -1, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc".ToCharArray(), 1, 3, new StringWriter()));
}
//[Fact]
//public void Encode_CharArray_ZeroCount_DoesNotCallIntoTextWriter()
//{
// // Arrange
// CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
// TextWriter output = new Mock<TextWriter>(MockBehavior.Strict).Object;
// // Act
// encoder.Encode("abc".ToCharArray(), 2, 0, output);
// // Assert
// // If we got this far (without TextWriter throwing), success!
//}
[Fact]
public void Encode_CharArray_AllCharsValid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz".ToCharArray(), 4, 2, output);
// Assert
Assert.Equal("xy", output.ToString());
}
[Fact]
public void Encode_CharArray_AllCharsInvalid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz".ToCharArray(), 4, 2, output);
// Assert
Assert.Equal("[U+0078][U+0079]", output.ToString());
}
[Fact]
public void Encode_CharArray_SomeCharsValid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz".ToCharArray(), 2, 3, output);
// Assert
Assert.Equal("c[U+0026]x", output.ToString());
}
[Fact]
public void Encode_StringSubstring_ParameterChecking_NegativeTestCases()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
// Act & assert
Assert.Throws<ArgumentNullException>(() => encoder.Encode((string)null, 0, 0, new StringWriter()));
Assert.Throws<ArgumentNullException>(() => encoder.Encode("abc", 0, 3, null));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc", -1, 2, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc", 2, 2, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc", 4, 0, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc", 2, -1, new StringWriter()));
Assert.Throws<ArgumentOutOfRangeException>(() => encoder.Encode("abc", 1, 3, new StringWriter()));
}
//[Fact]
//public void Encode_StringSubstring_ZeroCount_DoesNotCallIntoTextWriter()
//{
// // Arrange
// CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
// TextWriter output = new Mock<TextWriter>(MockBehavior.Strict).Object;
// // Act
// encoder.Encode("abc", 2, 0, output);
// // Assert
// // If we got this far (without TextWriter throwing), success!
//}
[Fact]
public void Encode_StringSubstring_AllCharsValid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz", 4, 2, output);
// Assert
Assert.Equal("xy", output.ToString());
}
//[Fact]
//public void Encode_StringSubstring_EntireString_AllCharsValid_ForwardDirectlyToOutput()
//{
// // Arrange
// CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
// var mockWriter = new Mock<TextWriter>(MockBehavior.Strict);
// mockWriter.Setup(o => o.Write("abc")).Verifiable();
// // Act
// encoder.Encode("abc", 0, 3, mockWriter.Object);
// // Assert
// mockWriter.Verify();
//}
[Fact]
public void Encode_StringSubstring_AllCharsInvalid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase();
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz", 4, 2, output);
// Assert
Assert.Equal("[U+0078][U+0079]", output.ToString());
}
[Fact]
public void Encode_StringSubstring_SomeCharsValid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
StringWriter output = new StringWriter();
// Act
encoder.Encode("abc&xyz", 2, 3, output);
// Assert
Assert.Equal("c[U+0026]x", output.ToString());
}
[Fact]
public void Encode_StringSubstring_EntireString_SomeCharsValid()
{
// Arrange
CustomUnicodeEncoderBase encoder = new CustomUnicodeEncoderBase(UnicodeRanges.All);
StringWriter output = new StringWriter();
// Act
const string input = "abc&xyz";
encoder.Encode(input, 0, input.Length, output);
// Assert
Assert.Equal("abc[U+0026]xyz", output.ToString());
}
private static bool IsSurrogateCodePoint(int codePoint)
{
return (0xD800 <= codePoint && codePoint <= 0xDFFF);
}
private sealed class CustomTextEncoderSettings : TextEncoderSettings
{
private readonly int[] _allowedCodePoints;
public CustomTextEncoderSettings(params int[] allowedCodePoints)
{
_allowedCodePoints = allowedCodePoints;
}
public override IEnumerable<int> GetAllowedCodePoints()
{
return _allowedCodePoints;
}
}
private sealed class CustomUnicodeEncoderBase : UnicodeEncoderBase
{
// We pass a (known bad) value of 1 for 'max output chars per input char',
// which also tests that the code behaves properly even if the original
// estimate is incorrect.
public CustomUnicodeEncoderBase(TextEncoderSettings filter)
: base(filter, maxOutputCharsPerInputChar: 1)
{
}
public CustomUnicodeEncoderBase(params UnicodeRange[] allowedRanges)
: this(new TextEncoderSettings(allowedRanges))
{
}
protected override void WriteEncodedScalar(ref Writer writer, uint value)
{
writer.Write(string.Format(CultureInfo.InvariantCulture, "[U+{0:X4}]", value));
}
}
}
}
|