diff --git a/internal/ntp/ntp_test.go b/internal/ntp/ntp_test.go index 3528b84ce..de88edfc3 100644 --- a/internal/ntp/ntp_test.go +++ b/internal/ntp/ntp_test.go @@ -10,7 +10,7 @@ import ( "github.com/authelia/authelia/v4/internal/configuration/validator" ) -func TestShouldCheckNTP(t *testing.T) { +func TestShouldCheckNTPV4(t *testing.T) { config := &schema.Configuration{ NTP: schema.NTPConfiguration{ Address: "time.cloudflare.com:123", @@ -26,3 +26,20 @@ func TestShouldCheckNTP(t *testing.T) { 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()) +} diff --git a/internal/ntp/util_test.go b/internal/ntp/util_test.go index 53cb9b819..caf07ca02 100644 --- a/internal/ntp/util_test.go +++ b/internal/ntp/util_test.go @@ -9,8 +9,33 @@ import ( "github.com/authelia/authelia/v4/internal/utils" ) -func TestShould(t *testing.T) { +func TestNtpIsOffsetTooLarge(t *testing.T) { maxOffset, _ := utils.ParseDurationString("1s") 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())) } + +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)) +}