mirror of
https://github.com/slackhq/nebula.git
synced 2026-08-15 09:27:01 +02:00
128 lines
4.0 KiB
Go
128 lines
4.0 KiB
Go
//go:build !e2e_testing
|
|
// +build !e2e_testing
|
|
|
|
package overlay
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
var runAdvMSSTests = []struct {
|
|
name string
|
|
tun *tun
|
|
r Route
|
|
expected int
|
|
}{
|
|
// Standard case, default MTU is the device max MTU
|
|
{"default", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0},
|
|
{"default-min", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0},
|
|
{"default-low", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160},
|
|
|
|
// Case where we have a route MTU set higher than the default
|
|
{"route", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400},
|
|
{"route-min", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400},
|
|
{"route-high", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0},
|
|
}
|
|
|
|
func TestTunAdvMSS(t *testing.T) {
|
|
for _, tt := range runAdvMSSTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
o := tt.tun.advMSS(tt.r)
|
|
if o != tt.expected {
|
|
t.Errorf("got %d, want %d", o, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func nameSet(names ...string) map[string]struct{} {
|
|
used := make(map[string]struct{}, len(names))
|
|
for _, n := range names {
|
|
used[n] = struct{}{}
|
|
}
|
|
return used
|
|
}
|
|
|
|
func TestValidateTunName(t *testing.T) {
|
|
// A device name must be shorter than IFNAMSIZ (i.e. IFNAMSIZ-1 chars max).
|
|
maxLenName := strings.Repeat("a", unix.IFNAMSIZ-1)
|
|
|
|
tests := []struct {
|
|
name string
|
|
tmpl string
|
|
wantErr bool
|
|
}{
|
|
{"short literal name is fine", "nebula1", false},
|
|
{"literal name at the max length is fine", maxLenName, false},
|
|
{"literal name at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ), true},
|
|
{"trailing template is fine", "nebula%d", false},
|
|
{"mid-string template is fine", "neb%dprod", false},
|
|
{"leading template is fine", "%dnebula", false},
|
|
{"template at the max static length is fine", strings.Repeat("a", unix.IFNAMSIZ-2) + "%d", false},
|
|
{"bare %d is rejected", "%d", true},
|
|
{"multiple %d is rejected", "neb%d%dprod", true},
|
|
{"template with no room for a digit is rejected", strings.Repeat("a", unix.IFNAMSIZ-1) + "%d", true},
|
|
{"mid-string template with no room for a digit is rejected", "neb%d" + strings.Repeat("a", unix.IFNAMSIZ-3), true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validateTunName(tt.tmpl)
|
|
if tt.wantErr && err == nil {
|
|
t.Fatalf("expected an error for %q, got none", tt.tmpl)
|
|
}
|
|
if !tt.wantErr && err != nil {
|
|
t.Fatalf("unexpected error for %q: %v", tt.tmpl, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNextTunName(t *testing.T) {
|
|
// A prefix long enough that only single-digit suffixes (0-9) fit within
|
|
// IFNAMSIZ, so marking all ten used exercises running out of names.
|
|
longPrefix := strings.Repeat("a", unix.IFNAMSIZ-2)
|
|
longUsed := make([]string, 0, 10)
|
|
for i := 0; i < 10; i++ {
|
|
longUsed = append(longUsed, longPrefix+string(rune('0'+i)))
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
tmpl string
|
|
used map[string]struct{}
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{"nothing used picks zero", "nebula%d", nil, "nebula0", false},
|
|
{"skips taken names", "nebula%d", nameSet("nebula0", "nebula1"), "nebula2", false},
|
|
{"picks the lowest free index", "nebula%d", nameSet("nebula0", "nebula2"), "nebula1", false},
|
|
{"ignores unrelated names", "nebula%d", nameSet("eth0", "tun5"), "nebula0", false},
|
|
{"mid-string placeholder picks zero", "neb%dprod", nil, "neb0prod", false},
|
|
{"mid-string placeholder skips taken", "neb%dprod", nameSet("neb0prod", "neb1prod"), "neb2prod", false},
|
|
{"leading placeholder picks zero", "%dnebula", nameSet("tun0"), "0nebula", false},
|
|
{"runs out of names within IFNAMSIZ", longPrefix + "%d", nameSet(longUsed...), "", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := nextTunName(tt.tmpl, tt.used)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected an error, got name %q", got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("got %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|