Skip to content

Commit

Permalink
Refactor turn tests, add session turn test
Browse files Browse the repository at this point in the history
  • Loading branch information
farao committed Jul 12, 2020
1 parent a83d762 commit 5df4c05
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
53 changes: 17 additions & 36 deletions test/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,25 @@ defmodule RoomTest do
System.put_env("SIGNALTOWER_TURN_SECRET", "verysecretpassphrase1234")
room_pid = create_room("r-room3")

go = fn timestamp_before ->
spawn_user_no_join(fn ->
GenServer.call(room_pid, {:join, self(), %{user: "1"}, 0})
# no token produced yet
spawn_user_no_join(fn ->
GenServer.call(room_pid, {:join, self(), %{user: "1"}, 0})
receive_and_check_turn_credentials(0)
end)

assert_receive(
{:to_user,
%{
event: "joined_room",
own_id: own_id,
turn_user: user,
turn_password: pw
}},
1000
)

[timestamp_str, id] = String.split(user, ":")
{timestamp, ""} = Integer.parse(timestamp_str)
assert own_id == id
assert System.os_time(:second) < timestamp
assert timestamp < System.os_time(:second) + 3 * 60 * 60 + 10
assert timestamp_before <= timestamp

assert pw ==
:crypto.mac(
:hmac,
:sha,
to_charlist("verysecretpassphrase1234"),
to_charlist(user)
)
|> Base.encode64()
end)
end
# previous token is depleted
spawn_user_no_join(fn ->
previous_expiry = System.os_time(:second) - 2000
GenServer.call(room_pid, {:join, self(), %{user: "1"}, previous_expiry})
receive_and_check_turn_credentials(previous_expiry)
end)

go.(0)
# is depleted
go.(System.os_time(:second) - 200)
# is still valid
go.(System.os_time(:second) + 200)
# previous token is still valid
spawn_user_no_join(fn ->
previous_expiry = System.os_time(:second) + 2000
GenServer.call(room_pid, {:join, self(), %{user: "1"}, previous_expiry})
receive_and_check_turn_credentials(previous_expiry)
end)

wait_for_breaks(2)
end
Expand Down
38 changes: 38 additions & 0 deletions test/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ defmodule SessionTest do
wait_for_breaks(2)
end

test "join and check turn expiry" do
System.put_env("SIGNALTOWER_TURN_SECRET", "verysecretpassphrase1234")

# no token produced yet
Session.handle_message(
%{
"event" => "join_room",
"room_id" => "s-room1",
"status" => %{user: "0"}
},
@initial_state
)
receive_and_check_turn_credentials(0)

# previous token is depleted
previous_expiry = System.os_time(:second) - 2000
Session.handle_message(
%{
"event" => "join_room",
"room_id" => "s-room1",
"status" => %{user: "0"}
},
%{room: nil, turn_token_expiry: previous_expiry}
)
receive_and_check_turn_credentials(previous_expiry)

# previous token is still valid
Session.handle_message(
%{
"event" => "join_room",
"room_id" => "s-room1",
"status" => %{user: "0"}
},
%{room: nil, turn_token_expiry: previous_expiry}
)
receive_and_check_turn_credentials(previous_expiry)
end

test "leave explicitly" do
_client1 =
create_client("s-room13", fn room, _ ->
Expand Down
29 changes: 29 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,33 @@ defmodule TestHelper do
def wait_for_breaks(n) when n > 0 do
1..n |> Enum.each(fn _ -> assert_receive :break, 10_000 end)
end

def receive_and_check_turn_credentials(expiry_before) do
assert_receive(
{:to_user,
%{
event: "joined_room",
own_id: own_id,
turn_user: user,
turn_password: pw
}},
1000
)

[expiry_str, id] = String.split(user, ":")
expiry = String.to_integer(expiry_str)
assert own_id == id
assert System.os_time(:second) < expiry
assert expiry < System.os_time(:second) + 3 * 60 * 60 + 10
assert expiry_before <= expiry

assert pw ==
:crypto.mac(
:hmac,
:sha,
to_charlist("verysecretpassphrase1234"),
to_charlist(user)
)
|> Base.encode64()
end
end

0 comments on commit 5df4c05

Please sign in to comment.