-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathprompt_test.go
More file actions
123 lines (108 loc) · 3.44 KB
/
prompt_test.go
File metadata and controls
123 lines (108 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package passphrase
import (
"testing"
passphrasevalidation "github.com/substantialcattle5/sietch/internal/passphrase"
)
// Test helper functions for character validation
func TestCharacterValidationHelpers(t *testing.T) {
tests := []struct {
name string
input string
upper bool
lower bool
digit bool
special bool
}{
{"empty", "", false, false, false, false},
{"upper only", "HELLO", true, false, false, false},
{"lower only", "hello", false, true, false, false},
{"digit only", "12345", false, false, true, false},
{"special only", "!@#$%", false, false, false, true},
{"mixed", "Hello123!", true, true, true, true},
{"no special", "Hello123", true, true, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasUppercase(tt.input); got != tt.upper {
t.Errorf("hasUppercase() = %v, want %v", got, tt.upper)
}
if got := hasLowercase(tt.input); got != tt.lower {
t.Errorf("hasLowercase() = %v, want %v", got, tt.lower)
}
if got := hasDigit(tt.input); got != tt.digit {
t.Errorf("hasDigit() = %v, want %v", got, tt.digit)
}
if got := hasSpecialChar(tt.input); got != tt.special {
t.Errorf("hasSpecialChar() = %v, want %v", got, tt.special)
}
})
}
}
// Test strength calculation
func TestCalculateStrengthScore(t *testing.T) {
tests := []struct {
name string
passphrase string
result passphrasevalidation.HybridValidationResult
want int
}{
{"empty", "", passphrasevalidation.HybridValidationResult{Score: 0}, 0},
{"short weak", "pass", passphrasevalidation.HybridValidationResult{Score: 2, IsCommon: false}, 4},
{"long strong", "VeryLongPassword123!", passphrasevalidation.HybridValidationResult{Score: 4, IsCommon: false}, 10},
{"common penalty", "password", passphrasevalidation.HybridValidationResult{Score: 1, IsCommon: true}, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := calculateStrengthScore(tt.passphrase, tt.result); got != tt.want {
t.Errorf("calculateStrengthScore() = %v, want %v", got, tt.want)
}
})
}
}
// Test strength labels
func TestGetStrengthLabel(t *testing.T) {
tests := []struct {
score int
want string
}{
{0, "Weak"},
{3, "Weak"},
{4, "Fair"},
{6, "Fair"},
{7, "Good"},
{8, "Good"},
{9, "Strong"},
{10, "Strong"},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := getStrengthLabel(tt.score); got != tt.want {
t.Errorf("getStrengthLabel(%d) = %v, want %v", tt.score, got, tt.want)
}
})
}
}
// Test feedback line counting (simple state test)
func TestFeedbackLineCount(t *testing.T) {
// Reset counter
feedbackLineCount = 0
// Simulate some feedback display
result := passphrasevalidation.HybridValidationResult{Valid: false, Score: 1}
showPassphraseFeedback("test", result)
// Should have counted some lines
if feedbackLineCount == 0 {
t.Error("Expected feedbackLineCount to be greater than 0 after showing feedback")
}
// Reset for other tests
feedbackLineCount = 0
}
// Test empty passphrase feedback (should return early)
func TestShowPassphraseFeedbackEmpty(t *testing.T) {
initialCount := feedbackLineCount
result := passphrasevalidation.HybridValidationResult{}
showPassphraseFeedback("", result)
// Should not change the line count for empty passphrase
if feedbackLineCount != initialCount {
t.Errorf("Expected feedbackLineCount to remain %d, got %d", initialCount, feedbackLineCount)
}
}