Repository: pion/dtls
Pull request #247 (2020-04-03 14:24:58)

Fix connCtx.RemoteAddr()
LocalAddr of the underlying conn was returned from RemoteAddr.
Add TestLocalAddrAndRemoteAddr.

Squash https://github.com/pion/dtls/pull/245 and https://github.com/pion/dtls/pull/246.
Lint errors are fixed.

closes #245 
closes #246

codecov[bot]: # [Codecov](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=h1) Report
> Merging [#247](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=desc) into [master](https://codecov.io/gh/pion/dtls/commit/e14179ef92b1e601ce5fa9214a2fa107c5ab4d86?src=pr&el=desc) will **decrease** coverage by `0.04%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pion/dtls/pull/247/graphs/tree.svg?width=650&token=bcCB2jB88I&height=150&src=pr)](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #247      +/-   ##
==========================================
- Coverage    77.8%   77.75%   -0.05%     
==========================================
  Files          76       76              
  Lines        4037     4037              
==========================================
- Hits         3141     3139       -2     
- Misses        619      620       +1     
- Partials      277      278       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #go | `77.79% <100%> (-0.03%)` | :arrow_down: |
| #wasm | `72.72% <100%> (+0.04%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/net/connctx/connctx.go](https://codecov.io/gh/pion/dtls/pull/247/diff?src=pr&el=tree#diff-aW50ZXJuYWwvbmV0L2Nvbm5jdHgvY29ubmN0eC5nbw==) | `86.41% <100%> (-2.47%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=footer). Last update [e14179e...7e83310](https://codecov.io/gh/pion/dtls/pull/247?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index fe6310c..6ce930d 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/internal/net/connctx/connctx.go b/internal/net/connctx/connctx.go
index 3503b42..3948994 100644
--- a/internal/net/connctx/connctx.go
+++ b/internal/net/connctx/connctx.go
@@ -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 {


Test patch:
diff --git a/internal/net/connctx/connctx_test.go b/internal/net/connctx/connctx_test.go
index cd906ab..7bc2c77 100644
--- a/internal/net/connctx/connctx_test.go
+++ b/internal/net/connctx/connctx_test.go
@@ -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")
+	}
+}