Skip to content

Commit

Permalink
Merge pull request #45 from verypossible/hci-bug
Browse files Browse the repository at this point in the history
Fix bug in the error returns of HCI
  • Loading branch information
danielspofford committed May 29, 2019
2 parents bc371e0 + aa5616c commit b425917
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/harald/hci.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ defmodule Harald.HCI do

@impl Serializable
def deserialize(<<4, rest::binary>>) do
Event.deserialize(rest)
case Event.deserialize(rest) do
{:ok, _} = ret -> ret
{:error, bin} when is_binary(bin) -> {:error, <<4, bin::binary>>}
{:error, data} -> {:error, data}
end
end

def deserialize(bin), do: {:error, bin}
Expand Down
17 changes: 17 additions & 0 deletions lib/harald/serializable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ defmodule Harald.Serializable do
@callback serialize(term()) :: {:ok, binary()} | {:error, term()}

@callback deserialize(binary()) :: {:ok, term()} | {:error, term()}

@doc """
Asserts that `bin` will serialize symmetrically.
If `bin` deserializes into `{:ok, data}`, `data` should serialize perfectly back into `bin`.
"""
defmacro assert_symmetry(mod, bin) do
quote bind_quoted: [bin: bin, mod: mod] do
import ExUnit.Assertions, only: [assert: 1, assert: 2]

case mod.deserialize(bin) do
{:ok, data} -> assert {:ok, bin} == mod.serialize(data)
{:error, data} when not is_binary(data) -> true
{:error, bin2} -> assert bin == bin2
end
end
end
end
8 changes: 3 additions & 5 deletions test/harald/hci_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Harald.HCITest do
use ExUnitProperties
alias Harald.Generators.HCI, as: HCIGen
alias Harald.HCI
require Harald.Serializable, as: Serializable

doctest Harald.HCI, import: true

Expand Down Expand Up @@ -61,11 +62,8 @@ defmodule Harald.HCITest do
end
end

check all parameters <- StreamData.binary() do
case HCI.deserialize(parameters) do
{:ok, data} -> assert {:ok, parameters} == HCI.serialize(data)
{:error, _} -> :ok
end
check all bin <- StreamData.binary() do
Serializable.assert_symmetry(HCI, bin)
end
end
end

0 comments on commit b425917

Please sign in to comment.