diff --git a/cmd/explain.go b/cmd/explain.go index 124ef15..c7518b3 100644 --- a/cmd/explain.go +++ b/cmd/explain.go @@ -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. diff --git a/pkg/core/core.go b/pkg/core/core.go index 9afab2b..5c6d8e4 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -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. diff --git a/pkg/core/core_test.go b/pkg/core/core_test.go index 6f46791..f782d20 100644 --- a/pkg/core/core_test.go +++ b/pkg/core/core_test.go @@ -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 {