2021-09-17 04:44:35 +00:00
package validator
import (
"testing"
"github.com/stretchr/testify/assert"
2022-02-28 03:15:01 +00:00
"github.com/stretchr/testify/require"
2021-09-17 04:44:35 +00:00
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
2022-02-28 03:15:01 +00:00
func newDefaultNTPConfig ( ) schema . Configuration {
return schema . Configuration {
2022-03-02 06:40:26 +00:00
NTP : schema . NTPConfiguration { } ,
2022-02-28 03:15:01 +00:00
}
2021-09-17 04:44:35 +00:00
}
2022-03-03 11:20:43 +00:00
func TestShouldSetDefaultNTPValues ( t * testing . T ) {
2021-09-17 04:44:35 +00:00
validator := schema . NewStructValidator ( )
config := newDefaultNTPConfig ( )
ValidateNTP ( & config , validator )
assert . Len ( t , validator . Errors ( ) , 0 )
2022-02-28 03:15:01 +00:00
assert . Equal ( t , schema . DefaultNTPConfiguration . Address , config . NTP . Address )
2022-03-03 11:20:43 +00:00
assert . Equal ( t , schema . DefaultNTPConfiguration . Version , config . NTP . Version )
assert . Equal ( t , schema . DefaultNTPConfiguration . MaximumDesync , config . NTP . MaximumDesync )
assert . Equal ( t , schema . DefaultNTPConfiguration . DisableStartupCheck , config . NTP . DisableStartupCheck )
2021-09-17 04:44:35 +00:00
}
func TestShouldSetDefaultNtpVersion ( t * testing . T ) {
validator := schema . NewStructValidator ( )
config := newDefaultNTPConfig ( )
2022-03-03 11:20:43 +00:00
config . NTP . MaximumDesync = - 1
2021-09-17 04:44:35 +00:00
ValidateNTP ( & config , validator )
assert . Len ( t , validator . Errors ( ) , 0 )
2022-02-28 03:15:01 +00:00
assert . Equal ( t , schema . DefaultNTPConfiguration . MaximumDesync , config . NTP . MaximumDesync )
2021-09-17 04:44:35 +00:00
}
2022-02-28 03:15:01 +00:00
func TestShouldRaiseErrorOnInvalidNTPVersion ( t * testing . T ) {
validator := schema . NewStructValidator ( )
config := newDefaultNTPConfig ( )
config . NTP . Version = 1
ValidateNTP ( & config , validator )
require . Len ( t , validator . Errors ( ) , 1 )
2023-04-13 10:58:18 +00:00
assert . EqualError ( t , validator . Errors ( ) [ 0 ] , "ntp: option 'version' must be either 3 or 4 but it's configured as '1'" )
2021-09-17 04:44:35 +00:00
}
2023-05-07 06:39:17 +00:00
func TestShouldRaiseErrorOnInvalidNTPScheme ( t * testing . T ) {
validator := schema . NewStructValidator ( )
config := newDefaultNTPConfig ( )
config . NTP . Address = & schema . AddressUDP { Address : MustParseAddress ( "tcp://abc:123" ) }
ValidateNTP ( & config , validator )
require . Len ( t , validator . Errors ( ) , 1 )
assert . EqualError ( t , validator . Errors ( ) [ 0 ] , "ntp: option 'address' with value 'tcp://abc:123' is invalid: scheme must be one of 'udp', 'udp4', or 'udp6' but is configured as 'tcp'" )
}