mirror of
https://github.com/fatedier/frp.git
synced 2026-03-30 21:59:15 +08:00
test/e2e: allocate dynamic ports outside whitelist ranges in server whitelist test (#5268)
This commit is contained in:
@@ -241,6 +241,31 @@ func (f *Framework) AllocPort() int {
|
||||
return port
|
||||
}
|
||||
|
||||
func (f *Framework) AllocPortExcludingRanges(ranges ...[2]int) int {
|
||||
for range 1000 {
|
||||
port := f.portAllocator.Get()
|
||||
ExpectTrue(port > 0, "alloc port failed")
|
||||
|
||||
inExcludedRange := false
|
||||
for _, portRange := range ranges {
|
||||
if port >= portRange[0] && port <= portRange[1] {
|
||||
inExcludedRange = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if inExcludedRange {
|
||||
f.portAllocator.Release(port)
|
||||
continue
|
||||
}
|
||||
|
||||
f.allocatedPorts = append(f.allocatedPorts, port)
|
||||
return port
|
||||
}
|
||||
|
||||
Failf("alloc port outside excluded ranges failed")
|
||||
return 0
|
||||
}
|
||||
|
||||
func (f *Framework) ReleasePort(port int) {
|
||||
f.portAllocator.Release(port)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
ginkgo.It("Ports Whitelist", func() {
|
||||
serverConf := consts.LegacyDefaultServerConfig
|
||||
clientConf := consts.LegacyDefaultClientConfig
|
||||
tcpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000})
|
||||
udpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000})
|
||||
|
||||
serverConf += `
|
||||
allow_ports = 10000-11000,11002,12000-13000
|
||||
@@ -37,8 +39,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
[tcp-port-not-allowed]
|
||||
type = tcp
|
||||
local_port = {{ .%s }}
|
||||
remote_port = 11001
|
||||
`, framework.TCPEchoServerPort)
|
||||
remote_port = %d
|
||||
`, framework.TCPEchoServerPort, tcpPortNotAllowed)
|
||||
clientConf += fmt.Sprintf(`
|
||||
[tcp-port-unavailable]
|
||||
type = tcp
|
||||
@@ -55,8 +57,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
[udp-port-not-allowed]
|
||||
type = udp
|
||||
local_port = {{ .%s }}
|
||||
remote_port = 11003
|
||||
`, framework.UDPEchoServerPort)
|
||||
remote_port = %d
|
||||
`, framework.UDPEchoServerPort, udpPortNotAllowed)
|
||||
|
||||
f.RunProcesses(serverConf, []string{clientConf})
|
||||
|
||||
@@ -65,7 +67,7 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
framework.NewRequestExpect(f).PortName(tcpPortName).Ensure()
|
||||
|
||||
// Not Allowed
|
||||
framework.NewRequestExpect(f).Port(11001).ExpectError(true).Ensure()
|
||||
framework.NewRequestExpect(f).Port(tcpPortNotAllowed).ExpectError(true).Ensure()
|
||||
|
||||
// Unavailable, already bind by frps
|
||||
framework.NewRequestExpect(f).PortName(consts.PortServerName).ExpectError(true).Ensure()
|
||||
@@ -76,7 +78,7 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
|
||||
// Not Allowed
|
||||
framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
|
||||
r.UDP().Port(11003)
|
||||
r.UDP().Port(udpPortNotAllowed)
|
||||
}).ExpectError(true).Ensure()
|
||||
})
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
ginkgo.It("Ports Whitelist", func() {
|
||||
serverConf := consts.DefaultServerConfig
|
||||
clientConf := consts.DefaultClientConfig
|
||||
tcpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000})
|
||||
udpPortNotAllowed := f.AllocPortExcludingRanges([2]int{10000, 11000}, [2]int{11002, 11002}, [2]int{12000, 13000})
|
||||
|
||||
serverConf += `
|
||||
allowPorts = [
|
||||
@@ -43,8 +45,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
name = "tcp-port-not-allowed"
|
||||
type = "tcp"
|
||||
localPort = {{ .%s }}
|
||||
remotePort = 11001
|
||||
`, framework.TCPEchoServerPort)
|
||||
remotePort = %d
|
||||
`, framework.TCPEchoServerPort, tcpPortNotAllowed)
|
||||
clientConf += fmt.Sprintf(`
|
||||
[[proxies]]
|
||||
name = "tcp-port-unavailable"
|
||||
@@ -64,8 +66,8 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
name = "udp-port-not-allowed"
|
||||
type = "udp"
|
||||
localPort = {{ .%s }}
|
||||
remotePort = 11003
|
||||
`, framework.UDPEchoServerPort)
|
||||
remotePort = %d
|
||||
`, framework.UDPEchoServerPort, udpPortNotAllowed)
|
||||
|
||||
f.RunProcesses(serverConf, []string{clientConf})
|
||||
|
||||
@@ -74,7 +76,7 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
framework.NewRequestExpect(f).PortName(tcpPortName).Ensure()
|
||||
|
||||
// Not Allowed
|
||||
framework.NewRequestExpect(f).Port(11001).ExpectError(true).Ensure()
|
||||
framework.NewRequestExpect(f).Port(tcpPortNotAllowed).ExpectError(true).Ensure()
|
||||
|
||||
// Unavailable, already bind by frps
|
||||
framework.NewRequestExpect(f).PortName(consts.PortServerName).ExpectError(true).Ensure()
|
||||
@@ -85,7 +87,7 @@ var _ = ginkgo.Describe("[Feature: Server Manager]", func() {
|
||||
|
||||
// Not Allowed
|
||||
framework.NewRequestExpect(f).RequestModify(func(r *request.Request) {
|
||||
r.UDP().Port(11003)
|
||||
r.UDP().Port(udpPortNotAllowed)
|
||||
}).ExpectError(true).Ensure()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user