tlsDialer_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2019, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "context"
  22. "fmt"
  23. "io/ioutil"
  24. "net"
  25. "strings"
  26. "testing"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  31. tris "github.com/Psiphon-Labs/tls-tris"
  32. utls "github.com/refraction-networking/utls"
  33. )
  34. func TestTLSDialerCompatibility(t *testing.T) {
  35. // This test checks that each TLS profile can successfully complete a TLS
  36. // handshake with various servers. By default, only the "psiphon" case is
  37. // run, which runs the same TLS listener used by a Psiphon server.
  38. //
  39. // An optional config file, when supplied, enables testing against remote
  40. // servers. Config should be newline delimited list of domain/IP:port TLS
  41. // host addresses to connect to.
  42. var configAddresses []string
  43. config, err := ioutil.ReadFile("tlsDialerCompatibility_test.config")
  44. if err == nil {
  45. configAddresses = strings.Split(string(config), "\n")
  46. }
  47. runner := func(address string) func(t *testing.T) {
  48. return func(t *testing.T) {
  49. testTLSDialerCompatibility(t, address)
  50. }
  51. }
  52. for _, address := range configAddresses {
  53. if len(address) > 0 {
  54. t.Run(address, runner(address))
  55. }
  56. }
  57. t.Run("psiphon", runner(""))
  58. }
  59. func testTLSDialerCompatibility(t *testing.T, address string) {
  60. if address == "" {
  61. // Same tls-tris config as psiphon/server/meek.go
  62. certificate, privateKey, err := common.GenerateWebServerCertificate(common.GenerateHostName())
  63. if err != nil {
  64. t.Fatalf("%s\n", err)
  65. }
  66. tlsCertificate, err := tris.X509KeyPair([]byte(certificate), []byte(privateKey))
  67. if err != nil {
  68. t.Fatalf("%s\n", err)
  69. }
  70. config := &tris.Config{
  71. Certificates: []tris.Certificate{tlsCertificate},
  72. NextProtos: []string{"http/1.1"},
  73. MinVersion: tris.VersionTLS10,
  74. UseExtendedMasterSecret: true,
  75. }
  76. tcpListener, err := net.Listen("tcp", "127.0.0.1:0")
  77. if err != nil {
  78. t.Fatalf("%s\n", err)
  79. }
  80. tlsListener := tris.NewListener(tcpListener, config)
  81. defer tlsListener.Close()
  82. address = tlsListener.Addr().String()
  83. go func() {
  84. for {
  85. conn, err := tlsListener.Accept()
  86. if err != nil {
  87. return
  88. }
  89. err = conn.(*tris.Conn).Handshake()
  90. if err != nil {
  91. t.Logf("server handshake: %s", err)
  92. }
  93. conn.Close()
  94. }
  95. }()
  96. }
  97. dialer := func(ctx context.Context, network, address string) (net.Conn, error) {
  98. d := &net.Dialer{}
  99. return d.DialContext(ctx, network, address)
  100. }
  101. clientParameters, err := parameters.NewClientParameters(nil)
  102. if err != nil {
  103. t.Fatalf("%s\n", err)
  104. }
  105. for _, tlsProfile := range protocol.SupportedTLSProfiles {
  106. repeats := 1
  107. if protocol.TLSProfileIsRandomized(tlsProfile) {
  108. repeats = 20
  109. }
  110. success := 0
  111. for i := 0; i < repeats; i++ {
  112. tlsConfig := &CustomTLSConfig{
  113. ClientParameters: clientParameters,
  114. Dial: dialer,
  115. UseDialAddrSNI: true,
  116. SkipVerify: true,
  117. TLSProfile: tlsProfile,
  118. }
  119. ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
  120. conn, err := CustomTLSDial(ctx, "tcp", address, tlsConfig)
  121. if err != nil {
  122. t.Logf("%s: %s\n", tlsProfile, err)
  123. } else {
  124. conn.Close()
  125. success += 1
  126. }
  127. cancelFunc()
  128. time.Sleep(100 * time.Millisecond)
  129. }
  130. result := fmt.Sprintf("%s: %d/%d successful\n", tlsProfile, success, repeats)
  131. if success == repeats {
  132. t.Logf(result)
  133. } else {
  134. t.Errorf(result)
  135. }
  136. }
  137. }
  138. func TestSelectTLSProfile(t *testing.T) {
  139. clientParameters, err := parameters.NewClientParameters(nil)
  140. if err != nil {
  141. t.Fatalf("%s\n", err)
  142. }
  143. selected := make(map[string]int)
  144. numSelections := 10000
  145. for i := 0; i < numSelections; i++ {
  146. profile := SelectTLSProfile(clientParameters.Get())
  147. selected[profile] += 1
  148. }
  149. // All TLS profiles should be selected at least once.
  150. for _, profile := range protocol.SupportedTLSProfiles {
  151. if selected[profile] < 1 {
  152. t.Errorf("TLS profile %s not selected", profile)
  153. }
  154. }
  155. // Randomized TLS profiles should be selected with expected probability.
  156. numRandomized := 0
  157. for profile, n := range selected {
  158. if protocol.TLSProfileIsRandomized(profile) {
  159. numRandomized += n
  160. }
  161. }
  162. t.Logf("ratio of randomized selected: %d/%d",
  163. numRandomized, numSelections)
  164. randomizedProbability := clientParameters.Get().Float(
  165. parameters.SelectRandomizedTLSProfileProbability)
  166. if numRandomized < int(0.9*float64(numSelections)*randomizedProbability) ||
  167. numRandomized > int(1.1*float64(numSelections)*randomizedProbability) {
  168. t.Error("Unexpected ratio")
  169. }
  170. // getUTLSClientHelloID should map each TLS profile to a utls ClientHelloID.
  171. for _, profile := range protocol.SupportedTLSProfiles {
  172. if getUTLSClientHelloID(profile) == utls.HelloGolang {
  173. t.Errorf("TLS profile %s has no utls ClientHelloID", profile)
  174. }
  175. }
  176. }
  177. func BenchmarkRandomizedGetClientHelloVersion(b *testing.B) {
  178. for n := 0; n < b.N; n++ {
  179. utlsClientHelloID := utls.HelloRandomized
  180. utlsClientHelloID.Seed, _ = utls.NewPRNGSeed()
  181. getClientHelloVersion(utlsClientHelloID)
  182. }
  183. }