1
0

tun_linux_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 TestValidateTunName(t *testing.T) {
  35. // A device name must be shorter than IFNAMSIZ (i.e. IFNAMSIZ-1 chars max).
  36. maxLenName := strings.Repeat("a", unix.IFNAMSIZ-1)
  37. tests := []struct {
  38. name string
  39. tmpl string
  40. wantErr bool
  41. }{
  42. {"short literal name is fine", "nebula1", false},
  43. {"literal name at the max length is fine", maxLenName, false},
  44. {"literal name at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ), true},
  45. {"trailing template is fine", "nebula%d", false},
  46. {"mid-string template is fine", "neb%dprod", false},
  47. {"leading template is fine", "%dnebula", false},
  48. {"template at the max length is fine", strings.Repeat("a", unix.IFNAMSIZ-3) + "%d", false},
  49. {"template at IFNAMSIZ is rejected", strings.Repeat("a", unix.IFNAMSIZ-2) + "%d", true},
  50. {"bare %d is rejected", "%d", true},
  51. {"multiple %d is rejected", "neb%d%dprod", true},
  52. {"over-long template is rejected", strings.Repeat("a", unix.IFNAMSIZ-1) + "%d", true},
  53. {"over-long mid-string template is rejected", "neb%d" + strings.Repeat("a", unix.IFNAMSIZ-3), true},
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. err := validateTunName(tt.tmpl)
  58. if tt.wantErr && err == nil {
  59. t.Fatalf("expected an error for %q, got none", tt.tmpl)
  60. }
  61. if !tt.wantErr && err != nil {
  62. t.Fatalf("unexpected error for %q: %v", tt.tmpl, err)
  63. }
  64. })
  65. }
  66. }