Exemplo n.º 1
0
        public void TestLogConnectMidline()
        {
            using (var stream = new MemoryStream()) {
                using (var logger = new ProtocolLogger(stream, true)
                {
                    RedactSecrets = false
                }) {
                    byte[] buf;

                    buf = Encoding.ASCII.GetBytes("PARTIAL LINE");
                    logger.LogClient(buf, 0, buf.Length);

                    logger.LogConnect(new Uri("proto://server.com"));

                    logger.LogServer(buf, 0, buf.Length);

                    logger.LogConnect(new Uri("proto://server.com"));
                }

                var buffer = stream.GetBuffer();
                int length = (int)stream.Length;

                var result = Encoding.ASCII.GetString(buffer, 0, length);

                Assert.AreEqual("C: PARTIAL LINE\r\nConnected to proto://server.com/\r\nS: PARTIAL LINE\r\nConnected to proto://server.com/\r\n", result);
            }
        }
Exemplo n.º 2
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);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void TestArgumentExceptions()
        {
            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((string)null));
            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((Stream)null));
            using (var logger = new ProtocolLogger(new MemoryStream())) {
                var buffer = new byte[1024];

                Assert.Throws <ArgumentNullException> (() => logger.LogConnect(null));
                Assert.Throws <ArgumentNullException> (() => logger.LogClient(null, 0, 0));
                Assert.Throws <ArgumentNullException> (() => logger.LogServer(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, 0, -1));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, 0, -1));
            }
        }
Exemplo n.º 4
0
        public void TestArgumentExceptions()
        {
            var enumeratedRights = new [] { AccessRight.OpenFolder, AccessRight.CreateFolder };

            Assert.Throws <ArgumentNullException> (() => new AccessControl(null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, "rk"));
            Assert.Throws <ArgumentNullException> (() => new AccessControl(null, enumeratedRights));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (string)null));
            Assert.Throws <ArgumentNullException> (() => new AccessControl("name", (IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AccessControlList(null));

            Assert.Throws <ArgumentNullException> (() => new AccessRights((IEnumerable <AccessRight>)null));
            Assert.Throws <ArgumentNullException> (() => new AccessRights((string)null));

            var rights = new AccessRights();

            Assert.Throws <ArgumentNullException> (() => rights.AddRange((string)null));
            Assert.Throws <ArgumentNullException> (() => rights.AddRange((IEnumerable <AccessRight>)null));

            Assert.Throws <ArgumentNullException> (() => new AlertEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new FolderNamespace('.', null));

            var             namespaces = new FolderNamespaceCollection();
            FolderNamespace ns;

            Assert.Throws <ArgumentNullException> (() => namespaces.Add(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Contains(null));
            Assert.Throws <ArgumentNullException> (() => namespaces.Remove(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => ns             = namespaces[-1]);
            Assert.Throws <ArgumentOutOfRangeException> (() => namespaces[-1] = new FolderNamespace('.', ""));

            namespaces.Add(new FolderNamespace('.', ""));
            Assert.Throws <ArgumentNullException> (() => namespaces[0] = null);

            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException(null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null));
            Assert.Throws <ArgumentNullException> (() => new FolderNotFoundException("message", null, new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message"));
            Assert.Throws <ArgumentNullException> (() => new FolderNotOpenException(null, FolderAccess.ReadOnly, "message", new Exception("message")));

            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs(null, "name"));
            Assert.Throws <ArgumentNullException> (() => new FolderRenamedEventArgs("name", null));

            Assert.Throws <ArgumentOutOfRangeException> (() => new MessageEventArgs(-1));

            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, MessageFlags.Answered, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null));
            Assert.Throws <ArgumentNullException> (() => new MessageFlagsChangedEventArgs(0, UniqueId.MinValue, MessageFlags.Answered, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, null, 1));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null));
            Assert.Throws <ArgumentNullException> (() => new MessageLabelsChangedEventArgs(0, UniqueId.MinValue, null, 1));

            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(null, "response"));
            Assert.Throws <ArgumentNullException> (() => new MessageSentEventArgs(new MimeMessage(), null));

            Assert.Throws <ArgumentNullException> (() => new MessageSummaryFetchedEventArgs(null));

            Assert.Throws <ArgumentNullException> (() => new MessagesVanishedEventArgs(null, false));

            Assert.Throws <ArgumentNullException> (() => new MetadataCollection(null));

            var metadataOptions = new MetadataOptions();

            Assert.Throws <ArgumentOutOfRangeException> (() => metadataOptions.Depth = 500);

            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, 1));
            Assert.Throws <ArgumentOutOfRangeException> (() => new ModSeqChangedEventArgs(-1, UniqueId.MinValue, 1));

            Assert.Throws <ArgumentOutOfRangeException> (() => new OrderBy(OrderByType.To, SortOrder.None));

            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((string)null));
            Assert.Throws <ArgumentNullException> (() => new ProtocolLogger((Stream)null));
            using (var logger = new ProtocolLogger(new MemoryStream())) {
                var buffer = new byte[1024];

                Assert.Throws <ArgumentNullException> (() => logger.LogConnect(null));
                Assert.Throws <ArgumentNullException> (() => logger.LogClient(null, 0, 0));
                Assert.Throws <ArgumentNullException> (() => logger.LogServer(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, -1, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogClient(buffer, 0, -1));
                Assert.Throws <ArgumentOutOfRangeException> (() => logger.LogServer(buffer, 0, -1));
            }

            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(null, new [] { UniqueId.MinValue }));
            Assert.Throws <ArgumentNullException> (() => new UniqueIdMap(new [] { UniqueId.MinValue }, null));
        }
Exemplo n.º 5
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);
                            }
                        }
                    }
                }
            }
        }