| 270 | } |
| 271 | |
| 272 | func TestCryptoConn_RW(t *testing.T) { |
| 273 | cipher := NewCipher([]byte(pass)) |
| 274 | var nilCipherHandler CipherHandler = func(conn net.Conn) (cryptoConn *CryptoConn, err error) { |
| 275 | cryptoConn = NewConn(conn, cipher) |
| 276 | return |
| 277 | } |
| 278 | |
| 279 | Convey("server client OK", t, func(c C) { |
| 280 | msg := "xxxxxxxxxxxxxxxx" |
| 281 | l, _ := NewCryptoListener("tcp", "127.0.0.1:0", nilCipherHandler) |
| 282 | wg := sync.WaitGroup{} |
| 283 | wg.Add(1) |
| 284 | go func() { |
| 285 | rBuf := make([]byte, len(msg)) |
| 286 | conn, err := l.Accept() |
| 287 | |
| 288 | if c, ok := conn.(*CryptoConn); ok { |
| 289 | conn, err = l.CHandler(c.Conn) |
| 290 | if err != nil { |
| 291 | err = errors.Wrap(err, "handle ETLS handler failed") |
| 292 | return |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | n, err := conn.Read(rBuf) |
| 297 | c.So(n, ShouldEqual, len(msg)) |
| 298 | c.So(string(rBuf), ShouldResemble, msg) |
| 299 | c.So(err, ShouldBeNil) |
| 300 | wg.Done() |
| 301 | }() |
| 302 | conn, _ := Dial("tcp", l.Addr().String(), cipher) |
| 303 | n, err := conn.Write([]byte(msg)) |
| 304 | So(n, ShouldEqual, len(msg)) |
| 305 | So(err, ShouldBeNil) |
| 306 | wg.Wait() |
| 307 | }) |
| 308 | } |
| 309 | |
| 310 | func TestDialTimeout(t *testing.T) { |
| 311 | Convey("Test dial timeout", t, func(c C) { |