aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/ipv6_test.go
blob: 3aa6d30a7ea1dbfade32561b7346cfeea2742c8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package proto

import (
	"bytes"
	"encoding/binary"
	"net/netip"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/ClickHouse/ch-go/internal/gold"
)

func TestIPv6_String(t *testing.T) {
	for _, v := range []netip.Addr{
		netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"),
		netip.MustParseAddr("2001:4860:4860::8888"),
	} {
		d := ToIPv6(v)
		require.Equal(t, v.String(), d.String())
	}
}

func IPv6FromInt(v int) IPv6 {
	s := IPv6{}
	binary.BigEndian.PutUint64(s[:], uint64(v))
	return s
}

func TestToIPv6(t *testing.T) {
	v := netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0")
	b := make([]byte, 16)
	binPutIPv6(b, v.As16())
	ip := binIPv6(b)
	require.Equal(t, v, ip.ToIP())
}

func TestColIPv6_NetAddr(t *testing.T) {
	input := []netip.Addr{
		netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"),
		netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:1"),
		netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:2"),
	}
	var d ColIPv6
	for _, v := range input {
		d = append(d, ToIPv6(v))
	}
	var netBuf Buffer
	d.EncodeColumn(&netBuf)
	t.Run("Golden", func(t *testing.T) {
		gold.Bytes(t, netBuf.Buf, "col_ipv6_netaddr")
	})
	t.Run("Decode", func(t *testing.T) {
		br := bytes.NewReader(netBuf.Buf)
		r := NewReader(br)

		var dec ColIPv6
		require.NoError(t, dec.DecodeColumn(r, len(input)))
		var output []netip.Addr
		for _, v := range dec {
			output = append(output, v.ToIP())
		}
		require.Equal(t, input, output)
	})
}