summaryrefslogtreecommitdiff
path: root/password_test.go
blob: cfe90e0f8d6f682d2287375b3d4826ab9a41ff4e (plain)
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
package kate

import (
	"strings"
	"testing"
	"testing/quick"
)

func TestHashPasswordComparePasswordRoundtrip(t *testing.T) {
	property := func(password string) bool {
		hash, err := HashPassword(password, nil)
		if err != nil {
			return false
		}

		match, err := ComparePassword(password, hash)
		if err != nil {
			return false
		}

		return match
	}

	if err := quick.Check(property, nil); err != nil {
		t.Errorf("roundtrip property failed: %v", err)
	}
}

func TestComparePasswordWrongPassword(t *testing.T) {
	property := func(password, wrongPassword string) bool {
		if password == wrongPassword {
			return true
		}

		hash, err := HashPassword(password, nil)
		if err != nil {
			return false
		}

		match, err := ComparePassword(wrongPassword, hash)
		if err != nil {
			return false
		}

		return !match
	}

	if err := quick.Check(property, nil); err != nil {
		t.Errorf("wrong password property failed: %v", err)
	}
}

func TestHashPasswordUniqueness(t *testing.T) {
	property := func(password string) bool {
		hash1, err1 := HashPassword(password, nil)
		hash2, err2 := HashPassword(password, nil)

		if err1 != nil || err2 != nil {
			return false
		}

		return hash1 != hash2
	}

	if err := quick.Check(property, nil); err != nil {
		t.Errorf("uniqueness property failed: %v", err)
	}
}

func TestHashPasswordFormat(t *testing.T) {
	property := func(password string) bool {
		hash, err := HashPassword(password, nil)
		if err != nil {
			return false
		}

		parts := strings.Split(hash, "$")
		if len(parts) != 6 {
			return false
		}

		return parts[0] == "" && parts[1] == "argon2id"
	}

	if err := quick.Check(property, nil); err != nil {
		t.Errorf("format property failed: %v", err)
	}
}

func TestComparePasswordMalformedHash(t *testing.T) {
	tests := []string{
		"",
		"invalid",
		"$argon2id",
		"$argon2id$v=19$m=65536,t=1,p=4",
		"$argon2id$v=19$m=65536,t=1,p=4$salt",
		"$wrong$v=19$m=65536,t=1,p=4$salt$hash",
		"$argon2id$v=999$m=65536,t=1,p=4$salt$hash",
		"$argon2id$v=19$invalid$salt$hash",
		"$argon2id$v=19$m=65536,t=1,p=4$!!!$hash",
		"$argon2id$v=19$m=65536,t=1,p=4$salt$!!!",
	}

	for _, malformedHash := range tests {
		t.Run("malformed_"+malformedHash, func(t *testing.T) {
			match, err := ComparePassword("password", malformedHash)
			if err == nil {
				t.Error("expected error for malformed hash")
			}
			if match {
				t.Error("should not match with malformed hash")
			}
		})
	}
}

func TestEmptyPassword(t *testing.T) {
	hash, err := HashPassword("", nil)
	if err != nil {
		t.Fatalf("HashPassword failed for empty password: %v", err)
	}

	match, err := ComparePassword("", hash)
	if err != nil {
		t.Fatalf("ComparePassword failed: %v", err)
	}
	if !match {
		t.Error("empty password should match its hash")
	}

	match, err = ComparePassword("nonempty", hash)
	if err != nil {
		t.Fatalf("ComparePassword failed: %v", err)
	}
	if match {
		t.Error("non-empty password should not match empty password hash")
	}
}

func TestHashPasswordWithConfig(t *testing.T) {
	tests := []struct {
		name   string
		config *Argon2Config
		want   string // Expected parameters in PHC format
	}{
		{
			name:   "nil config uses defaults",
			config: nil,
			want:   "$argon2id$v=19$m=65536,t=1,p=4$",
		},
		{
			name: "custom time parameter",
			config: &Argon2Config{
				Time: 3,
			},
			want: "$argon2id$v=19$m=65536,t=3,p=4$",
		},
		{
			name: "custom memory parameter",
			config: &Argon2Config{
				Memory: 32 * 1024, // 32MB
			},
			want: "$argon2id$v=19$m=32768,t=1,p=4$",
		},
		{
			name: "custom threads parameter",
			config: &Argon2Config{
				Threads: 2,
			},
			want: "$argon2id$v=19$m=65536,t=1,p=2$",
		},
		{
			name: "all custom parameters",
			config: &Argon2Config{
				Time:    2,
				Memory:  128 * 1024,
				Threads: 8,
				KeyLen:  64,
			},
			want: "$argon2id$v=19$m=131072,t=2,p=8$",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			hash, err := HashPassword("test", tt.config)
			if err != nil {
				t.Fatalf("HashPassword() error = %v", err)
			}

			// Check that the hash starts with expected PHC format parameters
			if !strings.HasPrefix(hash, tt.want) {
				t.Errorf("HashPassword() = %v, want prefix %v", hash, tt.want)
			}

			// Verify password still works
			match, err := ComparePassword("test", hash)
			if err != nil {
				t.Fatalf("ComparePassword() error = %v", err)
			}
			if !match {
				t.Error("Password should match its hash")
			}

			// Verify wrong password doesn't match
			match, err = ComparePassword("wrong", hash)
			if err != nil {
				t.Fatalf("ComparePassword() error = %v", err)
			}
			if match {
				t.Error("Wrong password should not match hash")
			}
		})
	}
}

func TestDefaultArgon2Config(t *testing.T) {
	if defaultArgon2Config.Time != 1 {
		t.Errorf("Expected defaultArgon2Config.Time=1, got %d", defaultArgon2Config.Time)
	}
	if defaultArgon2Config.Memory != 64*1024 {
		t.Errorf("Expected defaultArgon2Config.Memory=65536, got %d", defaultArgon2Config.Memory)
	}
	if defaultArgon2Config.Threads != 4 {
		t.Errorf("Expected defaultArgon2Config.Threads=4, got %d", defaultArgon2Config.Threads)
	}
	if defaultArgon2Config.KeyLen != 32 {
		t.Errorf("Expected defaultArgon2Config.KeyLen=32, got %d", defaultArgon2Config.KeyLen)
	}
}