コード例 #1
0
        public void When_SearchPbkdf_Expect_AlgorithmFound()
        {
            var collection = PasswordAlgorithmRepo.Search("pbkdfv2-*");

            Assert.IsTrue(collection.Any());
            Assert.IsTrue(collection.All(a => a.AlgorithmId.StartsWith("PBKDFv2")));
        }
コード例 #2
0
        public void When_SearchingForNonObsoleteAlgorithm_Expect_ExcludedObsoleteOnes()
        {
            PasswordAlgorithmRepo.RegisterAlgorithm(algorithm);

            var collection = PasswordAlgorithmRepo.Search("*", false);

            Assert.IsTrue(collection.Any());
            Assert.IsFalse(collection.Any(a => a.AlgorithmId == algorithm.AlgorithmId));
        }
コード例 #3
0
        public void When_AddingAlgorithm_Expect_ShowsInSearch()
        {
            PasswordAlgorithmRepo.RegisterAlgorithm(algorithm);

            var collection = PasswordAlgorithmRepo.Search("*", true);

            var foundAlgorithm = collection.FirstOrDefault(a => a.AlgorithmId == algorithm.AlgorithmId);

            Assert.AreSame(algorithm, foundAlgorithm);
        }
コード例 #4
0
        public void OneTimeCleanup()
        {
            foreach (var algorithm in PasswordAlgorithmRepo.Search("*", true))
            {
                PasswordAlgorithmRepo.DeregisterAlgorithm(algorithm.AlgorithmId);
            }

            foreach (var algorithm in originalAlgorithms)
            {
                PasswordAlgorithmRepo.RegisterAlgorithm(algorithm);
            }
        }
コード例 #5
0
        public void When_DeregisteringAlgorithm_Expect_NoLongerSearchable()
        {
            PasswordAlgorithmRepo.RegisterAlgorithm(algorithm);

            var myAlgorithm = PasswordAlgorithmRepo.Get(algorithm.AlgorithmId);

            Assert.IsNotNull(myAlgorithm);
            Assert.AreSame(algorithm, myAlgorithm);

            PasswordAlgorithmRepo.DeregisterAlgorithm(algorithm.AlgorithmId);

            Assert.Throws <PasswordHashException>(() => PasswordAlgorithmRepo.Get(algorithm.AlgorithmId), $"The given algorithm ID '{algorithm.AlgorithmId}' was not found in the repo.");
            Assert.IsFalse(PasswordAlgorithmRepo.Search(algorithm.AlgorithmId, true).Any());
        }
コード例 #6
0
        public void OneTimeSetup()
        {
            originalAlgorithms = PasswordAlgorithmRepo.Search("*", true);
            foreach (var algorithm in originalAlgorithms)
            {
                PasswordAlgorithmRepo.DeregisterAlgorithm(algorithm.AlgorithmId);
            }

            // current algorithm Values
            const string currentAlgorithmId = "MockCurrent";
            const bool   currentIsObsolete  = true;

            var currentMockAlgorithm = new Mock <IPasswordHashAlgorithm>();

            currentMockAlgorithm.SetupGet(m => m.AlgorithmId).Returns(currentAlgorithmId);
            currentMockAlgorithm.SetupGet(m => m.IsObsolete).Returns(currentIsObsolete);
            currentMockAlgorithm.Setup(m => m.Hash(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(new PasswordHashResponse
            {
                AlgorithmId = currentAlgorithmId,
                IsObsolete  = currentIsObsolete,
                Value       = CurrentHashValue
            });

            currentAlgorithm = currentMockAlgorithm.Object;
            PasswordAlgorithmRepo.RegisterAlgorithm(currentAlgorithm);

            // new algorithm values
            const string newAlgorithmId       = "MockNew";
            const bool   newAlgorithmObsolete = false;

            var newMockAlgorithm = new Mock <IPasswordHashAlgorithm>();

            newMockAlgorithm.SetupGet(m => m.AlgorithmId).Returns(newAlgorithmId);
            newMockAlgorithm.SetupGet(m => m.IsObsolete).Returns(newAlgorithmObsolete);
            newMockAlgorithm.Setup(m => m.Hash(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(new PasswordHashResponse
            {
                AlgorithmId = newAlgorithmId,
                IsObsolete  = newAlgorithmObsolete,
                Value       = NewHashValue
            });

            newAlgorithm = newMockAlgorithm.Object;
            PasswordAlgorithmRepo.RegisterAlgorithm(newAlgorithm);
        }
コード例 #7
0
        public void When_AdditionalObsoleteAlgorithmIsGiven_Expect_NotUsed()
        {
            const string tmpAlgorithmId       = "TmpAlgorithm";
            const bool   tmpAlgorithmObsolete = true;

            var tmpAlgorithm = new Mock <IPasswordHashAlgorithm>();

            tmpAlgorithm.SetupGet(m => m.AlgorithmId).Returns(tmpAlgorithmId);
            tmpAlgorithm.SetupGet(m => m.IsObsolete).Returns(tmpAlgorithmObsolete);
            tmpAlgorithm.Setup(m => m.Hash(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(new PasswordHashResponse
            {
                AlgorithmId = tmpAlgorithmId,
                IsObsolete  = tmpAlgorithmObsolete,
                Value       = CurrentHashValue
            });

            PasswordAlgorithmRepo.RegisterAlgorithm(tmpAlgorithm.Object);

            var passwordRequest = new PasswordHashRequest
            {
                AlgorithmId = currentAlgorithm.AlgorithmId,
                Password    = "******",
                Salt        = "Tmp"
            };

            var agileHash = new AgilePasswordHash();

            var response = agileHash.Hash(passwordRequest);

            Assert.AreEqual(response.AlgorithmId, currentAlgorithm.AlgorithmId);

            passwordRequest.AlgorithmId = null;

            response = agileHash.Hash(passwordRequest);

            Assert.AreEqual(response.AlgorithmId, newAlgorithm.AlgorithmId);

            PasswordAlgorithmRepo.DeregisterAlgorithm(tmpAlgorithmId);
        }
コード例 #8
0
 public void When_EmptyRepo_Expect_AlgorithmsRegistered()
 {
     Assert.IsTrue(PasswordAlgorithmRepo.Search("*").Any());
 }
コード例 #9
0
 public void When_SearchingEmptyValue_Expect_NoError()
 {
     Assert.IsTrue(PasswordAlgorithmRepo.Search("*").Any());
     Assert.IsFalse(PasswordAlgorithmRepo.Search(null).Any());
     Assert.IsFalse(PasswordAlgorithmRepo.Search(string.Empty).Any());
 }
コード例 #10
0
 public void When_DeregisteringNonExistingId_Expect_NoError()
 {
     Assert.DoesNotThrow(() => PasswordAlgorithmRepo.DeregisterAlgorithm("RandomId"));
 }