basic.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package basic
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/fatedier/frp/test/e2e/framework"
  7. "github.com/fatedier/frp/test/e2e/framework/consts"
  8. . "github.com/onsi/ginkgo"
  9. )
  10. var connTimeout = 2 * time.Second
  11. var _ = Describe("[Feature: Basic]", func() {
  12. f := framework.NewDefaultFramework()
  13. Describe("TCP && UDP", func() {
  14. types := []string{"tcp", "udp"}
  15. for _, t := range types {
  16. proxyType := t
  17. It(fmt.Sprintf("Expose a %s echo server", strings.ToUpper(proxyType)), func() {
  18. serverConf := consts.DefaultServerConfig
  19. clientConf := consts.DefaultClientConfig
  20. localPortName := ""
  21. protocol := "tcp"
  22. switch proxyType {
  23. case "tcp":
  24. localPortName = framework.TCPEchoServerPort
  25. protocol = "tcp"
  26. case "udp":
  27. localPortName = framework.UDPEchoServerPort
  28. protocol = "udp"
  29. }
  30. getProxyConf := func(proxyName string, portName string, extra string) string {
  31. return fmt.Sprintf(`
  32. [%s]
  33. type = %s
  34. local_port = {{ .%s }}
  35. remote_port = {{ .%s }}
  36. `+extra, proxyName, proxyType, localPortName, portName)
  37. }
  38. tests := []struct {
  39. proxyName string
  40. portName string
  41. extraConfig string
  42. }{
  43. {
  44. proxyName: "normal",
  45. portName: framework.GenPortName("Normal"),
  46. },
  47. {
  48. proxyName: "with-encryption",
  49. portName: framework.GenPortName("WithEncryption"),
  50. extraConfig: "use_encryption = true",
  51. },
  52. {
  53. proxyName: "with-compression",
  54. portName: framework.GenPortName("WithCompression"),
  55. extraConfig: "use_compression = true",
  56. },
  57. {
  58. proxyName: "with-encryption-and-compression",
  59. portName: framework.GenPortName("WithEncryptionAndCompression"),
  60. extraConfig: `
  61. use_encryption = true
  62. use_compression = true
  63. `,
  64. },
  65. }
  66. // build all client config
  67. for _, test := range tests {
  68. clientConf += getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n"
  69. }
  70. // run frps and frpc
  71. f.RunProcesses([]string{serverConf}, []string{clientConf})
  72. for _, test := range tests {
  73. framework.ExpectRequest(protocol, f.UsedPorts[test.portName],
  74. []byte(consts.TestString), []byte(consts.TestString), connTimeout, test.proxyName)
  75. }
  76. })
  77. }
  78. })
  79. Describe("STCP && SUDP", func() {
  80. types := []string{"stcp", "sudp"}
  81. for _, t := range types {
  82. proxyType := t
  83. It(fmt.Sprintf("Expose echo server with %s", strings.ToUpper(proxyType)), func() {
  84. serverConf := consts.DefaultServerConfig
  85. clientServerConf := consts.DefaultClientConfig
  86. clientVisitorConf := consts.DefaultClientConfig
  87. localPortName := ""
  88. protocol := "tcp"
  89. switch proxyType {
  90. case "stcp":
  91. localPortName = framework.TCPEchoServerPort
  92. protocol = "tcp"
  93. case "sudp":
  94. localPortName = framework.UDPEchoServerPort
  95. protocol = "udp"
  96. }
  97. correctSK := "abc"
  98. wrongSK := "123"
  99. getProxyServerConf := func(proxyName string, extra string) string {
  100. return fmt.Sprintf(`
  101. [%s]
  102. type = %s
  103. role = server
  104. sk = %s
  105. local_port = {{ .%s }}
  106. `+extra, proxyName, proxyType, correctSK, localPortName)
  107. }
  108. getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string {
  109. return fmt.Sprintf(`
  110. [%s]
  111. type = %s
  112. role = visitor
  113. server_name = %s
  114. sk = %s
  115. bind_port = {{ .%s }}
  116. `+extra, proxyName, proxyType, proxyName, visitorSK, portName)
  117. }
  118. tests := []struct {
  119. proxyName string
  120. bindPortName string
  121. visitorSK string
  122. extraConfig string
  123. expectError bool
  124. }{
  125. {
  126. proxyName: "normal",
  127. bindPortName: framework.GenPortName("Normal"),
  128. visitorSK: correctSK,
  129. },
  130. {
  131. proxyName: "with-encryption",
  132. bindPortName: framework.GenPortName("WithEncryption"),
  133. visitorSK: correctSK,
  134. extraConfig: "use_encryption = true",
  135. },
  136. {
  137. proxyName: "with-compression",
  138. bindPortName: framework.GenPortName("WithCompression"),
  139. visitorSK: correctSK,
  140. extraConfig: "use_compression = true",
  141. },
  142. {
  143. proxyName: "with-encryption-and-compression",
  144. bindPortName: framework.GenPortName("WithEncryptionAndCompression"),
  145. visitorSK: correctSK,
  146. extraConfig: `
  147. use_encryption = true
  148. use_compression = true
  149. `,
  150. },
  151. {
  152. proxyName: "with-error-sk",
  153. bindPortName: framework.GenPortName("WithErrorSK"),
  154. visitorSK: wrongSK,
  155. expectError: true,
  156. },
  157. }
  158. // build all client config
  159. for _, test := range tests {
  160. clientServerConf += getProxyServerConf(test.proxyName, test.extraConfig) + "\n"
  161. }
  162. for _, test := range tests {
  163. clientVisitorConf += getProxyVisitorConf(test.proxyName, test.bindPortName, test.visitorSK, test.extraConfig) + "\n"
  164. }
  165. // run frps and frpc
  166. f.RunProcesses([]string{serverConf}, []string{clientServerConf, clientVisitorConf})
  167. for _, test := range tests {
  168. expectResp := []byte(consts.TestString)
  169. if test.expectError {
  170. framework.ExpectRequestError(protocol, f.UsedPorts[test.bindPortName],
  171. []byte(consts.TestString), connTimeout, test.proxyName)
  172. continue
  173. }
  174. framework.ExpectRequest(protocol, f.UsedPorts[test.bindPortName],
  175. []byte(consts.TestString), expectResp, connTimeout, test.proxyName)
  176. }
  177. })
  178. }
  179. })
  180. })