2024-01-13 11:49:08 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCredential(t *testing.T) {
|
|
|
|
t.Run("MarshalJSON", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("password")
|
2024-01-13 11:49:08 +00:00
|
|
|
expectedJSON := []byte(`"password"`)
|
|
|
|
actualJSON, err := cred.MarshalJSON()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedJSON, actualJSON)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnmarshalJSON", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
expectedCred := Credential("password")
|
2024-01-13 11:49:08 +00:00
|
|
|
jsonData := []byte(`"password"`)
|
|
|
|
var actualCred Credential
|
|
|
|
err := actualCred.UnmarshalJSON(jsonData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedCred, actualCred)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnmarshalEnv", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("")
|
2024-01-13 11:49:08 +00:00
|
|
|
err := cred.UnmarshalEnv("", "password")
|
|
|
|
assert.NoError(t, err)
|
2024-02-20 18:35:35 +00:00
|
|
|
assert.Equal(t, Credential("password"), cred)
|
2024-01-13 11:49:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("IsSha256", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.False(t, cred.IsSha256())
|
|
|
|
assert.False(t, cred.IsHashed())
|
|
|
|
|
2024-02-20 18:35:35 +00:00
|
|
|
cred = "sha256:j1tsRqDEw9xvq/D7/9tMx6Jh/jMhk3UfjwIB2f1zgMo="
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.True(t, cred.IsSha256())
|
|
|
|
assert.True(t, cred.IsHashed())
|
|
|
|
|
2024-02-20 18:35:35 +00:00
|
|
|
cred = "argon2:$argon2id$v=19$m=65536,t=1," +
|
2024-01-13 11:49:08 +00:00
|
|
|
"p=4$WXJGqwIB2qd+pRmxMOw9Dg$X4gvR0ZB2DtQoN8vOnJPR2SeFdUhH9TyVzfV98sfWeE"
|
|
|
|
assert.False(t, cred.IsSha256())
|
|
|
|
assert.True(t, cred.IsHashed())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("IsArgon2", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.False(t, cred.IsArgon2())
|
|
|
|
assert.False(t, cred.IsHashed())
|
|
|
|
|
2024-02-20 18:35:35 +00:00
|
|
|
cred = "sha256:j1tsRqDEw9xvq/D7/9tMx6Jh/jMhk3UfjwIB2f1zgMo="
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.False(t, cred.IsArgon2())
|
|
|
|
assert.True(t, cred.IsHashed())
|
|
|
|
|
2024-02-20 18:35:35 +00:00
|
|
|
cred = "argon2:$argon2id$v=19$m=65536,t=1," +
|
2024-01-13 11:49:08 +00:00
|
|
|
"p=4$WXJGqwIB2qd+pRmxMOw9Dg$X4gvR0ZB2DtQoN8vOnJPR2SeFdUhH9TyVzfV98sfWeE"
|
|
|
|
assert.True(t, cred.IsArgon2())
|
|
|
|
assert.True(t, cred.IsHashed())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check-plain", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("password")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.True(t, cred.Check("password"))
|
|
|
|
assert.False(t, cred.Check("wrongpassword"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check-sha256", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("password")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.True(t, cred.Check("password"))
|
|
|
|
assert.False(t, cred.Check("wrongpassword"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check-sha256", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("sha256:rl3rgi4NcZkpAEcacZnQ2VuOfJ0FxAqCRaKB/SwdZoQ=")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.True(t, cred.Check("testuser"))
|
|
|
|
assert.False(t, cred.Check("notestuser"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check-argon2", func(t *testing.T) {
|
2024-02-20 18:35:35 +00:00
|
|
|
cred := Credential("argon2:$argon2id$v=19$m=4096,t=3," +
|
|
|
|
"p=1$MTIzNDU2Nzg$Ux/LWeTgJQPyfMMJo1myR64+o8rALHoPmlE1i/TR+58")
|
2024-01-13 11:49:08 +00:00
|
|
|
assert.True(t, cred.Check("testuser"))
|
|
|
|
assert.False(t, cred.Check("notestuser"))
|
|
|
|
})
|
|
|
|
|
2024-01-23 19:52:05 +00:00
|
|
|
t.Run("validate", func(t *testing.T) {
|
2024-01-13 11:49:08 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-02-20 18:35:35 +00:00
|
|
|
cred Credential
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential(""),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid plain credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("validPlain123"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid plain credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("invalid/Plain"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid sha256 credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("sha256:validBase64EncodedHash=="),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid sha256 credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("sha256:inval*idBase64"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid Argon2 credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("argon2:$argon2id$v=19$m=4096," +
|
|
|
|
"t=3,p=1$MTIzNDU2Nzg$zarsL19s86GzUWlAkvwt4gJBFuU/A9CVuCjNI4fksow"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Argon2 credential",
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("argon2:invalid"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Argon2 credential",
|
|
|
|
// testing argon2d errors, because it's not supported
|
2024-02-20 18:35:35 +00:00
|
|
|
cred: Credential("$argon2d$v=19$m=4096,t=3," +
|
|
|
|
"p=1$MTIzNDU2Nzg$Xqyd4R7LzXvvAEHaVU12+Nzf5OkHoYcwIEIIYJUDpz0"),
|
2024-01-13 11:49:08 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-01-23 19:52:05 +00:00
|
|
|
err := tt.cred.validate()
|
2024-01-13 11:49:08 +00:00
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|