public void TestReading()
        {
            do
            {
                int nread = blocks.Read(buf, 0, buf.Length);
                int mread = master.Read(mbuf, 0, mbuf.Length);

                Assert.AreEqual(mread, nread, "Did not read the expected number of bytes from the memory block stream");
                Assert.AreEqual(master.Position, blocks.Position, "The memory block stream's position did not match");

                for (int i = 0; i < mread; i++)
                {
                    Assert.AreEqual(mbuf[i], buf[i], "The bytes read do not match");
                }
            } while (master.Position < master.Length);
        }
Exemplo n.º 2
0
        static void AssertJwzMboxResults(string actual, Stream output)
        {
            var summary = File.ReadAllText(Path.Combine(MboxDataDir, "jwz-summary.txt")).Replace("\r\n", "\n");
            var original = new MemoryBlockStream();
            var expected = new byte[4096];
            var buffer = new byte[4096];
            int nx, n;

            // WORKAROUND: Mono's iso-2022-jp decoder breaks on this input in versions <= 3.2.3 but is fixed in 3.2.4+
            string iso2022jp = Encoding.GetEncoding("iso-2022-jp").GetString(Convert.FromBase64String("GyRAOjRGI0stGyhK"));

            if (iso2022jp != "佐藤豊")
            {
                actual = actual.Replace(iso2022jp, "佐藤豊");
            }

            Assert.AreEqual(summary, actual, "Summaries do not match for jwz.mbox");

            using (var stream = File.OpenRead(Path.Combine(MboxDataDir, "jwz.mbox.txt"))) {
                using (var filtered = new FilteredStream(original)) {
                    filtered.Add(new Dos2UnixFilter());
                    stream.CopyTo(filtered);
                    filtered.Flush();
                }
            }

            original.Position = 0;
            output.Position   = 0;

            Assert.AreEqual(original.Length, output.Length, "The length of the mbox did not match.");

            do
            {
                var position = original.Position;

                nx = original.Read(expected, 0, expected.Length);
                n  = output.Read(buffer, 0, buffer.Length);

                if (nx == 0)
                {
                    break;
                }

                for (int i = 0; i < nx; i++)
                {
                    if (buffer[i] == expected[i])
                    {
                        continue;
                    }

                    var strExpected = CharsetUtils.Latin1.GetString(expected, 0, nx);
                    var strActual   = CharsetUtils.Latin1.GetString(buffer, 0, n);

                    Assert.AreEqual(strExpected, strActual, "The mbox differs at position {0}", position + i);
                }
            } while (true);
        }
Exemplo n.º 3
0
        void FlushCommandQueue(MailboxAddress sender, IList <MailboxAddress> recipients, CancellationToken cancellationToken)
        {
            if (queued.Count == 0)
            {
                return;
            }

            try {
                var responses = new List <SmtpResponse> ();
                int rcpt      = 0;
                int nread;

                queue.Position = 0;
                while ((nread = queue.Read(input, 0, input.Length)) > 0)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    stream.Write(input, 0, nread);
                    logger.LogClient(input, 0, nread);
                }

                // Note: we need to read all responses from the server before we can process
                // them in case any of them have any errors so that we can RSET the state.
                for (int i = 0; i < queued.Count; i++)
                {
                    responses.Add(ReadResponse(cancellationToken));
                }

                for (int i = 0; i < queued.Count; i++)
                {
                    switch (queued [i])
                    {
                    case SmtpCommand.MailFrom:
                        ProcessMailFromResponse(responses[i], sender);
                        break;

                    case SmtpCommand.RcptTo:
                        ProcessRcptToResponse(responses[i], recipients[rcpt++]);
                        break;
                    }
                }
            } finally {
                queue.SetLength(0);
                queued.Clear();
            }
        }
Exemplo n.º 4
0
        private void PerformMutationOp()
        {
            int outcome = DiscreteDistribution.Sample(_rng, _opDistribution);

            switch (outcome)
            {
            case 0:     // Write.
            {
                PerformMutationOp_Write();
                break;
            }

            case 1:     // Write byte.
            {
                byte b = (byte)_rng.Next();
                _strmA.WriteByte(b);
                _strmB.WriteByte(b);
                Debug.WriteLine("WriteByte");
                break;
            }

            case 2:     // Change read/write head position.
            {
                PerformMutationOp_Position();
                break;
            }

            case 3:     // SetLength
            {
                PerformMutationOp_SetLength();
                break;
            }

            case 4:     // Seek
            {
                PerformMutationOp_Seek();
                break;
            }

            case 5:     // Trim
            {
                _strmB.Trim();
                Debug.WriteLine("Trim");
                break;
            }

            case 6:     // Read byte.
            {
                int a = _strmA.ReadByte();
                int b = _strmB.ReadByte();
                if (a != b)
                {
                    throw new Exception("ReadByte mismatch");
                }
                Debug.WriteLine("ReadByte");
                break;
            }

            case 7:     // Read
            {
                int len = _rng.Next(20000);

                byte[] abuf = new byte[len];
                byte[] bbuf = new byte[len];

                int alen = _strmA.Read(abuf, 0, len);
                int blen = _strmB.Read(bbuf, 0, len);

                if (alen != blen)
                {
                    throw new Exception("Read mismatch");
                }

                if (!Utils.AreEqual(abuf, bbuf))
                {
                    throw new Exception("Read mismatch");
                }
                Debug.WriteLine("Read");
                break;
            }
            }
        }
Exemplo n.º 5
0
        public void TestJwzMbox()
        {
            var summary = File.ReadAllText(Path.Combine(MboxDataDir, "jwz-summary.txt")).Replace("\r\n", "\n");
            var options = FormatOptions.Default.Clone();
            var original = new MemoryBlockStream();
            var output = new MemoryBlockStream();
            var builder = new StringBuilder();
            var expected = new byte[4096];
            var buffer = new byte[4096];
            int nx, n;

            options.NewLineFormat = NewLineFormat.Unix;

            using (var stream = File.OpenRead(Path.Combine(MboxDataDir, "jwz.mbox.txt"))) {
                var parser = new MimeParser(stream, MimeFormat.Mbox);
                int count  = 0;

                while (!parser.IsEndOfStream)
                {
                    var message = parser.ParseMessage();

                    builder.AppendFormat("{0}", parser.MboxMarker).Append('\n');
                    if (message.From.Count > 0)
                    {
                        builder.AppendFormat("From: {0}", message.From).Append('\n');
                    }
                    if (message.To.Count > 0)
                    {
                        builder.AppendFormat("To: {0}", message.To).Append('\n');
                    }
                    builder.AppendFormat("Subject: {0}", message.Subject).Append('\n');
                    builder.AppendFormat("Date: {0}", DateUtils.FormatDate(message.Date)).Append('\n');
                    DumpMimeTree(builder, message);
                    builder.Append('\n');

                    var marker = Encoding.UTF8.GetBytes((count > 0 ? "\n" : string.Empty) + parser.MboxMarker + "\n");
                    output.Write(marker, 0, marker.Length);
                    message.WriteTo(options, output);
                    count++;
                }
            }

            string actual = builder.ToString();

            // WORKAROUND: Mono's iso-2022-jp decoder breaks on this input in versions <= 3.2.3 but is fixed in 3.2.4+
            string iso2022jp = Encoding.GetEncoding("iso-2022-jp").GetString(Convert.FromBase64String("GyRAOjRGI0stGyhK"));

            if (iso2022jp != "佐藤豊")
            {
                actual = actual.Replace(iso2022jp, "佐藤豊");
            }

            Assert.AreEqual(summary, actual, "Summaries do not match for jwz.mbox");

            using (var stream = File.OpenRead(Path.Combine(MboxDataDir, "jwz.mbox.txt"))) {
                using (var filtered = new FilteredStream(original)) {
                    filtered.Add(new Dos2UnixFilter());
                    stream.CopyTo(filtered);
                    filtered.Flush();
                }
            }

            original.Position = 0;
            output.Position   = 0;

            Assert.AreEqual(original.Length, output.Length, "The length of the mbox did not match.");

            do
            {
                var position = original.Position;

                nx = original.Read(expected, 0, expected.Length);
                n  = output.Read(buffer, 0, buffer.Length);

                if (nx == 0)
                {
                    break;
                }

                for (int i = 0; i < nx; i++)
                {
                    if (buffer[i] == expected[i])
                    {
                        continue;
                    }

                    var strExpected = CharsetUtils.Latin1.GetString(expected, 0, nx);
                    var strActual   = CharsetUtils.Latin1.GetString(buffer, 0, n);

                    Assert.AreEqual(strExpected, strActual, "The mbox differs at position {0}", position + i);
                }
            } while (true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads a buffer from the archive.
        /// </summary>
        /// <param name="p">Pointer to the beginning of the buffer to fill.</param>
        /// <param name="count">Size of the buffer to fill.</param>
        /// <returns>Number of bytes actually read.</returns>
        /// <exception cref="SerializationException">Thrown if deserialization fails due to some reason.</exception>
        public unsafe long ReadBuffer(IntPtr p, long count)
        {
            // read payload type and size of the following buffer
            ReadAndCheckPayloadType(PayloadType.Buffer);
            long length = Leb128EncodingHelper.ReadInt64(mStream);

            // now we know how much bytes will be returned...
            long bytesReturned = Math.Min(length, count);

            if (mStream is MemoryBlockStream)
            {
                // the MemoryBlockStream provides a direct way to read from the underlying buffer more effiently
                // => let the stream directly copy data into the specified buffer
                MemoryBlockStream mbs = mStream as MemoryBlockStream;
                while (count > 0 && length > 0)
                {
                    int bytesToRead = (int)Math.Min(Math.Min(count, length), int.MaxValue);
                    if (mbs.Read(new Span <byte>(p.ToPointer(), bytesToRead)) < bytesToRead)
                    {
                        throw new SerializationException("Stream ended unexpectedly.");
                    }
                    length -= bytesToRead;
                    count  -= bytesToRead;
                    p      += bytesToRead;
                }
            }
            else
            {
                // some other stream
                // => copying data to a temporary buffer is needed before passing it to the stream

                if (mSerializer.mTempBuffer_BigBuffer.Length < 256 * 1024)
                {
                    mSerializer.mTempBuffer_BigBuffer = new byte[256 * 1024];
                }

                while (count > 0 && length > 0)
                {
                    int bytesToRead = (int)Math.Min(count, mSerializer.mTempBuffer_BigBuffer.Length);
                    if (mStream.Read(mSerializer.mTempBuffer_BigBuffer, 0, bytesToRead) != bytesToRead)
                    {
                        throw new SerializationException("Stream ended unexpectedly.");
                    }
                    Marshal.Copy(mSerializer.mTempBuffer_BigBuffer, 0, p, bytesToRead);
                    length -= bytesToRead;
                    count  -= bytesToRead;
                    p      += bytesToRead;
                }
            }

            // skip bytes that have not been read to ensure the stream can be read any further
            // (just for the case that that less bytes were requested)
            if (length > 0)
            {
                if (mStream.CanSeek)
                {
                    if (mStream.Position + length >= mStream.Length)
                    {
                        throw new SerializationException("Stream ended unexpectedly.");
                    }
                    mStream.Position += length;
                }
                else
                {
                    if (mSerializer.mTempBuffer_BigBuffer.Length < 256 * 1024)
                    {
                        mSerializer.mTempBuffer_BigBuffer = new byte[256 * 1024];
                    }
                    while (length > 0)
                    {
                        int bytesToRead = (int)Math.Min(length, mSerializer.mTempBuffer_BigBuffer.Length);
                        if (mStream.Read(mSerializer.mTempBuffer_BigBuffer, 0, bytesToRead) != bytesToRead)
                        {
                            throw new SerializationException("Stream ended unexpectedly.");
                        }
                        length -= bytesToRead;
                    }
                }
            }

            return(bytesReturned);
        }