process_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcp
  15. import (
  16. "io"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/fatedier/frp/utils/crypto"
  20. )
  21. func TestJoin(t *testing.T) {
  22. assert := assert.New(t)
  23. var (
  24. n int
  25. err error
  26. )
  27. text1 := "A document that gives tips for writing clear, idiomatic Go code. A must read for any new Go programmer. It augments the tour and the language specification, both of which should be read first."
  28. text2 := "A document that specifies the conditions under which reads of a variable in one goroutine can be guaranteed to observe values produced by writes to the same variable in a different goroutine."
  29. // Forward bytes directly.
  30. pr, pw := io.Pipe()
  31. pr2, pw2 := io.Pipe()
  32. pr3, pw3 := io.Pipe()
  33. pr4, pw4 := io.Pipe()
  34. conn1 := WrapReadWriteCloser(pr, pw2)
  35. conn2 := WrapReadWriteCloser(pr2, pw)
  36. conn3 := WrapReadWriteCloser(pr3, pw4)
  37. conn4 := WrapReadWriteCloser(pr4, pw3)
  38. go func() {
  39. Join(conn2, conn3)
  40. }()
  41. buf1 := make([]byte, 1024)
  42. buf2 := make([]byte, 1024)
  43. conn1.Write([]byte(text1))
  44. conn4.Write([]byte(text2))
  45. n, err = conn4.Read(buf1)
  46. assert.NoError(err)
  47. assert.Equal(text1, string(buf1[:n]))
  48. n, err = conn1.Read(buf2)
  49. assert.NoError(err)
  50. assert.Equal(text2, string(buf2[:n]))
  51. conn1.Close()
  52. conn2.Close()
  53. conn3.Close()
  54. conn4.Close()
  55. }
  56. func TestJoinEncrypt(t *testing.T) {
  57. assert := assert.New(t)
  58. var (
  59. n int
  60. err error
  61. )
  62. text1 := "1234567890"
  63. text2 := "abcdefghij"
  64. key := "authkey"
  65. // Forward enrypted bytes.
  66. pr, pw := io.Pipe()
  67. pr2, pw2 := io.Pipe()
  68. pr3, pw3 := io.Pipe()
  69. pr4, pw4 := io.Pipe()
  70. pr5, pw5 := io.Pipe()
  71. pr6, pw6 := io.Pipe()
  72. conn1 := WrapReadWriteCloser(pr, pw2)
  73. conn2 := WrapReadWriteCloser(pr2, pw)
  74. conn3 := WrapReadWriteCloser(pr3, pw4)
  75. conn4 := WrapReadWriteCloser(pr4, pw3)
  76. conn5 := WrapReadWriteCloser(pr5, pw6)
  77. conn6 := WrapReadWriteCloser(pr6, pw5)
  78. r1, err := crypto.NewReader(conn3, []byte(key))
  79. assert.NoError(err)
  80. w1, err := crypto.NewWriter(conn3, []byte(key))
  81. assert.NoError(err)
  82. r2, err := crypto.NewReader(conn4, []byte(key))
  83. assert.NoError(err)
  84. w2, err := crypto.NewWriter(conn4, []byte(key))
  85. assert.NoError(err)
  86. go Join(conn2, WrapReadWriteCloser(r1, w1))
  87. go Join(WrapReadWriteCloser(r2, w2), conn5)
  88. buf := make([]byte, 128)
  89. conn1.Write([]byte(text1))
  90. conn6.Write([]byte(text2))
  91. n, err = conn6.Read(buf)
  92. assert.NoError(err)
  93. assert.Equal(text1, string(buf[:n]))
  94. n, err = conn1.Read(buf)
  95. assert.NoError(err)
  96. assert.Equal(text2, string(buf[:n]))
  97. conn1.Close()
  98. conn2.Close()
  99. conn3.Close()
  100. conn4.Close()
  101. conn5.Close()
  102. conn6.Close()
  103. }