connection_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package kcp_test
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "v2ray.com/core/testing/assert"
  7. "v2ray.com/core/transport/internet"
  8. "v2ray.com/core/transport/internet/internal"
  9. . "v2ray.com/core/transport/internet/kcp"
  10. )
  11. type NoOpConn struct{}
  12. func (o *NoOpConn) Write(b []byte) (int, error) {
  13. return len(b), nil
  14. }
  15. func (o *NoOpConn) Close() error {
  16. return nil
  17. }
  18. func (o *NoOpConn) Read([]byte) (int, error) {
  19. panic("Should not be called.")
  20. }
  21. func (o *NoOpConn) LocalAddr() net.Addr {
  22. return nil
  23. }
  24. func (o *NoOpConn) RemoteAddr() net.Addr {
  25. return nil
  26. }
  27. func (o *NoOpConn) SetDeadline(time.Time) error {
  28. return nil
  29. }
  30. func (o *NoOpConn) SetReadDeadline(time.Time) error {
  31. return nil
  32. }
  33. func (o *NoOpConn) SetWriteDeadline(time.Time) error {
  34. return nil
  35. }
  36. func (o *NoOpConn) Id() internal.ConnectionId {
  37. return internal.ConnectionId{}
  38. }
  39. func (o *NoOpConn) Reset(auth internet.Authenticator, input func([]byte)) {}
  40. type NoOpRecycler struct{}
  41. func (o *NoOpRecycler) Put(internal.ConnectionId, net.Conn) {}
  42. func TestConnectionReadTimeout(t *testing.T) {
  43. assert := assert.On(t)
  44. conn := NewConnection(1, &NoOpConn{}, &NoOpRecycler{}, NewSimpleAuthenticator(), &Config{})
  45. conn.SetReadDeadline(time.Now().Add(time.Second))
  46. b := make([]byte, 1024)
  47. nBytes, err := conn.Read(b)
  48. assert.Int(nBytes).Equals(0)
  49. assert.Error(err).IsNotNil()
  50. conn.Terminate()
  51. }