@@ -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
543611end
0 commit comments