Skip to content

Commit

Permalink
Merge pull request #488 from hashicorp/issue_487
Browse files Browse the repository at this point in the history
Protect against ping periods that are out of range
  • Loading branch information
preetapan authored Oct 20, 2017
2 parents 555c6fa + 585c31c commit 3615aff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions coordinate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ func (c *Client) Update(node string, other *Coordinate, rtt time.Duration) (*Coo
return nil, err
}

const maxRTT = 10 * time.Second
if rtt <= 0 || rtt > maxRTT {
return nil, fmt.Errorf("round trip time not in valid range, duration %v is not a positive value less than %v ", rtt, maxRTT)
}

rttSeconds := c.latencyFilter(node, rtt.Seconds())
c.updateVivaldi(other, rttSeconds)
c.updateAdjustment(other, rttSeconds)
Expand Down
33 changes: 33 additions & 0 deletions coordinate/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coordinate

import (
"fmt"
"math"
"reflect"
"strings"
Expand Down Expand Up @@ -64,6 +65,38 @@ func TestClient_Update(t *testing.T) {
verifyEqualFloats(t, c.Vec[2], 99.0)
}

func TestClient_InvalidInPingValues(t *testing.T) {
config := DefaultConfig()
config.Dimensionality = 3

client, err := NewClient(config)
if err != nil {
t.Fatal(err)
}

// Place another node
other := NewCoordinate(config)
other.Vec[2] = 0.001
dist := client.DistanceTo(other)

// Update with a series of invalid ping periods, should return an error and estimated rtt remains unchanged
pings := []int{1<<63 - 1, -35, 11}

for _, ping := range pings {
expectedErr := fmt.Errorf("round trip time not in valid range, duration %v is not a positive value less than %v", ping, 10*time.Second)
_, err = client.Update("node", other, time.Duration(ping*secondsToNanoseconds))
if err == nil {
t.Fatalf("Unexpected error, wanted %v but got %v", expectedErr, err)
}

dist_new := client.DistanceTo(other)
if dist_new != dist {
t.Fatalf("distance estimate %v not equal to %v", dist_new, dist)
}
}

}

func TestClient_DistanceTo(t *testing.T) {
config := DefaultConfig()
config.Dimensionality = 3
Expand Down

0 comments on commit 3615aff

Please sign in to comment.