roundtripper_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package utls
  2. import (
  3. "crypto/rsa"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "math/big"
  8. "math/rand"
  9. "net/http"
  10. "os"
  11. "testing"
  12. "time"
  13. stdcontext "context"
  14. utls "github.com/refraction-networking/utls"
  15. "golang.org/x/net/http2"
  16. . "github.com/smartystreets/goconvey/convey"
  17. )
  18. func TestRoundTripper(t *testing.T) {
  19. runRoundTripperTest(t, "127.0.0.1:23802", "127.0.0.1:23801", "https://127.0.0.1:23802/", "https://127.0.0.1:23801/")
  20. }
  21. func TestRoundTripperOnH1DefaultPort(t *testing.T) {
  22. if os.Getuid() != 0 {
  23. t.SkipNow()
  24. }
  25. runRoundTripperTest(t, "127.0.0.1:23802", "127.0.0.1:443", "https://127.0.0.1:23802/", "https://127.0.0.1/")
  26. }
  27. func TestRoundTripperOnH2DefaultPort(t *testing.T) {
  28. if os.Getuid() != 0 {
  29. t.SkipNow()
  30. }
  31. runRoundTripperTest(t, "127.0.0.1:443", "127.0.0.1:23801", "https://127.0.0.1/", "https://127.0.0.1:23801/")
  32. }
  33. func runRoundTripperTest(t *testing.T, h2listen, h1listen, h2addr, h1addr string) {
  34. var selfSignedCert []byte
  35. var selfSignedPrivateKey *rsa.PrivateKey
  36. httpServerContext, cancel := stdcontext.WithCancel(stdcontext.Background())
  37. Convey("[Test]Set up http servers", t, func(c C) {
  38. c.Convey("[Test]Generate Self-Signed Cert", func(c C) {
  39. // Ported from https://gist.github.com/samuel/8b500ddd3f6118d052b5e6bc16bc4c09
  40. // note that we use the insecure math/rand here because some platforms
  41. // fail the test suite at build time in Debian, due to entropy starvation.
  42. // since that's not a problem at test time, we do *not* use a secure
  43. // mechanism for key generation.
  44. //
  45. // DO NOT REUSE THIS CODE IN PRODUCTION, IT IS DANGEROUS
  46. insecureRandReader := rand.New(rand.NewSource(1337))
  47. priv, err := rsa.GenerateKey(insecureRandReader, 4096)
  48. c.So(err, ShouldBeNil)
  49. template := x509.Certificate{
  50. SerialNumber: big.NewInt(1),
  51. Subject: pkix.Name{
  52. CommonName: "Testing Certificate",
  53. },
  54. NotBefore: time.Now(),
  55. NotAfter: time.Now().Add(time.Hour * 24 * 180),
  56. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
  57. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  58. BasicConstraintsValid: true,
  59. }
  60. derBytes, err := x509.CreateCertificate(insecureRandReader, &template, &template, priv.Public(), priv)
  61. c.So(err, ShouldBeNil)
  62. selfSignedPrivateKey = priv
  63. selfSignedCert = derBytes
  64. })
  65. c.Convey("[Test]Setup http2 server", func(c C) {
  66. listener, err := tls.Listen("tcp", h2listen, &tls.Config{
  67. NextProtos: []string{http2.NextProtoTLS},
  68. Certificates: []tls.Certificate{
  69. tls.Certificate{Certificate: [][]byte{selfSignedCert}, PrivateKey: selfSignedPrivateKey},
  70. },
  71. })
  72. c.So(err, ShouldBeNil)
  73. s := http.Server{}
  74. go s.Serve(listener)
  75. go func() {
  76. <-httpServerContext.Done()
  77. s.Close()
  78. }()
  79. })
  80. c.Convey("[Test]Setup http1 server", func(c C) {
  81. listener, err := tls.Listen("tcp", h1listen, &tls.Config{
  82. NextProtos: []string{"http/1.1"},
  83. Certificates: []tls.Certificate{
  84. tls.Certificate{Certificate: [][]byte{selfSignedCert}, PrivateKey: selfSignedPrivateKey},
  85. },
  86. })
  87. c.So(err, ShouldBeNil)
  88. s := http.Server{}
  89. go s.Serve(listener)
  90. go func() {
  91. <-httpServerContext.Done()
  92. s.Close()
  93. }()
  94. })
  95. })
  96. for _, v := range []struct {
  97. id utls.ClientHelloID
  98. name string
  99. }{
  100. {
  101. id: utls.HelloChrome_58,
  102. name: "HelloChrome_58",
  103. },
  104. {
  105. id: utls.HelloChrome_62,
  106. name: "HelloChrome_62",
  107. },
  108. {
  109. id: utls.HelloChrome_70,
  110. name: "HelloChrome_70",
  111. },
  112. {
  113. id: utls.HelloChrome_72,
  114. name: "HelloChrome_72",
  115. },
  116. {
  117. id: utls.HelloChrome_83,
  118. name: "HelloChrome_83",
  119. },
  120. {
  121. id: utls.HelloFirefox_55,
  122. name: "HelloFirefox_55",
  123. },
  124. {
  125. id: utls.HelloFirefox_55,
  126. name: "HelloFirefox_55",
  127. },
  128. {
  129. id: utls.HelloFirefox_63,
  130. name: "HelloFirefox_63",
  131. },
  132. {
  133. id: utls.HelloFirefox_65,
  134. name: "HelloFirefox_65",
  135. },
  136. {
  137. id: utls.HelloIOS_11_1,
  138. name: "HelloIOS_11_1",
  139. },
  140. {
  141. id: utls.HelloIOS_12_1,
  142. name: "HelloIOS_12_1",
  143. },
  144. } {
  145. t.Run("Testing fingerprint for "+v.name, func(t *testing.T) {
  146. rtter := NewUTLSHTTPRoundTripper(v.id, &utls.Config{
  147. InsecureSkipVerify: true,
  148. }, http.DefaultTransport, false)
  149. for count := 0; count <= 10; count++ {
  150. Convey("HTTP 1.1 Test", t, func(c C) {
  151. {
  152. req, err := http.NewRequest("GET", h2addr, nil)
  153. So(err, ShouldBeNil)
  154. _, err = rtter.RoundTrip(req)
  155. So(err, ShouldBeNil)
  156. }
  157. })
  158. Convey("HTTP 2 Test", t, func(c C) {
  159. {
  160. req, err := http.NewRequest("GET", h1addr, nil)
  161. So(err, ShouldBeNil)
  162. _, err = rtter.RoundTrip(req)
  163. So(err, ShouldBeNil)
  164. }
  165. })
  166. }
  167. })
  168. }
  169. cancel()
  170. }