测试密码格式

本文关键字:格式 密码 测试 | 更新日期: 2023-09-27 18:15:41

Ok,我正在编写一个会员密码更改的测试。下面的代码是我目前所拥有的。我需要一些帮助来检查密码格式。最小字符长度为7,最大长度为8。

我也可以测试密码格式是否清晰,但是我如何测试加密格式?

有人能帮忙吗?

    [TestMethod]
    public void TestChangePassword()
    {
        try      
        {
            AsaMembershipProvider prov = this.GetMembershipProvider();
            MembershipCreateStatus status;
            //creates user
            MembershipUser user = prov.CreateUser("testUserX", "12345", "test.UserX@abc.com", "", "", true, null, out status);
            //gets user
            user = prov.GetUser("testUserX", false); 
            Assert.AreEqual(user.UserName, "testUserX");
            //Authenticates username and password
            var isAuthenticated = prov.ValidateUser(user.UserName, "12345");
            Assert.IsTrue(isAuthenticated);

            //changes password
            prov.ChangePassword("testUserX",  "12345", "ABCDE");
            //Validates password has been changed
            prov.ValidateUser(user.UserName, "ABCDE");
            Assert.IsTrue(isAuthenticated);
            // Change password back
            prov.ChangePassword("testUserX", "ABCDE", "12345");
            //Validates password has been changed back
            prov.ValidateUser(user.UserName, "12345");

            //Deletes User
            prov.DeleteUser("testUserX", true);
            //tries to get user again
            user = prov.GetUser("testUserX", false);
            //test that no user is returned
            Assert.AreEqual(null, user);
        }
        catch (Exception ex)
        {
            LogMessage(ex);
            Assert.Fail(ex.Message);
        }
    }

测试密码格式

一种选择是编写一个针对asammembershipprovider的扩展方法,该方法将验证密码,然后从其中调用ChangePassword来进行实际的更改

这种方法的缺点是您将有更多的代码来维护

public static class CryptoExtensions {
    public static void ChangePasswordEx(this AsaMembershipProvider mp, string username, string oldPassword, string newPassword){
        // validate format of the password
        if (true /*validation code*/ )
        {
            throw new Exception("Invalid password format");
        }
        // rest of the code to encrypt and store the password
        mp.ChangePassword(username, oldPassword, newPassword);
    }
}
您的测试代码现在应该调用prov。使用prov修改密码ChangePasswordEx