Ejemplo n.º 1
0
        public void TestLoggingWithCustomPrefixes()
        {
            using (var stream = new MemoryStream()) {
                using (var logger = new ProtocolLogger(stream, true)
                {
                    ClientPrefix = "C> ", ServerPrefix = "S> "
                }) {
                    logger.LogConnect(new Uri("pop://pop.skyfall.net:110"));

                    var cmd = Encoding.ASCII.GetBytes("RETR 1\r\n");
                    logger.LogClient(cmd, 0, cmd.Length);

                    using (var response = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Pop3.Resources.comcast.retr1.txt")) {
                        using (var filtered = new FilteredStream(response)) {
                            var buffer = new byte[4096];
                            int n;

                            filtered.Add(new Unix2DosFilter());

                            while ((n = filtered.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                logger.LogServer(buffer, 0, n);
                            }
                        }
                    }
                }

                stream.Position = 0;

                using (var reader = new StreamReader(stream)) {
                    string line;

                    line = reader.ReadLine();
                    Assert.AreEqual("Connected to pop://pop.skyfall.net:110/", line);

                    line = reader.ReadLine();
                    Assert.AreEqual("C> RETR 1", line);

                    using (var response = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Pop3.Resources.comcast.retr1.txt")) {
                        using (var r = new StreamReader(response)) {
                            string expected;

                            while ((expected = r.ReadLine()) != null)
                            {
                                line = reader.ReadLine();

                                Assert.AreEqual("S> " + expected, line);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void TestCanReadWriteSeekTimeout()
        {
            var buffer = new byte[1024];

            using (var filtered = new FilteredStream(new CanReadWriteSeekStream(true, false, false, false))) {
                Assert.IsTrue(filtered.CanRead);
                Assert.IsFalse(filtered.CanWrite);
                Assert.IsFalse(filtered.CanSeek);
                Assert.IsFalse(filtered.CanTimeout);

                Assert.Throws <NotImplementedException> (() => filtered.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => filtered.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => filtered.Seek(0, SeekOrigin.End));
            }

            using (var filtered = new FilteredStream(new CanReadWriteSeekStream(false, true, false, false))) {
                Assert.IsFalse(filtered.CanRead);
                Assert.IsTrue(filtered.CanWrite);
                Assert.IsFalse(filtered.CanSeek);
                Assert.IsFalse(filtered.CanTimeout);

                Assert.Throws <NotSupportedException> (() => filtered.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotImplementedException> (() => filtered.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => filtered.Seek(0, SeekOrigin.End));
            }

            using (var filtered = new FilteredStream(new CanReadWriteSeekStream(false, false, true, false))) {
                Assert.IsFalse(filtered.CanRead);
                Assert.IsFalse(filtered.CanWrite);
                Assert.IsFalse(filtered.CanSeek);                   // FilteredStream can never seek
                Assert.IsFalse(filtered.CanTimeout);

                Assert.Throws <NotSupportedException> (() => filtered.Read(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => filtered.Write(buffer, 0, buffer.Length));
                Assert.Throws <NotSupportedException> (() => filtered.Seek(0, SeekOrigin.End));                 // FilteredStream can never seek
            }
        }
Ejemplo n.º 3
0
        public void TestCharsetFilter()
        {
            const string  french = "Wikipédia est un projet d’encyclopédie collective en ligne, universelle, multilingue et fonctionnant sur le principe du wiki. Wikipédia a pour objectif d’offrir un contenu librement réutilisable, objectif et vérifiable, que chacun peut modifier et améliorer.\n\nTous les rédacteurs des articles de Wikipédia sont bénévoles. Ils coordonnent leurs efforts au sein d'une communauté collaborative, sans dirigeant.";
            CharsetFilter filter;

            Assert.Throws <ArgumentNullException> (() => new CharsetFilter(null, "iso-8859-1"));
            Assert.Throws <ArgumentNullException> (() => new CharsetFilter("iso-8859-1", null));
            Assert.Throws <NotSupportedException> (() => new CharsetFilter("bogus charset", "iso-8859-1"));
            Assert.Throws <NotSupportedException> (() => new CharsetFilter("iso-8859-1", "bogus charset"));

            Assert.Throws <ArgumentNullException> (() => new CharsetFilter(null, Encoding.UTF8));
            Assert.Throws <ArgumentNullException> (() => new CharsetFilter(Encoding.UTF8, null));

            Assert.Throws <ArgumentOutOfRangeException> (() => new CharsetFilter(-1, 28591));
            Assert.Throws <ArgumentOutOfRangeException> (() => new CharsetFilter(28591, -1));

            filter = new CharsetFilter(Encoding.UTF8, CharsetUtils.Latin1);

            TestArgumentExceptions(filter);
            filter.Reset();

            // Try converting, no fallback
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(french))) {
                using (var filtered = new FilteredStream(stream)) {
                    var expected = Encoding.GetEncoding("iso-8859-15").GetBytes(french);
                    var buffer   = new byte[1024];
                    int length;

                    filtered.Add(new CharsetFilter("utf-8", "iso-8859-15"));

                    length  = filtered.Read(buffer, 0, expected.Length / 2);
                    length += filtered.Read(buffer, expected.Length / 2, buffer.Length - (expected.Length / 2));

                    // Note: this Flush() should do nothing but test a code-path
                    filtered.Flush();

                    Assert.AreEqual(expected.Length, length, "iso-8859-15 length");
                }
            }

            // Try converting with fallback (at least 1 char does not fit within iso-8859-1)
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(french))) {
                using (var filtered = new FilteredStream(stream)) {
                    var utf8     = Encoding.GetEncoding("utf-8", new EncoderReplacementFallback("?"), new DecoderReplacementFallback("?"));
                    var latin1   = Encoding.GetEncoding("iso-8859-1", new EncoderReplacementFallback("?"), new DecoderReplacementFallback("?"));
                    var expected = latin1.GetBytes(french);
                    var buffer   = new byte[1024];
                    int length;

                    filtered.Add(new CharsetFilter(utf8, latin1));

                    length  = filtered.Read(buffer, 0, expected.Length / 2);
                    length += filtered.Read(buffer, expected.Length / 2, buffer.Length - (expected.Length / 2));

                    // Note: this Flush() should do nothing but test a code-path
                    filtered.Flush();

                    Assert.AreEqual(expected.Length, length, "iso-8859-1 length");
                }
            }
        }
Ejemplo n.º 4
0
        public void TestLoggingWithTimestamps()
        {
            string format;

            using (var stream = new MemoryStream()) {
                using (var logger = new ProtocolLogger(stream, true)
                {
                    LogTimestamps = true
                }) {
                    format = logger.TimestampFormat;

                    logger.LogConnect(new Uri("pop://pop.skyfall.net:110"));

                    var cmd = Encoding.ASCII.GetBytes("RETR 1\r\n");
                    logger.LogClient(cmd, 0, cmd.Length);

                    using (var response = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Pop3.Resources.comcast.retr1.txt")) {
                        using (var filtered = new FilteredStream(response)) {
                            var buffer = new byte[4096];
                            int n;

                            filtered.Add(new Unix2DosFilter());

                            while ((n = filtered.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                logger.LogServer(buffer, 0, n);
                            }
                        }
                    }
                }

                stream.Position = 0;

                using (var reader = new StreamReader(stream)) {
                    DateTime timestamp;
                    string   line;

                    line = reader.ReadLine();

                    Assert.IsTrue(TryExtractTimestamp(ref line, format, out timestamp), "Connect timestamp");
                    Assert.AreEqual("Connected to pop://pop.skyfall.net:110/", line);

                    line = reader.ReadLine();
                    Assert.IsTrue(TryExtractTimestamp(ref line, format, out timestamp), "C: RETR 1 timestamp");
                    Assert.AreEqual("C: RETR 1", line);

                    using (var response = GetType().Assembly.GetManifestResourceStream("UnitTests.Net.Pop3.Resources.comcast.retr1.txt")) {
                        using (var r = new StreamReader(response)) {
                            string expected;

                            while ((expected = r.ReadLine()) != null)
                            {
                                line = reader.ReadLine();

                                Assert.IsTrue(TryExtractTimestamp(ref line, format, out timestamp), "S: timestamp");
                                Assert.AreEqual("S: " + expected, line);
                            }
                        }
                    }
                }
            }
        }