-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpassword_test.go
More file actions
118 lines (102 loc) · 2.89 KB
/
Copy pathpassword_test.go
File metadata and controls
118 lines (102 loc) · 2.89 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
package main
import (
"testing"
)
func TestPasswordCaching(t *testing.T) {
// Clear any cached password
ClearPasswordCache()
// Set a test password
testPassword := "test-password-123"
SetPasswordForTesting(testPassword)
// First call should return the cached password
password1, err := GetPassword()
if err != nil {
t.Fatalf("Failed to get password: %v", err)
}
if password1 != testPassword {
t.Errorf("Expected password %q, got %q", testPassword, password1)
}
// Second call should return the same cached password without prompting
password2, err := GetPassword()
if err != nil {
t.Fatalf("Failed to get password on second call: %v", err)
}
if password2 != testPassword {
t.Errorf("Expected cached password %q, got %q", testPassword, password2)
}
// Clear the cache
ClearPasswordCache()
// After clearing, the password should be gone
// (we can't test prompting in unit tests, so we just verify the cache was cleared)
// This would normally prompt for a password in real usage
}
func TestPasswordValidation(t *testing.T) {
tests := []struct {
name string
password string
expectError bool
}{
{
name: "Valid 12 character password",
password: "password1234",
expectError: false,
},
{
name: "Valid long password",
password: "this-is-a-very-long-password-that-should-work",
expectError: false,
},
{
name: "Too short password",
password: "short",
expectError: true,
},
{
name: "Exactly 12 characters",
password: "exactly12chr",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ClearPasswordCache()
if len(tt.password) >= 12 {
// Valid password
SetPasswordForTesting(tt.password)
password, err := GetPassword()
if err != nil {
t.Errorf("Unexpected error for valid password: %v", err)
}
if password != tt.password {
t.Errorf("Expected password %q, got %q", tt.password, password)
}
} else {
// Invalid password - we can't directly test GetPassword() failing
// because it would try to prompt, but we can verify the length check
if len(tt.password) >= 12 && tt.expectError {
t.Errorf("Password %q should be valid but test expects error", tt.password)
}
}
ClearPasswordCache()
})
}
}
func TestClearPasswordCache(t *testing.T) {
ClearPasswordCache()
// Set a password
testPassword := "test-password-for-clearing"
SetPasswordForTesting(testPassword)
// Verify it's set
password, err := GetPassword()
if err != nil {
t.Fatalf("Failed to get password: %v", err)
}
if password != testPassword {
t.Errorf("Expected password %q, got %q", testPassword, password)
}
// Clear the cache
ClearPasswordCache()
// The password should be cleared from memory
// We can't directly verify this without accessing internal state,
// but the function should have zeroed out the password bytes
}