From d8bcbc2881d2c4c9f1b144edca4707adb5ef3e5d Mon Sep 17 00:00:00 2001 From: johnzhanghua Date: Mon, 25 Sep 2023 17:09:37 +1000 Subject: [PATCH] fix probe_resist_test.go test cases, the repose header['Alt-Svc'] is different from different server Signed-off-by: johnzhanghua --- probe_resist_test.go | 66 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/probe_resist_test.go b/probe_resist_test.go index d064de4..4214c31 100644 --- a/probe_resist_test.go +++ b/probe_resist_test.go @@ -50,7 +50,13 @@ func TestGETAuthWrongProbeResist(t *testing.T) { responseReference.StatusCode, responseProbeResist.StatusCode) } if err = responsesAreEqual(responseProbeResist, responseReference); err != nil { - t.Fatal(err) + var e errorHeaderAlternativeServiceNotEqual + if !errors.As(err, &e) { + t.Fatal(err) + } + if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil { + t.Fatal(err) + } } if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil { t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+ @@ -78,7 +84,13 @@ func TestGETAuthWrongProbeResist(t *testing.T) { responseProbeResist.StatusCode) } if err = responsesAreEqual(responseProbeResist, responseReference); err != nil { - t.Fatal(err) + var e errorHeaderAlternativeServiceNotEqual + if !errors.As(err, &e) { + t.Fatal(err) + } + if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil { + t.Fatal(err) + } } if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil { t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+ @@ -174,7 +186,13 @@ func TestConnectAuthWrongProbeResist(t *testing.T) { responseReference.StatusCode, responseProbeResist.StatusCode) } if err = responsesAreEqual(responseProbeResist, responseReference); err != nil { - t.Fatal(err) + var e errorHeaderAlternativeServiceNotEqual + if !errors.As(err, &e) { + t.Fatal(err) + } + if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil { + t.Fatal(err) + } } if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil { t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+ @@ -202,7 +220,13 @@ func TestConnectAuthWrongProbeResist(t *testing.T) { t.Fatal(err) } if err = responsesAreEqual(responseProbeResist, responseReference); err != nil { - t.Fatal(err) + var e errorHeaderAlternativeServiceNotEqual + if !errors.As(err, &e) { + t.Fatal(err) + } + if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil { + t.Fatal(err) + } } if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil { t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+ @@ -263,6 +287,37 @@ func TestConnectAuthWrongProbeResistRedir(t *testing.T) { } } +type errorHeaderAlternativeServiceNotEqual struct { + ValueA []string + ValueB []string +} + +func (e errorHeaderAlternativeServiceNotEqual) Error() string { + return fmt.Sprintf("header 'Alt-Svc' not equal: %v, %v\n", e.ValueA, e.ValueB) +} + +func (e errorHeaderAlternativeServiceNotEqual) CheckAlternativeServiceError(serverAddrA, serverAddrB string) error { + if len(e.ValueA) == 0 || len(e.ValueB) == 0 { + return fmt.Errorf("header 'Alt-Svc' is empty: %w", e) + } + _, port, err := net.SplitHostPort(serverAddrA) + if err != nil { + return fmt.Errorf("failed to split server address :%w", err) + } + if !strings.Contains(e.ValueA[0], port) { + return fmt.Errorf("Alt-Svc address :%s does not contain the server port: %s", e.ValueA[0], port) + } + _, port, err = net.SplitHostPort(serverAddrB) + if err != nil { + return fmt.Errorf("failed to split server address :%w", err) + } + if !strings.Contains(e.ValueB[0], port) { + return fmt.Errorf("Alt-Svc address :%s does not contain the server port: %s", e.ValueB[0], port) + } + + return nil +} + // returns nil if are equal func responsesAreEqual(res1, res2 *http.Response) error { if res1 == nil { @@ -343,6 +398,9 @@ func responsesAreEqual(res1, res2 *http.Response) error { return fmt.Errorf("header \"%s: %s\" is absent in res2", k1, v1) } if errStr = stringSlicesAreEqual(v1, v2); errStr != "" { + if k1 == "Alt-Svc" { + return errorHeaderAlternativeServiceNotEqual{v1, v2} + } return fmt.Errorf("header \"%s\" is different: %s", k1, errStr) } }