fragmentor_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2018, 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 fragmentor
  20. import (
  21. "bytes"
  22. "context"
  23. "math/rand"
  24. "net"
  25. "testing"
  26. "time"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  31. "golang.org/x/sync/errgroup"
  32. )
  33. func TestFragmentor(t *testing.T) {
  34. listener, err := net.Listen("tcp", "127.0.0.1:0")
  35. if err != nil {
  36. t.Fatalf("net.Listen failed: %s", err)
  37. }
  38. address := listener.Addr().String()
  39. data := make([]byte, 1<<18)
  40. rand.Read(data)
  41. // This test is sensitive to OS buffering and timing; the delays are
  42. // intended to be sufficiently long to ensure fragmented writes are read
  43. // before additional data is written, even when running tests with the
  44. // race detector.
  45. tunnelProtocol := protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH
  46. bytesToFragment := 1 << 15
  47. minWriteBytes := 1
  48. maxWriteBytes := 512
  49. minDelay := 2 * time.Millisecond
  50. maxDelay := 2 * time.Millisecond
  51. clientParameters, err := parameters.NewClientParameters(nil)
  52. if err != nil {
  53. t.Fatalf("parameters.NewClientParameters failed: %s", err)
  54. }
  55. _, err = clientParameters.Set("", false, map[string]interface{}{
  56. "FragmentorProbability": 1.0,
  57. "FragmentorLimitProtocols": protocol.TunnelProtocols{tunnelProtocol},
  58. "FragmentorMinTotalBytes": bytesToFragment,
  59. "FragmentorMaxTotalBytes": bytesToFragment,
  60. "FragmentorMinWriteBytes": minWriteBytes,
  61. "FragmentorMaxWriteBytes": maxWriteBytes,
  62. "FragmentorMinDelay": minDelay,
  63. "FragmentorMaxDelay": maxDelay,
  64. "FragmentorDownstreamProbability": 1.0,
  65. "FragmentorDownstreamLimitProtocols": protocol.TunnelProtocols{tunnelProtocol},
  66. "FragmentorDownstreamMinTotalBytes": bytesToFragment,
  67. "FragmentorDownstreamMaxTotalBytes": bytesToFragment,
  68. "FragmentorDownstreamMinWriteBytes": minWriteBytes,
  69. "FragmentorDownstreamMaxWriteBytes": maxWriteBytes,
  70. "FragmentorDownstreamMinDelay": minDelay,
  71. "FragmentorDownstreamMaxDelay": maxDelay,
  72. })
  73. if err != nil {
  74. t.Fatalf("ClientParameters.Set failed: %s", err)
  75. }
  76. testGroup, testCtx := errgroup.WithContext(context.Background())
  77. testGroup.Go(func() error {
  78. conn, err := listener.Accept()
  79. if err != nil {
  80. return errors.Trace(err)
  81. }
  82. fragConn := NewConn(
  83. NewDownstreamConfig(clientParameters.Get(), tunnelProtocol, nil),
  84. func(message string) { t.Logf(message) },
  85. conn)
  86. defer fragConn.Close()
  87. readData := make([]byte, len(data))
  88. n := 0
  89. for n < len(data) {
  90. m, err := fragConn.Read(readData[n:])
  91. if err != nil {
  92. return errors.Trace(err)
  93. }
  94. if m > maxWriteBytes && n+maxWriteBytes <= bytesToFragment {
  95. return errors.Tracef("unexpected write size: %d, %d", m, n)
  96. }
  97. n += m
  98. }
  99. if !bytes.Equal(data, readData) {
  100. return errors.Tracef("data mismatch")
  101. }
  102. PRNG, err := prng.NewPRNG()
  103. if err != nil {
  104. return errors.Trace(err)
  105. }
  106. fragConn.SetPRNG(PRNG)
  107. _, err = fragConn.Write(data)
  108. if err != nil {
  109. return errors.Trace(err)
  110. }
  111. return nil
  112. })
  113. testGroup.Go(func() error {
  114. conn, err := net.Dial("tcp", address)
  115. if err != nil {
  116. return errors.Trace(err)
  117. }
  118. seed, err := prng.NewSeed()
  119. if err != nil {
  120. return errors.Trace(err)
  121. }
  122. fragConn := NewConn(
  123. NewUpstreamConfig(clientParameters.Get(), tunnelProtocol, seed),
  124. func(message string) { t.Logf(message) },
  125. conn)
  126. defer fragConn.Close()
  127. _, err = fragConn.Write(data)
  128. if err != nil {
  129. return errors.Trace(err)
  130. }
  131. t.Logf("%+v", fragConn.GetMetrics())
  132. readData := make([]byte, len(data))
  133. n := 0
  134. for n < len(data) {
  135. m, err := fragConn.Read(readData[n:])
  136. if err != nil {
  137. return errors.Trace(err)
  138. }
  139. if m > maxWriteBytes && n+maxWriteBytes <= bytesToFragment {
  140. return errors.Tracef("unexpected write size: %d, %d", m, n)
  141. }
  142. n += m
  143. }
  144. if !bytes.Equal(data, readData) {
  145. return errors.Tracef("data mismatch")
  146. }
  147. return nil
  148. })
  149. go func() {
  150. testGroup.Wait()
  151. }()
  152. <-testCtx.Done()
  153. listener.Close()
  154. err = testGroup.Wait()
  155. if err != nil {
  156. t.Errorf("goroutine failed: %s", err)
  157. }
  158. }