⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 megaco_codec_test_lib.erl

📁 OTP是开放电信平台的简称
💻 ERL
📖 第 1 页 / 共 3 页
字号:
	 expect_instruction(	   "Decode (initial) message",	   fun(Bin) when is_binary(Bin) ->		   (catch Decode(Bin));	      (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, Msg}, Bin) ->		   io:format("unexpected decode success - ", []),		   M = binary_to_list(Bin),		   {error, {unexpected_decode_success, {Msg, M}}};	      (Crap, _) ->		   {error, {unexpected_decode_result, Crap}}	   end)	],    expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function:    expect_decode_only%% Parameters:  InitialData -> list() | binary()%%              Decode -> function/1%%              Check -> function/2%% Description: This function simply decodes, with the Decode fun, %%              and expects it to succeed, which is checked by %%              calling the Check fun with the resulting message.%% ------------------------------------------------------------------expect_decode_only(InitialData, Decode, Check)   when is_list(InitialData) ->    expect_decode_only(list_to_binary(InitialData), Decode, Check);expect_decode_only(InitialData, Decode, Check)   when is_function(Decode) and is_function(Check) ->    Instructions = 	[	 %% Initial decode	 expect_instruction(	   "Decode (initial) message",	   fun(B) when is_binary(B) ->		   (catch Decode(B));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, Msg}, _B) when is_record(Msg, 'MegacoMessage') ->		   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_encode%% Parameters:  InitialData -> list() | binary()%%              Decode -> function/1%%              Encode -> function/1%%              Check -> function/2%% Description: This function simply decodes, with the Decode fun, %%              and then encodes, with the Encode fun, the megaco %%              message. The resulting binary message should be %%              identical, but if it is not, the messages are %%              decoded again and then if necessary checked, with %%              the Check fun.%% ------------------------------------------------------------------expect_decode_encode(InitialData, Decode, Encode, Check)   when is_list(InitialData) ->    expect_decode_encode(list_to_binary(InitialData), Decode, Encode, Check);expect_decode_encode(InitialData, Decode, Encode, Check)   when is_function(Decode) and is_function(Encode) and is_function(Check) ->    Instructions = 	[	 %% Initial decode	 expect_instruction(	   "Decode (initial) message",	   fun(B) when is_binary(B) ->		   (catch Decode(B));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, Msg}, B) when is_record(Msg, 'MegacoMessage') ->		   {ok, {Msg, B}};	      ({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),	 	 	 %% Encode the (decoded) message	 expect_instruction(	   "Encode message", 	   fun({Msg, _Bin}) when is_record(Msg, 'MegacoMessage') -> 		   (catch Encode(Msg));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, B}, {_, B}) ->		   io:format("binaries equal - done ", []),		   {ok, done};	      ({ok, B}, {Msg, _}) ->		   {ok, {Msg, B}};	      ({error, Reason}, _) ->		   {error, {unexpected_encode_failure, Reason}};	      (Crap, _) ->		   {error, {unexpected_encode_result, Crap}}	   end),	 	 	 %% Fallback instruction in case encode produced	 %% a binary not equal to the initial	 expect_instruction(	   "Decode message (if binaries not equal)", 	   fun(done) ->		   done;	      ({_Msg, B}) when is_binary(B) ->		   (catch Decode(B));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, Msg}, {Msg, _Bin}) when is_record(Msg, 'MegacoMessage') ->		   io:format("messages identical - done ", []),		   {ok, done};	       (done, _) ->		   io:format("done ", []),		   {ok, done};	       ({ok, Msg2}, {Msg1, _}) ->		   io:format("messages not identical - check - ", []),		   case (catch Check(Msg1, Msg2)) of		       ok ->			   io:format("equal ", []),			   {ok, done};		       Error ->			   io:format("not equal ", []),			   Error		   end;	       ({error, Reason}, _) ->		      {error, {unexpected_decode_failure, Reason}};	       (Crap, _) ->		      {error, {unexpected_decode_result, Crap}}	      end)	],    expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function:    expect_decode_encode_only%% Parameters:  InitialData -> list() | binary()%%              Decode -> function/1%%              Encode -> function/1%%              Check -> function/2%% Description: This function simply decodes, with the Decode fun, %%              and then encodes, with the Encode fun, the megaco %%              message. The resulting binary message is then checked%%              with the Check fun.%% ------------------------------------------------------------------expect_decode_encode_only(InitialData, Decode, Encode, Check)   when is_list(InitialData) ->    expect_decode_encode_only(list_to_binary(InitialData), 			      Decode, Encode, Check);expect_decode_encode_only(InitialData, Decode, Encode, Check)   when is_function(Decode) and is_function(Encode) and is_function(Check) ->    Instructions = 	[	 %% Initial decode	 expect_instruction(	   "Decode (initial) message",	   fun(B) when is_binary(B) ->		   (catch Decode(B));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, Msg}, B) when is_record(Msg, 'MegacoMessage') ->		   {ok, {Msg, B}};	      ({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),	 	 	 %% Encode the (decoded) message	 expect_instruction(	   "Encode message", 	   fun({Msg, _Bin}) when is_record(Msg, 'MegacoMessage') -> 		   (catch Encode(Msg));	      (Bad) ->		   {error, {invalid_data, Bad}}	   end,	   fun({ok, B2}, {_, B1}) ->		   io:format("encode ok - check bins - ", []),		   case (catch Check(B1, B2)) of		       ok ->			   {ok, done};		       Crap ->			   {error, {unexpected_encode_check_result, Crap}}		   end;	      ({error, Reason}, _) ->		   {error, {unexpected_encode_failure, Reason}};	      (Crap, _) ->		   {error, {unexpected_encode_result, Crap}}	   end)	],    expect_exec(Instructions, InitialData).%% ------------------------------------------------------------------%% Function:    expect_exec%% Parameters:  Instructions -> [instruction()]%%              InitialData -> term()%% Description: This function is the engine in the codec test %%              cases. It executes each instruction in turn.%% ------------------------------------------------------------------expect_exec(Instructions, InitialData) ->    expect_exec(Instructions, InitialData, 1).expect_exec([], _, _) ->    io:format("~n", []),    ok;expect_exec([#expect_instruction{description = Desc, 				 command     = Cmd, 				 verify      = Verify}|T], Data, Num) ->    io:format("~n   Exec command ~w: ~s => ", [Num, Desc]),    case Verify((catch Cmd(Data)), Data) of	{ok, NewData} ->	    io:format("ok", []),	    expect_exec(T, NewData, Num+1);	{error, Reason} ->	    io:format("error", []),	    {error, {Num, Desc, Reason}}    end.%% =======================================================================%% ------------------------------------------------------------------%% Internal functions%% ------------------------------------------------------------------d(F) ->    d(F, []).d(F, A) ->    d(get(dbg), F, A).d(true, F, A) ->    io:format("DBG:~w:" ++ F ++ "~n", [?MODULE|A]);d(_, _, _) ->    ok.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -