...

Source file src/github.com/mdlayher/arp/client_test.go

Documentation: github.com/mdlayher/arp

     1  package arp
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestClientClose(t *testing.T) {
    12  	p := &closeCapturePacketConn{}
    13  	c := &Client{p: p}
    14  
    15  	if err := c.Close(); err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	if !p.closed {
    20  		t.Fatal("client was not closed")
    21  	}
    22  }
    23  
    24  func TestClientSetDeadline(t *testing.T) {
    25  	p := &deadlineCapturePacketConn{}
    26  	c := &Client{p: p}
    27  
    28  	d := time.Now()
    29  	if err := c.SetDeadline(d); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	if want, got := d, p.r; want != got {
    34  		t.Fatalf("unexpected read deadline: %v != %v", want, got)
    35  	}
    36  	if want, got := d, p.w; want != got {
    37  		t.Fatalf("unexpected write deadline: %v != %v", want, got)
    38  	}
    39  }
    40  
    41  func TestClientSetReadDeadline(t *testing.T) {
    42  	p := &deadlineCapturePacketConn{}
    43  	c := &Client{p: p}
    44  
    45  	d := time.Now()
    46  	if err := c.SetReadDeadline(d); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if want, got := d, p.r; want != got {
    51  		t.Fatalf("unexpected read deadline: %v != %v", want, got)
    52  	}
    53  	if want, got := (time.Time{}), p.w; want != got {
    54  		t.Fatalf("non-zero write deadline: %v", got)
    55  	}
    56  }
    57  
    58  func TestClientSetWriteDeadline(t *testing.T) {
    59  	p := &deadlineCapturePacketConn{}
    60  	c := &Client{p: p}
    61  
    62  	d := time.Now()
    63  	if err := c.SetWriteDeadline(d); err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	if want, got := (time.Time{}), p.r; want != got {
    68  		t.Fatalf("non-zero read deadline: %v", got)
    69  	}
    70  	if want, got := d, p.w; want != got {
    71  		t.Fatalf("unexpected write deadline: %v != %v", want, got)
    72  	}
    73  }
    74  
    75  func TestClientHardwareAddr(t *testing.T) {
    76  	c := &Client{
    77  		ifi: &net.Interface{
    78  			HardwareAddr: net.HardwareAddr{0, 1, 2, 3, 4, 5},
    79  		},
    80  	}
    81  
    82  	if want, got := c.ifi.HardwareAddr.String(), c.HardwareAddr().String(); want != got {
    83  		t.Fatalf("unexpected hardware address: %v != %v", want, got)
    84  	}
    85  }
    86  
    87  func Test_newClient(t *testing.T) {
    88  	tests := []struct {
    89  		desc  string
    90  		addrs []netip.Addr
    91  		c     *Client
    92  		err   error
    93  	}{
    94  		{
    95  			desc: "no network addresses",
    96  			c:    &Client{},
    97  			err:  errNoIPv4Addr,
    98  		},
    99  		{
   100  			desc: "OK",
   101  			addrs: []netip.Addr{
   102  				netip.MustParseAddr("192.168.1.1"),
   103  			},
   104  			c: &Client{
   105  				ip: netip.MustParseAddr("192.168.1.1"),
   106  			},
   107  		},
   108  	}
   109  
   110  	for i, tt := range tests {
   111  		c, err := newClient(nil, nil, tt.addrs)
   112  		if err != nil {
   113  			if want, got := tt.err.Error(), err.Error(); want != got {
   114  				t.Fatalf("[%02d] test %q, unexpected error: %v != %v",
   115  					i, tt.desc, want, got)
   116  			}
   117  
   118  			continue
   119  		}
   120  
   121  		if want, got := tt.c, c; !reflect.DeepEqual(want, got) {
   122  			t.Fatalf("[%02d] test %q, unexpected Client: %v != %v",
   123  				i, tt.desc, want, got)
   124  		}
   125  	}
   126  }
   127  
   128  type closeCapturePacketConn struct {
   129  	closed bool
   130  
   131  	noopPacketConn
   132  }
   133  
   134  func (p *closeCapturePacketConn) Close() error {
   135  	p.closed = true
   136  	return nil
   137  }
   138  
   139  // deadlineCapturePacketConn is a net.PacketConn which captures read and
   140  // write deadlines.
   141  type deadlineCapturePacketConn struct {
   142  	r time.Time
   143  	w time.Time
   144  
   145  	noopPacketConn
   146  }
   147  
   148  func (p *deadlineCapturePacketConn) SetDeadline(t time.Time) error {
   149  	p.r = t
   150  	p.w = t
   151  	return nil
   152  }
   153  
   154  func (p *deadlineCapturePacketConn) SetReadDeadline(t time.Time) error {
   155  	p.r = t
   156  	return nil
   157  }
   158  
   159  func (p *deadlineCapturePacketConn) SetWriteDeadline(t time.Time) error {
   160  	p.w = t
   161  	return nil
   162  }
   163  
   164  // noopPacketConn is a net.PacketConn which simply no-ops any input.  It is
   165  // embedded in other implementations so they do not have to implement every
   166  // single method.
   167  type noopPacketConn struct{}
   168  
   169  func (noopPacketConn) ReadFrom(b []byte) (int, net.Addr, error)     { return 0, nil, nil }
   170  func (noopPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { return 0, nil }
   171  
   172  func (noopPacketConn) Close() error                       { return nil }
   173  func (noopPacketConn) LocalAddr() net.Addr                { return nil }
   174  func (noopPacketConn) SetDeadline(t time.Time) error      { return nil }
   175  func (noopPacketConn) SetReadDeadline(t time.Time) error  { return nil }
   176  func (noopPacketConn) SetWriteDeadline(t time.Time) error { return nil }
   177  func (noopPacketConn) HardwareAddr() net.HardwareAddr     { return nil }
   178  

View as plain text