From febff583e5a935437d7e5ee1869d3ce3d6b802df Mon Sep 17 00:00:00 2001 From: Dominic Evans Date: Thu, 29 Apr 2021 23:14:11 +0100 Subject: [PATCH] chore: fix some minor govet/staticheck failures - deprecated functions - copylocks on range - t.Fatal in non-test goroutines - use bytes.Equal - #nosec for math/crypto --- cli/cli.go | 2 +- client/client.go | 2 +- proxy_collection.go | 20 ++++++++++---------- proxy_test.go | 6 ++++-- stream/io_chan.go | 2 +- toxics/bandwidth.go | 5 ++++- toxics/bandwidth_test.go | 2 +- toxics/latency.go | 3 ++- toxics/slicer.go | 3 ++- toxics/slicer_test.go | 2 +- toxics/toxic.go | 1 + toxics/toxic_test.go | 3 ++- 12 files changed, 30 insertions(+), 21 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 4bffea48..e3115c01 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -602,7 +602,7 @@ func hint(m string) { } func errorf(m string, args ...interface{}) error { - return cli.NewExitError(fmt.Sprintf(m, args...), 1) + return cli.Exit(fmt.Sprintf(m, args...), 1) } func printWidth(col string, m string, numTabs int) { diff --git a/client/client.go b/client/client.go index 1259b66e..ceb61bfc 100644 --- a/client/client.go +++ b/client/client.go @@ -46,7 +46,7 @@ type Proxy struct { // NewClient creates a new client which provides the base of all communication // with Toxiproxy. Endpoint is the address to the proxy (e.g. localhost:8474 if -// not overriden) +// not overridden) func NewClient(endpoint string) *Client { if strings.HasPrefix(endpoint, "https://") { log.Fatal("the toxiproxy client does not support https") diff --git a/proxy_collection.go b/proxy_collection.go index 3070f5e7..8f9ab001 100644 --- a/proxy_collection.go +++ b/proxy_collection.go @@ -8,7 +8,7 @@ import ( ) // ProxyCollection is a collection of proxies. It's the interface for anything -// to add and remove proxies from the toxiproxy instance. It's responsibilty is +// to add and remove proxies from the toxiproxy instance. It's responsibility is // to maintain the integrity of the proxy set, by guarding for things such as // duplicate names. type ProxyCollection struct { @@ -79,27 +79,27 @@ func (collection *ProxyCollection) PopulateJson(data io.Reader) ([]*Proxy, error // Check for valid input before creating any proxies t := true - for i, p := range input { - if len(p.Name) < 1 { + for i := range input { + if len(input[i].Name) < 1 { return nil, joinError(fmt.Errorf("name at proxy %d", i+1), ErrMissingField) } - if len(p.Upstream) < 1 { + if len(input[i].Upstream) < 1 { return nil, joinError(fmt.Errorf("upstream at proxy %d", i+1), ErrMissingField) } - if p.Enabled == nil { + if input[i].Enabled == nil { input[i].Enabled = &t } } proxies := make([]*Proxy, 0, len(input)) - for _, p := range input { + for i := range input { proxy := NewProxy() - proxy.Name = p.Name - proxy.Listen = p.Listen - proxy.Upstream = p.Upstream + proxy.Name = input[i].Name + proxy.Listen = input[i].Listen + proxy.Upstream = input[i].Upstream - err = collection.AddOrReplace(proxy, *p.Enabled) + err = collection.AddOrReplace(proxy, *input[i].Enabled) if err != nil { break } diff --git a/proxy_test.go b/proxy_test.go index 78a7d9b5..212b542f 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -46,7 +46,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) { select { case <-tomb.Dying(): default: - t.Fatal("Failed to accept client") + t.Error("Failed to accept client") + return } return } @@ -55,7 +56,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) { val, err := ioutil.ReadAll(src) if err != nil { - t.Fatal("Failed to read from client") + t.Error("Failed to read from client") + return } response <- val diff --git a/stream/io_chan.go b/stream/io_chan.go index 4038c32e..7229035e 100644 --- a/stream/io_chan.go +++ b/stream/io_chan.go @@ -14,7 +14,7 @@ const ( NumDirections ) -// Stores a slice of bytes with its receive timestmap +// Stores a slice of bytes with its receive timestamp type StreamChunk struct { Data []byte Timestamp time.Time diff --git a/toxics/bandwidth.go b/toxics/bandwidth.go index 6c4624a9..f0dac05d 100644 --- a/toxics/bandwidth.go +++ b/toxics/bandwidth.go @@ -32,7 +32,10 @@ func (t *BandwidthToxic) Pipe(stub *ToxicStub) { for int64(len(p.Data)) > t.Rate*100 { select { case <-time.After(100 * time.Millisecond): - stub.Output <- &stream.StreamChunk{p.Data[:t.Rate*100], p.Timestamp} + stub.Output <- &stream.StreamChunk{ + Data: p.Data[:t.Rate*100], + Timestamp: p.Timestamp, + } p.Data = p.Data[t.Rate*100:] sleep -= 100 * time.Millisecond case <-stub.Interrupt: diff --git a/toxics/bandwidth_test.go b/toxics/bandwidth_test.go index 6a3cf8d5..95631c71 100644 --- a/toxics/bandwidth_test.go +++ b/toxics/bandwidth_test.go @@ -57,7 +57,7 @@ func TestBandwidthToxic(t *testing.T) { _, err = io.ReadAtLeast(serverConn, buf2, len(buf2)) if err != nil { t.Errorf("Proxy read failed: %v", err) - } else if bytes.Compare(buf, buf2) != 0 { + } else if !bytes.Equal(buf, buf2) { t.Errorf("Server did not read correct buffer from client!") } diff --git a/toxics/latency.go b/toxics/latency.go index 2baf549b..a56370af 100644 --- a/toxics/latency.go +++ b/toxics/latency.go @@ -19,8 +19,9 @@ func (t *LatencyToxic) GetBufferSize() int { func (t *LatencyToxic) delay() time.Duration { // Delay = t.Latency +/- t.Jitter delay := t.Latency - jitter := int64(t.Jitter) + jitter := t.Jitter if jitter > 0 { + //#nosec delay += rand.Int63n(jitter*2) - jitter } return time.Duration(delay) * time.Millisecond diff --git a/toxics/slicer.go b/toxics/slicer.go index 7bb83a64..dd4e05a6 100644 --- a/toxics/slicer.go +++ b/toxics/slicer.go @@ -8,7 +8,7 @@ import ( ) // The SlicerToxic slices data into multiple smaller packets -// to simulate real-world TCP behaviour. +// to simulate real-world TCP behavior. type SlicerToxic struct { // Average number of bytes to slice at AverageSize int `json:"average_size"` @@ -39,6 +39,7 @@ func (t *SlicerToxic) chunk(start int, end int) []int { // +1 in the size variation to offset favoring of smaller // numbers by integer division + //#nosec mid := start + (end-start)/2 + (rand.Intn(t.SizeVariation*2) - t.SizeVariation) + rand.Intn(1) left := t.chunk(start, mid) right := t.chunk(mid, end) diff --git a/toxics/slicer_test.go b/toxics/slicer_test.go index 6be1da19..b457be77 100644 --- a/toxics/slicer_test.go +++ b/toxics/slicer_test.go @@ -52,7 +52,7 @@ L: if reads < 480/2 || reads > 480/2+480 { t.Errorf("Expected to read about 480 times, but read %d times.", reads) } - if bytes.Compare(buf, data) != 0 { + if !bytes.Equal(buf, data) { t.Errorf("Server did not read correct buffer from client!") } } diff --git a/toxics/toxic.go b/toxics/toxic.go index 554b4742..5d66ac29 100644 --- a/toxics/toxic.go +++ b/toxics/toxic.go @@ -78,6 +78,7 @@ func NewToxicStub(input <-chan *stream.StreamChunk, output chan<- *stream.Stream func (s *ToxicStub) Run(toxic *ToxicWrapper) { s.running = make(chan struct{}) defer close(s.running) + //#nosec if rand.Float32() < toxic.Toxicity { toxic.Pipe(s) } else { diff --git a/toxics/toxic_test.go b/toxics/toxic_test.go index 2f5d87d6..93b25295 100644 --- a/toxics/toxic_test.go +++ b/toxics/toxic_test.go @@ -48,7 +48,8 @@ func WithEchoServer(t *testing.T, f func(string, chan []byte)) { select { case <-tomb.Dying(): default: - t.Fatal("Failed to accept client") + t.Error("Failed to accept client") + return } return }