public void DetectsCollision()
        {
            string pw      = TestValues.GetPw(2);
            string foundPw = string.Empty;
            var    logic   = new LogicImplementation();

            logic.CollisionFound  += (p) => { foundPw = p; };
            logic.ProgressChanged += (c, l) => { };
            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                var startTime = DateTime.Now;
                while ((foundPw == string.Empty) && ((DateTime.Now - startTime).TotalMilliseconds < 1000))
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception: {ex.GetType().ToString()} - {ex.Message} ");
            }
            logic.AbortSearch();
            Assert.IsFalse((foundPw == string.Empty), $"Logic - does not return a collision through its 'CollisionFound' event.");
            Assert.IsTrue((pw == foundPw), $"Logic - returns wrong value for a collision: \'{foundPw}\', expected: \'{pw}\'.");
        }
        public void LogicImplementationIsDerivedFromILogic()
        {
            ILogic logic = new LogicImplementation(new DummyDal()) as ILogic;

            Assert.IsNotNull(logic,
                             $"LogicLayerTests - Class \"LogicImplementation\" does not implement interface \"ILogic\"!");
        }
        public void HonorsAbortRequest()
        {
            string pw           = TestValues.GetPw(5);
            int    eventCounter = 0;
            var    logic        = new LogicImplementation();

            logic.CollisionFound  += (p) => { };
            logic.ProgressChanged += (c, l) => { eventCounter++; };
            var startTime = DateTime.Now;

            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                startTime = DateTime.Now;
                while ((eventCounter == 0) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception: {ex.GetType().ToString()} - {ex.Message} ");
            }
            logic.AbortSearch();
            eventCounter = 0;
            startTime    = DateTime.Now;
            while ((eventCounter <= 1) && ((DateTime.Now - startTime).TotalMilliseconds < 200))
            {
                Thread.Sleep(5);
            }
            Assert.IsTrue((eventCounter <= 1), $"Logic - AbortSearch does not stop background Task.");
        }
        public void HonorsReportInterval()
        {
            int    testInterval = 20;
            string pw           = TestValues.GetPw(5);
            ulong  pwCount      = 0;
            var    intervals    = new List <int>();
            var    previousTime = DateTime.Now;
            var    logic        = new LogicImplementation();
            var    cancelSource = new CancellationTokenSource();
            var    cancelToken  = cancelSource.Token;
            var    progress     = new Progress <ulong>((count) =>
            {
                pwCount    = count;
                DateTime t = DateTime.Now;
                intervals.Add((int)(t - previousTime).TotalMilliseconds);
                previousTime = t;
            });
            var collisionFound = new Progress <string>((result) => { });

            logic.ReportInterval = testInterval;
            var task      = Task.Run(() => { logic.SearchCollisions(Sha1Hash.CalculateFromString(pw), progress, collisionFound, cancelToken); });
            var startTime = DateTime.Now;

            while ((intervals.Count() < 4) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
            {
                Thread.Sleep(5);
            }
            Assert.IsFalse((intervals.Count() == 0), $"SearchCollisions - does not report progress through its 'progress' object.");
            cancelSource.Cancel();
            intervals.RemoveAt(0);
            int interval = (intervals[0] + intervals[1] + intervals[2]) / 3;

            Assert.IsTrue((Math.Abs(testInterval - interval) < 20),
                          $"SearchCollisions - does not report progress at requested interval of {testInterval}ms, was {interval}ms.");
        }
        public void CurrentDateIsCorrect()
        {
            var logic = new LogicImplementation(new DummyDal());

            Assert.IsTrue((logic.CurrentDate == DateTime.Now.Date),
                          $"LogicLayerTests - \"CurrentDate\" returns wrong value: was {logic.CurrentDate}, expected {DateTime.Now.Date}!");
        }
        static void Main()
        {
            LogicImplementation logicImpl = new LogicImplementation();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm(logicImpl));
        }
        public void SpeedIsCalculatedOK()
        {
            var logic = new LogicImplementation(new DummyDal());

            var speed = logic.GetSpeedInKmPerHour(new RunInfo(500, new TimeSpan(0, 20, 0)));

            Assert.IsTrue((Math.Abs(speed - 1.5) < 0.01),
                          $"LogicLayerTests - \"GetSpeedInKmPerHour\" returns wrong value: was {speed}, expected {1.5}!");
        }
        public void TestClose()
        {
            var dal   = new DummyDal();
            var logic = new LogicImplementation(dal);

            logic.Close();
            Assert.IsTrue((dal.CloseCalled),
                          $"LogicLayerTests - \"Close\" does not call corresponding method from data access implementation!");
        }
示例#9
0
        static void Main()
        {
            DataAccessImplementation dataAcces = new DataAccessImplementation();
            LogicImplementation      logic     = new LogicImplementation(dataAcces);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm(logic));
        }
        public void TestGetRunInfoForDate()
        {
            var dal   = new DummyDal();
            var logic = new LogicImplementation(dal);

            var info = logic.GetRunInfoForDate(new DateTime(42).Date);

            Assert.IsTrue((dal.GetRunInfoForDateCalledCount > 0),
                          $"LogicLayerTests - \"GetRunInfoForDate\" does not call corresponding method from data access implementation!");
            Assert.IsTrue((info.IsEqual(dal.StoredInfo)),
                          $"LogicLayerTests - \"GetRunInfoForDate\" does not return RunInfo as passed by data access implementation !");
        }
        public void CanStartSearchAfterAbort()
        {
            // start and abort a search.
            string pw           = TestValues.GetPw(5);
            string foundPw      = string.Empty;
            int    eventCounter = 0;
            var    logic        = new LogicImplementation();

            logic.CollisionFound  += (p) => { foundPw = p; };
            logic.ProgressChanged += (c, l) => { eventCounter++; };
            var startTime = DateTime.Now;

            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                startTime = DateTime.Now;
                while ((eventCounter == 0) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception: {ex.GetType().ToString()} - {ex.Message} ");
            }
            logic.AbortSearch();
            eventCounter = 0;
            startTime    = DateTime.Now;
            while ((eventCounter <= 1) && ((DateTime.Now - startTime).TotalMilliseconds < 200))
            {
                Thread.Sleep(5);
            }
            Assert.IsTrue((eventCounter <= 1), $"Logic - AbortSearch does not stop background Task.");
            // and start a second one
            pw      = TestValues.GetPw(2);
            foundPw = string.Empty;
            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                startTime = DateTime.Now;
                while ((foundPw == string.Empty) && ((DateTime.Now - startTime).TotalMilliseconds < 1000))
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception when started after an abort request: {ex.GetType().ToString()} - {ex.Message} ");
            }
            Assert.IsFalse((foundPw == string.Empty), $"Logic - does not return a collision when started after an abort request.");
            logic.AbortSearch();
        }
        public void TestLogicOmitsTimeFromDate()
        {
            var dal   = new DummyDal();
            var logic = new LogicImplementation(dal);
            var date  = new DateTime(1234, 5, 6, 12, 12, 12);
            var info  = new RunInfo(321, new TimeSpan(0, 15, 30));

            logic.SetRunInfoForDate(date, info);
            Assert.IsTrue((date.Date == dal.RequestedDate),
                          $"LogicLayerTests - \"SetRunInfoForDate\" does not omit time info from Date when passing on to data access implementation !");
            info = logic.GetRunInfoForDate(date);
            Assert.IsTrue((date.Date == dal.RequestedDate),
                          $"LogicLayerTests - \"SetRunInfoForDate\" does not omit time info from Date when passing on to data access implementation !");
        }
        public void TestSetRunInfoForDate()
        {
            var dal   = new DummyDal();
            var logic = new LogicImplementation(dal);
            var date  = new DateTime(1234, 5, 6);
            var info  = new RunInfo(321, new TimeSpan(0, 15, 30));

            logic.SetRunInfoForDate(date, info);

            Assert.IsTrue((dal.StoreRunInfoCalled),
                          $"LogicLayerTests - \"SetRunInfoForDate\" does not call corresponding method from data access implementation!");
            Assert.IsTrue((info.IsEqual(dal.StoredInfo)),
                          $"LogicLayerTests - \"SetRunInfoForDate\" does not pass RunInfo correctly to data access implementation !");
            Assert.IsTrue((date == dal.RequestedDate),
                          $"LogicLayerTests - \"SetRunInfoForDate\" does not pass date correctly to data access implementation !");
        }
        public void TestGetRunInfoForPastThirtyDays()
        {
            const int count = 30;
            var       dal   = new DummyDal();
            var       logic = new LogicImplementation(dal);

            dal.ReturnDateinInterval = true;

            var infos = logic.GetRunInfoForPastThirtyDays();

            Assert.IsTrue((infos.Length == count),
                          $"LogicLayerTests - \"GetRunInfoForPastThirtyDays\" does not return thirty RunInfo values!");
            for (int i = 0; i < count; i++)
            {
                Assert.IsTrue((infos[i].Interval == Extensions.EncodeDateToTimeSpan(DateTime.Now.Date - new TimeSpan(count - i - 1, 0, 0, 0))),
                              $"LogicLayerTests - \"GetRunInfoForPastThirtyDays\" does not return RunInfo values for correct dates in correct sequence (oldest first)!");
            }
        }
        public void TestSetRunInfoBusinessRules()
        {
            var dal   = new DummyDal();
            var logic = new LogicImplementation(dal);
            var date  = DateTime.Now.Date;

            try
            {
                var info = new RunInfo(-5, new TimeSpan(3210));
                logic.SetRunInfoForDate(date, info);
                Assert.Fail($"LogicLayerTests - \"SetRunInfoForDate\" does not throw ArgumentOutOfRangeException for negative distance!");
            }
            catch (ArgumentOutOfRangeException e)
            {
                // this is expected, no problem
            }
            catch (AssertFailedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                Assert.Fail($"LogicLayerTests - \"SetRunInfoForDate\" with negative distance throws {e.GetType().Name} instead of ArgumentOutOfRangeException !");
            }

            try
            {
                var info = new RunInfo(130, new TimeSpan(0, 0, 9));
                logic.SetRunInfoForDate(date, info);
                Assert.Fail($"LogicLayerTests - \"SetRunInfoForDate\" does not throw ArgumentOutOfRangeException for speed in excess of 50km/h!");
            }
            catch (ArgumentOutOfRangeException e)
            {
                // this is expected, no problem
            }
            catch (AssertFailedException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                Assert.Fail($"LogicLayerTests - \"SetRunInfoForDate\" with speed in excess of 50km/h throws {e.GetType().Name} instead of ArgumentOutOfRangeException !");
            }
        }
        public void ReportsProgress()
        {
            string pw             = TestValues.GetPw(5);
            ulong  pwCount        = 0;
            var    logic          = new LogicImplementation();
            var    cancelSource   = new CancellationTokenSource();
            var    cancelToken    = cancelSource.Token;
            var    progress       = new Progress <ulong>((count) => { pwCount = count; });
            var    collisionFound = new Progress <string>((result) => { });
            var    task           = Task.Run(() => { logic.SearchCollisions(Sha1Hash.CalculateFromString(pw), progress, collisionFound, cancelToken); });
            var    startTime      = DateTime.Now;

            while ((pwCount == 0) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
            {
                Thread.Sleep(5);
            }
            cancelSource.Cancel();
            Assert.IsFalse((pwCount == 0), $"SearchCollisions - does not report progress through its 'progress' object.");
        }
        public void ChecksForNullEvents()
        {
            string pw    = TestValues.GetPw(5);
            var    logic = new LogicImplementation();

            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                var startTime = DateTime.Now;
                while ((DateTime.Now - startTime).TotalMilliseconds < 200)
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception: {ex.GetType().ToString()} - {ex.Message} ");
            }
            logic.AbortSearch();
        }
        public void DetectsCollision()
        {
            string pw             = TestValues.GetPw(2);
            string foundPw        = string.Empty;
            var    logic          = new LogicImplementation();
            var    cancelSource   = new CancellationTokenSource();
            var    cancelToken    = cancelSource.Token;
            var    progress       = new Progress <ulong>((count) => { });
            var    collisionFound = new Progress <string>((result) => { foundPw = result; cancelSource.Cancel(); });
            var    task           = Task.Run(() => { logic.SearchCollisions(Sha1Hash.CalculateFromString(pw), progress, collisionFound, cancelToken); });
            var    startTime      = DateTime.Now;

            while ((foundPw == string.Empty) && ((DateTime.Now - startTime).TotalMilliseconds < 1000))
            {
                Thread.Sleep(5);
            }
            Assert.IsFalse((foundPw == string.Empty), $"SearchCollisions - does not return a collision through its 'collisionFound' progress object.");
            Assert.IsTrue((pw == foundPw), $"SearchCollisions - returns wrong value for a collision: \'{foundPw}\', expected: \'{pw}\'.");
            cancelSource.Cancel();
        }
        public void HonorsCancelRequest()
        {
            string pw             = TestValues.GetPw(5);
            ulong  pwCount        = 0;
            var    logic          = new LogicImplementation();
            var    cancelSource   = new CancellationTokenSource();
            var    cancelToken    = cancelSource.Token;
            var    progress       = new Progress <ulong>((count) => { pwCount = count; });
            var    collisionFound = new Progress <string>((result) => { });
            var    task           = Task.Run(() => { logic.SearchCollisions(Sha1Hash.CalculateFromString(pw), progress, collisionFound, cancelToken); });
            var    startTime      = DateTime.Now;

            while ((pwCount == 0) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
            {
                Thread.Sleep(5);
            }
            cancelSource.Cancel();
            var taskAborted = task.Wait(300);

            Assert.IsTrue(taskAborted, $"SearchCollisions - does not stop Task after 'Cancel' request.");
        }
        public void ReportsProgress()
        {
            string pw      = TestValues.GetPw(5);
            ulong  pwCount = 0;
            var    logic   = new LogicImplementation();

            logic.CollisionFound  += (p) => { };
            logic.ProgressChanged += (c, l) => { pwCount = c; };
            try
            {
                logic.StartSearchCollisionsTask(Sha1Hash.CalculateFromString(pw));
                var startTime = DateTime.Now;
                while ((pwCount == 0) && ((DateTime.Now - startTime).TotalMilliseconds < 500))
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail($"Logic - \'StartSearchCollisionsTask\' method throws an exception: {ex.GetType().ToString()} - {ex.Message} ");
            }
            logic.AbortSearch();
            Assert.IsFalse((pwCount == 0), $"Logic does not report # of passwords tested through its 'ProgressChanged' event.");
        }
        public void TestLogicImplementsILogic()
        {
            var f = new LogicImplementation();

            Assert.IsTrue((f is ILogic), $"LogicImplementation does not implement \'ILogic\' interface.");
        }