From 8f513c82ddc9b95788451827080a9f42817d124d Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk <daniel@dan-nrw.de> Date: Sun, 1 Jul 2018 16:18:38 +0200 Subject: [PATCH] implemented BitAtPosition for IPv6 --- net/ip.go | 10 +++++++++- net/ip_test.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/net/ip.go b/net/ip.go index 90628ece..8a532a2c 100644 --- a/net/ip.go +++ b/net/ip.go @@ -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 } diff --git a/net/ip_test.go b/net/ip_test.go index f1edd468..03f78ec9 100644 --- a/net/ip_test.go +++ b/net/ip_test.go @@ -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 { -- GitLab