Commit 3ea67516 authored by nisdas's avatar nisdas Committed by vyzo

add threshold validation

parent 5cd1316e
...@@ -32,20 +32,20 @@ type PeerScoreThresholds struct { ...@@ -32,20 +32,20 @@ type PeerScoreThresholds struct {
} }
func (p *PeerScoreThresholds) validate() error { func (p *PeerScoreThresholds) validate() error {
if p.GossipThreshold > 0 { if p.GossipThreshold > 0 || isInvalidNumber(p.GossipThreshold) {
return fmt.Errorf("invalid gossip threshold; it must be <= 0") return fmt.Errorf("invalid gossip threshold; it must be <= 0 and a valid number")
} }
if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold { if p.PublishThreshold > 0 || p.PublishThreshold > p.GossipThreshold || isInvalidNumber(p.PublishThreshold) {
return fmt.Errorf("invalid publish threshold; it must be <= 0 and <= gossip threshold") 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 { if p.GraylistThreshold > 0 || p.GraylistThreshold > p.PublishThreshold || isInvalidNumber(p.GraylistThreshold) {
return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold") return fmt.Errorf("invalid graylist threshold; it must be <= 0 and <= publish threshold and a valid number")
} }
if p.AcceptPXThreshold < 0 { if p.AcceptPXThreshold < 0 || isInvalidNumber(p.AcceptPXThreshold) {
return fmt.Errorf("invalid accept PX threshold; it must be >= 0") return fmt.Errorf("invalid accept PX threshold; it must be >= 0 and a valid number")
} }
if p.OpportunisticGraftThreshold < 0 { if p.OpportunisticGraftThreshold < 0 || isInvalidNumber(p.OpportunisticGraftThreshold) {
return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0") return fmt.Errorf("invalid opportunistic grafting threshold; it must be >= 0 and a valid number")
} }
return nil return nil
} }
......
...@@ -30,6 +30,21 @@ func TestPeerScoreThresholdsValidation(t *testing.T) { ...@@ -30,6 +30,21 @@ func TestPeerScoreThresholdsValidation(t *testing.T) {
if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() != nil { if (&PeerScoreThresholds{GossipThreshold: -1, PublishThreshold: -2, GraylistThreshold: -3, AcceptPXThreshold: 1, OpportunisticGraftThreshold: 2}).validate() != nil {
t.Fatal("expected validation success") 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) { func TestTopicScoreParamsValidation(t *testing.T) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment