test(ntp): add missing tests (#4693)

pull/4694/head
Manuel Nuñez 2023-01-03 19:12:19 -03:00 committed by GitHub
parent adaf069eab
commit 6c3d64a06c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/authelia/authelia/v4/internal/configuration/validator" "github.com/authelia/authelia/v4/internal/configuration/validator"
) )
func TestShouldCheckNTP(t *testing.T) { func TestShouldCheckNTPV4(t *testing.T) {
config := &schema.Configuration{ config := &schema.Configuration{
NTP: schema.NTPConfiguration{ NTP: schema.NTPConfiguration{
Address: "time.cloudflare.com:123", Address: "time.cloudflare.com:123",
@ -26,3 +26,20 @@ func TestShouldCheckNTP(t *testing.T) {
assert.NoError(t, ntp.StartupCheck()) assert.NoError(t, ntp.StartupCheck())
} }
func TestShouldCheckNTPV3(t *testing.T) {
config := &schema.Configuration{
NTP: schema.NTPConfiguration{
Address: "time.cloudflare.com:123",
Version: 3,
MaximumDesync: time.Second * 3,
},
}
sv := schema.NewStructValidator()
validator.ValidateNTP(config, sv)
ntp := NewProvider(&config.NTP)
assert.NoError(t, ntp.StartupCheck())
}

View File

@ -9,8 +9,33 @@ import (
"github.com/authelia/authelia/v4/internal/utils" "github.com/authelia/authelia/v4/internal/utils"
) )
func TestShould(t *testing.T) { func TestNtpIsOffsetTooLarge(t *testing.T) {
maxOffset, _ := utils.ParseDurationString("1s") maxOffset, _ := utils.ParseDurationString("1s")
assert.True(t, ntpIsOffsetTooLarge(maxOffset, time.Now(), time.Now().Add(time.Second*2))) assert.True(t, ntpIsOffsetTooLarge(maxOffset, time.Now(), time.Now().Add(time.Second*2)))
assert.True(t, ntpIsOffsetTooLarge(maxOffset, time.Now().Add(time.Second*2), time.Now()))
assert.False(t, ntpIsOffsetTooLarge(maxOffset, time.Now(), time.Now())) assert.False(t, ntpIsOffsetTooLarge(maxOffset, time.Now(), time.Now()))
} }
func TestNtpPacketToTime(t *testing.T) {
resp := &ntpPacket{
TxTimeSeconds: 60,
TxTimeFraction: 0,
}
expected := time.Unix(int64(float64(60)-ntpEpochOffset), 0)
ntpTime := ntpPacketToTime(resp)
assert.Equal(t, expected, ntpTime)
}
func TestLeapVersionClientMode(t *testing.T) {
v3Noleap := uint8(27)
v4Noleap := uint8(43)
v3leap := uint8(91)
v4leap := uint8(107)
assert.Equal(t, v3Noleap, ntpLeapVersionClientMode(false, ntpV3))
assert.Equal(t, v4Noleap, ntpLeapVersionClientMode(false, ntpV4))
assert.Equal(t, v3leap, ntpLeapVersionClientMode(true, ntpV3))
assert.Equal(t, v4leap, ntpLeapVersionClientMode(true, ntpV4))
}