github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/gmhttp/http_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Tests of internal functions and things with no better homes.
     6  
     7  package gmhttp
     8  
     9  import (
    10  	"bytes"
    11  	"io/fs"
    12  	"net/url"
    13  	"os"
    14  	"os/exec"
    15  	"reflect"
    16  	"regexp"
    17  	"strings"
    18  	"testing"
    19  
    20  	"github.com/hxx258456/ccgo/internal/testenv"
    21  )
    22  
    23  func TestForeachHeaderElement(t *testing.T) {
    24  	tests := []struct {
    25  		in   string
    26  		want []string
    27  	}{
    28  		{"Foo", []string{"Foo"}},
    29  		{" Foo", []string{"Foo"}},
    30  		{"Foo ", []string{"Foo"}},
    31  		{" Foo ", []string{"Foo"}},
    32  
    33  		{"foo", []string{"foo"}},
    34  		{"anY-cAsE", []string{"anY-cAsE"}},
    35  
    36  		{"", nil},
    37  		{",,,,  ,  ,,   ,,, ,", nil},
    38  
    39  		{" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}},
    40  	}
    41  	for _, tt := range tests {
    42  		var got []string
    43  		foreachHeaderElement(tt.in, func(v string) {
    44  			got = append(got, v)
    45  		})
    46  		if !reflect.DeepEqual(got, tt.want) {
    47  			t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
    48  		}
    49  	}
    50  }
    51  
    52  func TestCleanHost(t *testing.T) {
    53  	tests := []struct {
    54  		in, want string
    55  	}{
    56  		{"www.google.com", "www.google.com"},
    57  		{"www.google.com foo", "www.google.com"},
    58  		{"www.google.com/foo", "www.google.com"},
    59  		{" first character is a space", ""},
    60  		{"[1::6]:8080", "[1::6]:8080"},
    61  
    62  		// Punycode:
    63  		{"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
    64  		{"bücher.de", "xn--bcher-kva.de"},
    65  		{"bücher.de:8080", "xn--bcher-kva.de:8080"},
    66  		// Verify we convert to lowercase before punycode:
    67  		{"BÜCHER.de", "xn--bcher-kva.de"},
    68  		{"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
    69  		// Verify we normalize to NFC before punycode:
    70  		{"gophér.nfc", "xn--gophr-esa.nfc"},            // NFC input; no work needed
    71  		{"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
    72  	}
    73  	for _, tt := range tests {
    74  		got := cleanHost(tt.in)
    75  		if tt.want != got {
    76  			t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
    77  		}
    78  	}
    79  }
    80  
    81  // Test that cmd/go doesn't link in the HTTP server.
    82  //
    83  // This catches accidental dependencies between the HTTP transport and
    84  // server code.
    85  func TestCmdGoNoHTTPServer(t *testing.T) {
    86  	t.Parallel()
    87  	goBin := testenv.GoToolPath(t)
    88  	out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
    89  	if err != nil {
    90  		t.Fatalf("go tool nm: %v: %s", err, out)
    91  	}
    92  	wantSym := map[string]bool{
    93  		// Verify these exist: (sanity checking this test)
    94  		"net/http.(*Client).do":           true,
    95  		"net/http.(*Transport).RoundTrip": true,
    96  
    97  		// Verify these don't exist:
    98  		"net/http.http2Server":           false,
    99  		"net/http.(*Server).Serve":       false,
   100  		"net/http.(*ServeMux).ServeHTTP": false,
   101  		"net/http.DefaultServeMux":       false,
   102  	}
   103  	for sym, want := range wantSym {
   104  		got := bytes.Contains(out, []byte(sym))
   105  		if !want && got {
   106  			t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym)
   107  		}
   108  		if want && !got {
   109  			t.Errorf("expected to find symbol %q in cmd/go; not found", sym)
   110  		}
   111  	}
   112  }
   113  
   114  // Tests that the nethttpomithttp2 build tag doesn't rot too much,
   115  // even if there's not a regular builder on it.
   116  func TestOmitHTTP2(t *testing.T) {
   117  	if testing.Short() {
   118  		t.Skip("skipping in short mode")
   119  	}
   120  	t.Parallel()
   121  	goTool := testenv.GoToolPath(t)
   122  	out, err := exec.Command(goTool, "test", "-short", "-tags=nethttpomithttp2", "github.com/hxx258456/ccgo/gmhttp").CombinedOutput()
   123  	if err != nil {
   124  		t.Fatalf("go test -short failed: %v, %s", err, out)
   125  	}
   126  }
   127  
   128  // Tests that the nethttpomithttp2 build tag at least type checks
   129  // in short mode.
   130  // The TestOmitHTTP2 test above actually runs tests (in long mode).
   131  func TestOmitHTTP2Vet(t *testing.T) {
   132  	t.Parallel()
   133  	goTool := testenv.GoToolPath(t)
   134  	out, err := exec.Command(goTool, "vet", "-tags=nethttpomithttp2", "github.com/hxx258456/ccgo/gmhttp").CombinedOutput()
   135  	if err != nil {
   136  		t.Fatalf("go vet failed: %v, %s", err, out)
   137  	}
   138  }
   139  
   140  var valuesCount int
   141  
   142  func BenchmarkCopyValues(b *testing.B) {
   143  	b.ReportAllocs()
   144  	src := url.Values{
   145  		"a": {"1", "2", "3", "4", "5"},
   146  		"b": {"2", "2", "3", "4", "5"},
   147  		"c": {"3", "2", "3", "4", "5"},
   148  		"d": {"4", "2", "3", "4", "5"},
   149  		"e": {"1", "1", "2", "3", "4", "5", "6", "7", "abcdef", "l", "a", "b", "c", "d", "z"},
   150  		"j": {"1", "2"},
   151  		"m": nil,
   152  	}
   153  	for i := 0; i < b.N; i++ {
   154  		dst := url.Values{"a": {"b"}, "b": {"2"}, "c": {"3"}, "d": {"4"}, "j": nil, "m": {"x"}}
   155  		copyValues(dst, src)
   156  		if valuesCount = len(dst["a"]); valuesCount != 6 {
   157  			b.Fatalf(`%d items in dst["a"] but expected 6`, valuesCount)
   158  		}
   159  	}
   160  	if valuesCount == 0 {
   161  		b.Fatal("Benchmark wasn't run")
   162  	}
   163  }
   164  
   165  var forbiddenStringsFunctions = map[string]bool{
   166  	// Functions that use Unicode-aware case folding.
   167  	"EqualFold":      true,
   168  	"Title":          true,
   169  	"ToLower":        true,
   170  	"ToLowerSpecial": true,
   171  	"ToTitle":        true,
   172  	"ToTitleSpecial": true,
   173  	"ToUpper":        true,
   174  	"ToUpperSpecial": true,
   175  
   176  	// Functions that use Unicode-aware spaces.
   177  	"Fields":    true,
   178  	"TrimSpace": true,
   179  }
   180  
   181  // TestNoUnicodeStrings checks that nothing in net/http uses the Unicode-aware
   182  // strings and bytes package functions. HTTP is mostly ASCII based, and doing
   183  // Unicode-aware case folding or space stripping can introduce vulnerabilities.
   184  func TestNoUnicodeStrings(t *testing.T) {
   185  	if !testenv.HasSrc() {
   186  		t.Skip("source code not available")
   187  	}
   188  
   189  	re := regexp.MustCompile(`(strings|bytes).([A-Za-z]+)`)
   190  	if err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, err error) error {
   191  		if err != nil {
   192  			t.Fatal(err)
   193  		}
   194  
   195  		if path == "internal/ascii" {
   196  			return fs.SkipDir
   197  		}
   198  		if !strings.HasSuffix(path, ".go") ||
   199  			strings.HasSuffix(path, "_test.go") ||
   200  			path == "h2_bundle.go" || d.IsDir() {
   201  			return nil
   202  		}
   203  
   204  		contents, err := os.ReadFile(path)
   205  		if err != nil {
   206  			t.Fatal(err)
   207  		}
   208  		for lineNum, line := range strings.Split(string(contents), "\n") {
   209  			for _, match := range re.FindAllStringSubmatch(line, -1) {
   210  				if !forbiddenStringsFunctions[match[2]] {
   211  					continue
   212  				}
   213  				t.Errorf("disallowed call to %s at %s:%d", match[0], path, lineNum+1)
   214  			}
   215  		}
   216  
   217  		return nil
   218  	}); err != nil {
   219  		t.Fatal(err)
   220  	}
   221  }