Skip to content

Commit

Permalink
Merge pull request #803 from Icinga/TestAddressBin
Browse files Browse the repository at this point in the history
Test AddressBin
  • Loading branch information
julianbrost committed Sep 9, 2024
2 parents fb4c64f + eda6366 commit 1092ba4
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/icingadb/v1/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v1

import (
"database/sql/driver"
"github.com/stretchr/testify/require"
"testing"
)

func TestAddressBin_Value(t *testing.T) {
subtests := []struct {
name string
input *Host
output driver.Value
}{
{name: "nil-host"},
{name: "invalid-address", input: &Host{Address: "invalid"}},
{name: "IPv6", input: &Host{Address: "2001:db8::"}},
{name: "IPv4", input: &Host{Address: "192.0.2.0"}, output: []byte{192, 0, 2, 0}},
{name: "ffff-IPv4", input: &Host{Address: "::ffff:192.0.2.0"}, output: []byte{192, 0, 2, 0}},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
v, err := (AddressBin{Host: st.input}).Value()

require.NoError(t, err)
require.Equal(t, st.output, v)
})
}
}

func TestAddress6Bin_Value(t *testing.T) {
ffff192020 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 0, 2, 0}

subtests := []struct {
name string
input *Host
output driver.Value
}{
{name: "nil-host"},
{name: "invalid-address", input: &Host{Address: "invalid"}},
{"IPv6", &Host{Address6: "2001:db8::"}, []byte{32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{name: "IPv4", input: &Host{Address6: "192.0.2.0"}, output: ffff192020},
{name: "ffff-IPv4", input: &Host{Address6: "::ffff:192.0.2.0"}, output: ffff192020},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
v, err := (Address6Bin{Host: st.input}).Value()

require.NoError(t, err)
require.Equal(t, st.output, v)
})
}
}

0 comments on commit 1092ba4

Please sign in to comment.