server_test.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package main
  4. import (
  5. "net/netip"
  6. "testing"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/google/go-cmp/cmp/cmpopts"
  9. "tailscale.com/tailcfg"
  10. "tailscale.com/types/appctype"
  11. )
  12. func TestMakeConnectorsFromConfig(t *testing.T) {
  13. tcs := []struct {
  14. name string
  15. input *appctype.AppConnectorConfig
  16. want map[appctype.ConfigID]connector
  17. }{
  18. {
  19. "empty",
  20. &appctype.AppConnectorConfig{},
  21. nil,
  22. },
  23. {
  24. "DNAT",
  25. &appctype.AppConnectorConfig{
  26. DNAT: map[appctype.ConfigID]appctype.DNATConfig{
  27. "swiggity_swooty": {
  28. Addrs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")},
  29. To: []string{"example.org"},
  30. IP: []tailcfg.ProtoPortRange{{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}}},
  31. },
  32. },
  33. },
  34. map[appctype.ConfigID]connector{
  35. "swiggity_swooty": {
  36. Handlers: map[target]handler{
  37. {
  38. Dest: netip.MustParsePrefix("100.64.0.1/32"),
  39. Matching: tailcfg.ProtoPortRange{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}},
  40. }: &tcpRoundRobinHandler{To: []string{"example.org"}, ReachableIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")}},
  41. {
  42. Dest: netip.MustParsePrefix("fd7a:115c:a1e0::1/128"),
  43. Matching: tailcfg.ProtoPortRange{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}},
  44. }: &tcpRoundRobinHandler{To: []string{"example.org"}, ReachableIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")}},
  45. },
  46. },
  47. },
  48. },
  49. {
  50. "SNIProxy",
  51. &appctype.AppConnectorConfig{
  52. SNIProxy: map[appctype.ConfigID]appctype.SNIProxyConfig{
  53. "swiggity_swooty": {
  54. Addrs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")},
  55. AllowedDomains: []string{"example.org"},
  56. IP: []tailcfg.ProtoPortRange{{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}}},
  57. },
  58. },
  59. },
  60. map[appctype.ConfigID]connector{
  61. "swiggity_swooty": {
  62. Handlers: map[target]handler{
  63. {
  64. Dest: netip.MustParsePrefix("100.64.0.1/32"),
  65. Matching: tailcfg.ProtoPortRange{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}},
  66. }: &tcpSNIHandler{Allowlist: []string{"example.org"}, ReachableIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")}},
  67. {
  68. Dest: netip.MustParsePrefix("fd7a:115c:a1e0::1/128"),
  69. Matching: tailcfg.ProtoPortRange{Proto: 0, Ports: tailcfg.PortRange{First: 0, Last: 65535}},
  70. }: &tcpSNIHandler{Allowlist: []string{"example.org"}, ReachableIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1"), netip.MustParseAddr("fd7a:115c:a1e0::1")}},
  71. },
  72. },
  73. },
  74. },
  75. }
  76. for _, tc := range tcs {
  77. t.Run(tc.name, func(t *testing.T) {
  78. connectors := makeConnectorsFromConfig(tc.input)
  79. if diff := cmp.Diff(connectors, tc.want,
  80. cmpopts.IgnoreFields(tcpRoundRobinHandler{}, "DialContext"),
  81. cmpopts.IgnoreFields(tcpSNIHandler{}, "DialContext"),
  82. cmp.Comparer(func(x, y netip.Addr) bool {
  83. return x == y
  84. })); diff != "" {
  85. t.Fatalf("mismatch (-want +got):\n%s", diff)
  86. }
  87. })
  88. }
  89. }