tcp_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. )
  20. func TestWithCompression(t *testing.T) {
  21. assert := assert.New(t)
  22. // Forward compression bytes.
  23. pr, pw := io.Pipe()
  24. pr2, pw2 := io.Pipe()
  25. conn1 := WrapReadWriteCloser(pr, pw2)
  26. conn2 := WrapReadWriteCloser(pr2, pw)
  27. compressionStream1 := WithCompression(conn1)
  28. compressionStream2 := WithCompression(conn2)
  29. var (
  30. n int
  31. err error
  32. )
  33. text := "1234567812345678"
  34. buf := make([]byte, 256)
  35. go compressionStream1.Write([]byte(text))
  36. n, err = compressionStream2.Read(buf)
  37. assert.NoError(err)
  38. assert.Equal(text, string(buf[:n]))
  39. go compressionStream2.Write([]byte(text))
  40. n, err = compressionStream1.Read(buf)
  41. assert.NoError(err)
  42. assert.Equal(text, string(buf[:n]))
  43. }
  44. func TestWithEncryption(t *testing.T) {
  45. assert := assert.New(t)
  46. var (
  47. n int
  48. err error
  49. )
  50. text1 := "Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language."
  51. text2 := "An interactive introduction to Go in three sections. The first section covers basic syntax and data structures; the second discusses methods and interfaces; and the third introduces Go's concurrency primitives. Each section concludes with a few exercises so you can practice what you've learned. You can take the tour online or install it locally with"
  52. key := "authkey"
  53. // Forward enrypted bytes.
  54. pr, pw := io.Pipe()
  55. pr2, pw2 := io.Pipe()
  56. pr3, pw3 := io.Pipe()
  57. pr4, pw4 := io.Pipe()
  58. pr5, pw5 := io.Pipe()
  59. pr6, pw6 := io.Pipe()
  60. conn1 := WrapReadWriteCloser(pr, pw2)
  61. conn2 := WrapReadWriteCloser(pr2, pw)
  62. conn3 := WrapReadWriteCloser(pr3, pw4)
  63. conn4 := WrapReadWriteCloser(pr4, pw3)
  64. conn5 := WrapReadWriteCloser(pr5, pw6)
  65. conn6 := WrapReadWriteCloser(pr6, pw5)
  66. encryptStream1, err := WithEncryption(conn3, []byte(key))
  67. assert.NoError(err)
  68. encryptStream2, err := WithEncryption(conn4, []byte(key))
  69. assert.NoError(err)
  70. go Join(conn2, encryptStream1)
  71. go Join(encryptStream2, conn5)
  72. buf := make([]byte, 1024)
  73. conn1.Write([]byte(text1))
  74. conn6.Write([]byte(text2))
  75. n, err = conn6.Read(buf)
  76. assert.NoError(err)
  77. assert.Equal(text1, string(buf[:n]))
  78. n, err = conn1.Read(buf)
  79. assert.NoError(err)
  80. }