| 9 | "os" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 |
| 15 | "github.com/onsi/ginkgo/v2" |
| 16 | "github.com/onsi/gomega" |
| 17 |
| 18 | "github.com/fatedier/frp/pkg/util/log" |
| 19 | "github.com/fatedier/frp/test/e2e/framework" |
| 20 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 21 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 22 | "github.com/fatedier/frp/test/e2e/pkg/process" |
| 23 | ) |
| 24 |
| 25 | type compatTestContext struct { |
| 26 | CurrentFRPSPath string |
| 27 | CurrentFRPCPath string |
| 28 | BaselineFRPSPath string |
| 29 | BaselineFRPCPath string |
| 30 | BaselineVersion string |
| 31 | LogLevel string |
| 32 | Debug bool |
| 33 | RunCurrentCurrent bool |
| 34 | } |
| 35 |
| 36 | var compatCtx compatTestContext |
| 37 |
| 38 | func registerFlags(flags *flag.FlagSet) { |
| 39 | flags.StringVar(&compatCtx.CurrentFRPSPath, "current-frps-path", "../../bin/frps", "The current frps binary to use.") |
| 40 | flags.StringVar(&compatCtx.CurrentFRPCPath, "current-frpc-path", "../../bin/frpc", "The current frpc binary to use.") |
| 41 | flags.StringVar(&compatCtx.BaselineFRPSPath, "baseline-frps-path", "", "The baseline frps binary to use.") |
| 42 | flags.StringVar(&compatCtx.BaselineFRPCPath, "baseline-frpc-path", "", "The baseline frpc binary to use.") |
| 43 | flags.StringVar(&compatCtx.BaselineVersion, "baseline-version", "custom", "The baseline version label for reporting.") |
| 44 | flags.StringVar(&compatCtx.LogLevel, "log-level", "debug", "Log level.") |
| 45 | flags.BoolVar(&compatCtx.Debug, "debug", false, "Enable debug mode to print detailed info.") |
| 46 | flags.BoolVar(&compatCtx.RunCurrentCurrent, "run-current-current", true, "Run current frps/current frpc sanity checks.") |
| 47 | } |
| 48 |
| 49 | func validateCompatContext(t *compatTestContext) error { |
| 50 | paths := map[string]string{ |
| 51 | "current-frps-path": t.CurrentFRPSPath, |
| 52 | "current-frpc-path": t.CurrentFRPCPath, |
| 53 | "baseline-frps-path": t.BaselineFRPSPath, |
| 54 | "baseline-frpc-path": t.BaselineFRPCPath, |
| 55 | } |
| 56 | for name, path := range paths { |
| 57 | if path == "" { |
| 58 | return fmt.Errorf("%s can't be empty", name) |
| 59 | } |
| 60 | if _, err := os.Stat(path); err != nil { |
| 61 | return fmt.Errorf("load %s error: %v", name, err) |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 |
| 67 | func TestMain(m *testing.M) { |
| 68 | registerFlags(flag.CommandLine) |
| 69 | flag.Parse() |
| 70 |
| 71 | if err := validateCompatContext(&compatCtx); err != nil { |
| 72 | fmt.Println(err) |
| 73 | os.Exit(1) |
| 74 | } |
| 75 |
| 76 | framework.TestContext.Debug = compatCtx.Debug |
| 77 | framework.TestContext.LogLevel = compatCtx.LogLevel |
| 78 | log.InitLogger("console", compatCtx.LogLevel, 0, true) |
| 79 | os.Exit(m.Run()) |
| 80 | } |
| 81 |
| 82 | func TestCompatibilityE2E(t *testing.T) { |
| 83 | gomega.RegisterFailHandler(framework.Fail) |
| 84 |
| 85 | suiteConfig, reporterConfig := ginkgo.GinkgoConfiguration() |
| 86 | suiteConfig.EmitSpecProgress = true |
| 87 | suiteConfig.RandomizeAllSpecs = true |
| 88 | ginkgo.RunSpecs(t, "frp compatibility e2e suite", suiteConfig, reporterConfig) |
| 89 | } |
| 90 |
| 91 | var _ = ginkgo.Describe("[Compatibility: WireProtocol]", func() { |
| 92 | f := framework.NewDefaultFramework() |
| 93 |
| 94 | ginkgo.It("current frps and current frpc support v1 and v2", func() { |
| 95 | if !compatCtx.RunCurrentCurrent { |
| 96 | ginkgo.Skip("current/current sanity checks already ran") |
| 97 | } |
| 98 |
| 99 | webPort := f.AllocPort() |
| 100 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 101 | webServer.port = %d |
| 102 | `, webPort) |
| 103 |
| 104 | v1PortName := port.GenName("CompatCurrentV1") |
| 105 | v1ClientConf := tcpClientConfig("compat-current-v1", v1PortName, ` |
| 106 | clientID = "compat-current-v1" |
| 107 | transport.wireProtocol = "v1" |
| 108 | `) |
| 109 |
| 110 | v2PortName := port.GenName("CompatCurrentV2") |
| 111 | v2ClientConf := tcpClientConfig("compat-current-v2", v2PortName, ` |
| 112 | clientID = "compat-current-v2" |
| 113 | transport.wireProtocol = "v2" |
| 114 | `) |
| 115 |
| 116 | f.RunProcessesWithBinaries( |
| 117 | compatCtx.CurrentFRPSPath, |
| 118 | compatCtx.CurrentFRPCPath, |
| 119 | serverConf, |
| 120 | []string{v1ClientConf, v2ClientConf}, |
| 121 | ) |
| 122 |
| 123 | framework.NewRequestExpect(f).PortName(v1PortName).Ensure() |
| 124 | framework.NewRequestExpect(f).PortName(v2PortName).Ensure() |
| 125 | expectClientWireProtocol(webPort, "compat-current-v1", "v1") |
| 126 | expectClientWireProtocol(webPort, "compat-current-v2", "v2") |
| 127 | }) |
| 128 |
| 129 | ginkgo.It("current frps accepts baseline frpc using v1", func() { |
| 130 | webPort := f.AllocPort() |
| 131 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 132 | webServer.port = %d |
| 133 | `, webPort) |
| 134 |
| 135 | portName := port.GenName("CompatBaselineFRPC") |
| 136 | clientConf := tcpClientConfig("tcp", portName, "") |
| 137 |
| 138 | f.RunProcessesWithBinaries( |
| 139 | compatCtx.CurrentFRPSPath, |
| 140 | compatCtx.BaselineFRPCPath, |
| 141 | serverConf, |
| 142 | []string{clientConf}, |
| 143 | ) |
| 144 |
| 145 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 146 | expectSingleClientWireProtocol(webPort, "v1") |
| 147 | }) |
| 148 |
| 149 | ginkgo.It("baseline frps accepts current frpc defaulting to v1", func() { |
| 150 | portName := port.GenName("CompatBaselineFRPS") |
| 151 | clientConf := tcpClientConfig("tcp", portName, "") |
| 152 |
| 153 | f.RunProcessesWithBinaries( |
| 154 | compatCtx.BaselineFRPSPath, |
| 155 | compatCtx.CurrentFRPCPath, |
| 156 | consts.DefaultServerConfig, |
| 157 | []string{clientConf}, |
| 158 | ) |
| 159 |
| 160 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 161 | }) |
| 162 |
| 163 | ginkgo.It("baseline frps handles current frpc forced to v2 according to baseline support", func() { |
| 164 | portName := port.GenName("CompatBaselineFRPSForcedV2") |
| 165 | clientConf := tcpClientConfig("tcp", portName, ` |
| 166 | transport.wireProtocol = "v2" |
| 167 | `) |
| 168 |
| 169 | _, clientProcesses := f.RunProcessesWithBinaries( |
| 170 | compatCtx.BaselineFRPSPath, |
| 171 | compatCtx.CurrentFRPCPath, |
| 172 | consts.DefaultServerConfig, |
| 173 | []string{clientConf}, |
| 174 | ) |
| 175 |
| 176 | // frp v0.69.0 added control connection wireProtocol v2 support, so |
| 177 | // baseline frps v0.69.0 and newer should accept a current frpc forced |
| 178 | // to v2. Older known baselines still must reject the unsupported protocol. |
| 179 | // For custom baselines, the version is unknown and the binary may be either |
| 180 | // side of the support boundary, so this versioned expectation is skipped. |
| 181 | supportsV2, knownVersion := baselineSupportsControlWireProtocolV2(compatCtx.BaselineVersion) |
| 182 | if !knownVersion { |
| 183 | ginkgo.Skip(fmt.Sprintf("baseline version %q is not semver; skip versioned forced-v2 expectation", compatCtx.BaselineVersion)) |
| 184 | } |
| 185 | if supportsV2 { |
| 186 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 187 | return |
| 188 | } |
| 189 |
| 190 | expectProcessExit(clientProcesses[0], 5*time.Second) |
| 191 | framework.NewRequestExpect(f).PortName(portName).ExpectError(true).Ensure() |
| 192 | }) |
| 193 | }) |
| 194 |
| 195 | func tcpClientConfig(proxyName string, remotePortName string, extra string) string { |
| 196 | return fmt.Sprintf(` |
| 197 | serverAddr = "127.0.0.1" |
| 198 | serverPort = {{ .%s }} |
| 199 | loginFailExit = true |
| 200 | log.level = "trace" |
| 201 | %s |
| 202 |
| 203 | [[proxies]] |
| 204 | name = "%s" |
| 205 | type = "tcp" |
| 206 | localPort = {{ .%s }} |
| 207 | remotePort = {{ .%s }} |
| 208 | `, consts.PortServerName, extra, proxyName, framework.TCPEchoServerPort, remotePortName) |
| 209 | } |
| 210 |
| 211 | func expectProcessExit(p *process.Process, timeout time.Duration) { |
| 212 | select { |
| 213 | case <-p.Done(): |
| 214 | case <-time.After(timeout): |
| 215 | framework.Failf("process did not exit within %s; output:\n%s", timeout, p.Output()) |
| 216 | } |
| 217 | } |
| 218 |
| 219 | func baselineSupportsControlWireProtocolV2(version string) (supports bool, known bool) { |
| 220 | version = strings.TrimPrefix(version, "v") |
| 221 | parts := strings.Split(version, ".") |
| 222 | if len(parts) != 3 { |
| 223 | return false, false |
| 224 | } |
| 225 |
| 226 | major, err := strconv.Atoi(parts[0]) |
| 227 | if err != nil { |
| 228 | return false, false |
| 229 | } |
| 230 | minor, err := strconv.Atoi(parts[1]) |
| 231 | if err != nil { |
| 232 | return false, false |
| 233 | } |
| 234 | patch, err := strconv.Atoi(parts[2]) |
| 235 | if err != nil { |
| 236 | return false, false |
| 237 | } |
| 238 |
| 239 | return compareSemanticVersion(major, minor, patch, 0, 69, 0) >= 0, true |
| 240 | } |
| 241 |
| 242 | func compareSemanticVersion(major, minor, patch int, baseMajor, baseMinor, basePatch int) int { |
| 243 | if major != baseMajor { |
| 244 | return major - baseMajor |
| 245 | } |
| 246 | if minor != baseMinor { |
| 247 | return minor - baseMinor |
| 248 | } |
| 249 | return patch - basePatch |
| 250 | } |
| 251 |
| 252 | type wireClientInfo struct { |
| 253 | ClientID string `json:"clientID"` |
| 254 | WireProtocol string `json:"wireProtocol"` |
| 255 | } |
| 256 |
| 257 | func expectSingleClientWireProtocol(webPort int, wireProtocol string) { |
| 258 | clients := getWireClientInfos(webPort) |
| 259 | framework.ExpectEqual(len(clients), 1) |
| 260 | framework.ExpectEqual(clients[0].WireProtocol, wireProtocol) |
| 261 | } |
| 262 |
| 263 | func expectClientWireProtocol(webPort int, clientID string, wireProtocol string) { |
| 264 | for _, client := range getWireClientInfos(webPort) { |
| 265 | if client.ClientID == clientID { |
| 266 | framework.ExpectEqual(client.WireProtocol, wireProtocol) |
| 267 | return |
| 268 | } |
| 269 | } |
| 270 | framework.Failf("client %q not found in /api/clients response", clientID) |
| 271 | } |
| 272 |
| 273 | func getWireClientInfos(webPort int) []wireClientInfo { |
| 274 | client := http.Client{Timeout: consts.DefaultTimeout} |
| 275 | resp, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/api/clients", webPort)) |
| 276 | framework.ExpectNoError(err) |
| 277 | defer resp.Body.Close() |
| 278 | framework.ExpectEqual(resp.StatusCode, http.StatusOK) |
| 279 |
| 280 | content, err := io.ReadAll(resp.Body) |
| 281 | framework.ExpectNoError(err) |
| 282 |
| 283 | var clients []wireClientInfo |
| 284 | framework.ExpectNoError(json.Unmarshal(content, &clients)) |
| 285 | return clients |
| 286 | } |