From 5dea7039d65e5b94fa681b17bc4a3bf63aa47994 Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Wed, 8 May 2019 13:51:39 +0100 Subject: [CI] Added linters Now the CI will also run golangci-lint Signed-off-by: Andrea Barberio --- dhcpv4/async/client.go | 18 +++++++++++++----- dhcpv4/async/client_test.go | 8 ++++++-- dhcpv4/bsdp/bsdp.go | 2 +- dhcpv4/bsdp/bsdp_option_misc_test.go | 12 ++++++------ dhcpv4/bsdp/option_vendor_specific_information.go | 2 +- dhcpv4/dhcpv4.go | 8 ++++---- dhcpv4/dhcpv4_test.go | 3 ++- dhcpv4/nclient4/client_test.go | 12 +++++++----- dhcpv4/option_ip_address_lease_time.go | 3 +-- dhcpv4/options.go | 2 +- dhcpv4/server4/server_test.go | 4 +++- dhcpv4/ztpv4/parse_circuitid_test.go | 1 - 12 files changed, 45 insertions(+), 30 deletions(-) (limited to 'dhcpv4') diff --git a/dhcpv4/async/client.go b/dhcpv4/async/client.go index 4c2d3c1..54b500d 100644 --- a/dhcpv4/async/client.go +++ b/dhcpv4/async/client.go @@ -3,6 +3,7 @@ package async import ( "context" "fmt" + "log" "net" "sync" "time" @@ -138,14 +139,18 @@ func (c *Client) send(packet *dhcpv4.DHCPv4) { raddr, err := c.remoteAddr() if err != nil { - p.Reject(err) + _ = p.Reject(err) return } - c.connection.SetWriteDeadline(time.Now().Add(c.WriteTimeout)) + if err := c.connection.SetWriteDeadline(time.Now().Add(c.WriteTimeout)); err != nil { + log.Printf("Warning: cannot set write deadline: %v", err) + return + } _, err = c.connection.WriteTo(packet.ToBytes(), raddr) if err != nil { - p.Reject(err) + _ = p.Reject(err) + log.Printf("Warning: cannot write to %s: %v", raddr, err) return } @@ -158,7 +163,10 @@ func (c *Client) receive(_ *dhcpv4.DHCPv4) { received *dhcpv4.DHCPv4 ) - c.connection.SetReadDeadline(time.Now().Add(c.ReadTimeout)) + if err := c.connection.SetReadDeadline(time.Now().Add(c.ReadTimeout)); err != nil { + log.Printf("Warning: cannot set write deadline: %v", err) + return + } for { buffer := make([]byte, client4.MaxUDPReceivedPacketSize) n, _, _, _, err := c.connection.ReadMsgUDP(buffer, oobdata) @@ -177,7 +185,7 @@ func (c *Client) receive(_ *dhcpv4.DHCPv4) { c.packetsLock.Lock() if p, ok := c.packets[received.TransactionID]; ok { delete(c.packets, received.TransactionID) - p.Resolve(received) + _ = p.Resolve(received) } c.packetsLock.Unlock() } diff --git a/dhcpv4/async/client_test.go b/dhcpv4/async/client_test.go index b134afe..2269d57 100644 --- a/dhcpv4/async/client_test.go +++ b/dhcpv4/async/client_test.go @@ -26,7 +26,9 @@ func serve(ctx context.Context, addr *net.UDPAddr, response *dhcpv4.DHCPv4) erro case <-ctx.Done(): return default: - conn.SetReadDeadline(time.Now().Add(1 * time.Second)) + if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil { + panic(err) + } n, _, _, src, err := conn.ReadMsgUDP(buffer, oobdata) if err != nil { continue @@ -35,7 +37,9 @@ func serve(ctx context.Context, addr *net.UDPAddr, response *dhcpv4.DHCPv4) erro if err != nil { continue } - conn.SetWriteDeadline(time.Now().Add(1 * time.Second)) + if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); err != nil { + panic(err) + } _, err = conn.WriteTo(response.ToBytes(), src) if err != nil { continue diff --git a/dhcpv4/bsdp/bsdp.go b/dhcpv4/bsdp/bsdp.go index c9f7c26..d90e884 100644 --- a/dhcpv4/bsdp/bsdp.go +++ b/dhcpv4/bsdp/bsdp.go @@ -93,7 +93,7 @@ func NewInformListForInterface(ifname string, replyPort uint16) (*Packet, error) if err != nil { return nil, fmt.Errorf("could not get local IPv4 addr for %s: %v", iface.Name, err) } - if localIPs == nil || len(localIPs) == 0 { + if len(localIPs) == 0 { return nil, fmt.Errorf("could not get local IPv4 addr for %s", iface.Name) } return NewInformList(iface.HardwareAddr, localIPs[0], replyPort) diff --git a/dhcpv4/bsdp/bsdp_option_misc_test.go b/dhcpv4/bsdp/bsdp_option_misc_test.go index 44e2813..675a5db 100644 --- a/dhcpv4/bsdp/bsdp_option_misc_test.go +++ b/dhcpv4/bsdp/bsdp_option_misc_test.go @@ -22,7 +22,7 @@ func TestGetReplyPort(t *testing.T) { require.Equal(t, uint16(1234), port) o = VendorOptions{dhcpv4.Options{}} - port, err = o.ReplyPort() + _, err = o.ReplyPort() require.Error(t, err, "no reply port present") } @@ -40,7 +40,7 @@ func TestGetServerPriority(t *testing.T) { require.Equal(t, uint16(1234), prio) o = VendorOptions{dhcpv4.Options{}} - prio, err = o.ServerPriority() + _, err = o.ServerPriority() require.Error(t, err, "no server prio present") } @@ -73,19 +73,19 @@ func TestGetVersion(t *testing.T) { require.Equal(t, ver, Version1_1) o = VendorOptions{dhcpv4.Options{}} - ver, err = o.Version() + _, err = o.Version() require.Error(t, err, "no version present") o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{}}} - ver, err = o.Version() + _, err = o.Version() require.Error(t, err, "empty version field") o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{1}}} - ver, err = o.Version() + _, err = o.Version() require.Error(t, err, "version option too short") o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{1, 2, 3}}} - ver, err = o.Version() + _, err = o.Version() require.Error(t, err, "version option too long") } diff --git a/dhcpv4/bsdp/option_vendor_specific_information.go b/dhcpv4/bsdp/option_vendor_specific_information.go index abdd9a5..4c2365c 100644 --- a/dhcpv4/bsdp/option_vendor_specific_information.go +++ b/dhcpv4/bsdp/option_vendor_specific_information.go @@ -153,5 +153,5 @@ func parseOption(code dhcpv4.OptionCode, data []byte) fmt.Stringer { if d != nil && d.FromBytes(data) == nil { return d } - return dhcpv4.OptionGeneric{data} + return dhcpv4.OptionGeneric{Data: data} } diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go index cfb2f9a..b15c9e2 100644 --- a/dhcpv4/dhcpv4.go +++ b/dhcpv4/dhcpv4.go @@ -33,10 +33,10 @@ const ( // minPacketLen is the minimum DHCP header length. minPacketLen = 236 - // Maximum length of the ClientHWAddr (client hardware address) according to - // RFC 2131, Section 2. This is the link-layer destination a server - // must send responses to. - maxHWAddrLen = 16 + // MaxHWAddrLen is the maximum hardware address length of the ClientHWAddr + // (client hardware address) according to RFC 2131, Section 2. This is the + // link-layer destination a server must send responses to. + MaxHWAddrLen = 16 // MaxMessageSize is the maximum size in bytes that a DHCPv4 packet can hold. MaxMessageSize = 576 diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go index 8d6ec54..996b481 100644 --- a/dhcpv4/dhcpv4_test.go +++ b/dhcpv4/dhcpv4_test.go @@ -216,13 +216,14 @@ func TestDHCPv4NewRequestFromOffer(t *testing.T) { require.NoError(t, err) offer.SetBroadcast() offer.UpdateOption(OptMessageType(MessageTypeOffer)) - req, err := NewRequestFromOffer(offer) + _, err = NewRequestFromOffer(offer) require.Error(t, err) // Now add the option so it doesn't error out. offer.UpdateOption(OptServerIdentifier(net.IPv4(192, 168, 0, 1))) // Broadcast request + var req *DHCPv4 req, err = NewRequestFromOffer(offer) require.NoError(t, err) require.Equal(t, MessageTypeRequest, req.MessageType()) diff --git a/dhcpv4/nclient4/client_test.go b/dhcpv4/nclient4/client_test.go index 6d7da1f..274c94b 100644 --- a/dhcpv4/nclient4/client_test.go +++ b/dhcpv4/nclient4/client_test.go @@ -37,7 +37,7 @@ func (h *handler) handle(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) { if len(h.responses) > 0 { for _, resp := range h.responses[0] { - conn.WriteTo(resp.ToBytes(), peer) + _, _ = conn.WriteTo(resp.ToBytes(), peer) } h.responses = h.responses[1:] } @@ -65,7 +65,9 @@ func serveAndClient(ctx context.Context, responses [][]*dhcpv4.DHCPv4, opts ...C if err != nil { panic(err) } - go s.Serve() + go func() { + _ = s.Serve() + }() return mc, serverConn } @@ -77,7 +79,7 @@ func ComparePacket(got *dhcpv4.DHCPv4, want *dhcpv4.DHCPv4) error { if (want == nil || got == nil) && (got != want) { return fmt.Errorf("packet got %v, want %v", got, want) } - if bytes.Compare(got.ToBytes(), want.ToBytes()) != 0 { + if !bytes.Equal(got.ToBytes(), want.ToBytes()) { return fmt.Errorf("packet got %v, want %v", got, want) } return nil @@ -266,8 +268,8 @@ func TestSimpleSendAndReadDiscardGarbage(t *testing.T) { defer mc.Close() // Too short for valid DHCPv4 packet. - udpConn.WriteTo([]byte{0x01}, nil) - udpConn.WriteTo([]byte{0x01, 0x2}, nil) + _, _ = udpConn.WriteTo([]byte{0x01}, nil) + _, _ = udpConn.WriteTo([]byte{0x01, 0x2}, nil) rcvd, err := mc.SendAndRead(ctx, DefaultServers, pkt, nil) if err != nil { diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go index 20ee2b8..9bc8156 100644 --- a/dhcpv4/option_ip_address_lease_time.go +++ b/dhcpv4/option_ip_address_lease_time.go @@ -1,7 +1,6 @@ package dhcpv4 import ( - "fmt" "time" "github.com/u-root/u-root/pkg/uio" @@ -27,7 +26,7 @@ func (d Duration) ToBytes() []byte { // String returns a human-readable string for this option. func (d Duration) String() string { - return fmt.Sprintf("%s", time.Duration(d)) + return time.Duration(d).String() } // OptIPAddressLeaseTime returns a new IP address lease time option. diff --git a/dhcpv4/options.go b/dhcpv4/options.go index 37607b7..11db5f7 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -175,7 +175,7 @@ func (o Options) sortedKeys() []int { codes = append(codes, int(k)) } - sort.Sort(sort.IntSlice(codes)) + sort.Ints(codes) return codes } diff --git a/dhcpv4/server4/server_test.go b/dhcpv4/server4/server_test.go index cd46774..d24d49b 100644 --- a/dhcpv4/server4/server_test.go +++ b/dhcpv4/server4/server_test.go @@ -77,7 +77,9 @@ func setUpClientAndServer(t *testing.T, iface net.Interface, handler Handler) (* if err != nil { t.Fatal(err) } - go s.Serve() + go func() { + _ = s.Serve() + }() clientConn, err := nclient4.NewIPv4UDPConn("", caddr.Port) if err != nil { diff --git a/dhcpv4/ztpv4/parse_circuitid_test.go b/dhcpv4/ztpv4/parse_circuitid_test.go index b075a16..94061c4 100644 --- a/dhcpv4/ztpv4/parse_circuitid_test.go +++ b/dhcpv4/ztpv4/parse_circuitid_test.go @@ -45,7 +45,6 @@ func TestFormatCircuitID(t *testing.T) { name string circuit *CircuitID want string - fail bool }{ {name: "empty", circuit: &CircuitID{}, want: ",,,,"}, {name: "juniperQFX pattern", circuit: &CircuitID{Slot: "0", Module: "0", Port: "0", SubPort: "0"}, want: "0,0,0,0,"}, -- cgit v1.2.3