serve_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipn
  4. import (
  5. "testing"
  6. "tailscale.com/ipn/ipnstate"
  7. "tailscale.com/tailcfg"
  8. )
  9. func TestCheckFunnelAccess(t *testing.T) {
  10. caps := func(c ...tailcfg.NodeCapability) []tailcfg.NodeCapability { return c }
  11. const portAttr tailcfg.NodeCapability = "https://tailscale.com/cap/funnel-ports?ports=443,8080-8090,8443,"
  12. tests := []struct {
  13. port uint16
  14. caps []tailcfg.NodeCapability
  15. wantErr bool
  16. }{
  17. {443, caps(portAttr), true}, // No "funnel" attribute
  18. {443, caps(portAttr, tailcfg.NodeAttrFunnel), true},
  19. {443, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), false},
  20. {8443, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), false},
  21. {8321, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), true},
  22. {8083, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), false},
  23. {8091, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), true},
  24. {3000, caps(portAttr, tailcfg.CapabilityHTTPS, tailcfg.NodeAttrFunnel), true},
  25. }
  26. for _, tt := range tests {
  27. cm := tailcfg.NodeCapMap{}
  28. for _, c := range tt.caps {
  29. cm[c] = nil
  30. }
  31. err := CheckFunnelAccess(tt.port, &ipnstate.PeerStatus{CapMap: cm})
  32. switch {
  33. case err != nil && tt.wantErr,
  34. err == nil && !tt.wantErr:
  35. continue
  36. case tt.wantErr:
  37. t.Fatalf("got no error, want error")
  38. case !tt.wantErr:
  39. t.Fatalf("got error %v, want no error", err)
  40. }
  41. }
  42. }
  43. func TestHasPathHandler(t *testing.T) {
  44. tests := []struct {
  45. name string
  46. cfg ServeConfig
  47. want bool
  48. }{
  49. {
  50. name: "empty-config",
  51. cfg: ServeConfig{},
  52. want: false,
  53. },
  54. {
  55. name: "with-bg-path-handler",
  56. cfg: ServeConfig{
  57. TCP: map[uint16]*TCPPortHandler{80: {HTTP: true}},
  58. Web: map[HostPort]*WebServerConfig{
  59. "foo.test.ts.net:80": {Handlers: map[string]*HTTPHandler{
  60. "/": {Path: "/tmp"},
  61. }},
  62. },
  63. },
  64. want: true,
  65. },
  66. {
  67. name: "with-fg-path-handler",
  68. cfg: ServeConfig{
  69. TCP: map[uint16]*TCPPortHandler{
  70. 443: {HTTPS: true},
  71. },
  72. Foreground: map[string]*ServeConfig{
  73. "abc123": {
  74. TCP: map[uint16]*TCPPortHandler{80: {HTTP: true}},
  75. Web: map[HostPort]*WebServerConfig{
  76. "foo.test.ts.net:80": {Handlers: map[string]*HTTPHandler{
  77. "/": {Path: "/tmp"},
  78. }},
  79. },
  80. },
  81. },
  82. },
  83. want: true,
  84. },
  85. {
  86. name: "with-no-bg-path-handler",
  87. cfg: ServeConfig{
  88. TCP: map[uint16]*TCPPortHandler{443: {HTTPS: true}},
  89. Web: map[HostPort]*WebServerConfig{
  90. "foo.test.ts.net:443": {Handlers: map[string]*HTTPHandler{
  91. "/": {Proxy: "http://127.0.0.1:3000"},
  92. }},
  93. },
  94. AllowFunnel: map[HostPort]bool{"foo.test.ts.net:443": true},
  95. },
  96. want: false,
  97. },
  98. {
  99. name: "with-no-fg-path-handler",
  100. cfg: ServeConfig{
  101. Foreground: map[string]*ServeConfig{
  102. "abc123": {
  103. TCP: map[uint16]*TCPPortHandler{443: {HTTPS: true}},
  104. Web: map[HostPort]*WebServerConfig{
  105. "foo.test.ts.net:443": {Handlers: map[string]*HTTPHandler{
  106. "/": {Proxy: "http://127.0.0.1:3000"},
  107. }},
  108. },
  109. AllowFunnel: map[HostPort]bool{"foo.test.ts.net:443": true},
  110. },
  111. },
  112. },
  113. want: false,
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. got := tt.cfg.HasPathHandler()
  119. if tt.want != got {
  120. t.Errorf("HasPathHandler() = %v, want %v", got, tt.want)
  121. }
  122. })
  123. }
  124. }