Skip to content

Commit

Permalink
Fix negative shift amount during prefix usage calc (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Apr 19, 2023
1 parent d01b55e commit df57e6c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
6 changes: 5 additions & 1 deletion prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,12 @@ func (p *Prefix) availablePrefixes() (uint64, []string) {
totalAvailable := uint64(0)
availablePrefixes := []string{}
for _, pfx := range pfxs {
bits := maxBits - pfx.Bits()
if bits < 0 {
continue
}
// same as: totalAvailable += uint64(math.Pow(float64(2), float64(maxBits-pfx.Bits)))
totalAvailable += 1 << (maxBits - pfx.Bits())
totalAvailable += 1 << bits
availablePrefixes = append(availablePrefixes, pfx.String())
}
// we are not reporting more that 2^31 available prefixes
Expand Down
81 changes: 81 additions & 0 deletions prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -1644,3 +1645,83 @@ func TestNamespaceFromContext(t *testing.T) {
})
}
}
func TestAvailablePrefixes(t *testing.T) {
testCases := []struct {
name string
cidr string
expectedTotal uint64
expectedAvailablePfx []string
}{
{
name: "192.168.0.0/32",
cidr: "192.168.0.0/32",
expectedTotal: 0,
expectedAvailablePfx: []string{},
},
{
name: "192.168.0.0/31",
cidr: "192.168.0.0/31",
expectedTotal: 0,
expectedAvailablePfx: []string{},
},
{
name: "192.168.0.0/30",
cidr: "192.168.0.0/30",
expectedTotal: 1,
expectedAvailablePfx: []string{"192.168.0.0/30"},
},
{
name: "192.168.0.0/24",
cidr: "192.168.0.0/24",
expectedTotal: 64,
expectedAvailablePfx: []string{"192.168.0.0/24"},
},
{
name: "2001:0db8:85a3::/128",
cidr: "2001:0db8:85a3::/128",
expectedTotal: 0,
expectedAvailablePfx: []string{},
},
{
name: "2001:0db8:85a3::/127",
cidr: "2001:0db8:85a3::/127",
expectedTotal: 0,
expectedAvailablePfx: []string{},
},
{
name: "2001:0db8:85a3::/126",
cidr: "2001:0db8:85a3::/126",
expectedTotal: 1,
expectedAvailablePfx: []string{"2001:db8:85a3::/126"},
},
{
name: "Invalid CIDR",
cidr: "Invalid CIDR",
expectedTotal: 0,
expectedAvailablePfx: []string{},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
prefix := &Prefix{
Cidr: tc.cidr,
isParent: false,
availableChildPrefixes: make(map[string]bool),
}

totalAvailable, availablePrefixes := prefix.availablePrefixes()

assert.Equal(
t, tc.expectedTotal, totalAvailable,
"Expected totalAvailable: %d, got: %d",
tc.expectedTotal, totalAvailable,
)
assert.ElementsMatchf(
t, availablePrefixes, tc.expectedAvailablePfx,
"Expected availablePrefixes: %v, got: %v",
tc.expectedAvailablePfx, availablePrefixes,
)
})
}
}

0 comments on commit df57e6c

Please sign in to comment.