From 0f430abcb189988a7faf55386557b2b74afa6f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Tue, 24 Sep 2013 10:28:16 +0200 Subject: Teach the ASN.1 compiler the no_ok_wrapper option Add the no_ok_wrapper option so that the generated M:encode/2 and M:decode/2 functions will not wrap a successful return value in an {ok,...} tuple. Errors will cause exceptions. Eliminating the wrapping tuple allows simpler nesting of calls. --- lib/asn1/test/asn1_SUITE.erl | 20 ++++++++++++---- lib/asn1/test/testPrim.erl | 44 ++++++++++++++++++++++++++++------- lib/asn1/test/test_undecoded_rest.erl | 37 ++++++++++++++++++++++------- 3 files changed, 81 insertions(+), 20 deletions(-) (limited to 'lib/asn1/test') diff --git a/lib/asn1/test/asn1_SUITE.erl b/lib/asn1/test/asn1_SUITE.erl index a558a2941f..61b360ddf2 100644 --- a/lib/asn1/test/asn1_SUITE.erl +++ b/lib/asn1/test/asn1_SUITE.erl @@ -297,7 +297,15 @@ cover(_) -> testPrim(Config) -> test(Config, fun testPrim/3). testPrim(Config, Rule, Opts) -> - asn1_test_lib:compile_all(["Prim", "Real"], Config, [Rule|Opts]), + Files = ["Prim","Real"], + asn1_test_lib:compile_all(Files, Config, [Rule|Opts]), + do_test_prim(Rule, false), + asn1_test_lib:compile_all(Files, Config, [no_ok_wrapper,Rule|Opts]), + do_test_prim(Rule, true). + +do_test_prim(Rule, NoOkWrapper) -> + io:format("No ok wrapper: ~p\n", [NoOkWrapper]), + put(no_ok_wrapper, NoOkWrapper), testPrim:bool(Rule), testPrim:int(Rule), testPrim:enum(Rule), @@ -925,10 +933,14 @@ testNortel(Config, Rule, Opts) -> test_undecoded_rest(Config) -> test(Config, fun test_undecoded_rest/3). test_undecoded_rest(Config, Rule, Opts) -> + do_test_undecoded_rest(Config, Rule, Opts), + do_test_undecoded_rest(Config, Rule, [no_ok_wrapper|Opts]), + do_test_undecoded_rest(Config, Rule, [undec_rest|Opts]), + do_test_undecoded_rest(Config, Rule, [no_ok_wrapper,undec_rest|Opts]). + +do_test_undecoded_rest(Config, Rule, Opts) -> asn1_test_lib:compile("P-Record", Config, [Rule|Opts]), - ok = test_undecoded_rest:test([], Config), - asn1_test_lib:compile("P-Record", Config, [Rule,undec_rest|Opts]), - test_undecoded_rest:test(undec_rest, Config). + test_undecoded_rest:test(Opts, Config). testTcapsystem(Config) -> test(Config, fun testTcapsystem/3). diff --git a/lib/asn1/test/testPrim.erl b/lib/asn1/test/testPrim.erl index 5e3890b175..e07379e634 100644 --- a/lib/asn1/test/testPrim.erl +++ b/lib/asn1/test/testPrim.erl @@ -36,8 +36,7 @@ bool(Rules) -> case Rules of ber -> [begin - {error,{asn1,{encode_boolean,517}}} = - (catch 'Prim':encode(T, 517)) + {error,{asn1,{encode_boolean,517}}} = enc_error(T, 517) end || T <- Types], ok; _ -> @@ -90,12 +89,12 @@ enum(Rules) -> roundtrip('Enum', monday), roundtrip('Enum', thursday), - {error,{asn1,{_,4}}} = (catch 'Prim':encode('Enum', 4)), + {error,{asn1,{_,4}}} = enc_error('Enum', 4), case Rules of Per when Per =:= per; Per =:= uper -> - {ok,<<0>>} = 'Prim':encode('SingleEnumVal', true), - {ok,<<0>>} = 'Prim':encode('SingleEnumValExt', true); + <<0>> = roundtrip('SingleEnumVal', true), + <<0>> = roundtrip('SingleEnumValExt', true); ber -> ok end, @@ -132,10 +131,32 @@ null(_Rules) -> ok. roundtrip(T, V) -> - asn1_test_lib:roundtrip_enc('Prim', T, V, V). + roundtrip(T, V, V). roundtrip(Type, Value, ExpectedValue) -> - asn1_test_lib:roundtrip_enc('Prim', Type, Value, ExpectedValue). + case get(no_ok_wrapper) of + false -> + asn1_test_lib:roundtrip_enc('Prim', Type, Value, ExpectedValue); + true -> + M = 'Prim', + Enc = M:encode(Type, Value), + ExpectedValue = M:decode(Type, Enc), + Enc + end. + +enc_error(T, V) -> + case get(no_ok_wrapper) of + false -> + 'Prim':encode(T, V); + true -> + try 'Prim':encode(T, V) of + _ -> + ?t:fail() + catch + _:Reason -> + Reason + end + end. real(_Rules) -> %%========================================================== @@ -177,4 +198,11 @@ real_roundtrip(T, V) -> real_roundtrip(T, V, V). real_roundtrip(Type, Value, ExpectedValue) -> - asn1_test_lib:roundtrip('Real', Type, Value, ExpectedValue). + case get(no_ok_wrapper) of + false -> + asn1_test_lib:roundtrip('Real', Type, Value, ExpectedValue); + true -> + M = 'Real', + ExpectedValue = M:decode(Type, M:encode(Type, Value)), + ok + end. diff --git a/lib/asn1/test/test_undecoded_rest.erl b/lib/asn1/test/test_undecoded_rest.erl index fe04178bb1..91e614d38a 100644 --- a/lib/asn1/test/test_undecoded_rest.erl +++ b/lib/asn1/test/test_undecoded_rest.erl @@ -26,21 +26,42 @@ %% testing OTP-5104 -test(Opt, Config) -> +test(Opts, Config) -> {ok,Msg} = asn1ct:value('P-Record', 'PersonnelRecord', [{i,?config(case_dir, Config)}]), - {ok,Bytes0} = 'P-Record':encode('PersonnelRecord', Msg), + Bytes0 = encode(Opts, 'PersonnelRecord', Msg), Bytes1 = iolist_to_binary([Bytes0, <<55,55,55>>]), - case Opt of - undec_rest -> - {ok,Msg,R} = 'P-Record':decode('PersonnelRecord', Bytes1), + case proplists:get_bool(undec_rest, Opts) of + true -> + {Msg,R} = decode(Opts, 'PersonnelRecord', Bytes1), case R of - <<55,55,55>> -> ok; + <<55,55,55>> -> + ok; BStr when is_bitstring(BStr) -> PadLen = (8 - (bit_size(BStr) rem 8)) rem 8, <<0,55,55,55>> = <<0:PadLen, BStr/bitstring>> end; - _ -> - {ok,Msg} = 'P-Record':decode('PersonnelRecord', Bytes1) + false -> + Msg = decode(Opts, 'PersonnelRecord', Bytes1) end, ok. + +encode(Opts, T, V) -> + M = 'P-Record', + case proplists:get_bool(no_ok_wrapper, Opts) of + false -> + {ok,Enc} = M:encode(T, V), + Enc; + true -> + Enc = M:encode(T, V), + true = is_binary(Enc), + Enc + end. + +decode(Opts, T, E) -> + M = 'P-Record', + case {proplists:get_bool(no_ok_wrapper, Opts),M:decode(T, E)} of + {false,{ok,Val}} -> Val; + {false,{ok,Val,Rest}} -> {Val,Rest}; + {true,Result} -> Result + end. -- cgit v1.2.3