Skip to content
Snippets Groups Projects
Commit 8f513c82 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

implemented BitAtPosition for IPv6

parent 0b259e59
No related branches found
No related tags found
No related merge requests found
......@@ -182,5 +182,13 @@ func (ip IP) bitAtPositionIPv4(pos uint8) bool {
}
func (ip IP) bitAtPositionIPv6(pos uint8) bool {
panic("No IPv6 support yet!")
if pos > 128 {
return false
}
if pos <= 64 {
return (ip.higher & (1 << (64 - pos))) != 0
}
return (ip.lower & (1 << (128 - pos))) != 0
}
......@@ -313,17 +313,41 @@ func TestBitAtPosition(t *testing.T) {
expected bool
}{
{
name: "Bit 8 from 1.0.0.0 -> 0",
name: "IPv4: Bit 8 from 1.0.0.0 -> 0",
input: IPv4FromOctets(10, 0, 0, 0),
position: 8,
expected: false,
},
{
name: "Bit 8 from 11.0.0.0 -> 1",
name: "IPv4: Bit 8 from 11.0.0.0 -> 1",
input: IPv4FromOctets(11, 0, 0, 0),
position: 8,
expected: true,
},
{
name: "IPv6: Bit 16 from 2001:678:1e0:: -> 1",
input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
position: 16,
expected: true,
},
{
name: "IPv6: Bit 17 from 2001:678:1e0:: -> 0",
input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0),
position: 17,
expected: false,
},
{
name: "IPv6: Bit 113 from 2001:678:1e0::cafe -> 1",
input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0xcafe),
position: 113,
expected: true,
},
{
name: "IPv6: Bit 115 from 2001:678:1e0::cafe -> 0",
input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0xcafe),
position: 115,
expected: false,
},
}
for _, test := range tests {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment