From 3ea675161932beb275a7bac97e4b0c6ab95da06a Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 31 Mar 2021 21:10:55 +0800 Subject: [PATCH] add threshold validation --- score_params.go | 20 ++++++++++---------- score_params_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/score_params.go b/score_params.go index 2355c50..4a0a537 100644 --- a/score_params.go +++ b/score_params.go @@ -32,20 +32,20 @@ type PeerScoreThresholds struct { } func (p *PeerScoreThresholds) validate() error { - if p.GossipThreshold > 0 { - return fmt.Errorf("invalid gossip threshold; it must be <= 0") + if p.GossipThreshold > 0 || isInvalidNumber(p.GossipThreshold) { + return fmt.Errorf("invalid gossip threshold; it must be <= 0 and a valid number") } - if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold { - return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold") + if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold || isInvalidNumber(p.PublishThreshold) { + return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold and a valid number") } - if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold { - return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold") + if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold || isInvalidNumber(p.GraylistThreshold) { + return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold and a valid number") } - if p.AcceptPXThreshold < 0 { - return fmt.Errorf("invalid accept PX threshold; it must be >= 0") + if p.AcceptPXThreshold < 0 || isInvalidNumber(p.AcceptPXThreshold) { + return fmt.Errorf("invalid accept PX threshold; it must be >= 0 and a valid number") } - if p.OpportunisticGraftThreshold < 0 { - return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0") + if p.OpportunisticGraftThreshold < 0 || isInvalidNumber(p.OpportunisticGraftThreshold) { + return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0 and a valid number") } return nil } diff --git a/score_params_test.go b/score_params_test.go index 18188fa..37f03ab 100644 --- a/score_params_test.go +++ b/score_params_test.go @@ -30,6 +30,21 @@ func TestPeerScoreThresholdsValidation(t *testing.T) { if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() != nil { t.Fatal("expected validation success") } + if (&PeerScoreThresholds{GossipThreshold: math.Inf(-1), PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: math.Inf(-1), GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: math.Inf(-1), AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: math.NaN(), OpportunisticGraftThreshold: 2}).validate() == nil { + t.Fatal("expected validation error") + } + if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: math.Inf(0)}).validate() == nil { + t.Fatal("expected validation error") + } } func TestTopicScoreParamsValidation(t *testing.T) { -- GitLab