You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.8 KiB

  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. package http2
  5. import (
  6. "bytes"
  7. "errors"
  8. "flag"
  9. "fmt"
  10. "net/http"
  11. "os/exec"
  12. "strconv"
  13. "strings"
  14. "testing"
  15. "golang.org/x/net/http2/hpack"
  16. )
  17. var knownFailing = flag.Bool("known_failing", false, "Run known-failing tests.")
  18. func condSkipFailingTest(t *testing.T) {
  19. if !*knownFailing {
  20. t.Skip("Skipping known-failing test without --known_failing")
  21. }
  22. }
  23. func init() {
  24. inTests = true
  25. DebugGoroutines = true
  26. flag.BoolVar(&VerboseLogs, "verboseh2", VerboseLogs, "Verbose HTTP/2 debug logging")
  27. }
  28. func TestSettingString(t *testing.T) {
  29. tests := []struct {
  30. s Setting
  31. want string
  32. }{
  33. {Setting{SettingMaxFrameSize, 123}, "[MAX_FRAME_SIZE = 123]"},
  34. {Setting{1<<16 - 1, 123}, "[UNKNOWN_SETTING_65535 = 123]"},
  35. }
  36. for i, tt := range tests {
  37. got := fmt.Sprint(tt.s)
  38. if got != tt.want {
  39. t.Errorf("%d. for %#v, string = %q; want %q", i, tt.s, got, tt.want)
  40. }
  41. }
  42. }
  43. type twriter struct {
  44. t testing.TB
  45. st *serverTester // optional
  46. }
  47. func (w twriter) Write(p []byte) (n int, err error) {
  48. if w.st != nil {
  49. ps := string(p)
  50. for _, phrase := range w.st.logFilter {
  51. if strings.Contains(ps, phrase) {
  52. return len(p), nil // no logging
  53. }
  54. }
  55. }
  56. w.t.Logf("%s", p)
  57. return len(p), nil
  58. }
  59. // like encodeHeader, but don't add implicit pseudo headers.
  60. func encodeHeaderNoImplicit(t *testing.T, headers ...string) []byte {
  61. var buf bytes.Buffer
  62. enc := hpack.NewEncoder(&buf)
  63. for len(headers) > 0 {
  64. k, v := headers[0], headers[1]
  65. headers = headers[2:]
  66. if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil {
  67. t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
  68. }
  69. }
  70. return buf.Bytes()
  71. }
  72. // Verify that curl has http2.
  73. func requireCurl(t *testing.T) {
  74. out, err := dockerLogs(curl(t, "--version"))
  75. if err != nil {
  76. t.Skipf("failed to determine curl features; skipping test")
  77. }
  78. if !strings.Contains(string(out), "HTTP2") {
  79. t.Skip("curl doesn't support HTTP2; skipping test")
  80. }
  81. }
  82. func curl(t *testing.T, args ...string) (container string) {
  83. out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "gohttp2/curl"}, args...)...).Output()
  84. if err != nil {
  85. t.Skipf("Failed to run curl in docker: %v, %s", err, out)
  86. }
  87. return strings.TrimSpace(string(out))
  88. }
  89. // Verify that h2load exists.
  90. func requireH2load(t *testing.T) {
  91. out, err := dockerLogs(h2load(t, "--version"))
  92. if err != nil {
  93. t.Skipf("failed to probe h2load; skipping test: %s", out)
  94. }
  95. if !strings.Contains(string(out), "h2load nghttp2/") {
  96. t.Skipf("h2load not present; skipping test. (Output=%q)", out)
  97. }
  98. }
  99. func h2load(t *testing.T, args ...string) (container string) {
  100. out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "--entrypoint=/usr/local/bin/h2load", "gohttp2/curl"}, args...)...).Output()
  101. if err != nil {
  102. t.Skipf("Failed to run h2load in docker: %v, %s", err, out)
  103. }
  104. return strings.TrimSpace(string(out))
  105. }
  106. type puppetCommand struct {
  107. fn func(w http.ResponseWriter, r *http.Request)
  108. done chan<- bool
  109. }
  110. type handlerPuppet struct {
  111. ch chan puppetCommand
  112. }
  113. func newHandlerPuppet() *handlerPuppet {
  114. return &handlerPuppet{
  115. ch: make(chan puppetCommand),
  116. }
  117. }
  118. func (p *handlerPuppet) act(w http.ResponseWriter, r *http.Request) {
  119. for cmd := range p.ch {
  120. cmd.fn(w, r)
  121. cmd.done <- true
  122. }
  123. }
  124. func (p *handlerPuppet) done() { close(p.ch) }
  125. func (p *handlerPuppet) do(fn func(http.ResponseWriter, *http.Request)) {
  126. done := make(chan bool)
  127. p.ch <- puppetCommand{fn, done}
  128. <-done
  129. }
  130. func dockerLogs(container string) ([]byte, error) {
  131. out, err := exec.Command("docker", "wait", container).CombinedOutput()
  132. if err != nil {
  133. return out, err
  134. }
  135. exitStatus, err := strconv.Atoi(strings.TrimSpace(string(out)))
  136. if err != nil {
  137. return out, errors.New("unexpected exit status from docker wait")
  138. }
  139. out, err = exec.Command("docker", "logs", container).CombinedOutput()
  140. exec.Command("docker", "rm", container).Run()
  141. if err == nil && exitStatus != 0 {
  142. err = fmt.Errorf("exit status %d: %s", exitStatus, out)
  143. }
  144. return out, err
  145. }
  146. func kill(container string) {
  147. exec.Command("docker", "kill", container).Run()
  148. exec.Command("docker", "rm", container).Run()
  149. }
  150. func cleanDate(res *http.Response) {
  151. if d := res.Header["Date"]; len(d) == 1 {
  152. d[0] = "XXX"
  153. }
  154. }
  155. func TestSorterPoolAllocs(t *testing.T) {
  156. ss := []string{"a", "b", "c"}
  157. h := http.Header{
  158. "a": nil,
  159. "b": nil,
  160. "c": nil,
  161. }
  162. sorter := new(sorter)
  163. if allocs := testing.AllocsPerRun(100, func() {
  164. sorter.SortStrings(ss)
  165. }); allocs >= 1 {
  166. t.Logf("SortStrings allocs = %v; want <1", allocs)
  167. }
  168. if allocs := testing.AllocsPerRun(5, func() {
  169. if len(sorter.Keys(h)) != 3 {
  170. t.Fatal("wrong result")
  171. }
  172. }); allocs > 0 {
  173. t.Logf("Keys allocs = %v; want <1", allocs)
  174. }
  175. }