Repository: pion/turn
Pull request #50 (2019-06-15 10:43:20)

Improve five_tuple code coverage
1. change Protocol to uint8.
2. FiveTuple.Equal check with Protocol.


codecov-io: # [Codecov](https://codecov.io/gh/pion/turn/pull/50?src=pr&el=h1) Report
> Merging [#50](https://codecov.io/gh/pion/turn/pull/50?src=pr&el=desc) into [master](https://codecov.io/gh/pion/turn/commit/62a099262ae8e577ef3b53b578532d3718b92595?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

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

```diff
@@           Coverage Diff           @@
##           master      #50   +/-   ##
=======================================
  Coverage   11.79%   11.79%           
=======================================
  Files          16       16           
  Lines         670      670           
=======================================
  Hits           79       79           
  Misses        574      574           
  Partials       17       17
```


| [Impacted Files](https://codecov.io/gh/pion/turn/pull/50?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/allocation/five\_tuple.go](https://codecov.io/gh/pion/turn/pull/50/diff?src=pr&el=tree#diff-aW50ZXJuYWwvYWxsb2NhdGlvbi9maXZlX3R1cGxlLmdv) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/turn/pull/50?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/turn/pull/50?src=pr&el=footer). Last update [62a0992...a165058](https://codecov.io/gh/pion/turn/pull/50?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

songjiayang: LGTM

Patch:
diff --git a/internal/allocation/five_tuple.go b/internal/allocation/five_tuple.go
index dd70744..7c88757 100644
--- a/internal/allocation/five_tuple.go
+++ b/internal/allocation/five_tuple.go
@@ -7,12 +7,12 @@ import (
 )
 
 // Protocol is an enum for relay protocol
-type Protocol int
+type Protocol uint8
 
 // Network protocols for relay
 const (
 	UDP Protocol = iota
-	TCP Protocol = iota
+	TCP
 )
 
 // FiveTuple is the combination (client IP address and port, server IP
@@ -28,5 +28,5 @@ type FiveTuple struct {
 
 // Equal asserts if two FiveTuples are equal
 func (f *FiveTuple) Equal(b *FiveTuple) bool {
-	return ipnet.AddrEqual(f.SrcAddr, b.SrcAddr) && ipnet.AddrEqual(f.DstAddr, b.DstAddr)
+	return f.Protocol == b.Protocol && ipnet.AddrEqual(f.SrcAddr, b.SrcAddr) && ipnet.AddrEqual(f.DstAddr, b.DstAddr)
 }


Test patch:
diff --git a/internal/allocation/five_tuple_test.go b/internal/allocation/five_tuple_test.go
new file mode 100644
index 0000000..0076a5b
--- /dev/null
+++ b/internal/allocation/five_tuple_test.go
@@ -0,0 +1,73 @@
+package allocation
+
+import (
+	"net"
+	"testing"
+)
+
+func TestFiveTupleProtocol(t *testing.T) {
+	udpExpect := Protocol(0)
+	tcpExpect := Protocol(1)
+
+	if udpExpect != UDP {
+		t.Errorf("Invalid UDP Protocol value, expect %d but %d", udpExpect, UDP)
+	}
+
+	if tcpExpect != TCP {
+		t.Errorf("Invalid TCP Protocol value, expect %d but %d", tcpExpect, TCP)
+	}
+}
+
+func TestFiveTupleEqual(t *testing.T) {
+	srcAddr1, _ := net.ResolveUDPAddr("udp", "0.0.0.0:3478")
+	srcAddr2, _ := net.ResolveUDPAddr("udp", "0.0.0.0:3479")
+
+	dstAddr1, _ := net.ResolveUDPAddr("udp", "0.0.0.0:3480")
+	dstAddr2, _ := net.ResolveUDPAddr("udp", "0.0.0.0:3481")
+
+	tt := []struct {
+		name   string
+		expect bool
+		a      *FiveTuple
+		b      *FiveTuple
+	}{
+		{
+			"Equal",
+			true,
+			&FiveTuple{UDP, srcAddr1, dstAddr1},
+			&FiveTuple{UDP, srcAddr1, dstAddr1},
+		},
+		{
+			"DifferentProtocol",
+			false,
+			&FiveTuple{UDP, srcAddr1, dstAddr1},
+			&FiveTuple{TCP, srcAddr1, dstAddr1},
+		},
+		{
+			"DifferentSrcAddr",
+			false,
+			&FiveTuple{UDP, srcAddr1, dstAddr1},
+			&FiveTuple{UDP, srcAddr2, dstAddr1},
+		},
+		{
+			"DifferentDstAddr",
+			false,
+			&FiveTuple{UDP, srcAddr1, dstAddr1},
+			&FiveTuple{UDP, srcAddr1, dstAddr2},
+		},
+	}
+
+	for _, tc := range tt {
+		a := tc.a
+		b := tc.b
+		expect := tc.expect
+
+		t.Run(tc.name, func(t *testing.T) {
+			fact := a.Equal(b)
+
+			if expect != fact {
+				t.Errorf("%v, %v equal check should be %t, but %t", a, b, expect, fact)
+			}
+		})
+	}
+}