2021-09-17 11:44:35 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-28 10:15:01 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-17 11:44:35 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func newDefaultNTPConfig() schema.Configuration {
|
|
|
|
return schema.Configuration{
|
|
|
|
NTP: &schema.NTPConfiguration{},
|
|
|
|
}
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultNtpAddress(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.Address, config.NTP.Address)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultNtpVersion(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.Version, config.NTP.Version)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultNtpMaximumDesync(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.MaximumDesync, config.NTP.MaximumDesync)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldSetDefaultNtpDisableStartupCheck(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.Equal(t, schema.DefaultNTPConfiguration.DisableStartupCheck, config.NTP.DisableStartupCheck)
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnMaximumDesyncString(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
2022-02-28 10:15:01 +07:00
|
|
|
config.NTP.MaximumDesync = "a second"
|
2021-09-17 11:44:35 +07:00
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "ntp: option 'max_desync' can't be parsed: could not parse 'a second' as a duration")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorOnInvalidNTPVersion(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultNTPConfig()
|
|
|
|
config.NTP.Version = 1
|
|
|
|
|
|
|
|
ValidateNTP(&config, validator)
|
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "ntp: option 'version' must be either 3 or 4 but it is configured as '1'")
|
2021-09-17 11:44:35 +07:00
|
|
|
}
|