📄 megaco_codec_test_lib.erl
字号:
case (catch encode_action_requests(Codec, Ver, EC, ARs)) of {ok, EncodedARs} -> TR2 = TR1#'TransactionRequest'{actions = EncodedARs}, {transactionRequest, TR2}; Error -> throw({error, {actions_encode_failed, Error, TR1}}) end.encode_action_requests(Codec, Ver, EC, ARs) -> d("encode_action_requests -> entry"), Codec:encode_action_requests(EC, Ver, ARs).%% *** action_first_encode_decode ***action_first_encode_decode(Codec, DynamicDecode, Ver, EC, M1) -> d("action_first_encode_decode -> entry"), case (catch action_first_encode_message(Codec, Ver, EC, M1)) of {ok, Bin} -> decode_message(Codec, DynamicDecode, Ver, EC, Bin); Error -> Error end.action_first_encode_message(Codec, Ver, EC, M1) -> d("action_first_encode_message -> entry"), Mess1 = M1#'MegacoMessage'.mess, {transactions, Trans1} = Mess1#'Message'.messageBody, Trans2 = encode_action(Codec, Ver, EC, Trans1), Mess2 = Mess1#'Message'{messageBody = {transactions, Trans2}}, M2 = M1#'MegacoMessage'{mess = Mess2}, encode_message(Codec, Ver, EC, M2).encode_action(Codec, Ver, EC, Trans) when list(Trans) -> d("encode_action -> entry"), [encode_action1(Codec, Ver, EC, T) || T <- Trans].encode_action1(Codec, Ver, EC, {transactionRequest, TR1}) -> d("encode_action1 -> entry"), #'TransactionRequest'{actions = ARs1} = TR1, ARs2 = [encode_action_request(Codec, Ver, EC, AR) || AR <- ARs1], TR2 = TR1#'TransactionRequest'{actions = ARs2}, {transactionRequest, TR2}.encode_action_request(Codec, Ver, EC, AR) -> d("encode_action_request -> entry"), case (catch Codec:encode_action_request(EC, Ver, AR)) of {ok, Bin} -> Bin; Error -> throw({error, {encode_action_request_failed, Error, AR}}) end.encode_message(Codec, Ver, EC, M) -> d("encode_message -> entry with" "~n Codec: ~p" "~n Ver: ~p" "~n EC: ~p" "~n M: ~p", [Codec, Ver, EC, M]), case (catch Codec:encode_message(EC, Ver, M)) of {ok, Bin} -> d("encode_message -> encode - ok: " "~n~s", [binary_to_list(Bin)]), {ok, Bin}; Error -> d("encode_message -> encode - failed"), throw({error, {message_encode_failed, Error, M}}) end.decode_message(Codec, true, _Ver, EC, M) -> d("decode_message -> entry - when using dynamic"), Codec:decode_message(EC, dynamic, M);decode_message(Codec, _, Ver, EC, M) -> d("decode_message -> entry with" "~n Codec: ~p" "~n Ver: ~p" "~n EC: ~p", [Codec, Ver, EC]), Codec:decode_message(EC, Ver, M).%% =======================================================================%% ------------------------------------------------------------------%% Create an instruction record%% ------------------------------------------------------------------expect_instruction(Desc, Cmd, Verify) when is_list(Desc) and is_function(Cmd) and is_function(Verify) -> #expect_instruction{description = Desc, command = Cmd, verify = Verify}. %% ------------------------------------------------------------------%% Function: expect_encode%% Parameters: Msg -> MegacoMessage%% Encode -> function/1%% Check -> function/1%% Description: This function simply encodes, with the Encode fun, %% and expects this to fail. The failure reason is %% checked with the Check fun.%% ------------------------------------------------------------------expect_encode(InitialData, Encode, Check) when is_function(Encode) and is_function(Check) -> Instructions = [ %% Initial encode expect_instruction( "Encode (initial) message", fun(Msg) when is_record(Msg, 'MegacoMessage') -> (catch Encode(Msg)); (Bad) -> {error, {invalid_data, Bad}} end, fun({error, Reason}, _) -> io:format("check error reason ", []), case (catch Check(Reason)) of ok -> {ok, done}; Error -> Error end; ({ok, Bin}, Msg) when is_binary(Bin) -> M = binary_to_list(Bin), {error, {unexpected_encode_success, {M, Msg}}}; (Crap, _) -> {error, {unexpected_encode_result, Crap}} end) ], expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function: expect_encode_only%% Parameters: InitialData -> list() | binary()%% Encode -> function/1%% Check -> function/1%% Description: This function simply encodes, with the Encode fun, %% and expects it to succeed, which is checked by %% calling the Check fun with the resulting message.%% ------------------------------------------------------------------expect_encode_only(InitialData, Encode, Check) when is_function(Encode) and is_function(Check) -> Instructions = [ %% Initial encode expect_instruction( "Encode (initial) message", fun(Msg) when is_record(Msg, 'MegacoMessage') -> (catch Encode(Msg)); (Bad) -> {error, {invalid_data, Bad}} end, fun({ok, Bin}, _Msg) when is_binary(Bin) -> case (catch Check(Bin)) of ok -> {ok, done}; Error -> Error end; (Crap, _) -> {error, {unexpected_encode_result, Crap}} end) ], expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function: expect_encode_decode%% Parameters: InitialData -> MegacoMessage%% Encode -> function/1%% Decode -> function/1%% Check -> function/2%% Description: This function simply encodes, with the Encode fun, and %% then decodes, with the Decode fun, the megaco message. %% The resulting message should be identical, but if it %% is not, the messages are checked, with the Check fun.%% ------------------------------------------------------------------expect_encode_decode(InitialData, Encode, Decode, Check) when is_function(Encode) and is_function(Decode) and is_function(Check) -> Instructions = [ %% Initial encode expect_instruction( "Encode (initial) message", fun(M) when is_record(M, 'MegacoMessage') -> (catch Encode(M)); (Bad) -> {error, {invalid_data, Bad}} end, fun({ok, Bin}, M) when is_binary(Bin) -> {ok, {Bin, M}}; ({error, Reason}, _) -> {error, {unexpected_encode_failure, Reason}}; (Crap, _) -> {error, {unexpected_encode_result, Crap}} end), %% Decode the (encoded) message expect_instruction( "Decode message", fun({Bin, _}) when is_binary(Bin) -> (catch Decode(Bin)); (Bad) -> {error, {invalid_data, Bad}} end, fun({ok, Msg1}, {_Bin, Msg1}) when is_record(Msg1, 'MegacoMessage') -> io:format("messages identical - done ", []), {ok, done}; ({ok, Msg2}, {_Bin, Msg1}) -> io:format("messages not identical - check - ", []), case (catch Check(Msg1, Msg2)) of ok -> io:format("equal ", []), {ok, done}; Error -> io:format("not equal ", []), io:format("~nError: ~p~n", [Error]), Error end; (Crap, _) -> {error, {unexpected_decode_result, Crap}} end) ], expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function: expect_encode_decode_only%% Parameters: InitialData -> MegacoMessage%% Encode -> function/1%% Decode -> function/1%% Check -> function/2%% Description: This function simply encodes, with the Encode fun, %% and then decodes, with the Decode fun, the megaco %% message and expects it to succeed. The resulting %% message is checked by calling the Check fun with the %% resulting message.%% ------------------------------------------------------------------expect_encode_decode_only(InitialData, Encode, Decode, Check) when is_function(Encode) and is_function(Decode) and is_function(Check) -> Instructions = [ %% Initial encode expect_instruction( "Encode (initial) message", fun(M) when is_record(M, 'MegacoMessage') -> (catch Encode(M)); (Bad) -> {error, {invalid_data, Bad}} end, fun({ok, Bin}, M) when is_binary(Bin) -> {ok, {Bin, M}}; ({error, Reason}, _) -> {error, {unexpected_encode_failure, Reason}}; (Crap, _) -> {error, {unexpected_encode_result, Crap}} end), %% Decode the (encoded) message expect_instruction( "Decode message", fun({Bin, _}) when is_binary(Bin) -> (catch Decode(Bin)); (Bad) -> {error, {invalid_data, Bad}} end, fun({ok, Msg}, _B) when is_record(Msg, 'MegacoMessage') -> io:format("decoded - now check ", []), case (catch Check(Msg)) of ok -> {ok, done}; Error -> Error end; ({error, R}, _) -> {Line, Mod, Reason} = case lists:keysearch(reason, 1, R) of {value, {reason, {L, M, Raw}}} when is_list(Raw) -> {L, M, lists:flatten(Raw)}; {value, {reason, {L, M, Raw}}} -> {L, M, Raw}; _ -> {-1, undefined, R} end, Tokens = case lists:keysearch(token, 1, R) of {value, {token, T}} -> T; _ -> undefined end, {error, {unexpected_decode_failure, {Mod, Line, Reason, Tokens}}}; (Crap, _) -> {error, {unexpected_decode_result, Crap}} end) ], expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function: expect_decode%% Parameters: InitialData -> list() | binary()%% Decode -> function/1%% Check -> function/1%% Description: This function simply decodes, with the Decode fun, %% and expects this to fail. The failure reason is %% checked with the Check fun.%% ------------------------------------------------------------------expect_decode(InitialData, Decode, Check) when is_list(InitialData) -> expect_decode(list_to_binary(InitialData), Decode, Check);expect_decode(InitialData, Decode, Check) when is_function(Decode) and is_function(Check) -> Instructions = [ %% Initial decode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -