"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/dhcpd/dhcpd_test.go" (19 Nov 2020, 3547 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "dhcpd_test.go": 0.104.1_vs_0.104.3.

    1 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
    2 
    3 package dhcpd
    4 
    5 import (
    6     "bytes"
    7     "net"
    8     "os"
    9     "testing"
   10     "time"
   11 
   12     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
   13     "github.com/stretchr/testify/assert"
   14 )
   15 
   16 func TestMain(m *testing.M) {
   17     testutil.DiscardLogOutput(m)
   18 }
   19 
   20 func testNotify(flags uint32) {
   21 }
   22 
   23 // Leases database store/load
   24 func TestDB(t *testing.T) {
   25     var err error
   26     s := Server{}
   27     s.conf.DBFilePath = dbFilename
   28 
   29     conf := V4ServerConf{
   30         Enabled:    true,
   31         RangeStart: "192.168.10.100",
   32         RangeEnd:   "192.168.10.200",
   33         GatewayIP:  "192.168.10.1",
   34         SubnetMask: "255.255.255.0",
   35         notify:     testNotify,
   36     }
   37     s.srv4, err = v4Create(conf)
   38     assert.True(t, err == nil)
   39 
   40     s.srv6, err = v6Create(V6ServerConf{})
   41     assert.True(t, err == nil)
   42 
   43     l := Lease{}
   44     l.IP = net.ParseIP("192.168.10.100").To4()
   45     l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
   46     exp1 := time.Now().Add(time.Hour)
   47     l.Expiry = exp1
   48     s.srv4.(*v4Server).addLease(&l)
   49 
   50     l2 := Lease{}
   51     l2.IP = net.ParseIP("192.168.10.101").To4()
   52     l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb")
   53     s.srv4.AddStaticLease(l2)
   54 
   55     _ = os.Remove("leases.db")
   56     s.dbStore()
   57     s.srv4.ResetLeases(nil)
   58 
   59     s.dbLoad()
   60 
   61     ll := s.srv4.GetLeases(LeasesAll)
   62 
   63     assert.Equal(t, "aa:aa:aa:aa:aa:bb", ll[0].HWAddr.String())
   64     assert.Equal(t, "192.168.10.101", ll[0].IP.String())
   65     assert.Equal(t, int64(leaseExpireStatic), ll[0].Expiry.Unix())
   66 
   67     assert.Equal(t, "aa:aa:aa:aa:aa:aa", ll[1].HWAddr.String())
   68     assert.Equal(t, "192.168.10.100", ll[1].IP.String())
   69     assert.Equal(t, exp1.Unix(), ll[1].Expiry.Unix())
   70 
   71     _ = os.Remove("leases.db")
   72 }
   73 
   74 func TestIsValidSubnetMask(t *testing.T) {
   75     assert.True(t, isValidSubnetMask([]byte{255, 255, 255, 0}))
   76     assert.True(t, isValidSubnetMask([]byte{255, 255, 254, 0}))
   77     assert.True(t, isValidSubnetMask([]byte{255, 255, 252, 0}))
   78     assert.True(t, !isValidSubnetMask([]byte{255, 255, 253, 0}))
   79     assert.True(t, !isValidSubnetMask([]byte{255, 255, 255, 1}))
   80 }
   81 
   82 func TestNormalizeLeases(t *testing.T) {
   83     dynLeases := []*Lease{}
   84     staticLeases := []*Lease{}
   85 
   86     lease := &Lease{}
   87     lease.HWAddr = []byte{1, 2, 3, 4}
   88     dynLeases = append(dynLeases, lease)
   89     lease = new(Lease)
   90     lease.HWAddr = []byte{1, 2, 3, 5}
   91     dynLeases = append(dynLeases, lease)
   92 
   93     lease = new(Lease)
   94     lease.HWAddr = []byte{1, 2, 3, 4}
   95     lease.IP = []byte{0, 2, 3, 4}
   96     staticLeases = append(staticLeases, lease)
   97     lease = new(Lease)
   98     lease.HWAddr = []byte{2, 2, 3, 4}
   99     staticLeases = append(staticLeases, lease)
  100 
  101     leases := normalizeLeases(staticLeases, dynLeases)
  102 
  103     assert.True(t, len(leases) == 3)
  104     assert.True(t, bytes.Equal(leases[0].HWAddr, []byte{1, 2, 3, 4}))
  105     assert.True(t, bytes.Equal(leases[0].IP, []byte{0, 2, 3, 4}))
  106     assert.True(t, bytes.Equal(leases[1].HWAddr, []byte{2, 2, 3, 4}))
  107     assert.True(t, bytes.Equal(leases[2].HWAddr, []byte{1, 2, 3, 5}))
  108 }
  109 
  110 func TestOptions(t *testing.T) {
  111     code, val := parseOptionString(" 12  hex  abcdef ")
  112     assert.Equal(t, uint8(12), code)
  113     assert.True(t, bytes.Equal([]byte{0xab, 0xcd, 0xef}, val))
  114 
  115     code, _ = parseOptionString(" 12  hex  abcdef1 ")
  116     assert.Equal(t, uint8(0), code)
  117 
  118     code, val = parseOptionString("123 ip 1.2.3.4")
  119     assert.Equal(t, uint8(123), code)
  120     assert.Equal(t, "1.2.3.4", net.IP(string(val)).String())
  121 
  122     code, _ = parseOptionString("256 ip 1.1.1.1")
  123     assert.Equal(t, uint8(0), code)
  124     code, _ = parseOptionString("-1 ip 1.1.1.1")
  125     assert.Equal(t, uint8(0), code)
  126     code, _ = parseOptionString("12 ip 1.1.1.1x")
  127     assert.Equal(t, uint8(0), code)
  128     code, _ = parseOptionString("12 x 1.1.1.1")
  129     assert.Equal(t, uint8(0), code)
  130 }