Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
uMailServer/internal/api/password_test.go at main · uMailServer/uMailServer · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
uMailServer
uMailServer
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
uMailServer
/
internal
/
api
/
password_test.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
283 lines (238 loc) · 7.99 KB
main
Breadcrumbs
uMailServer
/
internal
/
api
/
password_test.go
Copy path
Top
File metadata and controls
Code
Blame
283 lines (238 loc) · 7.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package
api
import
(
"strings"
"testing"
"time"
"github.com/umailserver/umailserver/internal/db"
)
// setupTestServer creates a test server with the given config
func
setupPasswordTestServer
(
t
*
testing.
T
,
cfg
Config
)
*
Server
{
tmpDir
:=
t
.
TempDir
()
database
,
err
:=
db
.
Open
(
tmpDir
+
"/test.db"
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to open db: %v"
,
err
)
}
t
.
Cleanup
(
func
() {
database
.
Close
() })
return
NewServer
(
database
,
nil
,
cfg
)
}
// TestHashPasswordArgon2id tests Argon2id password hashing
func
TestHashPasswordArgon2id
(
t
*
testing.
T
) {
hash
,
err
:=
hashPasswordArgon2id
(
"testpassword"
)
if
err
!=
nil
{
t
.
Fatalf
(
"hashPasswordArgon2id failed: %v"
,
err
)
}
// Verify format
if
!
strings
.
HasPrefix
(
hash
,
"$argon2id$"
) {
t
.
Errorf
(
"expected argon2id hash, got %s"
,
hash
)
}
// Verify it has all parts
parts
:=
strings
.
Split
(
hash
,
"$"
)
if
len
(
parts
)
!=
6
{
t
.
Errorf
(
"expected 6 parts, got %d: %s"
,
len
(
parts
),
hash
)
}
}
// TestVerifyPasswordArgon2id tests Argon2id password verification
func
TestVerifyPasswordArgon2id
(
t
*
testing.
T
) {
password
:=
"testpassword123"
hash
,
err
:=
hashPasswordArgon2id
(
password
)
if
err
!=
nil
{
t
.
Fatalf
(
"hashPasswordArgon2id failed: %v"
,
err
)
}
// Correct password
if
!
verifyPasswordArgon2id
(
password
,
hash
) {
t
.
Error
(
"verifyPasswordArgon2id should return true for correct password"
)
}
// Wrong password
if
verifyPasswordArgon2id
(
"wrongpassword"
,
hash
) {
t
.
Error
(
"verifyPasswordArgon2id should return false for wrong password"
)
}
}
// TestVerifyPasswordArgon2id_InvalidFormat tests verification with invalid formats
func
TestVerifyPasswordArgon2id_InvalidFormat
(
t
*
testing.
T
) {
tests
:=
[]
struct
{
name
string
hash
string
valid
bool
}{
{
"empty string"
,
""
,
false
},
{
"no dollar signs"
,
"argon2id"
,
false
},
{
"wrong algorithm"
,
"$bcrypt$v=19$m=65536,t=1,p=4$salt$hash"
,
false
},
{
"too few parts"
,
"$argon2id$v=19$m=65536,t=1,p=4$salt"
,
false
},
{
"invalid params"
,
"$argon2id$v=19$invalid$salt$hash"
,
false
},
{
"invalid salt hex"
,
"$argon2id$v=19$m=65536,t=1,p=4$nothex$hash"
,
false
},
{
"invalid hash hex"
,
"$argon2id$v=19$m=65536,t=1,p=4$salt$nothex"
,
false
},
}
for
_
,
tc
:=
range
tests
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing.
T
) {
result
:=
verifyPasswordArgon2id
(
"password"
,
tc
.
hash
)
if
result
!=
tc
.
valid
{
t
.
Errorf
(
"verifyPasswordArgon2id(%q): expected %v, got %v"
,
tc
.
hash
,
tc
.
valid
,
result
)
}
})
}
}
// TestVerifyPasswordArgon2id_InvalidParameters tests with parameters that fail validation
func
TestVerifyPasswordArgon2id_InvalidParameters
(
t
*
testing.
T
) {
// Negative time
hash
:=
"$argon2id$v=19$m=65536,t=-1,p=4$aabbccdd$aabbccdd"
if
verifyPasswordArgon2id
(
"password"
,
hash
) {
t
.
Error
(
"should fail with negative time"
)
}
// Zero threads
hash
=
"$argon2id$v=19$m=65536,t=1,p=0$aabbccdd$aabbccdd"
if
verifyPasswordArgon2id
(
"password"
,
hash
) {
t
.
Error
(
"should fail with zero threads"
)
}
// Too many threads
hash
=
"$argon2id$v=19$m=65536,t=1,p=256$aabbccdd$aabbccdd"
if
verifyPasswordArgon2id
(
"password"
,
hash
) {
t
.
Error
(
"should fail with too many threads"
)
}
// Empty hash
hash
=
"$argon2id$v=19$m=65536,t=1,p=4$aabbccdd$"
if
verifyPasswordArgon2id
(
"password"
,
hash
) {
t
.
Error
(
"should fail with empty hash"
)
}
}
// TestServer_HashPassword_Argon2id tests server password hashing with argon2id
func
TestServer_HashPassword_Argon2id
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
PasswordHasher
:
"argon2id"
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
hash
,
err
:=
server
.
hashPassword
(
"testpassword"
)
if
err
!=
nil
{
t
.
Fatalf
(
"hashPassword failed: %v"
,
err
)
}
if
!
strings
.
HasPrefix
(
hash
,
"$argon2id$"
) {
t
.
Errorf
(
"expected argon2id hash, got %s"
,
hash
)
}
}
// TestServer_HashPassword_Bcrypt tests server password hashing with bcrypt (default)
func
TestServer_HashPassword_Bcrypt
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
PasswordHasher
:
"bcrypt"
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
hash
,
err
:=
server
.
hashPassword
(
"testpassword"
)
if
err
!=
nil
{
t
.
Fatalf
(
"hashPassword failed: %v"
,
err
)
}
if
!
strings
.
HasPrefix
(
hash
,
"$2"
) {
t
.
Errorf
(
"expected bcrypt hash starting with $2, got %s"
,
hash
)
}
}
// TestServer_VerifyPassword_Argon2id tests verifying argon2id passwords
func
TestServer_VerifyPassword_Argon2id
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
PasswordHasher
:
"argon2id"
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
password
:=
"testpassword123"
hash
,
_
:=
server
.
hashPassword
(
password
)
// Correct password
matches
,
needsRehash
:=
server
.
verifyPassword
(
password
,
hash
)
if
!
matches
{
t
.
Error
(
"verifyPassword should return matches=true for correct password"
)
}
if
needsRehash
{
t
.
Error
(
"argon2id hash should not need rehash when argon2id is configured"
)
}
// Wrong password
matches
,
_
=
server
.
verifyPassword
(
"wrongpassword"
,
hash
)
if
matches
{
t
.
Error
(
"verifyPassword should return matches=false for wrong password"
)
}
}
// TestServer_VerifyPassword_Bcrypt tests verifying bcrypt passwords
func
TestServer_VerifyPassword_Bcrypt
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
PasswordHasher
:
"bcrypt"
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
password
:=
"testpassword123"
hash
,
_
:=
server
.
hashPassword
(
password
)
// Correct password
matches
,
needsRehash
:=
server
.
verifyPassword
(
password
,
hash
)
if
!
matches
{
t
.
Error
(
"verifyPassword should return matches=true for correct password"
)
}
if
needsRehash
{
t
.
Error
(
"bcrypt hash should not need rehash when bcrypt is configured"
)
}
}
// TestServer_VerifyPassword_BcryptNeedsRehash tests that bcrypt needs rehash when argon2id preferred
func
TestServer_VerifyPassword_BcryptNeedsRehash
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
PasswordHasher
:
"argon2id"
,
// Prefer argon2id
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
// Create a bcrypt hash manually
bcryptHash
:=
"$2a$10$N9qo8uLOickgx2ZMRZoMy.MqrqhmM6JGKpS4G3R1G2JH8YpfB0Bqy"
matches
,
needsRehash
:=
server
.
verifyPassword
(
"testpassword"
,
bcryptHash
)
// Note: This bcrypt hash is for "testpassword", but we can't verify it matches
// Just check the logic flow - if it was valid, it would need rehash
if
!
needsRehash
&&
matches
{
t
.
Error
(
"bcrypt hash should need rehash when argon2id is configured"
)
}
}
// TestServer_VerifyPassword_UnknownFormat tests verifying unknown hash format
func
TestServer_VerifyPassword_UnknownFormat
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
matches
,
needsRehash
:=
server
.
verifyPassword
(
"password"
,
"$unknown$hash"
)
if
matches
{
t
.
Error
(
"verifyPassword should return false for unknown format"
)
}
if
needsRehash
{
t
.
Error
(
"verifyPassword should return needsRehash=false for unknown format"
)
}
}
// TestServer_VerifyPassword_InvalidBcrypt tests verifying invalid bcrypt hash
func
TestServer_VerifyPassword_InvalidBcrypt
(
t
*
testing.
T
) {
cfg
:=
Config
{
JWTSecret
:
"test-secret"
,
TokenExpiry
:
time
.
Hour
,
}
server
:=
setupPasswordTestServer
(
t
,
cfg
)
// Invalid bcrypt hash (starts with $2 but is invalid)
matches
,
_
:=
server
.
verifyPassword
(
"password"
,
"$2a$10$invalid"
)
if
matches
{
t
.
Error
(
"verifyPassword should return false for invalid bcrypt hash"
)
}
}
// TestHashPasswordArgon2id_UniqueHashes tests that each hash is unique (different salt)
func
TestHashPasswordArgon2id_UniqueHashes
(
t
*
testing.
T
) {
password
:=
"samepassword"
hash1
,
err
:=
hashPasswordArgon2id
(
password
)
if
err
!=
nil
{
t
.
Fatalf
(
"first hash failed: %v"
,
err
)
}
hash2
,
err
:=
hashPasswordArgon2id
(
password
)
if
err
!=
nil
{
t
.
Fatalf
(
"second hash failed: %v"
,
err
)
}
if
hash1
==
hash2
{
t
.
Error
(
"hashes should be unique due to random salt"
)
}
// But both should verify
if
!
verifyPasswordArgon2id
(
password
,
hash1
) {
t
.
Error
(
"first hash should verify"
)
}
if
!
verifyPasswordArgon2id
(
password
,
hash2
) {
t
.
Error
(
"second hash should verify"
)
}
}
You can’t perform that action at this time.