2020-05-07 10:25:58 +02:00
|
|
|
package wg
|
2019-03-28 20:27:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
func Test_State_AssignOverlayAddr(t *testing.T) {
|
2019-03-28 20:27:08 +01:00
|
|
|
type args struct {
|
|
|
|
ipnet *net.IPNet
|
|
|
|
name string
|
|
|
|
}
|
2019-03-29 22:15:17 +01:00
|
|
|
_, ipv4net, _ := net.ParseCIDR("10.0.0.0/8")
|
|
|
|
_, ipv6net, _ := net.ParseCIDR("2001:db8::/32")
|
2019-03-28 20:27:08 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2019-03-29 22:15:17 +01:00
|
|
|
want string
|
2019-03-28 20:27:08 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"assign in big ipv4 net",
|
2019-03-29 22:15:17 +01:00
|
|
|
args{ipv4net, "test"},
|
|
|
|
"10.221.153.165", // if we ever have to change this, we should probably also mark it as a breaking change
|
2019-03-28 20:27:08 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"assign in ipv6 net",
|
2019-03-29 22:15:17 +01:00
|
|
|
args{ipv6net, "test"},
|
|
|
|
"2001:db8:c575:7277:b806:e994:13dd:99a5", // if we ever have to change this, we should probably also mark it as a breaking change
|
2019-03-28 20:27:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-05-07 14:40:22 +02:00
|
|
|
s := &State{}
|
|
|
|
s.AssignOverlayAddr(tt.args.ipnet, tt.args.name)
|
2019-03-28 20:27:08 +01:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
if !reflect.DeepEqual(s.OverlayAddr.IP.String(), tt.want) {
|
|
|
|
t.Errorf("assignOverlayAddr() set = %s, want %s", s.OverlayAddr, tt.want)
|
2019-03-28 20:27:08 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is just to ensure - if we ever change the hashing function - that it spreads the results in a way that at least
|
|
|
|
// avoids the most obvious collisions.
|
2020-05-07 14:40:22 +02:00
|
|
|
func Test_State_AssignOverlayAddr_no_obvious_collisions(t *testing.T) {
|
2019-03-29 22:15:17 +01:00
|
|
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
|
2019-03-28 20:27:08 +01:00
|
|
|
assignments := make(map[string]string)
|
|
|
|
for _, n := range []string{"test", "test1", "test2", "1test", "2test"} {
|
2020-05-07 14:40:22 +02:00
|
|
|
s := &State{}
|
|
|
|
s.AssignOverlayAddr(ipnet, n)
|
|
|
|
if assigned, ok := assignments[s.OverlayAddr.String()]; ok {
|
2019-03-28 20:27:08 +01:00
|
|
|
t.Errorf("IP assignment collision: hash(%s) = hash(%s)", n, assigned)
|
|
|
|
}
|
2020-05-07 14:40:22 +02:00
|
|
|
assignments[s.OverlayAddr.String()] = n
|
2019-03-28 20:27:08 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 20:50:44 +01:00
|
|
|
|
|
|
|
// This should ensure the obvious fact that the same name should map to the same IP if called twice.
|
2020-05-07 14:40:22 +02:00
|
|
|
func Test_State_AssignOverlayAddr_consistent(t *testing.T) {
|
2019-03-29 22:15:17 +01:00
|
|
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
2020-05-07 14:40:22 +02:00
|
|
|
s1 := &State{}
|
|
|
|
s1.AssignOverlayAddr(ipnet, "test")
|
|
|
|
s2 := &State{}
|
|
|
|
s2.AssignOverlayAddr(ipnet, "test")
|
|
|
|
if s1.OverlayAddr.String() != s2.OverlayAddr.String() {
|
|
|
|
t.Errorf("assignOverlayAddr() %s != %s", s1.OverlayAddr, s2.OverlayAddr)
|
2019-03-29 20:50:44 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 22:15:17 +01:00
|
|
|
|
2020-05-07 14:40:22 +02:00
|
|
|
func Test_State_AssignOverlayAddr_repeatable(t *testing.T) {
|
2019-03-29 22:15:17 +01:00
|
|
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
2020-05-07 14:40:22 +02:00
|
|
|
s := &State{}
|
|
|
|
s.AssignOverlayAddr(ipnet, "test")
|
|
|
|
gen1 := s.OverlayAddr.String()
|
|
|
|
s.AssignOverlayAddr(ipnet, "test")
|
|
|
|
gen2 := s.OverlayAddr.String()
|
2019-03-29 22:15:17 +01:00
|
|
|
if gen1 != gen2 {
|
|
|
|
t.Errorf("assignOverlayAddr() %s != %s", gen1, gen2)
|
|
|
|
}
|
|
|
|
}
|