// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using Xunit; namespace System.Text.Tests { public static class NegativeEncodingTests { public static IEnumerable Encodings_TestData() { yield return new object[] { new UnicodeEncoding(false, false) }; yield return new object[] { new UnicodeEncoding(true, false) }; yield return new object[] { new UnicodeEncoding(true, true) }; yield return new object[] { new UnicodeEncoding(true, true) }; yield return new object[] { new UTF7Encoding(true) }; yield return new object[] { new UTF7Encoding(false) }; yield return new object[] { new UTF8Encoding(true, true) }; yield return new object[] { new UTF8Encoding(false, true) }; yield return new object[] { new UTF8Encoding(true, false) }; yield return new object[] { new UTF8Encoding(false, false) }; yield return new object[] { new ASCIIEncoding() }; yield return new object[] { new UTF32Encoding(true, true, true) }; yield return new object[] { new UTF32Encoding(true, true, false) }; yield return new object[] { new UTF32Encoding(true, false, false) }; yield return new object[] { new UTF32Encoding(true, false, true) }; yield return new object[] { new UTF32Encoding(false, true, true) }; yield return new object[] { new UTF32Encoding(false, true, false) }; yield return new object[] { new UTF32Encoding(false, false, false) }; yield return new object[] { new UTF32Encoding(false, false, true) }; } [Theory] [MemberData(nameof(Encodings_TestData))] public static unsafe void GetByteCount_Invalid(Encoding encoding) { // Chars is null Assert.Throws(encoding is ASCIIEncoding ? "chars" : "s", () => encoding.GetByteCount((string)null)); Assert.Throws("chars", () => encoding.GetByteCount((char[])null)); Assert.Throws("chars", () => encoding.GetByteCount(null, 0, 0)); // Index or count < 0 Assert.Throws("index", () => encoding.GetByteCount(new char[3], -1, 0)); Assert.Throws("count", () => encoding.GetByteCount(new char[3], 0, -1)); // Index + count > chars.Length Assert.Throws("chars", () => encoding.GetByteCount(new char[3], 0, 4)); Assert.Throws("chars", () => encoding.GetByteCount(new char[3], 1, 3)); Assert.Throws("chars", () => encoding.GetByteCount(new char[3], 2, 2)); Assert.Throws("chars", () => encoding.GetByteCount(new char[3], 3, 1)); Assert.Throws("chars", () => encoding.GetByteCount(new char[3], 4, 0)); char[] chars = new char[3]; fixed (char* pChars = chars) { char* pCharsLocal = pChars; Assert.Throws("chars", () => encoding.GetByteCount(null, 0)); Assert.Throws("count", () => encoding.GetByteCount(pCharsLocal, -1)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static unsafe void GetBytes_Invalid(Encoding encoding) { string expectedStringParamName = encoding is ASCIIEncoding ? "chars" : "s"; // Source is null Assert.Throws("s", () => encoding.GetBytes((string)null)); Assert.Throws("chars", () => encoding.GetBytes((char[])null)); Assert.Throws("chars", () => encoding.GetBytes(null, 0, 0)); Assert.Throws(expectedStringParamName, () => encoding.GetBytes((string)null, 0, 0, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes((char[])null, 0, 0, new byte[1], 0)); // Bytes is null Assert.Throws("bytes", () => encoding.GetBytes("abc", 0, 3, null, 0)); Assert.Throws("bytes", () => encoding.GetBytes(new char[3], 0, 3, null, 0)); // Char index < 0 Assert.Throws("index", () => encoding.GetBytes(new char[1], -1, 0)); Assert.Throws("charIndex", () => encoding.GetBytes("a", -1, 0, new byte[1], 0)); Assert.Throws("charIndex", () => encoding.GetBytes(new char[1], -1, 0, new byte[1], 0)); // Char count < 0 Assert.Throws("count", () => encoding.GetBytes(new char[1], 0, -1)); Assert.Throws("charCount", () => encoding.GetBytes("a", 0, -1, new byte[1], 0)); Assert.Throws("charCount", () => encoding.GetBytes(new char[1], 0, -1, new byte[1], 0)); // Char index + count > source.Length Assert.Throws("chars", () => encoding.GetBytes(new char[1], 2, 0)); Assert.Throws(expectedStringParamName, () => encoding.GetBytes("a", 2, 0, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes(new char[1], 2, 0, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes(new char[1], 1, 1)); Assert.Throws(expectedStringParamName, () => encoding.GetBytes("a", 1, 1, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes(new char[1], 1, 1, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes(new char[1], 0, 2)); Assert.Throws(expectedStringParamName, () => encoding.GetBytes("a", 0, 2, new byte[1], 0)); Assert.Throws("chars", () => encoding.GetBytes(new char[1], 0, 2, new byte[1], 0)); // Byte index < 0 Assert.Throws("byteIndex", () => encoding.GetBytes("a", 0, 1, new byte[1], -1)); Assert.Throws("byteIndex", () => encoding.GetBytes(new char[1], 0, 1, new byte[1], -1)); // Byte index > bytes.Length Assert.Throws("byteIndex", () => encoding.GetBytes("a", 0, 1, new byte[1], 2)); Assert.Throws("byteIndex", () => encoding.GetBytes(new char[1], 0, 1, new byte[1], 2)); // Bytes does not have enough capacity to accomodate result Assert.Throws("bytes", () => encoding.GetBytes("a", 0, 1, new byte[0], 0)); Assert.Throws("bytes", () => encoding.GetBytes("abc", 0, 3, new byte[1], 0)); Assert.Throws("bytes", () => encoding.GetBytes("\uD800\uDC00", 0, 2, new byte[1], 0)); Assert.Throws("bytes", () => encoding.GetBytes(new char[1], 0, 1, new byte[0], 0)); Assert.Throws("bytes", () => encoding.GetBytes(new char[3], 0, 3, new byte[1], 0)); Assert.Throws("bytes", () => encoding.GetBytes("\uD800\uDC00".ToCharArray(), 0, 2, new byte[1], 0)); char[] chars = new char[3]; byte[] bytes = new byte[3]; byte[] smallBytes = new byte[1]; fixed (char* pChars = chars) fixed (byte* pBytes = bytes) fixed (byte* pSmallBytes = smallBytes) { char* pCharsLocal = pChars; byte* pBytesLocal = pBytes; byte* pSmallBytesLocal = pSmallBytes; // Bytes or chars is null Assert.Throws("chars", () => encoding.GetBytes((char*)null, 0, pBytesLocal, bytes.Length)); Assert.Throws("bytes", () => encoding.GetBytes(pCharsLocal, chars.Length, (byte*)null, bytes.Length)); // CharCount or byteCount is negative Assert.Throws("charCount", () => encoding.GetBytes(pCharsLocal, -1, pBytesLocal, bytes.Length)); Assert.Throws("byteCount", () => encoding.GetBytes(pCharsLocal, chars.Length, pBytesLocal, -1)); // Bytes does not have enough capacity to accomodate result Assert.Throws("bytes", () => encoding.GetBytes(pCharsLocal, chars.Length, pSmallBytesLocal, smallBytes.Length)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static unsafe void GetCharCount_Invalid(Encoding encoding) { // Bytes is null Assert.Throws("bytes", () => encoding.GetCharCount(null)); Assert.Throws("bytes", () => encoding.GetCharCount(null, 0, 0)); // Index or count < 0 Assert.Throws("index", () => encoding.GetCharCount(new byte[4], -1, 0)); Assert.Throws("count", () => encoding.GetCharCount(new byte[4], 0, -1)); // Index + count > bytes.Length Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 5, 0)); Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 4, 1)); Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 3, 2)); Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 2, 3)); Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 1, 4)); Assert.Throws("bytes", () => encoding.GetCharCount(new byte[4], 0, 5)); byte[] bytes = new byte[4]; fixed (byte* pBytes = bytes) { byte* pBytesLocal = pBytes; Assert.Throws("bytes", () => encoding.GetCharCount(null, 0)); Assert.Throws("count", () => encoding.GetCharCount(pBytesLocal, -1)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static unsafe void GetChars_Invalid(Encoding encoding) { // Bytes is null Assert.Throws("bytes", () => encoding.GetChars(null)); Assert.Throws("bytes", () => encoding.GetChars(null, 0, 0)); Assert.Throws("bytes", () => encoding.GetChars(null, 0, 0, new char[0], 0)); // Chars is null Assert.Throws("chars", () => encoding.GetChars(new byte[4], 0, 4, null, 0)); // Index < 0 Assert.Throws("index", () => encoding.GetChars(new byte[4], -1, 4)); Assert.Throws("byteIndex", () => encoding.GetChars(new byte[4], -1, 4, new char[1], 0)); // Count < 0 Assert.Throws("count", () => encoding.GetChars(new byte[4], 0, -1)); Assert.Throws("byteCount", () => encoding.GetChars(new byte[4], 0, -1, new char[1], 0)); // Count > bytes.Length Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 0, 5)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 0, 5, new char[1], 0)); // Index + count > bytes.Length Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 5, 0)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 5, 0, new char[1], 0)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 4, 1)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 4, 1, new char[1], 0)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 3, 2)); Assert.Throws("bytes", () => encoding.GetChars(new byte[4], 3, 2, new char[1], 0)); // CharIndex < 0 or >= chars.Length Assert.Throws("charIndex", () => encoding.GetChars(new byte[4], 0, 4, new char[1], -1)); Assert.Throws("charIndex", () => encoding.GetChars(new byte[4], 0, 4, new char[1], 2)); // Chars does not have enough capacity to accomodate result Assert.Throws("chars", () => encoding.GetChars(new byte[4], 0, 4, new char[1], 1)); byte[] bytes = new byte[encoding.GetMaxByteCount(2)]; char[] chars = new char[4]; char[] smallChars = new char[1]; fixed (byte* pBytes = bytes) fixed (char* pChars = chars) fixed (char* pSmallChars = smallChars) { byte* pBytesLocal = pBytes; char* pCharsLocal = pChars; char* pSmallCharsLocal = pSmallChars; // Bytes or chars is null Assert.Throws("bytes", () => encoding.GetChars((byte*)null, 0, pCharsLocal, chars.Length)); Assert.Throws("chars", () => encoding.GetChars(pBytesLocal, bytes.Length, (char*)null, chars.Length)); // ByteCount or charCount is negative Assert.Throws("byteCount", () => encoding.GetChars(pBytesLocal, -1, pCharsLocal, chars.Length)); Assert.Throws("charCount", () => encoding.GetChars(pBytesLocal, bytes.Length, pCharsLocal, -1)); // Chars does not have enough capacity to accomodate result Assert.Throws("chars", () => encoding.GetChars(pBytesLocal, bytes.Length, pSmallCharsLocal, smallChars.Length)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static void GetMaxByteCount_Invalid(Encoding encoding) { Assert.Throws("charCount", () => encoding.GetMaxByteCount(-1)); if (!(encoding is ASCIIEncoding)) { Assert.Throws("charCount", () => encoding.GetMaxByteCount(int.MaxValue / 2)); } Assert.Throws("charCount", () => encoding.GetMaxByteCount(int.MaxValue)); // Make sure that GetMaxByteCount respects the MaxCharCount property of EncoderFallback // However, Utf7Encoding ignores this if (!(encoding is UTF7Encoding)) { Encoding customizedMaxCharCountEncoding = Encoding.GetEncoding(encoding.CodePage, new HighMaxCharCountEncoderFallback(), DecoderFallback.ReplacementFallback); Assert.Throws("charCount", () => customizedMaxCharCountEncoding.GetMaxByteCount(2)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static void GetMaxCharCount_Invalid(Encoding encoding) { Assert.Throws("byteCount", () => encoding.GetMaxCharCount(-1)); // TODO: find a more generic way to find what byteCount is invalid if (encoding is UTF8Encoding) { Assert.Throws("byteCount", () => encoding.GetMaxCharCount(int.MaxValue)); } // Make sure that GetMaxCharCount respects the MaxCharCount property of DecoderFallback // However, Utf7Encoding ignores this if (!(encoding is UTF7Encoding) && !(encoding is UTF32Encoding)) { Encoding customizedMaxCharCountEncoding = Encoding.GetEncoding(encoding.CodePage, EncoderFallback.ReplacementFallback, new HighMaxCharCountDecoderFallback()); Assert.Throws("byteCount", () => customizedMaxCharCountEncoding.GetMaxCharCount(2)); } } [Theory] [MemberData(nameof(Encodings_TestData))] public static void GetString_Invalid(Encoding encoding) { // Bytes is null Assert.Throws("bytes", () => encoding.GetString(null)); Assert.Throws("bytes", () => encoding.GetString(null, 0, 0)); // Index or count < 0 Assert.Throws(encoding is ASCIIEncoding ? "byteIndex" : "index", () => encoding.GetString(new byte[1], -1, 0)); Assert.Throws(encoding is ASCIIEncoding ? "byteCount" : "count", () => encoding.GetString(new byte[1], 0, -1)); // Index + count > bytes.Length Assert.Throws("bytes", () => encoding.GetString(new byte[1], 2, 0)); Assert.Throws("bytes", () => encoding.GetString(new byte[1], 1, 1)); Assert.Throws("bytes", () => encoding.GetString(new byte[1], 0, 2)); } public static unsafe void Encode_Invalid(Encoding encoding, string chars, int index, int count) { Assert.Equal(EncoderFallback.ExceptionFallback, encoding.EncoderFallback); char[] charsArray = chars.ToCharArray(); byte[] bytes = new byte[encoding.GetMaxByteCount(count)]; if (index == 0 && count == chars.Length) { Assert.Throws(() => encoding.GetByteCount(chars)); Assert.Throws(() => encoding.GetByteCount(charsArray)); Assert.Throws(() => encoding.GetBytes(chars)); Assert.Throws(() => encoding.GetBytes(charsArray)); } Assert.Throws(() => encoding.GetBytes(charsArray, index, count)); Assert.Throws(() => encoding.GetBytes(chars, index, count, bytes, 0)); Assert.Throws(() => encoding.GetBytes(charsArray, index, count, bytes, 0)); fixed (char* pChars = chars) fixed (byte* pBytes = bytes) { char* pCharsLocal = pChars; byte* pBytesLocal = pBytes; Assert.Throws(() => encoding.GetByteCount(pCharsLocal + index, count)); Assert.Throws(() => encoding.GetBytes(pCharsLocal + index, count, pBytesLocal, bytes.Length)); } } public static unsafe void Decode_Invalid(Encoding encoding, byte[] bytes, int index, int count) { Assert.Equal(DecoderFallback.ExceptionFallback, encoding.DecoderFallback); char[] chars = new char[encoding.GetMaxCharCount(count)]; if (index == 0 && count == bytes.Length) { Assert.Throws(() => encoding.GetCharCount(bytes)); Assert.Throws(() => encoding.GetChars(bytes)); Assert.Throws(() => encoding.GetString(bytes)); } Assert.Throws(() => encoding.GetCharCount(bytes, index, count)); Assert.Throws(() => encoding.GetChars(bytes, index, count)); Assert.Throws(() => encoding.GetString(bytes, index, count)); Assert.Throws(() => encoding.GetChars(bytes, index, count, chars, 0)); fixed (byte* pBytes = bytes) fixed (char* pChars = chars) { byte* pBytesLocal = pBytes; char* pCharsLocal = pChars; Assert.Throws(() => encoding.GetCharCount(pBytesLocal + index, count)); Assert.Throws(() => encoding.GetChars(pBytesLocal + index, count, pCharsLocal, chars.Length)); Assert.Throws(() => encoding.GetString(pBytesLocal + index, count)); } } public static IEnumerable Encoders_TestData() { foreach (object[] encodingTestData in Encodings_TestData()) { Encoding encoding = (Encoding)encodingTestData[0]; yield return new object[] { encoding.GetEncoder(), true }; yield return new object[] { encoding.GetEncoder(), false }; } } [Theory] [MemberData(nameof(Encoders_TestData))] public static void Encoder_GetByteCount_Invalid(Encoder encoder, bool flush) { // Chars is null Assert.Throws("chars", () => encoder.GetByteCount(null, 0, 0, flush)); // Index is invalid Assert.Throws("index", () => encoder.GetByteCount(new char[4], -1, 0, flush)); Assert.Throws("chars", () => encoder.GetByteCount(new char[4], 5, 0, flush)); // Count is invalid Assert.Throws("count", () => encoder.GetByteCount(new char[4], 0, -1, flush)); Assert.Throws("chars", () => encoder.GetByteCount(new char[4], 0, 5, flush)); Assert.Throws("chars", () => encoder.GetByteCount(new char[4], 1, 4, flush)); } [Theory] [MemberData(nameof(Encoders_TestData))] public static void Encoder_GetBytes_Invalid(Encoder encoder, bool flush) { // Chars is null Assert.Throws("chars", () => encoder.GetBytes(null, 0, 0, new byte[4], 0, flush)); // CharIndex is invalid Assert.Throws("charIndex", () => encoder.GetBytes(new char[4], -1, 0, new byte[4], 0, flush)); Assert.Throws("chars", () => encoder.GetBytes(new char[4], 5, 0, new byte[4], 0, flush)); // CharCount is invalid Assert.Throws("charCount", () => encoder.GetBytes(new char[4], 0, -1, new byte[4], 0, flush)); Assert.Throws("chars", () => encoder.GetBytes(new char[4], 0, 5, new byte[4], 0, flush)); Assert.Throws("chars", () => encoder.GetBytes(new char[4], 1, 4, new byte[4], 0, flush)); // Bytes is null Assert.Throws("bytes", () => encoder.GetBytes(new char[1], 0, 1, null, 0, flush)); // ByteIndex is invalid Assert.Throws("byteIndex", () => encoder.GetBytes(new char[1], 0, 1, new byte[4], -1, flush)); Assert.Throws("byteIndex", () => encoder.GetBytes(new char[1], 0, 1, new byte[4], 5, flush)); // Bytes does not have enough space int byteCount = encoder.GetByteCount(new char[] { 'a' }, 0, 1, flush); Assert.Throws("bytes", () => encoder.GetBytes(new char[] { 'a' }, 0, 1, new byte[byteCount - 1], 0, flush)); } [Theory] [MemberData(nameof(Encoders_TestData))] public static void Encoder_Convert_Invalid(Encoder encoder, bool flush) { int charsUsed = 0; int bytesUsed = 0; bool completed = false; Action verifyOutParams = () => { Assert.Equal(0, charsUsed); Assert.Equal(0, bytesUsed); Assert.False(completed); }; // Chars is null Assert.Throws("chars", () => encoder.Convert(null, 0, 0, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // CharIndex is invalid Assert.Throws("charIndex", () => encoder.Convert(new char[4], -1, 0, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("chars", () => encoder.Convert(new char[4], 5, 0, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // CharCount is invalid Assert.Throws("charCount", () => encoder.Convert(new char[4], 0, -1, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("chars", () => encoder.Convert(new char[4], 0, 5, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("chars", () => encoder.Convert(new char[4], 1, 4, new byte[4], 0, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // Bytes is null Assert.Throws("bytes", () => encoder.Convert(new char[1], 0, 1, null, 0, 0, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // ByteIndex is invalid Assert.Throws("byteIndex", () => encoder.Convert(new char[1], 0, 0, new byte[4], -1, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("bytes", () => encoder.Convert(new char[1], 0, 0, new byte[4], 5, 0, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // ByteCount is invalid Assert.Throws("byteCount", () => encoder.Convert(new char[1], 0, 0, new byte[4], 0, -1, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("bytes", () => encoder.Convert(new char[1], 0, 0, new byte[4], 0, 5, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); Assert.Throws("bytes", () => encoder.Convert(new char[1], 0, 0, new byte[4], 1, 4, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); // Bytes does not have enough space int byteCount = encoder.GetByteCount(new char[] { 'a' }, 0, 1, flush); Assert.Throws("bytes", () => encoder.Convert(new char[] { 'a' }, 0, 1, new byte[byteCount - 1], 0, byteCount - 1, flush, out charsUsed, out bytesUsed, out completed)); verifyOutParams(); } } public class HighMaxCharCountEncoderFallback : EncoderFallback { public override int MaxCharCount => int.MaxValue; public override EncoderFallbackBuffer CreateFallbackBuffer() => ReplacementFallback.CreateFallbackBuffer(); } public class HighMaxCharCountDecoderFallback : DecoderFallback { public override int MaxCharCount => int.MaxValue; public override DecoderFallbackBuffer CreateFallbackBuffer() => ReplacementFallback.CreateFallbackBuffer(); } }