Skip to content

Commit

Permalink
Add encoding and decoding of TURN methods
Browse files Browse the repository at this point in the history
Implementation added for all TURN methods except
ChannelBind.
  • Loading branch information
Arkadiusz Gil committed Feb 28, 2017
1 parent 8bb8074 commit ad65223
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
12 changes: 11 additions & 1 deletion lib/jerboa/format/header/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ defmodule Jerboa.Format.Header.Type do
@typedoc """
The atom representing a STUN method
"""
@type t :: :binding
@type t :: :binding | :allocate | :refresh | :send | :data | :create_permission

@doc false
def encode(:binding), do: <<0x001::12>>
def encode(:allocate), do: <<0x003::12>>
def encode(:refresh), do: <<0x004::12>>
def encode(:send), do: <<0x006::12>>
def encode(:data), do: <<0x007::12>>
def encode(:create_permission), do: <<0x008::12>>

@doc false
def decode(<<0x001::12>>), do: {:ok, :binding}
def decode(<<0x003::12>>), do: {:ok, :allocate}
def decode(<<0x004::12>>), do: {:ok, :refresh}
def decode(<<0x006::12>>), do: {:ok, :send}
def decode(<<0x007::12>>), do: {:ok, :data}
def decode(<<0x008::12>>), do: {:ok, :create_permission}
def decode(<<m::12>>), do: {:error, UnknownMethodError.exception(method: m)}
end

Expand Down
42 changes: 18 additions & 24 deletions test/jerboa/format/head_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,29 @@ defmodule Jerboa.Format.HeaderTest do
end
end

describe "Header.*.encode/1" do
test "Header.Length.encode/1 encodes length into 16 bits (two bytes)" do
x = Header.Length.encode(%Params{body: <<0,1,0,1>>})

test "bind request method and class in 14 bit type" do
x = Header.Type.encode(%Params{class: :request, method: :binding})

assert 14 === bit_size x
assert <<0x0001::16>> == <<0::2, x::14-bits>>
end

test "length into 16 bits (two bytes)" do
x = Header.Length.encode(%Params{body: <<0,1,0,1>>})

assert 16 === bit_size x
assert <<0, 4>> = x
end
assert 16 === bit_size x
assert <<0, 4>> = x
end

test "binding success response" do
params = %Params{class: :success, method: :binding}
test "Header.Type encode/1 and decode/1 return opposite results" do
allowed = [binding: [:request, :success, :failure],
allocate: [:request, :success, :failure],
refresh: [:request, :success, :failure],
create_permission: [:request, :success, :failure],
send: [:indication],
data: [:indication]]

bin = Header.Type.encode(params)
for {method, classes} <- allowed do
for class <- classes do
params = %Params{class: class, method: method}

assert <<4, 1::6>> == bin
end
end
bin = Header.Type.encode(params)

describe "Header.*.decode/1" do

test "binding request" do
assert {:ok, :request, :binding} == Header.Type.decode <<0::6, 1>>
assert {:ok, ^class, ^method} = Header.Type.decode(bin)
end
end
end

Expand Down

0 comments on commit ad65223

Please sign in to comment.