Skip to content

Commit

Permalink
fix: adjust ssz unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avilagaston9 committed Apr 20, 2024
1 parent 0392dc6 commit 38bc1ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
1 change: 0 additions & 1 deletion lib/ssz_ex/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule SszEx.Utils do

alias LambdaEthereumConsensus.Utils.BitList
alias LambdaEthereumConsensus.Utils.BitVector
alias SszEx.Error

@allowed_uints [8, 16, 32, 64, 128, 256]
@bits_per_byte 8
Expand Down
44 changes: 23 additions & 21 deletions test/unit/ssz_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ defmodule Unit.SSZExTest do
error_message =
"Invalid binary length while encoding list of {:int, 32}.\nExpected max_size: 3.\nFound: 4\n"

result = SszEx.encode([230, 380, 523, 23_423], {:list, {:int, 32}, 3})
{:error, result} = SszEx.encode([230, 380, 523, 23_423], {:list, {:int, 32}, 3})

assert error_message == "#{result}"
end
Expand All @@ -444,7 +444,7 @@ defmodule Unit.SSZExTest do
error_message =
"Invalid binary length while decoding list of {:int, 32}.\nExpected max_size: 3.\nFound: 16\n"

result =
{:error, result} =
SszEx.decode(
<<230, 0, 0, 0, 124, 1, 0, 0, 11, 2, 0, 0, 127, 91, 0, 0>>,
{:list, {:int, 32}, 3}
Expand All @@ -457,7 +457,7 @@ defmodule Unit.SSZExTest do
error_message =
"Invalid binary length while decoding vector of {:int, 32}.\nExpected size 12 bytes.\nFound: 6.\n"

result =
{:error, result} =
SszEx.decode(
<<230, 0, 0, 0, 124, 1>>,
{:vector, {:int, 32}, 3}
Expand All @@ -470,7 +470,7 @@ defmodule Unit.SSZExTest do
error_message =
"Invalid binary length while decoding vector of {:int, 32}.\nExpected size 12 bytes.\nFound: 13.\n"

result =
{:error, result} =
SszEx.decode(
<<230, 0, 0, 0, 124, 1, 230, 0, 0, 0, 124, 1, 2>>,
{:vector, {:int, 32}, 3}
Expand All @@ -483,7 +483,7 @@ defmodule Unit.SSZExTest do
error_message =
"Invalid binary length while decoding vector of {:int, 32}.\nExpected size 12 bytes.\nFound: 13.\n"

result =
{:error, result} =
SszEx.decode(
<<230, 0, 0, 0, 124, 1, 230, 0, 0, 0, 124, 1, 1::1>>,
{:vector, {:int, 32}, 3}
Expand Down Expand Up @@ -601,14 +601,14 @@ defmodule Unit.SSZExTest do
assert {:ok, ^encoded_bytes} = SszEx.encode(decoded_bytes, {:bitlist, 31})

encoded_bytes = <<7>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitlist, 1})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitlist, 1})

encoded_bytes = <<124, 3>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitlist, 1})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitlist, 1})

encoded_bytes = <<0>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitlist, 1})
assert %Error{} = SszEx.decode(encoded_bytes, {:bitlist, 16})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitlist, 1})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitlist, 16})
end

test "serialize and deserialize bitvector" do
Expand All @@ -621,13 +621,13 @@ defmodule Unit.SSZExTest do
assert {:ok, ^encoded_bytes} = SszEx.encode(decoded_bytes, {:bitvector, 16})

encoded_bytes = <<255, 255, 255, 255, 255, 1>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitvector, 33})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitvector, 33})

encoded_bytes = <<255, 255, 255, 255, 255, 1::1>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitvector, 41})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitvector, 41})

encoded_bytes = <<0>>
assert %Error{} = SszEx.decode(encoded_bytes, {:bitvector, 9})
assert {:error, %Error{}} = SszEx.decode(encoded_bytes, {:bitvector, 9})
end

test "hash tree root of byte list" do
Expand Down Expand Up @@ -738,21 +738,23 @@ defmodule Unit.SSZExTest do
<<0, 0, 0>>

assert SszEx.decode(encoded_checkpoint, Checkpoint) ==
%Error{
message:
"Invalid binary length while decoding Elixir.Types.Checkpoint. \nExpected 40. \nFound 3.\n"
}
{:error,
%Error{
message:
"Invalid binary length while decoding Elixir.Types.Checkpoint. \nExpected 40. \nFound 3.\n"
}}
end

test "decode longer checkpoint" do
encoded_checkpoint =
<<0::size(41 * 8)>>

assert SszEx.decode(encoded_checkpoint, Checkpoint) ==
%Error{
message:
"Invalid binary length while decoding Elixir.Types.Checkpoint. \nExpected 40. \nFound 41.\n"
}
{:error,
%Error{
message:
"Invalid binary length while decoding Elixir.Types.Checkpoint. \nExpected 40. \nFound 41.\n"
}}
end

test "hash tree root of list exceeding max size" do
Expand All @@ -761,7 +763,7 @@ defmodule Unit.SSZExTest do
error =
"Invalid binary length while merkleizing list of {:int, 8}.\nExpected max_size: 2.\nFound: 4\n"

result = SszEx.hash_tree_root(initial_list, {:list, {:int, 8}, 2})
{:error, result} = SszEx.hash_tree_root(initial_list, {:list, {:int, 8}, 2})
assert error == "#{result}"
end
end

0 comments on commit 38bc1ec

Please sign in to comment.