package auth import ( "strings" "testing" ) // 随机密码只允许使用去混淆字符集,且长度精确可控。 func TestRandomPasswordCharsetAndLength(t *testing.T) { const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789" for i := 0; i < 50; i++ { pwd, err := RandomPassword(16) if err != nil { t.Fatalf("生成随机密码: %v", err) } if len(pwd) != 16 { t.Fatalf("密码长度应为 16,得到 %d", len(pwd)) } for _, r := range pwd { if !strings.ContainsRune(alphabet, r) { t.Fatalf("密码含字符集外字符 %q: %s", r, pwd) } } } } // bcrypt 只取前 72 字节,超出必须显式拒绝而不是静默截断。 func TestMaxPasswordBytesMatchesBcrypt(t *testing.T) { if MaxPasswordBytes != 72 { t.Fatalf("MaxPasswordBytes 应为 bcrypt 的 72 字节上限,得到 %d", MaxPasswordBytes) } }