Creates a new directory that is automatically removed when this class is disposed.
Inheritance: IDisposable
コード例 #1
0
        public void Authenticate()
        {
            var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
                CallBase = true
            };
            reporterMock.Protected()
                .Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
                .Throws<AuthenticationException>();

            TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
            TestHelpers.CreateRequestMock(channelListUri, channelBody);

            using (var testDir = new TempDirectory()) {
                using (var reporter = reporterMock.Object) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";

                    reporter.ServiceUri = serviceUri;
                    reporter.ChannelListUri = channelListUri;

                    reporter.Start();
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);

                    reporter.Authenticate("username", "password");
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.Active, reporter.Status);
                    Assert.AreEqual("username", reporter.Username);
                    Assert.AreEqual(IntelSession.HashPassword("password"), reporter.PasswordHash);

                    // So that Dispose() works
                    TestHelpers.CreateRequestError<WebException>(serviceUri);
                }
            }
        }
コード例 #2
0
        public void AuthenticateAuthError()
        {
            var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
                CallBase = true
            };
            reporterMock.Protected()
                .Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
                .Throws<AuthenticationException>();

            TestHelpers.CreateRequestMock(serviceUri, "500 ERROR AUTH");
            TestHelpers.CreateRequestMock(channelListUri, channelBody);

            using (var testDir = new TempDirectory()) {
                using (var reporter = reporterMock.Object) {
                    reporter.Path = testDir.FullName;

                    reporter.ServiceUri = serviceUri;
                    reporter.ChannelListUri = channelListUri;

                    reporter.Start();
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);

                    reporter.Authenticate("username", "password");
                    Thread.Sleep(100);
                }
            }
        }
コード例 #3
0
        public void WebErrorClear()
        {
            var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
                CallBase = true
            };

            TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
            TestHelpers.CreateRequestMock(channelListUri, channelBody);

            var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
            var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
            sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
                .Returns(true);

            IntelSession session = null;
            reporterMock.Protected()
                .Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
                .Returns(new Func<IntelSession>(delegate() {
                    if (session == null) throw new WebException();
                    return session;
                }));

            using (var testDir = new TempDirectory()) {
                using (var reporter = reporterMock.Object) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";

                    reporter.ServiceUri = serviceUri;
                    reporter.ChannelListUri = channelListUri;

                    reporter.Start();
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);

                    reporter.OnIntelReported(testEvent);
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
                    Assert.AreEqual(1, reporter.IntelDropped);
                    Assert.AreEqual(0, reporter.IntelSent);

                    session = sessionMock.Object;
                    reporter.OnIntelReported(testEvent);
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.Active, reporter.Status);
                    Assert.AreEqual(1, reporter.IntelDropped);
                    Assert.AreEqual(1, reporter.IntelSent);
                }
            }
        }
コード例 #4
0
        public void StartWebError()
        {
            using (var testDir = new TempDirectory()) {
                using (var reporter = new IntelReporter()) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";

                    reporter.ChannelListUri = channelListUri;
                    TestHelpers.CreateRequestError<WebException>(channelListUri);

                    reporter.ServiceUri = serviceUri;
                    var requestBody = TestHelpers.CreateRequestError<WebException>(serviceUri);

                    reporter.Start();
                    Thread.Sleep(100);

                    Assert.IsTrue(reporter.IsRunning);
                    Assert.IsTrue(requestBody.Length > 0);
                    Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
                }
            }
        }
コード例 #5
0
        public void StartStop()
        {
            using (var testDir = new TempDirectory()) {
                using (var reporter = new IntelReporter()) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";

                    reporter.ChannelListUri = channelListUri;
                    TestHelpers.CreateRequestMock(channelListUri, channelBody);

                    reporter.ServiceUri = serviceUri;
                    var requestBody = TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");

                    reporter.Start();
                    Thread.Sleep(100);

                    Assert.IsTrue(reporter.IsRunning);
                    Assert.IsTrue(requestBody.Length > 0);
                    Assert.AreEqual(IntelStatus.Active, reporter.Status);
                    Assert.AreEqual(2, reporter.Channels.Count);
                    Assert.IsNotNull(reporter.Channels.Single(x => x.Name == channelList[0]));
                    Assert.IsNotNull(reporter.Channels.Single(x => x.Name == channelList[1]));
                    Assert.IsTrue(reporter.Channels.All(x => x.IsRunning));

                    TestHelpers.Cleanup();
                    requestBody = TestHelpers.CreateRequestMock(serviceUri, "201 AUTH Logged Off");
                    reporter.Stop();
                    Thread.Sleep(100);

                    Assert.IsFalse(reporter.IsRunning);
                    Assert.IsTrue(requestBody.Length > 0);
                    Assert.AreEqual(IntelStatus.Stopped, reporter.Status);
                    Assert.IsTrue(reporter.Channels.All(x => !x.IsRunning));
                }
            }
        }
コード例 #6
0
        public void StartNoAuth()
        {
            using (var testDir = new TempDirectory()) {
                using (var reporter = new IntelReporter()) {
                    reporter.Path = testDir.FullName;

                    reporter.ChannelListUri = channelListUri;
                    TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));

                    reporter.Start();
                    Thread.Sleep(100);

                    Assert.IsTrue(reporter.IsRunning);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
                }
            }
        }
コード例 #7
0
        public void StartAuthError()
        {
            using (var testDir = new TempDirectory()) {
                using (var reporter = new IntelReporter()) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";

                    reporter.ChannelListUri = channelListUri;
                    TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));

                    reporter.ServiceUri = serviceUri;
                    var requestBody = TestHelpers.CreateRequestMock(serviceUri, "500 ERROR AUTH");

                    reporter.Start();
                    Thread.Sleep(100);

                    Assert.IsTrue(reporter.IsRunning);
                    Assert.IsTrue(requestBody.Length > 0);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
                }
            }
        }
コード例 #8
0
        public void NetworkErrorToAuthError()
        {
            var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
                CallBase = true
            };

            TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
            var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
            var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
            sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
                .Returns(true);

            Exception exception = new WebException();
            reporterMock.Protected()
                .Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
                .Returns(() => { throw exception; });

            using (var testDir = new TempDirectory()) {
                using (var reporter = reporterMock.Object) {
                    reporter.Path = testDir.FullName;
                    reporter.Username = "******";
                    reporter.PasswordHash = "password";
                    reporter.AuthenticationRetryTimeout = new TimeSpan(0, 0, 0, 0, 10);

                    reporter.ServiceUri = serviceUri;
                    reporter.ChannelListUri = channelListUri;

                    reporter.Start();
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);

                    reporter.OnIntelReported(testEvent);
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
                    Assert.AreEqual(1, reporter.IntelDropped);
                    Assert.AreEqual(0, reporter.IntelSent);

                    exception = new AuthenticationException();
                    reporter.OnIntelReported(testEvent);
                    Thread.Sleep(100);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
                    Assert.AreEqual(2, reporter.IntelDropped);
                    Assert.AreEqual(0, reporter.IntelSent);
                }
            }
        }
コード例 #9
0
        public void IntelReported()
        {
            using (var testDir = new TempDirectory()) {
                using (var reporter = new IntelReporter()) {
                    reporter.Path = testDir.FullName;
                    IntelEventArgs received = null;
                    reporter.IntelReported += (sender, e) => received = e;

                    reporter.ChannelListUri = channelListUri;
                    TestHelpers.CreateRequestMock(channelListUri, channelBody);

                    reporter.Start();
                    var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
                    Thread.Sleep(100);
                    Assert.AreEqual(0, reporter.IntelDropped);
                    Assert.AreEqual(0, reporter.IntelSent);

                    reporter.Channels.First().OnIntelReported(testEvent);
                    Thread.Sleep(100);
                    Assert.IsTrue(reporter.IsRunning);
                    Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
                    Assert.AreEqual(received, testEvent);
                    Assert.AreEqual(1, reporter.IntelDropped);
                    Assert.AreEqual(0, reporter.IntelSent);
                }
            }
        }