Skip to content

Commit 49b87c3

Browse files
authored
Add more unit tests (#25)
1 parent d7cafae commit 49b87c3

File tree

8 files changed

+125
-57
lines changed

8 files changed

+125
-57
lines changed

lib/ex_ice/candidate_pair.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defmodule ExICE.CandidatePair do
5252
}
5353
end
5454

55+
@doc false
5556
@spec schedule_keepalive(t(), Process.dest()) :: t()
5657
def schedule_keepalive(pair, dest \\ self())
5758

@@ -65,6 +66,7 @@ defmodule ExICE.CandidatePair do
6566
%{pair | keepalive_timer: ref}
6667
end
6768

69+
@doc false
6870
@spec recompute_priority(t(), ICEAgent.role()) :: t()
6971
def recompute_priority(pair, role) do
7072
%__MODULE__{pair | priority: priority(role, pair.local_cand, pair.remote_cand)}

test/candidate_pair_test.exs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
defmodule ExICE.CandidatePairTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

4-
# alias ExICE.{Candidate, CandidatePair}
4+
alias ExICE.{Candidate, CandidatePair}
55

6-
# describe "CandidatePair.new/3" do
7-
# test "creates candidate pair with correct priority" do
8-
# addr1 = {192, 168, 1, 1}
9-
# port1 = 12_345
10-
# c1 = Candidate.new(:host, addr1, port1, addr1, port1, nil)
11-
# c1 = %Candidate{c1 | priority: 100}
6+
test "new/3" do
7+
addr1 = {192, 168, 1, 1}
8+
port1 = 12_345
9+
c1 = Candidate.new(:host, addr1, port1, addr1, port1, nil)
10+
c1 = %Candidate{c1 | priority: 100}
1211

13-
# addr2 = {192, 168, 1, 2}
14-
# port2 = 23_456
15-
# c2 = Candidate.new(:host, addr2, port2, addr2, port2, nil)
16-
# c2 = %Candidate{c2 | priority: 200}
12+
addr2 = {192, 168, 1, 2}
13+
port2 = 23_456
14+
c2 = Candidate.new(:host, addr2, port2, addr2, port2, nil)
15+
c2 = %Candidate{c2 | priority: 200}
1716

18-
# c1c2 = CandidatePair.new(:controlling, :frozen, c1, c2)
19-
# assert c1c2.priority == 429_496_730_000
17+
c1c2 = CandidatePair.new(c1, c2, :controlling, :frozen)
18+
assert c1c2.priority == 429_496_730_000
2019

21-
# c2c1 = CandidatePair.new(:controlled, :frozen, c1, c2)
22-
# assert c2c1.priority == 429_496_730_001
20+
c2c1 = CandidatePair.new(c1, c2, :controlled, :frozen)
21+
assert c2c1.priority == 429_496_730_001
2322

24-
# assert abs(c1c2.priority - c2c1.priority) == 1
25-
# end
26-
# end
23+
assert abs(c1c2.priority - c2c1.priority) == 1
24+
end
2725
end

test/candidate_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
defmodule ExICE.CandidateTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

44
alias ExICE.Candidate
55

6-
test "candidate's foundation is calculated correctly" do
6+
test "candidate's foundation" do
77
# FIXME socket shouldn't be nil
88
ip = {192, 168, 1, 1}
99
port = 12_345
@@ -18,7 +18,7 @@ defmodule ExICE.CandidateTest do
1818
assert f1 != f2
1919
end
2020

21-
test "marshal returns correct candidate string representation" do
21+
test "marshal/1" do
2222
ip = {192, 168, 1, 1}
2323
port = 12_345
2424
expected_m_c = "936255739 1 UDP 1234 192.168.1.1 12345 typ host"
@@ -29,7 +29,7 @@ defmodule ExICE.CandidateTest do
2929
assert m_c == expected_m_c
3030
end
3131

32-
test "unmarshal returns correct candidate from its string representation" do
32+
test "unmarshal/1" do
3333
ip = {192, 168, 1, 1}
3434
port = 12_345
3535
m_c = "936255739 1 UDP 1234 192.168.1.1 12345 typ host"

test/gatherer_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ExICE.GathererTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

44
alias ExICE.{Candidate, Gatherer}
55

test/ice_agent/impl_test.exs

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,42 @@ defmodule ExICE.ICEAgent.ImplTest do
1919
end
2020
end
2121

22+
describe "add_remote_candidate/2" do
23+
setup do
24+
ice_agent =
25+
ICEAgent.Impl.new(
26+
controlling_process: self(),
27+
role: :controlling,
28+
if_discovery_module: IfDiscovery.Mock,
29+
transport_module: Transport.Mock
30+
)
31+
32+
%{ice_agent: ice_agent}
33+
end
34+
35+
test "with correct remote candidate", %{ice_agent: ice_agent} do
36+
remote_cand = Candidate.new(:host, {192, 168, 0, 2}, 8445, nil, nil, nil)
37+
ice_agent = ICEAgent.Impl.add_remote_candidate(ice_agent, Candidate.marshal(remote_cand))
38+
39+
assert [%Candidate{} = r_cand] = ice_agent.remote_cands
40+
# override id for the purpose of comparision
41+
r_cand = %Candidate{r_cand | id: remote_cand.id}
42+
assert r_cand == remote_cand
43+
end
44+
45+
test "with invalid remote candidate", %{ice_agent: ice_agent} do
46+
ice_agent = ICEAgent.Impl.add_remote_candidate(ice_agent, "some invalid candidate string")
47+
assert [] == ice_agent.remote_cands
48+
end
49+
50+
test "after setting end-of-candidates", %{ice_agent: ice_agent} do
51+
remote_cand = Candidate.new(:host, {192, 168, 0, 2}, 8445, nil, nil, nil)
52+
ice_agent = ICEAgent.Impl.end_of_candidates(ice_agent)
53+
ice_agent = ICEAgent.Impl.add_remote_candidate(ice_agent, Candidate.marshal(remote_cand))
54+
assert [] == ice_agent.remote_cands
55+
end
56+
end
57+
2258
describe "incoming binding request" do
2359
setup do
2460
ice_agent =
@@ -502,42 +538,74 @@ defmodule ExICE.ICEAgent.ImplTest do
502538
end
503539
end
504540

505-
test "gather srflx candidates" do
506-
ice_agent =
507-
ICEAgent.Impl.new(
508-
controlling_process: self(),
509-
role: :controlling,
510-
transport_module: Transport.Mock,
511-
if_discovery_module: IfDiscovery.Mock,
512-
stun_servers: ["stun:192.168.0.3:19302"]
513-
)
541+
describe "gather srflx candidates" do
542+
setup do
543+
ice_agent =
544+
ICEAgent.Impl.new(
545+
controlling_process: self(),
546+
role: :controlling,
547+
transport_module: Transport.Mock,
548+
if_discovery_module: IfDiscovery.Mock,
549+
stun_servers: ["stun:192.168.0.3:19302"]
550+
)
551+
|> ICEAgent.Impl.set_remote_credentials("someufrag", "somepwd")
552+
|> ICEAgent.Impl.gather_candidates()
514553

515-
ice_agent = ICEAgent.Impl.set_remote_credentials(ice_agent, "someufrag", "somepwd")
516-
ice_agent = ICEAgent.Impl.gather_candidates(ice_agent)
517-
[local_cand] = ice_agent.local_cands
554+
[local_cand] = ice_agent.local_cands
518555

519-
# assert no transactions are started until handle_timeout is called
520-
assert [{_socket, nil}] = :ets.lookup(:transport_mock, local_cand.socket)
556+
# assert no transactions are started until handle_timeout is called
557+
assert [{_socket, nil}] = :ets.lookup(:transport_mock, local_cand.socket)
521558

522-
ice_agent = ICEAgent.Impl.handle_timeout(ice_agent)
559+
%{ice_agent: ice_agent}
560+
end
523561

524-
assert [{_socket, packet}] = :ets.lookup(:transport_mock, local_cand.socket)
525-
assert {:ok, req} = ExSTUN.Message.decode(packet)
562+
test "success response", %{ice_agent: ice_agent} do
563+
[local_cand] = ice_agent.local_cands
526564

527-
resp =
528-
Message.new(req.transaction_id, %Type{class: :success_response, method: :binding}, [
529-
%XORMappedAddress{address: {192, 168, 0, 2}, port: local_cand.port + 1}
530-
])
531-
|> Message.encode()
565+
ice_agent = ICEAgent.Impl.handle_timeout(ice_agent)
532566

533-
ice_agent =
534-
ICEAgent.Impl.handle_udp(ice_agent, local_cand.socket, {192, 168, 0, 3}, 19_302, resp)
567+
assert [{_socket, packet}] = :ets.lookup(:transport_mock, local_cand.socket)
568+
assert {:ok, req} = ExSTUN.Message.decode(packet)
535569

536-
assert [srflx_cand | _cands] = ice_agent.local_cands
537-
assert srflx_cand.type == :srflx
538-
assert srflx_cand.address == {192, 168, 0, 2}
539-
assert srflx_cand.port == local_cand.port + 1
570+
resp =
571+
Message.new(req.transaction_id, %Type{class: :success_response, method: :binding}, [
572+
%XORMappedAddress{address: {192, 168, 0, 2}, port: local_cand.port + 1}
573+
])
574+
|> Message.encode()
575+
576+
ice_agent =
577+
ICEAgent.Impl.handle_udp(ice_agent, local_cand.socket, {192, 168, 0, 3}, 19_302, resp)
578+
579+
# assert there is a new, srflx candidate
580+
assert [srflx_cand | _cands] = ice_agent.local_cands
581+
assert srflx_cand.type == :srflx
582+
assert srflx_cand.address == {192, 168, 0, 2}
583+
assert srflx_cand.port == local_cand.port + 1
584+
# assert gathering transaction succeeded
585+
assert ice_agent.gathering_transactions[req.transaction_id].state == :complete
586+
end
587+
588+
test "error response", %{ice_agent: ice_agent} do
589+
[local_cand] = ice_agent.local_cands
540590

541-
assert ice_agent.gathering_transactions[req.transaction_id].state == :complete
591+
ice_agent = ICEAgent.Impl.handle_timeout(ice_agent)
592+
593+
assert [{_socket, packet}] = :ets.lookup(:transport_mock, local_cand.socket)
594+
assert {:ok, req} = ExSTUN.Message.decode(packet)
595+
596+
resp =
597+
Message.new(req.transaction_id, %Type{class: :error_response, method: :binding}, [
598+
%ErrorCode{code: 400}
599+
])
600+
|> Message.encode()
601+
602+
ice_agent =
603+
ICEAgent.Impl.handle_udp(ice_agent, local_cand.socket, {192, 168, 0, 3}, 19_302, resp)
604+
605+
# assert there are no new candidates
606+
assert [local_cand] == ice_agent.local_cands
607+
# assert gathering transaction failed
608+
assert ice_agent.gathering_transactions[req.transaction_id].state == :failed
609+
end
542610
end
543611
end

test/ice_agent_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ExICE.ICEAgentTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

44
alias ExICE.ICEAgent
55

test/integration/p2p_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule ExICE.Integration.P2PTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

44
require Logger
55

test/uri_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ExICE.URITest do
44
alias ExICE.URI
55

66
describe "URI.parse/1" do
7-
test "parses valid URI correctly" do
7+
test "with valid URI" do
88
for {uri_string, expected_uri} <- [
99
{
1010
"stun:stun.l.google.com:19302",
@@ -27,7 +27,7 @@ defmodule ExICE.URITest do
2727
end
2828
end
2929

30-
test "returns an error for invalid URI" do
30+
test "with invalid URI" do
3131
for invalid_uri_string <- [
3232
"",
3333
"some random string",

0 commit comments

Comments
 (0)