Ejemplo n.º 1
0
        public void Base64Stream_WithVeryLongString_ShouldEncodeProperly()
        {
            var writeStateInfo = new Base64WriteStateInfo(10, new byte[0], new byte[0], 70, 0);
            var s = new Base64Stream(writeStateInfo);

            byte[] buffer = Encoding.UTF8.GetBytes(LongString);
            s.EncodeBytes(buffer, 0, buffer.Length);
            string encodedString = s.GetEncodedString();

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int    result         = s.DecodeBytes(stringToDecode, 0, encodedString.Length);

            Assert.Equal(LongString, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 2
0
        public void Base64Stream_WithVeryLongString_ShouldEncodeProperly()
        {
            var writeStateInfo = new Base64WriteStateInfo(10, new byte[0], new byte[0], 70, 0);
            var s = new Base64Stream(writeStateInfo);

            byte[] buffer = Encoding.UTF8.GetBytes(LongString);
            s.EncodeBytes(buffer, 0, buffer.Length);
            string encodedString = s.GetEncodedString();

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int result = s.DecodeBytes(stringToDecode, 0, encodedString.Length);

            Assert.Equal(LongString, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 3
0
        public void Base64Stream_WithBasicAsciiString_ShouldEncodeAndDecode(string testHeader)
        {
            var s = new Base64Stream(new Base64WriteStateInfo());
            var testHeaderBytes = Encoding.UTF8.GetBytes(testHeader);
            s.EncodeBytes(testHeaderBytes, 0, testHeaderBytes.Length);

            string encodedString = s.GetEncodedString();
            for (int i = 0; i < encodedString.Length; i++)
            {
                Assert.InRange((byte)encodedString[i], 0, 127);
            }

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int result = s.DecodeBytes(stringToDecode, 0, encodedString.Length);
            Assert.Equal(testHeader, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 4
0
        public void Base64Stream_WithVerySmallBuffer_ShouldTriggerBufferResize_AndShouldEncodeProperly()
        {
            var s = new Base64Stream(new Base64WriteStateInfo(10, new byte[0], new byte[0], 70, 0));

            const string TestString = "0123456789abcdef";

            byte[] buffer = Encoding.UTF8.GetBytes(TestString);
            s.EncodeBytes(buffer, 0, buffer.Length);
            string encodedString = s.GetEncodedString();

            Assert.Equal("MDEyMzQ1Njc4OWFiY2RlZg==", encodedString);

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int    result         = s.DecodeBytes(stringToDecode, 0, encodedString.Length);

            Assert.Equal(TestString, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 5
0
        public void Base64Stream_WithVerySmallBuffer_ShouldTriggerBufferResize_AndShouldEncodeProperly()
        {
            var s = new Base64Stream(new Base64WriteStateInfo(10, new byte[0], new byte[0], 70, 0));

            const string TestString = "0123456789abcdef";

            byte[] buffer = Encoding.UTF8.GetBytes(TestString);
            s.EncodeBytes(buffer, 0, buffer.Length);
            string encodedString = s.GetEncodedString();

            Assert.Equal("MDEyMzQ1Njc4OWFiY2RlZg==", encodedString);

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int result = s.DecodeBytes(stringToDecode, 0, encodedString.Length);

            Assert.Equal(TestString, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 6
0
        public void Base64Stream_WithBasicAsciiString_ShouldEncodeAndDecode(string testHeader)
        {
            var s = new Base64Stream(new Base64WriteStateInfo());
            var testHeaderBytes = Encoding.UTF8.GetBytes(testHeader);

            s.EncodeBytes(testHeaderBytes, 0, testHeaderBytes.Length);

            string encodedString = s.GetEncodedString();

            for (int i = 0; i < encodedString.Length; i++)
            {
                Assert.InRange((byte)encodedString[i], 0, 127);
            }

            byte[] stringToDecode = Encoding.ASCII.GetBytes(encodedString);
            int    result         = s.DecodeBytes(stringToDecode, 0, encodedString.Length);

            Assert.Equal(testHeader, Encoding.UTF8.GetString(stringToDecode, 0, result));
        }
Ejemplo n.º 7
0
        internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding)
        {
            StringBuilder builder = new StringBuilder();

            if ((encoding == null) && IsAscii(value, false))
            {
                return(value);
            }
            if (encoding == null)
            {
                encoding = Encoding.GetEncoding("utf-8");
            }
            string bodyName = encoding.BodyName();

            if (encoding == Encoding.BigEndianUnicode)
            {
                bodyName = "utf-16be";
            }
            builder.Append("=?");
            builder.Append(bodyName);
            builder.Append("?");
            builder.Append(base64Encoding ? "B" : "Q");
            builder.Append("?");
            byte[] bytes = encoding.GetBytes(value);
            if (base64Encoding)
            {
                Base64Stream stream = new Base64Stream(-1);
                stream.EncodeBytes(bytes, 0, bytes.Length, true);
                builder.Append(Encoding.ASCII.GetString(stream.WriteState.Buffer, 0, stream.WriteState.Length));
            }
            else
            {
                QuotedPrintableStream stream2 = new QuotedPrintableStream(-1);
                stream2.EncodeBytes(bytes, 0, bytes.Length);
                builder.Append(Encoding.ASCII.GetString(stream2.WriteState.Buffer, 0, stream2.WriteState.Length));
            }
            builder.Append("?=");
            return(builder.ToString());
        }
Ejemplo n.º 8
0
 internal void Write()
 {
     while (true)
     {
         // do not append a space when writing from a stream since this means
         // it's writing the email body
         _written += _parent.EncodeBytes(_buffer, _offset + _written, _count - _written, false, false);
         if (_written < _count)
         {
             IAsyncResult result = _parent.BaseStream.BeginWrite(_parent.WriteState.Buffer, 0, _parent.WriteState.Length, s_onWrite, this);
             if (!result.CompletedSynchronously)
             {
                 break;
             }
             CompleteWrite(result);
         }
         else
         {
             InvokeCallback();
             break;
         }
     }
 }