1
0

tun_linux_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //go:build !e2e_testing
  2. // +build !e2e_testing
  3. package overlay
  4. import (
  5. "strings"
  6. "testing"
  7. "golang.org/x/sys/unix"
  8. )
  9. var runAdvMSSTests = []struct {
  10. name string
  11. tun *tun
  12. r Route
  13. expected int
  14. }{
  15. // Standard case, default MTU is the device max MTU
  16. {"default", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{}, 0},
  17. {"default-min", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1440}, 0},
  18. {"default-low", &tun{DefaultMTU: 1440, MaxMTU: 1440}, Route{MTU: 1200}, 1160},
  19. // Case where we have a route MTU set higher than the default
  20. {"route", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{}, 1400},
  21. {"route-min", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 1440}, 1400},
  22. {"route-high", &tun{DefaultMTU: 1440, MaxMTU: 8941}, Route{MTU: 8941}, 0},
  23. }
  24. func TestTunAdvMSS(t *testing.T) {
  25. for _, tt := range runAdvMSSTests {
  26. t.Run(tt.name, func(t *testing.T) {
  27. o := tt.tun.advMSS(tt.r)
  28. if o != tt.expected {
  29. t.Errorf("got %d, want %d", o, tt.expected)
  30. }
  31. })
  32. }
  33. }
  34. func nameSet(names ...string) map[string]struct{} {
  35. used := make(map[string]struct{}, len(names))
  36. for _, n := range names {
  37. used[n] = struct{}{}
  38. }
  39. return used
  40. }
  41. func TestValidateTunName(t *testing.T) {
  42. // A device name must be shorter than IFNAMSIZ (i.e. IFNAMSIZ-1 chars max).
  43. maxLenName := strings.Repeat("a", unix.IFNAMSIZ-1)
  44. tests := []struct {
  45. name string
  46. tmpl string
  47. wantErr bool
  48. }{
  49. {"short literal name is fine", "nebula1", false},
  50. {"literal name at the max length is fine", maxLenName, false},
  51. {"literal name at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ), true},
  52. {"trailing template is fine", "nebula%d", false},
  53. {"mid-string template is fine", "neb%dprod", false},
  54. {"leading template is fine", "%dnebula", false},
  55. {"template at the max static length is fine", strings.Repeat("a", unix.IFNAMSIZ-2) + "%d", false},
  56. {"bare %d is rejected", "%d", true},
  57. {"multiple %d is rejected", "neb%d%dprod", true},
  58. {"template with no room for a digit is rejected", strings.Repeat("a", unix.IFNAMSIZ-1) + "%d", true},
  59. {"mid-string template with no room for a digit is rejected", "neb%d" + strings.Repeat("a", unix.IFNAMSIZ-3), true},
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. err := validateTunName(tt.tmpl)
  64. if tt.wantErr && err == nil {
  65. t.Fatalf("expected an error for %q, got none", tt.tmpl)
  66. }
  67. if !tt.wantErr && err != nil {
  68. t.Fatalf("unexpected error for %q: %v", tt.tmpl, err)
  69. }
  70. })
  71. }
  72. }
  73. func TestNextTunName(t *testing.T) {
  74. // A prefix long enough that only single-digit suffixes (0-9) fit within
  75. // IFNAMSIZ, so marking all ten used exercises running out of names.
  76. longPrefix := strings.Repeat("a", unix.IFNAMSIZ-2)
  77. longUsed := make([]string, 0, 10)
  78. for i := 0; i < 10; i++ {
  79. longUsed = append(longUsed, longPrefix+string(rune('0'+i)))
  80. }
  81. tests := []struct {
  82. name string
  83. tmpl string
  84. used map[string]struct{}
  85. want string
  86. wantErr bool
  87. }{
  88. {"nothing used picks zero", "nebula%d", nil, "nebula0", false},
  89. {"skips taken names", "nebula%d", nameSet("nebula0", "nebula1"), "nebula2", false},
  90. {"picks the lowest free index", "nebula%d", nameSet("nebula0", "nebula2"), "nebula1", false},
  91. {"ignores unrelated names", "nebula%d", nameSet("eth0", "tun5"), "nebula0", false},
  92. {"mid-string placeholder picks zero", "neb%dprod", nil, "neb0prod", false},
  93. {"mid-string placeholder skips taken", "neb%dprod", nameSet("neb0prod", "neb1prod"), "neb2prod", false},
  94. {"leading placeholder picks zero", "%dnebula", nameSet("tun0"), "0nebula", false},
  95. {"runs out of names within IFNAMSIZ", longPrefix + "%d", nameSet(longUsed...), "", true},
  96. }
  97. for _, tt := range tests {
  98. t.Run(tt.name, func(t *testing.T) {
  99. got, err := nextTunName(tt.tmpl, tt.used)
  100. if tt.wantErr {
  101. if err == nil {
  102. t.Fatalf("expected an error, got name %q", got)
  103. }
  104. return
  105. }
  106. if err != nil {
  107. t.Fatalf("unexpected error: %v", err)
  108. }
  109. if got != tt.want {
  110. t.Errorf("got %q, want %q", got, tt.want)
  111. }
  112. })
  113. }
  114. }