Пример #1
0
        public void DecodeIfNotBinary_NulCharacters()
        {
            Action <string, Encoding, bool> verify = (text, encoding, shallThrow) =>
            {
                using (var stream = new MemoryStream(encoding.GetBytes(text)))
                {
                    if (shallThrow)
                    {
                        Assert.Throws(typeof(InvalidDataException), () => EncodedStringText.DecodeIfNotBinary(stream, encoding));
                    }
                    else
                    {
                        Assert.Equal(text, EncodedStringText.DecodeIfNotBinary(stream, encoding));
                        Assert.True(stream.CanRead);
                    }
                }
            };

            verify("", Encoding.BigEndianUnicode, false);
            verify("\0abc", Encoding.GetEncoding(437), false);
            verify("a\0bc", Encoding.UTF8, false);
            verify("abc\0", Encoding.GetEncoding(1252), false);
            verify("a\0b\0c", Encoding.UTF32, false);

            verify("\0\0abc", Encoding.Unicode, true);
            verify("a\0\0bc", Encoding.ASCII, true);
            verify("abc\0\0", Encoding.Default, true);
        }
        public void DecodeIfNotBinary()
        {
            var encoding = Encoding.GetEncoding(1252);
            var bytes    = new byte[] { 0x81, 0x8D, 0x8F, 0x90, 0x9D };

            using (var stream = new MemoryStream(bytes))
            {
                Assert.Equal("\x81\x8D\x8F\x90\x9D", EncodedStringText.DecodeIfNotBinary(stream, encoding));
                Assert.True(stream.CanRead);
            }

            var text = "abc def baz aeiouy äëïöüû";

            bytes = GetBytes(encoding, text);
            using (var stream = new MemoryStream(bytes))
            {
                Assert.Equal(text, EncodedStringText.DecodeIfNotBinary(stream, encoding));
                Assert.True(stream.CanRead);
            }

            // Test binary detection with a real binary
            using (var stream = new MemoryStream(ProprietaryTestResources.NetFX.v4_0_30319.System))
            {
                Assert.Throws(typeof(InvalidDataException), () => EncodedStringText.DecodeIfNotBinary(stream, encoding));
                Assert.True(stream.CanRead);
            }

            // Large file decode
            text  = new String('x', 1024 * 1024);
            bytes = GetBytes(encoding, text);
            using (var stream = new MemoryStream(bytes))
            {
                Assert.Equal(text, EncodedStringText.DecodeIfNotBinary(stream, encoding));
                Assert.True(stream.CanRead);
            }
        }