Skip to content

Commit

Permalink
Fix connCtx.RemoteAddr()
Browse files Browse the repository at this point in the history
LocalAddr of the underlying conn was returned from RemoteAddr.
Add TestLocalAddrAndRemoteAddr.
  • Loading branch information
bjdgyc authored and at-wat committed Apr 3, 2020
1 parent e14179e commit 513a0e3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
* [Atsushi Watanabe](https://github.com/at-wat)
* [Julien Salleyron](https://github.com/juliens) - *Server Name Indication*
* [Jeroen de Bruijn](https://github.com/vidavidorra)
* [bjdgyc](https://github.com/bjdgyc)

### License
MIT License - see [LICENSE](LICENSE) for full text
2 changes: 1 addition & 1 deletion internal/net/connctx/connctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (c *connCtx) LocalAddr() net.Addr {
}

func (c *connCtx) RemoteAddr() net.Addr {
return c.nextConn.LocalAddr()
return c.nextConn.RemoteAddr()
}

func (c *connCtx) Conn() net.Conn {
Expand Down
33 changes: 33 additions & 0 deletions internal/net/connctx/connctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,36 @@ func TestWriteClosed(t *testing.T) {
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
}
}

// Test for TestLocalAddrAndRemoteAddr
type stringAddr struct {
network string
addr string
}

func (a stringAddr) Network() string { return a.network }
func (a stringAddr) String() string { return a.addr }

type connAddrMock struct{}

func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
func (*connAddrMock) Read(b []byte) (n int, err error) { panic("unimplemented") }
func (*connAddrMock) Write(b []byte) (n int, err error) { panic("unimplemented") }
func (*connAddrMock) Close() error { panic("unimplemented") }
func (*connAddrMock) SetDeadline(t time.Time) error { panic("unimplemented") }
func (*connAddrMock) SetReadDeadline(t time.Time) error { panic("unimplemented") }
func (*connAddrMock) SetWriteDeadline(t time.Time) error { panic("unimplemented") }

func TestLocalAddrAndRemoteAddr(t *testing.T) {
c := New(&connAddrMock{})
al := c.LocalAddr()
ar := c.RemoteAddr()

if al.String() != "local_addr" {
t.Error("Wrong LocalAddr implementation")
}
if ar.String() != "remote_addr" {
t.Error("Wrong RemoteAddr implementation")
}
}

0 comments on commit 513a0e3

Please sign in to comment.