New application aeserialization for shared deps with compiler
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2018, Aeternity Anstalt
|
||||
%%%-------------------------------------------------------------------
|
||||
|
||||
-module(aeser_api_encoder_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(TEST_MODULE, aeser_api_encoder).
|
||||
-define(TYPES, [ {key_block_hash , 32}
|
||||
, {micro_block_hash , 32}
|
||||
, {block_tx_hash , 32}
|
||||
, {block_state_hash , 32}
|
||||
, {channel , 32}
|
||||
, {contract_pubkey , 32}
|
||||
, {transaction , not_applicable}
|
||||
, {tx_hash , 32}
|
||||
, {oracle_pubkey , 32}
|
||||
, {oracle_query_id , 32}
|
||||
, {account_pubkey , 32}
|
||||
, {signature , 64}
|
||||
, {name , not_applicable}
|
||||
, {commitment , 32}
|
||||
, {peer_pubkey , 32}
|
||||
, {state , 32}
|
||||
, {poi , not_applicable}]).
|
||||
|
||||
encode_decode_test_() ->
|
||||
[{"Byte sizes are correct",
|
||||
fun() ->
|
||||
lists:foreach(
|
||||
fun({Type, ByteSize}) ->
|
||||
{_Type, _, ByteSize} = {Type, ByteSize,
|
||||
?TEST_MODULE:byte_size_for_type(Type)}
|
||||
end,
|
||||
?TYPES)
|
||||
end
|
||||
},
|
||||
{"Serialize/deserialize known types",
|
||||
fun() ->
|
||||
lists:foreach(
|
||||
fun({Type, Size0}) ->
|
||||
ByteSize =
|
||||
case Size0 of
|
||||
not_applicable -> 42;
|
||||
_ when is_integer(Size0) -> Size0
|
||||
end,
|
||||
Key = <<42:ByteSize/unit:8>>,
|
||||
EncodedKey = ?TEST_MODULE:encode(Type, Key),
|
||||
{Type, Key} = ?TEST_MODULE:decode(EncodedKey),
|
||||
{ok, Key} = ?TEST_MODULE:safe_decode(Type, EncodedKey)
|
||||
end,
|
||||
?TYPES)
|
||||
end
|
||||
},
|
||||
{"Key size check works",
|
||||
fun() ->
|
||||
lists:foreach(
|
||||
fun({_Type, not_applicable}) -> ok;
|
||||
({Type, ByteSize}) ->
|
||||
CheckIlligalSize =
|
||||
fun(S) ->
|
||||
Key = <<42:S/unit:8>>,
|
||||
EncodedKey = ?TEST_MODULE:encode(Type, Key),
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, EncodedKey)
|
||||
end,
|
||||
CheckIlligalSize(0),
|
||||
CheckIlligalSize(ByteSize - 1),
|
||||
CheckIlligalSize(ByteSize + 1)
|
||||
end,
|
||||
?TYPES)
|
||||
end
|
||||
},
|
||||
{"Missing prefix",
|
||||
fun() ->
|
||||
lists:foreach(
|
||||
fun({Type, Size0}) ->
|
||||
ByteSize =
|
||||
case Size0 of
|
||||
not_applicable -> 42;
|
||||
_ when is_integer(Size0) -> Size0
|
||||
end,
|
||||
Key = <<42:ByteSize/unit:8>>,
|
||||
EncodedKey = ?TEST_MODULE:encode(Type, Key),
|
||||
<<_PartOfPrefix:1/unit:8, RestOfKey/binary>> = EncodedKey,
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey),
|
||||
|
||||
<<_PrefixWithoutDelimiter:2/unit:8, RestOfKey1/binary>> = EncodedKey,
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey1),
|
||||
|
||||
<<_WholePrefix:3/unit:8, RestOfKey2/binary>> = EncodedKey,
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey2)
|
||||
end,
|
||||
?TYPES)
|
||||
end
|
||||
},
|
||||
{"Piece of encoded key",
|
||||
fun() ->
|
||||
lists:foreach(
|
||||
fun({Type, Size0}) ->
|
||||
ByteSize =
|
||||
case Size0 of
|
||||
not_applicable -> 42;
|
||||
_ when is_integer(Size0) -> Size0
|
||||
end,
|
||||
Key = <<42:ByteSize/unit:8>>,
|
||||
EncodedKey = ?TEST_MODULE:encode(Type, Key),
|
||||
HalfKeySize = byte_size(EncodedKey) div 2,
|
||||
<<HalfKey:HalfKeySize/unit:8, RestOfKey/binary>> = EncodedKey,
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, HalfKey),
|
||||
{error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey)
|
||||
end,
|
||||
?TYPES)
|
||||
end
|
||||
},
|
||||
{"Encode/decode binary with only zeros",
|
||||
fun() ->
|
||||
Bins = [<<0:Size/unit:8>> || Size <- lists:seq(1,64)],
|
||||
lists:foreach(
|
||||
fun(Bin) ->
|
||||
lists:foreach(
|
||||
fun({Type, S}) ->
|
||||
case S =:= byte_size(Bin) orelse S =:= not_applicable of
|
||||
true ->
|
||||
Encoded = ?TEST_MODULE:encode(Type, Bin),
|
||||
{ok, Decoded} = ?TEST_MODULE:safe_decode(Type, Encoded),
|
||||
?assertEqual(Decoded, Bin);
|
||||
false ->
|
||||
ok
|
||||
end,
|
||||
Encoded1 = base58:binary_to_base58(Bin),
|
||||
Decoded1 = base58:base58_to_binary(Encoded1),
|
||||
?assertEqual(Bin, Decoded1)
|
||||
end, ?TYPES)
|
||||
end,
|
||||
Bins)
|
||||
end}
|
||||
].
|
||||
@@ -0,0 +1,87 @@
|
||||
%%% -*- erlang-indent-level:4; indent-tabs-mode: nil -*-
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2018, Aeternity Anstalt
|
||||
%%% @doc
|
||||
%%% EUnit tests for aeser_chain_objects
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(aeser_chain_objects_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(DEFAULT_TAG, account).
|
||||
-define(DEFAULT_VERSION, 1).
|
||||
|
||||
basic_test() ->
|
||||
Template = [{foo, int}, {bar, binary}],
|
||||
Values = [{foo, 1}, {bar, <<2>>}],
|
||||
?assertEqual(Values, deserialize(Template, serialize(Template, Values))).
|
||||
|
||||
basic_fail_test() ->
|
||||
Template = [{foo, int}, {bar, binary}],
|
||||
Values = [{foo, 1}, {bar, 1}],
|
||||
?assertError({illegal_field, _, _, _, _, _}, serialize(Template, Values)).
|
||||
|
||||
list_test() ->
|
||||
Template = [{foo, [int]}, {bar, [binary]}, {baz, [int]}],
|
||||
Values = [{foo, [1]}, {bar, [<<2>>, <<2>>]}, {baz, []}],
|
||||
?assertEqual(Values, deserialize(Template, serialize(Template, Values))).
|
||||
|
||||
list_fail_test() ->
|
||||
Template = [{foo, [int]}, {bar, [binary]}],
|
||||
Values = [{foo, [1]}, {bar, [2, <<2>>]}],
|
||||
?assertError({illegal_field, _, _, _, _, _}, serialize(Template, Values)).
|
||||
|
||||
deep_list_test() ->
|
||||
Template = [{foo, [[int]]}, {bar, [[[[[binary]]]]]}],
|
||||
Values = [{foo, [[1]]}, {bar, [[[[[<<2>>]]]]]}],
|
||||
?assertEqual(Values, deserialize(Template, serialize(Template, Values))).
|
||||
|
||||
deep_list_fail_test() ->
|
||||
Template = [{foo, [[int]]}, {bar, [[[[[binary]]]]]}],
|
||||
Values = [{foo, [[1]]}, {bar, [[[[[2]]]]]}],
|
||||
?assertError({illegal_field, _, _, _, _, _}, serialize(Template, Values)).
|
||||
|
||||
array_test() ->
|
||||
Template = [{foo, {int, binary}}, {bar, [{int, int}]}, {baz, {int}}],
|
||||
Values = [{foo, {1, <<"foo">>}}, {bar, [{1, 2}, {3, 4}, {5, 6}]}, {baz, {1}}],
|
||||
?assertEqual(Values, deserialize(Template, serialize(Template, Values))).
|
||||
|
||||
array_fail_test() ->
|
||||
Template = [{foo, {int, binary}}, {bar, [{int, int}]}, {baz, {int}}],
|
||||
Values = [{foo, {1, <<"foo">>}}, {bar, [{1, 2}, {3, 4}, {5, 6}]}, {baz, {1, 1}}],
|
||||
?assertError({illegal_field, _, _, _, _, _}, serialize(Template, Values)).
|
||||
|
||||
deep_array_test() ->
|
||||
Template = [{foo, {{int, binary}}}, {bar, [{{int}, int}]}, {baz, {{int}}}],
|
||||
Values = [{foo, {{1, <<"foo">>}}}, {bar, [{{1}, 2}, {{3}, 4}, {{5}, 6}]}, {baz, {{1}}}],
|
||||
?assertEqual(Values, deserialize(Template, serialize(Template, Values))).
|
||||
|
||||
deep_array_fail_test() ->
|
||||
Template = [{foo, {{int, binary}}}, {bar, [{{int}, int}]}, {baz, {{binary}}}],
|
||||
Values = [{foo, {{1, <<"foo">>}}}, {bar, [{{1}, 2}, {{3}, 4}, {{5}, 6}]}, {baz, {{1}}}],
|
||||
?assertError({illegal_field, _, _, _, _, _}, serialize(Template, Values)).
|
||||
|
||||
tag_fail_test() ->
|
||||
Template = [{foo, int}, {bar, binary}],
|
||||
Values = [{foo, 1}, {bar, <<2>>}],
|
||||
?assertError({illegal_serialization, _, _, _, _, _, _},
|
||||
deserialize(Template, serialize(Template, Values), signed_tx, ?DEFAULT_VERSION)).
|
||||
|
||||
vsn_fail_test() ->
|
||||
Template = [{foo, int}, {bar, binary}],
|
||||
Values = [{foo, 1}, {bar, <<2>>}],
|
||||
?assertError({illegal_serialization, _, _, _, _, _, _},
|
||||
deserialize(Template, serialize(Template, Values), ?DEFAULT_TAG, 2)).
|
||||
|
||||
deserialize(Template, Bin) ->
|
||||
deserialize(Template, Bin, ?DEFAULT_TAG, ?DEFAULT_VERSION).
|
||||
|
||||
deserialize(Template, Bin, Tag, Vsn) ->
|
||||
aeser_chain_objects:deserialize(Tag, Vsn, Template, Bin).
|
||||
|
||||
serialize(Template, Bin) ->
|
||||
serialize(Template, Bin, ?DEFAULT_TAG, ?DEFAULT_VERSION).
|
||||
|
||||
serialize(Template, Bin, Tag, Vsn) ->
|
||||
aeser_chain_objects:serialize(Tag, Vsn, Template, Bin).
|
||||
@@ -0,0 +1,132 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2017, Aeternity Anstalt
|
||||
%%% @doc Tests for Recursive Length Prefix
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
|
||||
-module(aeser_rlp_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(UNTAGGED_SIZE_LIMIT , 55).
|
||||
-define(UNTAGGED_LIMIT , 127).
|
||||
-define(BYTE_ARRAY_OFFSET , 128).
|
||||
-define(LIST_OFFSET , 192).
|
||||
|
||||
-define(TEST_MODULE, aeser_rlp).
|
||||
|
||||
rlp_one_byte_test() ->
|
||||
B = <<42>>,
|
||||
B = ?TEST_MODULE:encode(B),
|
||||
B = ?TEST_MODULE:decode(B).
|
||||
|
||||
rlp_another_one_byte_test() ->
|
||||
B = <<127>>,
|
||||
B = ?TEST_MODULE:encode(B),
|
||||
B = ?TEST_MODULE:decode(B).
|
||||
|
||||
rlp_zero_bytes_test() ->
|
||||
B = <<>>,
|
||||
S = ?BYTE_ARRAY_OFFSET + 0,
|
||||
<<S, B/binary>> = ?TEST_MODULE:encode(B).
|
||||
|
||||
rlp_two_bytes_test() ->
|
||||
B = <<128>>,
|
||||
S = ?BYTE_ARRAY_OFFSET + 1,
|
||||
<<S, B/binary>> = ?TEST_MODULE:encode(B).
|
||||
|
||||
rlp_one_byte_size_bytes_test() ->
|
||||
L = 55,
|
||||
S = ?BYTE_ARRAY_OFFSET + L,
|
||||
X = << <<X>> || X <- lists:seq(1,L)>>,
|
||||
E = <<S, X/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_tagged_size_one_byte_bytes_test() ->
|
||||
L = 56,
|
||||
Tag = ?BYTE_ARRAY_OFFSET + ?UNTAGGED_SIZE_LIMIT + 1,
|
||||
X = list_to_binary(lists:duplicate(L, 42)),
|
||||
S = byte_size(X),
|
||||
E = <<Tag, S, X/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_tagged_size_two_bytes_bytes_test() ->
|
||||
L = 256,
|
||||
SizeSize = 2,
|
||||
Tag = ?BYTE_ARRAY_OFFSET + ?UNTAGGED_SIZE_LIMIT + SizeSize,
|
||||
X = list_to_binary(lists:duplicate(L, 42)),
|
||||
S = byte_size(X),
|
||||
E = <<Tag, S:SizeSize/unit:8, X/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_zero_bytes_list_test() ->
|
||||
L = 0,
|
||||
Tag = ?LIST_OFFSET + L,
|
||||
X = [],
|
||||
E = <<Tag>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_one_byte_list_test() ->
|
||||
L = 1,
|
||||
Tag = ?LIST_OFFSET + L,
|
||||
X = lists:duplicate(L, <<42>>),
|
||||
E = <<Tag, 42>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_byte_array_list_test() ->
|
||||
L = 55,
|
||||
Tag = ?LIST_OFFSET + L,
|
||||
X = lists:duplicate(L, <<42>>),
|
||||
Y = list_to_binary(X),
|
||||
E = <<Tag, Y/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_byte_array_tagged_size_one_byte_list_test() ->
|
||||
L = 56,
|
||||
SizeSize = 1,
|
||||
Tag = ?LIST_OFFSET + ?UNTAGGED_SIZE_LIMIT + SizeSize,
|
||||
X = lists:duplicate(L, <<42>>),
|
||||
Y = list_to_binary(X),
|
||||
S = byte_size(Y),
|
||||
E = <<Tag, S:SizeSize/unit:8, Y/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
rlp_byte_array_tagged_size_two_bytes_list_test() ->
|
||||
L = 256,
|
||||
SizeSize = 2,
|
||||
Tag = ?LIST_OFFSET + ?UNTAGGED_SIZE_LIMIT + SizeSize,
|
||||
X = lists:duplicate(L, <<42>>),
|
||||
Y = list_to_binary(X),
|
||||
S = byte_size(Y),
|
||||
E = <<Tag, S:SizeSize/unit:8, Y/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E).
|
||||
|
||||
illegal_size_encoding_list_test() ->
|
||||
%% Ensure we start with somehting legal.
|
||||
L = 56,
|
||||
SizeSize = 1,
|
||||
Tag = ?LIST_OFFSET + ?UNTAGGED_SIZE_LIMIT + SizeSize,
|
||||
X = lists:duplicate(L, <<42>>),
|
||||
Y = list_to_binary(X),
|
||||
S = byte_size(Y),
|
||||
E = <<Tag, S:SizeSize/unit:8, Y/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E),
|
||||
|
||||
%% Add leading zeroes to the size field.
|
||||
E1 = <<(Tag + 1), 0, S:SizeSize/unit:8, Y/binary>>,
|
||||
?assertError(leading_zeroes_in_size, ?TEST_MODULE:decode(E1)).
|
||||
|
||||
illegal_size_encoding_byte_array_test() ->
|
||||
%% Ensure we start with somehting legal.
|
||||
L = 256,
|
||||
SizeSize = 2,
|
||||
Tag = ?BYTE_ARRAY_OFFSET + ?UNTAGGED_SIZE_LIMIT + SizeSize,
|
||||
X = list_to_binary(lists:duplicate(L, 42)),
|
||||
S = byte_size(X),
|
||||
E = <<Tag, S:SizeSize/unit:8, X/binary>> = ?TEST_MODULE:encode(X),
|
||||
X = ?TEST_MODULE:decode(E),
|
||||
|
||||
%% Add leading zeroes to the size field.
|
||||
E1 = <<(Tag + 1), 0, S:SizeSize/unit:8, X/binary>>,
|
||||
?assertError(leading_zeroes_in_size, ?TEST_MODULE:decode(E1)).
|
||||
|
||||
Reference in New Issue
Block a user