frp/test/e2e/basic/basic.go

277 lines
7.4 KiB
Go
Raw Normal View History

2020-09-07 14:57:23 +08:00
package basic
import (
"fmt"
"strings"
"github.com/fatedier/frp/test/e2e/framework"
"github.com/fatedier/frp/test/e2e/framework/consts"
"github.com/fatedier/frp/test/e2e/mock/server"
2021-03-31 16:57:39 +08:00
"github.com/fatedier/frp/test/e2e/pkg/port"
"github.com/fatedier/frp/test/e2e/pkg/request"
2020-09-07 14:57:23 +08:00
. "github.com/onsi/ginkgo"
)
var _ = Describe("[Feature: Basic]", func() {
f := framework.NewDefaultFramework()
Describe("TCP && UDP", func() {
types := []string{"tcp", "udp"}
for _, t := range types {
proxyType := t
It(fmt.Sprintf("Expose a %s echo server", strings.ToUpper(proxyType)), func() {
serverConf := consts.DefaultServerConfig
clientConf := consts.DefaultClientConfig
localPortName := ""
protocol := "tcp"
switch proxyType {
case "tcp":
localPortName = framework.TCPEchoServerPort
protocol = "tcp"
case "udp":
localPortName = framework.UDPEchoServerPort
protocol = "udp"
}
getProxyConf := func(proxyName string, portName string, extra string) string {
return fmt.Sprintf(`
[%s]
type = %s
local_port = {{ .%s }}
remote_port = {{ .%s }}
`+extra, proxyName, proxyType, localPortName, portName)
}
tests := []struct {
proxyName string
portName string
extraConfig string
}{
{
proxyName: "normal",
2021-03-31 16:57:39 +08:00
portName: port.GenName("Normal"),
2020-09-07 14:57:23 +08:00
},
{
proxyName: "with-encryption",
2021-03-31 16:57:39 +08:00
portName: port.GenName("WithEncryption"),
2020-09-07 14:57:23 +08:00
extraConfig: "use_encryption = true",
},
{
proxyName: "with-compression",
2021-03-31 16:57:39 +08:00
portName: port.GenName("WithCompression"),
2020-09-07 14:57:23 +08:00
extraConfig: "use_compression = true",
},
{
proxyName: "with-encryption-and-compression",
2021-03-31 16:57:39 +08:00
portName: port.GenName("WithEncryptionAndCompression"),
2020-09-07 14:57:23 +08:00
extraConfig: `
use_encryption = true
use_compression = true
`,
},
}
// build all client config
for _, test := range tests {
clientConf += getProxyConf(test.proxyName, test.portName, test.extraConfig) + "\n"
}
// run frps and frpc
f.RunProcesses([]string{serverConf}, []string{clientConf})
for _, test := range tests {
2021-03-31 16:57:39 +08:00
framework.NewRequestExpect(f).
RequestModify(framework.SetRequestProtocol(protocol)).
2021-03-31 16:57:39 +08:00
PortName(test.portName).
Explain(test.proxyName).
Ensure()
2020-09-07 14:57:23 +08:00
}
})
}
})
Describe("STCP && SUDP", func() {
types := []string{"stcp", "sudp"}
for _, t := range types {
proxyType := t
It(fmt.Sprintf("Expose echo server with %s", strings.ToUpper(proxyType)), func() {
serverConf := consts.DefaultServerConfig
clientServerConf := consts.DefaultClientConfig
clientVisitorConf := consts.DefaultClientConfig
localPortName := ""
protocol := "tcp"
switch proxyType {
case "stcp":
localPortName = framework.TCPEchoServerPort
protocol = "tcp"
case "sudp":
localPortName = framework.UDPEchoServerPort
protocol = "udp"
}
correctSK := "abc"
wrongSK := "123"
getProxyServerConf := func(proxyName string, extra string) string {
return fmt.Sprintf(`
[%s]
type = %s
role = server
sk = %s
local_port = {{ .%s }}
`+extra, proxyName, proxyType, correctSK, localPortName)
}
getProxyVisitorConf := func(proxyName string, portName, visitorSK, extra string) string {
return fmt.Sprintf(`
[%s]
type = %s
role = visitor
server_name = %s
sk = %s
bind_port = {{ .%s }}
`+extra, proxyName, proxyType, proxyName, visitorSK, portName)
}
tests := []struct {
proxyName string
bindPortName string
visitorSK string
extraConfig string
expectError bool
}{
{
proxyName: "normal",
2021-03-31 16:57:39 +08:00
bindPortName: port.GenName("Normal"),
2020-09-07 14:57:23 +08:00
visitorSK: correctSK,
},
{
proxyName: "with-encryption",
2021-03-31 16:57:39 +08:00
bindPortName: port.GenName("WithEncryption"),
2020-09-07 14:57:23 +08:00
visitorSK: correctSK,
extraConfig: "use_encryption = true",
},
{
proxyName: "with-compression",
2021-03-31 16:57:39 +08:00
bindPortName: port.GenName("WithCompression"),
2020-09-07 14:57:23 +08:00
visitorSK: correctSK,
extraConfig: "use_compression = true",
},
{
proxyName: "with-encryption-and-compression",
2021-03-31 16:57:39 +08:00
bindPortName: port.GenName("WithEncryptionAndCompression"),
2020-09-07 14:57:23 +08:00
visitorSK: correctSK,
extraConfig: `
use_encryption = true
use_compression = true
`,
},
{
proxyName: "with-error-sk",
2021-03-31 16:57:39 +08:00
bindPortName: port.GenName("WithErrorSK"),
2020-09-07 14:57:23 +08:00
visitorSK: wrongSK,
expectError: true,
},
}
// build all client config
for _, test := range tests {
clientServerConf += getProxyServerConf(test.proxyName, test.extraConfig) + "\n"
}
for _, test := range tests {
clientVisitorConf += getProxyVisitorConf(test.proxyName, test.bindPortName, test.visitorSK, test.extraConfig) + "\n"
}
// run frps and frpc
f.RunProcesses([]string{serverConf}, []string{clientServerConf, clientVisitorConf})
for _, test := range tests {
2021-03-31 16:57:39 +08:00
framework.NewRequestExpect(f).
RequestModify(framework.SetRequestProtocol(protocol)).
2021-03-31 16:57:39 +08:00
PortName(test.bindPortName).
Explain(test.proxyName).
ExpectError(test.expectError).
Ensure()
2020-09-07 14:57:23 +08:00
}
})
}
})
2021-03-31 16:57:39 +08:00
Describe("TCPMUX", func() {
It("Type tcpmux", func() {
serverConf := consts.DefaultServerConfig
clientConf := consts.DefaultClientConfig
tcpmuxHTTPConnectPortName := port.GenName("TCPMUX")
serverConf += fmt.Sprintf(`
tcpmux_httpconnect_port = {{ .%s }}
`, tcpmuxHTTPConnectPortName)
getProxyConf := func(proxyName string, extra string) string {
return fmt.Sprintf(`
[%s]
type = tcpmux
multiplexer = httpconnect
local_port = {{ .%s }}
custom_domains = %s
`+extra, proxyName, port.GenName(proxyName), proxyName)
2021-03-31 16:57:39 +08:00
}
tests := []struct {
proxyName string
extraConfig string
}{
{
proxyName: "normal",
},
{
proxyName: "with-encryption",
extraConfig: "use_encryption = true",
},
{
proxyName: "with-compression",
extraConfig: "use_compression = true",
},
{
proxyName: "with-encryption-and-compression",
extraConfig: `
use_encryption = true
use_compression = true
`,
},
}
// build all client config
for _, test := range tests {
clientConf += getProxyConf(test.proxyName, test.extraConfig) + "\n"
localServer := server.New(server.TCP, server.WithBindPort(f.AllocPort()), server.WithRespContent([]byte(test.proxyName)))
f.RunServer(port.GenName(test.proxyName), localServer)
2021-03-31 16:57:39 +08:00
}
2021-03-31 16:57:39 +08:00
// run frps and frpc
f.RunProcesses([]string{serverConf}, []string{clientConf})
// Request without HTTP connect should get error
framework.NewRequestExpect(f).
PortName(tcpmuxHTTPConnectPortName).
ExpectError(true).
Explain("request without HTTP connect expect error").
Ensure()
proxyURL := fmt.Sprintf("http://127.0.0.1:%d", f.PortByName(tcpmuxHTTPConnectPortName))
// Request with incorrect connect hostname
framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
2021-03-31 16:57:39 +08:00
r.Proxy(proxyURL, "invalid")
}).ExpectError(true).Explain("request without HTTP connect expect error").Ensure()
// Request with correct connect hostname
for _, test := range tests {
framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
2021-03-31 16:57:39 +08:00
r.Proxy(proxyURL, test.proxyName)
}).ExpectResp([]byte(test.proxyName)).Explain(test.proxyName).Ensure()
2021-03-31 16:57:39 +08:00
}
})
})
2020-09-07 14:57:23 +08:00
})