| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //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)
- }
- })
- }
- }
|