Skip to content

Commit 513a0e3

Browse files
bjdgycat-wat
authored andcommitted
Fix connCtx.RemoteAddr()
LocalAddr of the underlying conn was returned from RemoteAddr. Add TestLocalAddrAndRemoteAddr.
1 parent e14179e commit 513a0e3

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
140140
* [Atsushi Watanabe](https://github.com/at-wat)
141141
* [Julien Salleyron](https://github.com/juliens) - *Server Name Indication*
142142
* [Jeroen de Bruijn](https://github.com/vidavidorra)
143+
* [bjdgyc](https://github.com/bjdgyc)
143144

144145
### License
145146
MIT License - see [LICENSE](LICENSE) for full text

internal/net/connctx/connctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (c *connCtx) LocalAddr() net.Addr {
148148
}
149149

150150
func (c *connCtx) RemoteAddr() net.Addr {
151-
return c.nextConn.LocalAddr()
151+
return c.nextConn.RemoteAddr()
152152
}
153153

154154
func (c *connCtx) Conn() net.Conn {

internal/net/connctx/connctx_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,36 @@ func TestWriteClosed(t *testing.T) {
195195
t.Errorf("Wrong data length, expected %d, got %d", 0, n)
196196
}
197197
}
198+
199+
// Test for TestLocalAddrAndRemoteAddr
200+
type stringAddr struct {
201+
network string
202+
addr string
203+
}
204+
205+
func (a stringAddr) Network() string { return a.network }
206+
func (a stringAddr) String() string { return a.addr }
207+
208+
type connAddrMock struct{}
209+
210+
func (*connAddrMock) RemoteAddr() net.Addr { return stringAddr{"remote_net", "remote_addr"} }
211+
func (*connAddrMock) LocalAddr() net.Addr { return stringAddr{"local_net", "local_addr"} }
212+
func (*connAddrMock) Read(b []byte) (n int, err error) { panic("unimplemented") }
213+
func (*connAddrMock) Write(b []byte) (n int, err error) { panic("unimplemented") }
214+
func (*connAddrMock) Close() error { panic("unimplemented") }
215+
func (*connAddrMock) SetDeadline(t time.Time) error { panic("unimplemented") }
216+
func (*connAddrMock) SetReadDeadline(t time.Time) error { panic("unimplemented") }
217+
func (*connAddrMock) SetWriteDeadline(t time.Time) error { panic("unimplemented") }
218+
219+
func TestLocalAddrAndRemoteAddr(t *testing.T) {
220+
c := New(&connAddrMock{})
221+
al := c.LocalAddr()
222+
ar := c.RemoteAddr()
223+
224+
if al.String() != "local_addr" {
225+
t.Error("Wrong LocalAddr implementation")
226+
}
227+
if ar.String() != "remote_addr" {
228+
t.Error("Wrong RemoteAddr implementation")
229+
}
230+
}

0 commit comments

Comments
 (0)