Skip to content

Commit

Permalink
Merge pull request #84 from bschaatsbergen/c/improve-netmask-funcs
Browse files Browse the repository at this point in the history
chore: improve the netmask function by separating it
  • Loading branch information
bschaatsbergen authored Dec 10, 2023
2 parents 46aff11 + ad54975 commit 869bd45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cmd/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func getNetworkDetails(network *net.IPNet) *networkDetailsToDisplay {
}

// Obtain the netmask and prefix length.
details.Netmask = core.GetNetMask(network)
netmask := core.GetNetmask(network)
// A human-readable representation of the netmask is displayed in the output.
details.Netmask = core.NetMaskToIPAddress(netmask)
details.PrefixLength = core.GetPrefixLength(details.Netmask)

// Obtain the base address of the network.
Expand Down
13 changes: 9 additions & 4 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ func Overlaps(network1, network2 *net.IPNet) bool {
return network1.Contains(network2.IP) || network2.Contains(network1.IP)
}

// GetNetMask returns the netmask of the given IP network.
func GetNetMask(network *net.IPNet) net.IP {
netMask := net.IP(network.Mask)
return netMask
// GetNetmask retrieves the netmask associated with the provided IP network.
func GetNetmask(network *net.IPNet) net.IPMask {
return network.Mask
}

// NetMaskToIPAddress converts a netmask represented as a sequence of bytes
// to its corresponding IP address representation.
func NetMaskToIPAddress(netmask net.IPMask) net.IP {
return net.IP(netmask)
}

// GetPrefixLength returns the prefix length from the given netmask.
Expand Down
50 changes: 46 additions & 4 deletions pkg/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,71 @@ func TestGetPrefixLength(t *testing.T) {

tests := []struct {
name string
netMask net.IP
netmask net.IP
expectedPrefixLength int
}{
{
name: "Get the prefix length of an IPv4 netmask",
netMask: net.IP(IPv4CIDR.Mask),
netmask: net.IP(IPv4CIDR.Mask),
expectedPrefixLength: 16,
},
{
name: "Get the prefix length of an IPv6 netmask",
netMask: net.IP(IPv6CIDR.Mask),
netmask: net.IP(IPv6CIDR.Mask),
expectedPrefixLength: 106,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prefixLength := core.GetPrefixLength(tt.netMask)
prefixLength := core.GetPrefixLength(tt.netmask)
assert.Equal(t, tt.expectedPrefixLength, prefixLength, "Prefix length is not correct")
})
}
}

func TestGetNetMask(t *testing.T) {
IPv4CIDR, err := core.ParseCIDR("10.0.0.0/16")
if err != nil {
t.Log(err)
t.Fail()
}

IPv6CIDR, err := core.ParseCIDR("2001:db8:1234:1a00::/106")
if err != nil {
t.Log(err)
t.Fail()
}

tests := []struct {
name string
CIDR *net.IPNet
expectedNetmask net.IPMask
}{
{
name: "Get the netmask of an IPv4 CIDR range",
CIDR: IPv4CIDR,
expectedNetmask: net.IPMask{0xff, 0xff, 0x00, 0x00},
},
{
name: "Get the netmask of an IPv6 CIDR range",
CIDR: IPv6CIDR,
expectedNetmask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x0, 0x0},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
netmask := core.GetNetmask(tt.CIDR)
if err != nil {
t.Log(err)
t.Fail()
}
assert.Equal(t, tt.expectedNetmask, netmask, "Netmask is not correct")
})
}
}

func TestGetFirstUsableIPAddress(t *testing.T) {
IPv4CIDR, err := core.ParseCIDR("10.0.0.0/16")
if err != nil {
Expand Down

0 comments on commit 869bd45

Please sign in to comment.