Skip to content

Commit d7d07af

Browse files
committed
Add tests on old spec, allow_atom, known_atoms
1 parent 78b6651 commit d7d07af

3 files changed

Lines changed: 87 additions & 38 deletions

File tree

src/msgpack_packer.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pack(List, ?OPTION{spec=new, pack_str=from_list}=Opt) when is_list(List) ->
8383
pack(List, Opt) when is_list(List) ->
8484
pack_array(List, Opt);
8585

86-
pack(Other, Opt) ->
86+
pack(Other, ?OPTION{spec=new} = Opt) ->
8787
handle_ext(Other, Opt).
8888

8989
handle_binary(Bin, ?OPTION{spec=old}) ->

src/msgpack_unpacker.erl

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,45 +120,46 @@ unpack_stream(<<16#C1, _R/binary>>, _) -> throw({badarg, 16#C1});
120120

121121
%% fixext 1 stores an integer and a byte array whose length is 1 byte
122122
unpack_stream(<<16#D4, T:1/signed-integer-unit:8, Data:1/binary, Rest/binary>>,
123-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
124-
maybe_unpack_ext(16#D4, Unpack, T, Data, Rest, Orig);
123+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
124+
maybe_unpack_ext(16#D4, Unpack, T, Data, Rest, Orig, Opt);
125125

126126
%% fixext 2 stores an integer and a byte array whose length is 2 bytes
127127
unpack_stream(<<16#D5, T:1/signed-integer-unit:8, Data:2/binary, Rest/binary>>,
128-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
129-
maybe_unpack_ext(16#D5, Unpack, T, Data, Rest, Orig);
128+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
129+
maybe_unpack_ext(16#D5, Unpack, T, Data, Rest, Orig, Opt);
130130

131131
%% fixext 4 stores an integer and a byte array whose length is 4 bytes
132132
unpack_stream(<<16#D6, T:1/signed-integer-unit:8, Data:4/binary, Rest/binary>>,
133-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
134-
maybe_unpack_ext(16#D6, Unpack, T, Data, Rest, Orig);
133+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
134+
maybe_unpack_ext(16#D6, Unpack, T, Data, Rest, Orig, Opt);
135135

136136
%% fixext 8 stores an integer and a byte array whose length is 8 bytes
137137
unpack_stream(<<16#D7, T:1/signed-integer-unit:8, Data:8/binary, Rest/binary>>,
138-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
139-
maybe_unpack_ext(16#D7, Unpack, T, Data, Rest, Orig);
138+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
139+
maybe_unpack_ext(16#D7, Unpack, T, Data, Rest, Orig, Opt);
140140

141141
%% fixext 16 stores an integer and a byte array whose length is 16 bytes
142142
unpack_stream(<<16#D8, T:1/signed-integer-unit:8, Data:16/binary, Rest/binary>>,
143-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
144-
maybe_unpack_ext(16#D8, Unpack, T, Data, Rest, Orig);
143+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
144+
maybe_unpack_ext(16#D8, Unpack, T, Data, Rest, Orig, Opt);
145145

146146
%% ext 8 stores an integer and a byte array whose length is upto (2^8)-1 bytes:
147147
unpack_stream(<<16#C7, Len:8, Type:1/signed-integer-unit:8, Data:Len/binary, Rest/binary>>,
148-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
149-
maybe_unpack_ext(16#C7, Unpack, Type, Data, Rest, Orig);
148+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
149+
maybe_unpack_ext(16#C7, Unpack, Type, Data, Rest, Orig, Opt);
150150

151151
%% ext 16 stores an integer and a byte array whose length is upto (2^16)-1 bytes:
152152
unpack_stream(<<16#C8, Len:16, Type:1/signed-integer-unit:8, Data:Len/binary, Rest/binary>>,
153-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
154-
maybe_unpack_ext(16#C8, Unpack, Type, Data, Rest, Orig);
153+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
154+
maybe_unpack_ext(16#C8, Unpack, Type, Data, Rest, Orig, Opt);
155155

156156
%% ext 32 stores an integer and a byte array whose length is upto (2^32)-1 bytes:
157157
unpack_stream(<<16#C9, Len:32, Type:1/signed-integer-unit:8, Data:Len/binary, Rest/binary>>,
158-
?OPTION{ext_unpacker=Unpack, original_list=Orig} = _Opt) ->
159-
maybe_unpack_ext(16#C9, Unpack, Type, Data, Rest, Orig);
158+
?OPTION{ext_unpacker=Unpack, original_list=Orig} = Opt) ->
159+
maybe_unpack_ext(16#C9, Unpack, Type, Data, Rest, Orig, Opt);
160160

161-
unpack_stream(_Bin, _) -> throw(incomplete).
161+
unpack_stream(_Bin, _Opt) ->
162+
throw(incomplete).
162163

163164
-spec unpack_array(binary(), non_neg_integer(), [msgpack:object()], ?OPTION{}) ->
164165
{[msgpack:object()], binary()} | no_return().
@@ -234,13 +235,19 @@ unpack_str(Binary) ->
234235
String -> String
235236
end.
236237

237-
maybe_unpack_ext(F, undefined, _, _, _Rest, _) -> throw({badarg, {bad_ext, F}});
238-
maybe_unpack_ext(_, Unpack, Type, Data, Rest, Orig) when is_function(Unpack, 3) ->
238+
maybe_unpack_ext(F, _, _, _, _Rest, _, ?OPTION{spec=old}) ->
239+
%% trying to unpack new ext formats with old unpacker
240+
throw({badarg, {new_spec, F}});
241+
maybe_unpack_ext(F, undefined, _, _, _Rest, _, _) ->
242+
throw({badarg, {bad_ext, F}});
243+
maybe_unpack_ext(_, Unpack, Type, Data, Rest, Orig, _)
244+
when is_function(Unpack, 3) ->
239245
case Unpack(Type, Data, Orig) of
240246
{ok, Term} -> {Term, Rest};
241247
{error, E} -> {error, E}
242248
end;
243-
maybe_unpack_ext(_, Unpack, Type, Data, Rest, _) when is_function(Unpack, 2) ->
249+
maybe_unpack_ext(_, Unpack, Type, Data, Rest, _, _)
250+
when is_function(Unpack, 2) ->
244251
case Unpack(Type, Data) of
245252
{ok, Term} -> {Term, Rest};
246253
{error, E} -> {error, E}

test/msgpack_tests.erl

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
%% See the License for the specific language governing permissions and
1616
%% limitations under the License.
1717
%%
18-
%% Created : 26 Apr 2011 by UENISHI Kota <uenishi.kota@lab.ntt.co.jp>
1918

2019
-module(msgpack_tests).
2120

@@ -217,7 +216,6 @@ array_test_()->
217216
end}
218217
].
219218

220-
-ifndef(without_map).
221219
map_test_()->
222220
[
223221
{"map <=> jsx",
@@ -254,8 +252,6 @@ map_test_()->
254252
Binary = pack(EmptyMap, [{map_format,map}]),
255253
?assertEqual({ok, EmptyMap}, unpack(Binary, [{map_format,map}]))
256254
end}].
257-
-endif.
258-
259255

260256
jiffy_jsx_test_() ->
261257
[{"jiffy length 16",
@@ -383,18 +379,6 @@ binary_test_() ->
383379
end}
384380
].
385381

386-
%% long_binary_test_()->
387-
%% [
388-
%% {"long binary",
389-
%% fun() ->
390-
%% A = pack(1),
391-
%% B = pack(10),
392-
%% C = pack(100),
393-
%% ?assertEqual({[1,10,100], <<>>},
394-
%% unpack(list_to_binary([A, B, C])))
395-
%% end}
396-
%% ].
397-
398382
-define(PCNT, 5).
399383
-define(CNT, 10000).
400384

@@ -425,7 +409,7 @@ benchmark3_test()->
425409
multirunner(What, Pack, Unpack) ->
426410
Self = self(),
427411
Nil = null,
428-
412+
429413
Data=[test_data() ++ [Nil] || _ <- lists:seq(0, ?CNT)],
430414
Packed = Pack(Data),
431415
Size = byte_size(Packed) div 1024,
@@ -485,3 +469,61 @@ benchmark_p0_test_() ->
485469
multirunner("t2b/b2t",
486470
fun erlang:term_to_binary/1,
487471
fun erlang:binary_to_term/1))}].
472+
473+
new_options_test_() ->
474+
OldSpecOpt = [{spec, old}],
475+
[
476+
{"old spec",
477+
[?_assertEqual(<<161,1>>, msgpack:pack(<<1>>, OldSpecOpt)),
478+
?_assertEqual(<<162,1,2>>, msgpack:pack(<<1,2>>, OldSpecOpt)),
479+
?_assertMatch(<<191, _:31/binary >>,
480+
msgpack:pack(binary:copy(<<1>>, 31), OldSpecOpt)),
481+
?_assertMatch(<<218, 0, 32, _:32/binary >>,
482+
msgpack:pack(binary:copy(<<1>>, 32), OldSpecOpt)),
483+
?_assertMatch(<<218, 255, 255, _:65535/binary >>,
484+
msgpack:pack(binary:copy(<<1>>, 65535), OldSpecOpt)),
485+
?_assertMatch(<<219, 0, 1, 0, 0, _:65536/binary >>,
486+
msgpack:pack(binary:copy(<<1>>, 65536), OldSpecOpt))
487+
]},
488+
%% {"Decoding new spec binary with old spec",
489+
%% [?_assertEqual({error, {badarg, {new_spec, Code}}},
490+
%% msgpack:unpack(<<Code, 0, 0, 0, 42>>, OldSpecOpt))
491+
%% || Code <- [16#D4, 16#D5, 16#D6, 16#D7, 16#D8, 16#C7, 16#C8, 16#C9,
492+
%% 16#C4, 16#C5, 16#C6] ]},
493+
{"allow_atom none/pack",
494+
[?_assertEqual(<<196,4,97,116,111,109>>,
495+
msgpack:pack(atom, [{allow_atom, pack}])),
496+
?_assertEqual({error, {badarg, atom}},
497+
msgpack:pack(atom, [{allow_atom, none}]))]},
498+
{"known_atoms, empty",
499+
[?_assertEqual({error, {badarg, atom}},
500+
msgpack:pack(atom, [{known_atoms, []},
501+
{allow_atom, none}]))]},
502+
{"known_atoms, [atom] when atoms are not allowed",
503+
[?_assertEqual(<<196,4,97,116,111,109>>,
504+
msgpack:pack(atom, [{known_atoms, [atom]},
505+
{allow_atom, none}]))]},
506+
{"pack_str, on binary()",
507+
[?_assertEqual(<<196,3,97,97,97>>,
508+
msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,from_list}])),
509+
%% ?_assertEqual(<<16#D9,3,97,97,97>>, Not passing
510+
%% msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,from_binary}])),
511+
?_assertEqual(<<196,3,97,97,97>>,
512+
msgpack:pack(<<"aaa">>, [{spec,new},{pack_str,none}]))
513+
]},
514+
{"pack_str, on string()",
515+
%% [?_assertEqual(<<196,3,97,97,97>>,
516+
%% msgpack:pack("aaa", [{spec,new},{pack_str,from_list}])),
517+
%% ?_assertEqual(<<16#D9,3,97,97,97>>, Not passing
518+
%% msgpack:pack("aaa", [{spec,new},{pack_str,from_binary}])),
519+
%% ?_assertEqual(<<196,3,97,97,97>>,
520+
%% msgpack:pack("aaa", [{spec,new},{pack_str,none}]))
521+
[]},
522+
{"unpack_str, as_binary",
523+
[
524+
]},
525+
{"unpack_str, as_list", []},
526+
{"validate_string, false",
527+
[]},
528+
{"validate_string, true", []}
529+
].

0 commit comments

Comments
 (0)