public void QuotedPrintableStream_EmbededCRAndLFSpltBetweenWrites_EncodeCrlfFlagNotHonored()
        {
            // When split across writes, the stream encodes CRLF even though we asked it not to.
            var outputStream = new MemoryStream();
            var testStream = new QuotedPrintableStream(outputStream, false);

            byte[] bytesToWrite1 = Encoding.ASCII.GetBytes("Hello \r");
            testStream.Write(bytesToWrite1, 0, bytesToWrite1.Length);

            byte[] bytesToWrite2 = Encoding.ASCII.GetBytes("\n World");
            testStream.Write(bytesToWrite2, 0, bytesToWrite2.Length);

            testStream.Flush();

            // We told it not to encode them, but they got split across writes so it could not 
            // detect the sequence.  This can happen any time we try to encode only a subset 
            // of the data at a time.
            const string ExpectedOutput = "Hello =0D=0A World";
            outputStream.Seek(0, SeekOrigin.Begin);
            byte[] bytesRead = new byte[Encoding.ASCII.GetByteCount(ExpectedOutput) * 2];
            int bytesReadCount = outputStream.Read(bytesRead, 0, bytesRead.Length);

            string results = Encoding.ASCII.GetString(bytesRead, 0, bytesReadCount);
            Assert.Equal(ExpectedOutput, results);
        }
Example #2
0
        public void QuotedPrintableStream_EmbededCRAndLFSpltBetweenWrites_EncodeCrlfFlagNotHonored()
        {
            // When split across writes, the stream encodes CRLF even though we asked it not to.
            var outputStream = new MemoryStream();
            var testStream   = new QuotedPrintableStream(outputStream, false);

            byte[] bytesToWrite1 = Encoding.ASCII.GetBytes("Hello \r");
            testStream.Write(bytesToWrite1, 0, bytesToWrite1.Length);

            byte[] bytesToWrite2 = Encoding.ASCII.GetBytes("\n World");
            testStream.Write(bytesToWrite2, 0, bytesToWrite2.Length);

            testStream.Flush();

            // We told it not to encode them, but they got split across writes so it could not
            // detect the sequence.  This can happen any time we try to encode only a subset
            // of the data at a time.
            const string ExpectedOutput = "Hello =0D=0A World";

            outputStream.Seek(0, SeekOrigin.Begin);
            byte[] bytesRead      = new byte[Encoding.ASCII.GetByteCount(ExpectedOutput) * 2];
            int    bytesReadCount = outputStream.Read(bytesRead, 0, bytesRead.Length);

            string results = Encoding.ASCII.GetString(bytesRead, 0, bytesReadCount);

            Assert.Equal(ExpectedOutput, results);
        }
        /// <summary>
        /// Sets body data from the specified stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="transferEncoding">Specifies content-transfer-encoding to use to encode data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>transferEncoding</b> is null reference.</exception>
        public void SetData(Stream stream, string transferEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (transferEncoding == null)
            {
                throw new ArgumentNullException("transferEncoding");
            }

            if (transferEncoding == MIME_TransferEncodings.QuotedPrintable)
            {
                using (MemoryStream mem_stream = new MemoryStream())
                {
                    QuotedPrintableStream encoder = new QuotedPrintableStream(new SmartStream(mem_stream, false),
                                                                              FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, Workaround.Definitions.MaxStreamLineLength);
                    encoder.Flush();
                    mem_stream.Position = 0;
                    SetEncodedData(transferEncoding, mem_stream);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Base64)
            {
                using (MemoryStream mem_stream = new MemoryStream())
                {
                    Base64Stream encoder = new Base64Stream(mem_stream, false, true, FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, Workaround.Definitions.MaxStreamLineLength);
                    encoder.Finish();
                    mem_stream.Position = 0;
                    SetEncodedData(transferEncoding, mem_stream);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Binary)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.EightBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.SevenBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else
            {
                throw new NotSupportedException("Not supported Content-Transfer-Encoding '" + transferEncoding +
                                                "'.");
            }
        }
        /// <summary>
        /// Sets body data from the specified stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="transferEncoding">Specifies content-transfer-encoding to use to encode data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>transferEncoding</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when this method is accessed and this body is not bounded to any entity.</exception>
        public void SetData(Stream stream, string transferEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (transferEncoding == null)
            {
                throw new ArgumentNullException("transferEncoding");
            }

            if (String2.Equals(transferEncoding, MIME_TransferEncodings.QuotedPrintable, StringComparison2.InvariantCultureIgnoreCase))
            {
                using (MemoryStreamEx fs = new MemoryStreamEx())
                {
                    QuotedPrintableStream encoder = new QuotedPrintableStream(new SmartStream(fs, false), FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, 84000);
                    encoder.Flush();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding, fs);
                }
            }
            else if (String2.Equals(transferEncoding, MIME_TransferEncodings.Base64, StringComparison2.InvariantCultureIgnoreCase))
            {
                using (MemoryStreamEx fs = new MemoryStreamEx())
                {
                    Base64Stream encoder = new Base64Stream(fs, false, true, FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, 84000);
                    encoder.Finish();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding, fs);
                }
            }
            else if (String2.Equals(transferEncoding, MIME_TransferEncodings.Binary, StringComparison2.InvariantCultureIgnoreCase))
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (String2.Equals(transferEncoding, MIME_TransferEncodings.EightBit, StringComparison2.InvariantCultureIgnoreCase))
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (String2.Equals(transferEncoding, MIME_TransferEncodings.SevenBit, StringComparison2.InvariantCultureIgnoreCase))
            {
                SetEncodedData(transferEncoding, stream);
            }
            else
            {
                throw new NotSupportedException("Not supported Content-Transfer-Encoding '" + transferEncoding + "'.");
            }
        }
        /// <summary>
        /// Sets body data from the specified stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="transferEncoding">Specifies content-transfer-encoding to use to encode data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>transferEncoding</b> is null reference.</exception>
        public void SetData(Stream stream, string transferEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (transferEncoding == null)
            {
                throw new ArgumentNullException("transferEncoding");
            }

            if (transferEncoding == MIME_TransferEncodings.QuotedPrintable)
            {
                using (FileStream fs = File.Create(Path.GetTempFileName())){
                    QuotedPrintableStream encoder = new QuotedPrintableStream(new SmartStream(fs, false), FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, 32000);
                    encoder.Flush();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding, fs);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Base64)
            {
                using (FileStream fs = File.Create(Path.GetTempFileName())){
                    Base64Stream encoder = new Base64Stream(fs, false, true, FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, 32000);
                    encoder.Finish();
                    fs.Position = 0;
                    SetEncodedData(transferEncoding, fs);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Binary)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.EightBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.SevenBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else
            {
                throw new NotSupportedException("Not supported Content-Transfer-Encoding '" + transferEncoding + "'.");
            }
        }
        public static void TestEncodeStream(string input, string expectedOutput, string encodingName, bool encodeCRLF)
        {
            Encoding encoding =
                encodingName == "ASCII" ? Encoding.ASCII :
                encodingName == "UTF8" ? Encoding.UTF8 :
                Encoding.Default;

            var outputStream = new MemoryStream();
            var testStream = new QuotedPrintableStream(outputStream, encodeCRLF);

            byte[] bytesToWrite = encoding.GetBytes(input);
            testStream.Write(bytesToWrite, 0, bytesToWrite.Length);
            testStream.Flush();

            outputStream.Seek(0, SeekOrigin.Begin);
            byte[] bytesRead = new byte[encoding.GetByteCount(expectedOutput) * 2];
            int bytesReadCount = outputStream.Read(bytesRead, 0, bytesRead.Length);

            string results = encoding.GetString(bytesRead, 0, bytesReadCount);
            Assert.Equal(expectedOutput, results);
        }
Example #7
0
        public static void TestEncodeStream(string input, string expectedOutput, string encodingName, bool encodeCRLF)
        {
            Encoding encoding =
                encodingName == "ASCII" ? Encoding.ASCII :
                encodingName == "UTF8" ? Encoding.UTF8 :
                Encoding.Default;

            var outputStream = new MemoryStream();
            var testStream   = new QuotedPrintableStream(outputStream, encodeCRLF);

            byte[] bytesToWrite = encoding.GetBytes(input);
            testStream.Write(bytesToWrite, 0, bytesToWrite.Length);
            testStream.Flush();

            outputStream.Seek(0, SeekOrigin.Begin);
            byte[] bytesRead      = new byte[encoding.GetByteCount(expectedOutput) * 2];
            int    bytesReadCount = outputStream.Read(bytesRead, 0, bytesRead.Length);

            string results = encoding.GetString(bytesRead, 0, bytesReadCount);

            Assert.Equal(expectedOutput, results);
        }
        /// <summary>
        /// Sets body data from the specified stream.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="transferEncoding">Specifies content-transfer-encoding to use to encode data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>transferEncoding</b> is null reference.</exception>
        public void SetData(Stream stream, string transferEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (transferEncoding == null)
            {
                throw new ArgumentNullException("transferEncoding");
            }

            if (transferEncoding == MIME_TransferEncodings.QuotedPrintable)
            {
                using (MemoryStream mem_stream = new MemoryStream())
                {
                    QuotedPrintableStream encoder = new QuotedPrintableStream(new SmartStream(mem_stream, false),
                                                                              FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, Workaround.Definitions.MaxStreamLineLength);
                    encoder.Flush();
                    mem_stream.Position = 0;
                    SetEncodedData(transferEncoding, mem_stream);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Base64)
            {
                using (MemoryStream mem_stream = new MemoryStream())
                {
                    Base64Stream encoder = new Base64Stream(mem_stream, false, true, FileAccess.ReadWrite);
                    Net_Utils.StreamCopy(stream, encoder, Workaround.Definitions.MaxStreamLineLength);
                    encoder.Finish();
                    mem_stream.Position = 0;
                    SetEncodedData(transferEncoding, mem_stream);
                }
            }
            else if (transferEncoding == MIME_TransferEncodings.Binary)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.EightBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else if (transferEncoding == MIME_TransferEncodings.SevenBit)
            {
                SetEncodedData(transferEncoding, stream);
            }
            else
            {
                throw new NotSupportedException("Not supported Content-Transfer-Encoding '" + transferEncoding +
                                                "'.");
            }
        }