tlsCompatibility_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. )
  33. func TestTLSCompatibility(t *testing.T) {
  34. // Config should be newline delimited list of domain/IP:port TLS host
  35. // addresses to connect to.
  36. config, err := ioutil.ReadFile("tlsCompatibility_test.config")
  37. if err != nil {
  38. // Skip, don't fail, if config file is not present
  39. t.Skipf("error loading configuration file: %s", err)
  40. }
  41. addresses := strings.Split(string(config), "\n")
  42. runner := func(address string) func(t *testing.T) {
  43. return func(t *testing.T) {
  44. testTLSCompatibility(t, address)
  45. }
  46. }
  47. for _, address := range addresses {
  48. if len(address) > 0 {
  49. t.Run(address, runner(address))
  50. }
  51. }
  52. t.Run("psiphon", runner(""))
  53. }
  54. func testTLSCompatibility(t *testing.T, address string) {
  55. if address == "" {
  56. // Same tls-tris config as psiphon/server/meek.go
  57. certificate, privateKey, err := common.GenerateWebServerCertificate(common.GenerateHostName())
  58. if err != nil {
  59. t.Fatalf("%s\n", err)
  60. }
  61. tlsCertificate, err := tris.X509KeyPair([]byte(certificate), []byte(privateKey))
  62. if err != nil {
  63. t.Fatalf("%s\n", err)
  64. }
  65. config := &tris.Config{
  66. Certificates: []tris.Certificate{tlsCertificate},
  67. NextProtos: []string{"http/1.1"},
  68. MinVersion: tris.VersionTLS10,
  69. UseExtendedMasterSecret: true,
  70. }
  71. tcpListener, err := net.Listen("tcp", "127.0.0.1:0")
  72. if err != nil {
  73. t.Fatalf("%s\n", err)
  74. }
  75. tlsListener := tris.NewListener(tcpListener, config)
  76. defer tlsListener.Close()
  77. address = tlsListener.Addr().String()
  78. go func() {
  79. for {
  80. conn, err := tlsListener.Accept()
  81. if err != nil {
  82. return
  83. }
  84. err = conn.(*tris.Conn).Handshake()
  85. if err != nil {
  86. t.Logf("server handshake: %s", err)
  87. }
  88. conn.Close()
  89. }
  90. }()
  91. }
  92. dialer := func(ctx context.Context, network, address string) (net.Conn, error) {
  93. d := &net.Dialer{}
  94. return d.DialContext(ctx, network, address)
  95. }
  96. clientParameters, err := parameters.NewClientParameters(nil)
  97. if err != nil {
  98. t.Fatalf("%s\n", err)
  99. }
  100. for _, tlsProfile := range protocol.SupportedTLSProfiles {
  101. repeats := 1
  102. if protocol.TLSProfileIsRandomized(tlsProfile) {
  103. repeats = 20
  104. }
  105. success := 0
  106. for i := 0; i < repeats; i++ {
  107. tlsConfig := &CustomTLSConfig{
  108. ClientParameters: clientParameters,
  109. Dial: dialer,
  110. UseDialAddrSNI: true,
  111. SkipVerify: true,
  112. TLSProfile: tlsProfile,
  113. }
  114. ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
  115. conn, err := CustomTLSDial(ctx, "tcp", address, tlsConfig)
  116. if err != nil {
  117. t.Logf("%s: %s\n", tlsProfile, err)
  118. } else {
  119. conn.Close()
  120. success += 1
  121. }
  122. cancelFunc()
  123. time.Sleep(100 * time.Millisecond)
  124. }
  125. result := fmt.Sprintf("%s: %d/%d successful\n", tlsProfile, success, repeats)
  126. if success == repeats {
  127. t.Logf(result)
  128. } else {
  129. t.Errorf(result)
  130. }
  131. }
  132. }