Skip to content

Commit 99fc27a

Browse files
committed
Requested changes
1 parent bad6656 commit 99fc27a

File tree

2 files changed

+97
-59
lines changed

2 files changed

+97
-59
lines changed

test/priv/gatherer_test.exs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,90 @@ defmodule ExICE.Priv.GathererTest do
100100
assert port in port_range
101101
end
102102
end
103+
104+
describe "host to prefabricated srflx mapper" do
105+
@ipv4 {10, 10, 10, 10}
106+
@ipv6 {64_512, 0, 0, 0, 0, 0, 0, 1}
107+
@invalid_ip :invalid_ip
108+
109+
@ipv4_filter &:inet.is_ipv4_address(&1)
110+
111+
test "adds srflx candidate" do
112+
ip_filter = fn
113+
{192, 168, 0, 2} -> false
114+
_ -> true
115+
end
116+
117+
{local_preferences, host_cands} = setup_gatherer(ip_filter)
118+
119+
assert [%Candidate.Srflx{base: %{address: @ipv4}}] =
120+
Gatherer.fabricate_srflx_candidates(
121+
host_cands,
122+
fn _ip -> @ipv4 end,
123+
local_preferences
124+
)
125+
end
126+
127+
test "creates only one candidate if external ip is repeated" do
128+
{local_preferences, host_cands} = setup_gatherer(@ipv4_filter)
129+
130+
assert [%Candidate.Srflx{base: %{address: @ipv4}}] =
131+
Gatherer.fabricate_srflx_candidates(
132+
host_cands,
133+
fn _ip -> @ipv4 end,
134+
local_preferences
135+
)
136+
end
137+
138+
test "ignores one to one mapping" do
139+
{local_preferences, host_cands} = setup_gatherer(@ipv4_filter)
140+
141+
assert [] ==
142+
Gatherer.fabricate_srflx_candidates(
143+
host_cands,
144+
fn ip -> ip end,
145+
local_preferences
146+
)
147+
end
148+
149+
test "ignores if ip types is not the same" do
150+
{local_preferences, host_cands} = setup_gatherer(@ipv4_filter)
151+
152+
assert [] ==
153+
Gatherer.fabricate_srflx_candidates(
154+
host_cands,
155+
fn _ip -> @ipv6 end,
156+
local_preferences
157+
)
158+
end
159+
160+
test "ignores when function returns nil value" do
161+
{local_preferences, host_cands} = setup_gatherer(@ipv4_filter)
162+
163+
assert [] ==
164+
Gatherer.fabricate_srflx_candidates(
165+
host_cands,
166+
fn _ip -> nil end,
167+
local_preferences
168+
)
169+
end
170+
171+
test "ignores when function returns invalid value" do
172+
{local_preferences, host_cands} = setup_gatherer(@ipv4_filter)
173+
174+
assert [] ==
175+
Gatherer.fabricate_srflx_candidates(
176+
host_cands,
177+
fn _ip -> @invalid_ip end,
178+
local_preferences
179+
)
180+
end
181+
182+
defp setup_gatherer(ip_filter) do
183+
gatherer = Gatherer.new(IfDiscovery.Mock, Transport.Mock, ip_filter, [0])
184+
assert {:ok, sockets} = Gatherer.open_sockets(gatherer)
185+
186+
Gatherer.gather_host_candidates(gatherer, %{}, sockets)
187+
end
188+
end
103189
end

test/priv/ice_agent_test.exs

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,64 +2635,29 @@ defmodule ExICE.Priv.ICEAgentTest do
26352635
alias ExICE.Priv.Candidate
26362636

26372637
@ipv4 {10, 10, 10, 10}
2638-
@ipv6 {64_512, 0, 0, 0, 0, 0, 0, 1}
2639-
@invalid_ip :invalid_ip
26402638

26412639
test "adds srflx candidate" do
2642-
ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @ipv4 end)
2643-
2644-
assert [%Candidate.Srflx{base: %{address: @ipv4}}] = srflx_candidates(ice_agent)
2645-
2646-
assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}}
2647-
assert_receive {:ex_ice, _pid, {:new_candidate, srflx_cand}}
2648-
2649-
assert host_cand =~ "typ host"
2650-
assert srflx_cand =~ "typ srflx"
2651-
end
2652-
2653-
test "creates only one candidate if external ip is repeated" do
2654-
ice_agent = spawn_ice_agent(IfDiscovery.MockMulti, fn _ip -> @ipv4 end)
2640+
ice_agent =
2641+
%ICEAgent{gathering_state: :complete} =
2642+
ICEAgent.new(
2643+
controlling_process: self(),
2644+
role: :controlled,
2645+
transport_module: Transport.Mock,
2646+
if_discovery_module: IfDiscovery.MockSingle,
2647+
host_to_srflx_ip_mapper: fn _ip -> @ipv4 end
2648+
)
2649+
|> ICEAgent.set_remote_credentials("remoteufrag", "remotepwd")
2650+
|> ICEAgent.gather_candidates()
26552651

26562652
assert [%Candidate.Srflx{base: %{address: @ipv4}}] = srflx_candidates(ice_agent)
26572653

26582654
assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}}
2659-
assert_receive {:ex_ice, _pid, {:new_candidate, host_cand_2}}
26602655
assert_receive {:ex_ice, _pid, {:new_candidate, srflx_cand}}
26612656

26622657
assert host_cand =~ "typ host"
2663-
assert host_cand_2 =~ "typ host"
26642658
assert srflx_cand =~ "typ srflx"
26652659
end
26662660

2667-
test "ignores one to one mapping" do
2668-
ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn ip -> ip end)
2669-
2670-
assert [] == srflx_candidates(ice_agent)
2671-
2672-
assert_receive {:ex_ice, _pid, {:new_candidate, host_cand}}
2673-
refute_receive {:ex_ice, _pid, {:new_candidate, _srflx_cand}}
2674-
2675-
assert host_cand =~ "typ host"
2676-
end
2677-
2678-
test "ignores if ip types is not the same" do
2679-
ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @ipv6 end)
2680-
2681-
assert [] == srflx_candidates(ice_agent)
2682-
end
2683-
2684-
test "ignores when function returns nil value" do
2685-
ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> nil end)
2686-
2687-
assert [] == srflx_candidates(ice_agent)
2688-
end
2689-
2690-
test "ignores when function returns invalid value" do
2691-
ice_agent = spawn_ice_agent(IfDiscovery.MockSingle, fn _ip -> @invalid_ip end)
2692-
2693-
assert [] == srflx_candidates(ice_agent)
2694-
end
2695-
26962661
test "works with STUN enabled" do
26972662
ice_agent =
26982663
ICEAgent.new(
@@ -2732,19 +2697,6 @@ defmodule ExICE.Priv.ICEAgentTest do
27322697
assert [%Candidate.Srflx{}] = srflx_candidates(ice_agent)
27332698
end
27342699

2735-
defp spawn_ice_agent(discovery_module, host_to_srflx_ip_mapper) do
2736-
%ICEAgent{gathering_state: :complete} =
2737-
ICEAgent.new(
2738-
controlling_process: self(),
2739-
role: :controlled,
2740-
transport_module: Transport.Mock,
2741-
if_discovery_module: discovery_module,
2742-
host_to_srflx_ip_mapper: host_to_srflx_ip_mapper
2743-
)
2744-
|> ICEAgent.set_remote_credentials("remoteufrag", "remotepwd")
2745-
|> ICEAgent.gather_candidates()
2746-
end
2747-
27482700
defp srflx_candidates(ice_agent) do
27492701
ice_agent.local_cands
27502702
|> Map.values()

0 commit comments

Comments
 (0)