Skip to content

Commit df57e6c

Browse files
authored
Fix negative shift amount during prefix usage calc (#114)
1 parent d01b55e commit df57e6c

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

prefix.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,12 @@ func (p *Prefix) availablePrefixes() (uint64, []string) {
638638
totalAvailable := uint64(0)
639639
availablePrefixes := []string{}
640640
for _, pfx := range pfxs {
641+
bits := maxBits - pfx.Bits()
642+
if bits < 0 {
643+
continue
644+
}
641645
// same as: totalAvailable += uint64(math.Pow(float64(2), float64(maxBits-pfx.Bits)))
642-
totalAvailable += 1 << (maxBits - pfx.Bits())
646+
totalAvailable += 1 << bits
643647
availablePrefixes = append(availablePrefixes, pfx.String())
644648
}
645649
// we are not reporting more that 2^31 available prefixes

prefix_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111
"testing"
1212

13+
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415
"golang.org/x/sync/errgroup"
1516
)
@@ -1644,3 +1645,83 @@ func TestNamespaceFromContext(t *testing.T) {
16441645
})
16451646
}
16461647
}
1648+
func TestAvailablePrefixes(t *testing.T) {
1649+
testCases := []struct {
1650+
name string
1651+
cidr string
1652+
expectedTotal uint64
1653+
expectedAvailablePfx []string
1654+
}{
1655+
{
1656+
name: "192.168.0.0/32",
1657+
cidr: "192.168.0.0/32",
1658+
expectedTotal: 0,
1659+
expectedAvailablePfx: []string{},
1660+
},
1661+
{
1662+
name: "192.168.0.0/31",
1663+
cidr: "192.168.0.0/31",
1664+
expectedTotal: 0,
1665+
expectedAvailablePfx: []string{},
1666+
},
1667+
{
1668+
name: "192.168.0.0/30",
1669+
cidr: "192.168.0.0/30",
1670+
expectedTotal: 1,
1671+
expectedAvailablePfx: []string{"192.168.0.0/30"},
1672+
},
1673+
{
1674+
name: "192.168.0.0/24",
1675+
cidr: "192.168.0.0/24",
1676+
expectedTotal: 64,
1677+
expectedAvailablePfx: []string{"192.168.0.0/24"},
1678+
},
1679+
{
1680+
name: "2001:0db8:85a3::/128",
1681+
cidr: "2001:0db8:85a3::/128",
1682+
expectedTotal: 0,
1683+
expectedAvailablePfx: []string{},
1684+
},
1685+
{
1686+
name: "2001:0db8:85a3::/127",
1687+
cidr: "2001:0db8:85a3::/127",
1688+
expectedTotal: 0,
1689+
expectedAvailablePfx: []string{},
1690+
},
1691+
{
1692+
name: "2001:0db8:85a3::/126",
1693+
cidr: "2001:0db8:85a3::/126",
1694+
expectedTotal: 1,
1695+
expectedAvailablePfx: []string{"2001:db8:85a3::/126"},
1696+
},
1697+
{
1698+
name: "Invalid CIDR",
1699+
cidr: "Invalid CIDR",
1700+
expectedTotal: 0,
1701+
expectedAvailablePfx: []string{},
1702+
},
1703+
}
1704+
1705+
for _, tc := range testCases {
1706+
t.Run(tc.name, func(t *testing.T) {
1707+
prefix := &Prefix{
1708+
Cidr: tc.cidr,
1709+
isParent: false,
1710+
availableChildPrefixes: make(map[string]bool),
1711+
}
1712+
1713+
totalAvailable, availablePrefixes := prefix.availablePrefixes()
1714+
1715+
assert.Equal(
1716+
t, tc.expectedTotal, totalAvailable,
1717+
"Expected totalAvailable: %d, got: %d",
1718+
tc.expectedTotal, totalAvailable,
1719+
)
1720+
assert.ElementsMatchf(
1721+
t, availablePrefixes, tc.expectedAvailablePfx,
1722+
"Expected availablePrefixes: %v, got: %v",
1723+
tc.expectedAvailablePfx, availablePrefixes,
1724+
)
1725+
})
1726+
}
1727+
}

0 commit comments

Comments
 (0)