diff --git a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderConvert2.cs b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderConvert2.cs index f193a1bbbf4..8f32fee64ac 100644 --- a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderConvert2.cs +++ b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderConvert2.cs @@ -190,63 +190,6 @@ namespace System.Text.Tests } #endregion - #region Negative Test Cases - // NegTest1: ArgumentNullException should be thrown when chars or bytes is a null reference - [Fact] - public void NegTest1() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, null, 0, 0, new byte[1], 0, 0, true, typeof(ArgumentNullException), "101.1"); - VerificationHelper(encoder, new char[1], 0, 0, null, 0, 0, true, typeof(ArgumentNullException), "101.2"); - } - - // NegTest2: ArgumentOutOfRangeException should be thrown when charIndex, charCount, byteIndex, or byteCount is less than zero - [Fact] - public void NegTest2() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 0, -1, new byte[1], 0, 0, true, typeof(ArgumentOutOfRangeException), "102.1"); - VerificationHelper(encoder, new char[1], 0, 0, new byte[1], 0, -1, true, typeof(ArgumentOutOfRangeException), "102.2"); - VerificationHelper(encoder, new char[1], -1, 0, new byte[1], 0, 0, true, typeof(ArgumentOutOfRangeException), "102.3"); - VerificationHelper(encoder, new char[1], 0, 0, new byte[1], -1, 0, true, typeof(ArgumentOutOfRangeException), "102.4"); - } - - // NegTest3: ArgumentException should be thrown when The output buffer is too small to contain any of the converted input - [Fact] - public void NegTest3() - { - Encoder encoder = Encoding.Unicode.GetEncoder(); - char[] chars = new char[c_SIZE_OF_ARRAY]; - for (int i = 0; i < chars.Length; ++i) - { - chars[i] = _generator.GetChar(-55); - } - byte[] bytes1 = new byte[1]; - - VerificationHelper(encoder, chars, 0, chars.Length, bytes1, 0, bytes1.Length, true, typeof(ArgumentException), "103.1"); - } - - // NegTest4: ArgumentOutOfRangeException should be thrown when The length of chars - charIndex is less than charCount - [Fact] - public void NegTest4() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 1, 1, new byte[1], 0, 1, true, typeof(ArgumentOutOfRangeException), "104.1"); - } - - // NegTest5: ArgumentOutOfRangeException should be thrown when The length of bytes - byteIndex is less than byteCount - [Fact] - public void NegTest5() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 0, 1, new byte[1], 1, 1, true, typeof(ArgumentOutOfRangeException), "105.1"); - } - #endregion - private void VerificationHelper(Encoder encoder, char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, bool flush, int expectedCharsUsed, int expectedBytesUsed, bool expectedCompleted, string errorno) @@ -260,18 +203,5 @@ namespace System.Text.Tests Assert.Equal(expectedBytesUsed, bytesUsed); Assert.Equal(expectedCompleted, completed); } - - private void VerificationHelper(Encoder encoder, char[] chars, int charIndex, int charCount, byte[] bytes, - int byteIndex, int byteCount, bool flush, Type desired, string errorno) where T : Exception - { - int charsUsed; - int bytesUsed; - bool completed; - Assert.Throws(() => - { - encoder.Convert(chars, charIndex, charCount, bytes, byteIndex, byteCount, flush, out charsUsed, - out bytesUsed, out completed); - }); - } } } diff --git a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetByteCount2.cs b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetByteCount2.cs index 3fc5a5f8720..f19b49671e1 100644 --- a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetByteCount2.cs +++ b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetByteCount2.cs @@ -118,48 +118,13 @@ namespace System.Text.Tests VerificationHelper(encoder, chars, chars.Length - 1, 1, false, 2, "005.4"); } #endregion - - #region Negative Test Cases - // NegTest1: ArgumentNullException should be thrown when chars is a null reference - [Fact] - public void NegTest1() - { - VerificationHelper(Encoding.UTF8.GetEncoder(), null, 0, 0, true, typeof(ArgumentNullException), "101.1"); - } - - // NegTest2: ArgumentOutOfRangeException should be thrown when index or count is less than zero. - [Fact] - public void NegTest2() - { - VerificationHelper(Encoding.UTF8.GetEncoder(), new char[1], 0, -1, true, typeof(ArgumentOutOfRangeException), "102.1"); - VerificationHelper(Encoding.UTF8.GetEncoder(), new char[1], -1, 0, true, typeof(ArgumentOutOfRangeException), "102.2"); - } - - // NegTest3: ArgumentOutOfRangeException should be thrown when index and count do not denote a valid range in char - [Fact] - public void NegTest3() - { - VerificationHelper(Encoding.UTF8.GetEncoder(), new char[1], 0, 2, true, typeof(ArgumentOutOfRangeException), "103.1"); - VerificationHelper(Encoding.UTF8.GetEncoder(), new char[1], 1, 1, true, typeof(ArgumentOutOfRangeException), "103.2"); - } - #endregion - + private void VerificationHelper(Encoder encoder, char[] chars, int index, int count, bool flush, int expected, string errorno) { int ret = encoder.GetByteCount(chars, index, count, flush); Assert.Equal(expected, ret); } - private void VerificationHelper(Encoder encoder, char[] chars, int index, int count, bool flush, Type expected, string errorno) where T : Exception - { - string str = new string(chars); - - Assert.Throws(() => - { - int ret = encoder.GetByteCount(chars, index, count, flush); - }); - } - private bool IsHighSurrogate(char c) { return ((c >= HIGH_SURROGATE_START) && (c <= HIGH_SURROGATE_END)); diff --git a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetBytes2.cs b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetBytes2.cs index acddbdbf4b4..356e4aab2bc 100644 --- a/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetBytes2.cs +++ b/src/libraries/System.Text.Encoding/tests/Encoder/EncoderGetBytes2.cs @@ -128,66 +128,6 @@ namespace System.Text.Tests } #endregion - #region Negative Test Cases - // NegTest1: ArgumentNullException should be thrown when chars is a null reference or bytes is a null reference - [Fact] - public void NegTest1() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, null, 0, 0, new byte[1], 0, true, typeof(ArgumentNullException), "101.1"); - VerificationHelper(encoder, new char[1], 0, 0, null, 0, true, typeof(ArgumentNullException), "101.2"); - VerificationHelper(encoder, null, 0, 0, new byte[1], 0, false, typeof(ArgumentNullException), "101.3"); - VerificationHelper(encoder, new char[1], 0, 0, null, 0, false, typeof(ArgumentNullException), "101.4"); - } - - // NegTest2: ArgumentOutOfRangeException should be thrown when charIndex or charCount or byteIndex is less than zero. - [Fact] - public void NegTest2() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 0, -1, new byte[1], 0, true, typeof(ArgumentOutOfRangeException), "102.1"); - VerificationHelper(encoder, new char[1], 0, 0, new byte[1], -1, true, typeof(ArgumentOutOfRangeException), "102.2"); - VerificationHelper(encoder, new char[1], -1, 0, new byte[1], -1, true, typeof(ArgumentOutOfRangeException), "102.3"); - } - - // NegTest3: ArgumentException should be thrown when byteCount is less than the resulting number of bytes - [Fact] - public void NegTest3() - { - Encoder encoder = Encoding.Unicode.GetEncoder(); - char[] chars = new char[c_SIZE_OF_ARRAY]; - for (int i = 0; i < chars.Length; ++i) - { - chars[i] = _generator.GetChar(-55); - } - byte[] bytes1 = new byte[1]; - - VerificationHelper(encoder, chars, 0, chars.Length, bytes1, 1, true, typeof(ArgumentException), "103.1"); - VerificationHelper(encoder, chars, 0, chars.Length, bytes1, 1, false, typeof(ArgumentException), "103.2"); - } - - // NegTest4: ArgumentOutOfRangeException should be thrown when charIndex and charCount do not denote a valid range in chars - [Fact] - public void NegTest4() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 1, 1, new byte[1], 0, true, typeof(ArgumentOutOfRangeException), "104.1"); - VerificationHelper(encoder, new char[1], 0, 2, new byte[1], 0, true, typeof(ArgumentOutOfRangeException), "104.2"); - } - - // NegTest5: ArgumentOutOfRangeException should be thrown when byteIndex is not a valid index in bytes - [Fact] - public void NegTest5() - { - Encoder encoder = Encoding.UTF8.GetEncoder(); - - VerificationHelper(encoder, new char[1], 0, 1, new byte[1], 2, true, typeof(ArgumentOutOfRangeException), "105.1"); - } - #endregion - private void VerificationHelper(Encoder encoder, char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush, int expectedRetVal, string errorno) { @@ -195,17 +135,6 @@ namespace System.Text.Tests Assert.Equal(expectedRetVal, actualRetVal); } - private void VerificationHelper(Encoder encoder, char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, - bool flush, Type expected, string errorno) where T : Exception - { - string str = new string(chars); - - Assert.Throws(() => - { - int actualRetVal = encoder.GetBytes(chars, charIndex, charCount, bytes, byteIndex, flush); - }); - } - private bool IsHighSurrogate(char c) { return ((c >= HIGH_SURROGATE_START) && (c <= HIGH_SURROGATE_END)); diff --git a/src/libraries/System.Text.Encoding/tests/NegativeEncodingTests.cs b/src/libraries/System.Text.Encoding/tests/NegativeEncodingTests.cs index eda1affcebd..86af295ac09 100644 --- a/src/libraries/System.Text.Encoding/tests/NegativeEncodingTests.cs +++ b/src/libraries/System.Text.Encoding/tests/NegativeEncodingTests.cs @@ -356,6 +356,123 @@ namespace System.Text.Tests 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