1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2004-2011. All Rights Reserved.
    5: %% 
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %% 
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %% 
   17: %% %CopyrightEnd%
   18: %%
   19: %%
   20: 
   21: %%----------------------------------------------------------------------
   22: %% Purpose : Test suite for c-client/erl-server
   23: %%----------------------------------------------------------------------
   24: 
   25: -module(c_client_erl_server_proto_SUITE).
   26: -include_lib("common_test/include/ct.hrl").
   27: 
   28: -export([init_per_testcase/2, end_per_testcase/2,
   29: 	all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   30: 	 init_per_group/2,end_per_group/2, 
   31: 	 void_test/1, long_test/1, long_long_test/1,
   32: 	 unsigned_short_test/1, unsigned_long_test/1,
   33: 	 unsigned_long_long_test/1, double_test/1, char_test/1,
   34: 	 wchar_test/1, octet_test/1, bool_test/1, struct_test/1,
   35: 	 struct2_test/1, seq1_test/1, seq2_test/1, seq3_test/1,
   36: 	 seq4_test/1, seq5_test/1, array1_test/1, array2_test/1,
   37: 	 enum_test/1, string1_test/1, string2_test/1, string3_test/1,
   38: 	 string4_test/1, pid_test/1, port_test/1, ref_test/1, term_test/1,
   39: 	 typedef_test/1, inline_sequence_test/1, term_sequence_test/1,
   40: 	 term_struct_test/1, wstring1_test/1]).
   41: 
   42: -define(DEFAULT_TIMEOUT, 20000).
   43: -define(PORT_TIMEOUT, 15000).
   44: -define(ERLANG_SERVER_NAME, idl_erlang_server).
   45: -define(C_CLIENT_NODE_NAME, c_client_idl_test).
   46:   
   47: %% Add/remove code path and watchdog before/after each test case.
   48: %%
   49: init_per_testcase(_Case, Config) ->
   50:     DataDir = ?config(data_dir, Config),
   51:     code:add_patha(DataDir),
   52: 
   53:     %% Since other test suites use the module m_i, we have
   54:     %% to make sure we are using the right m_i module.
   55:     code:purge(m_i),
   56:     code:load_file(m_i),
   57: 
   58:     WatchDog = test_server:timetrap(?DEFAULT_TIMEOUT),
   59:     [{watchdog, WatchDog}| Config].
   60: 
   61: end_per_testcase(_Case, Config) ->
   62:     DataDir = ?config(data_dir, Config),
   63:     code:del_path(DataDir),
   64:     WatchDog = ?config(watchdog, Config),
   65:     test_server:timetrap_cancel(WatchDog).
   66: 
   67: suite() -> [{ct_hooks,[ts_install_cth]}].
   68: 
   69: all() -> 
   70:     [void_test, long_test, long_long_test,
   71:      unsigned_short_test, unsigned_long_test,
   72:      unsigned_long_long_test, double_test, char_test,
   73:      wchar_test, octet_test, bool_test, struct_test,
   74:      struct2_test, seq1_test, seq2_test, seq3_test,
   75:      seq4_test, seq5_test, array1_test, array2_test,
   76:      enum_test, string1_test, string2_test, string3_test,
   77:      string4_test, pid_test, port_test, ref_test, term_test,
   78:      typedef_test, inline_sequence_test, term_sequence_test,
   79:      term_struct_test, wstring1_test].
   80: 
   81: groups() -> 
   82:     [].
   83: 
   84: init_per_suite(Config) ->
   85:     Config.
   86: 
   87: end_per_suite(_Config) ->
   88:     ok.
   89: 
   90: init_per_group(_GroupName, Config) ->
   91:     Config.
   92: 
   93: end_per_group(_GroupName, Config) ->
   94:     Config.
   95: 
   96: array1_test(doc) -> "";
   97: array1_test(suite) -> [];
   98: array1_test(Config) ->
   99:     do_test(array1_test, Config). 
  100: 
  101: array2_test(doc) -> "";
  102: array2_test(suite) -> [];
  103: array2_test(Config) ->
  104:     do_test(array2_test, Config).
  105: 
  106: bool_test(doc) -> "";
  107: bool_test(suite) -> [];
  108: bool_test(Config) ->
  109:     do_test(bool_test, Config).
  110: 
  111: char_test(doc) -> "";
  112: char_test(suite) -> [];
  113: char_test(Config) ->
  114:     do_test(char_test, Config).
  115: 
  116: double_test(doc) -> "";
  117: double_test(suite) -> [];
  118: double_test(Config) ->
  119:     do_test(double_test, Config).
  120: 
  121: enum_test(doc) -> "";
  122: enum_test(suite) -> [];
  123: enum_test(Config) ->
  124:     do_test(enum_test, Config).
  125: 
  126: inline_sequence_test(doc) -> "";
  127: inline_sequence_test(suite) -> [];
  128: inline_sequence_test(Config) ->
  129:     do_test(inline_sequence_test, Config).
  130: 
  131: long_long_test(doc) -> "";
  132: long_long_test(suite) -> [];
  133: long_long_test(Config) ->
  134:     do_test(long_long_test, Config).
  135: 
  136: long_test(doc) -> "";
  137: long_test(suite) -> [];
  138: long_test(Config) ->
  139:     do_test(long_test, Config).
  140: 
  141: octet_test(doc) -> "";
  142: octet_test(suite) -> [];
  143: octet_test(Config) ->
  144:     do_test(octet_test, Config).
  145: 
  146: pid_test(doc) -> "";
  147: pid_test(suite) -> [];
  148: pid_test(Config) ->
  149:     do_test(pid_test, Config).
  150: 
  151: port_test(doc) -> "";
  152: port_test(suite) -> [];
  153: port_test(Config) ->
  154:     do_test(port_test, Config).
  155: 
  156: ref_test(doc) -> "";
  157: ref_test(suite) -> [];
  158: ref_test(Config) ->
  159:     do_test(ref_test, Config).
  160: 
  161: seq1_test(doc) -> "";
  162: seq1_test(suite) -> [];
  163: seq1_test(Config) ->
  164:     do_test(seq1_test, Config).
  165: 
  166: seq2_test(doc) -> "";
  167: seq2_test(suite) -> [];
  168: seq2_test(Config) ->
  169:     do_test(seq2_test, Config).
  170: 
  171: seq3_test(doc) -> "";
  172: seq3_test(suite) -> [];
  173: seq3_test(Config) ->
  174:     do_test(seq3_test, Config).
  175: 
  176: seq4_test(doc) -> "";
  177: seq4_test(suite) -> [];
  178: seq4_test(Config) ->
  179:     do_test(seq4_test, Config).
  180: 
  181: seq5_test(doc) -> "";
  182: seq5_test(suite) -> [];
  183: seq5_test(Config) ->
  184:     do_test(seq5_test, Config).
  185: 
  186: string1_test(doc) -> "";
  187: string1_test(suite) -> [];
  188: string1_test(Config) ->
  189:     do_test(string1_test, Config).
  190: 
  191: string2_test(doc) -> "";
  192: string2_test(suite) -> [];
  193: string2_test(Config) ->
  194:     do_test(string2_test, Config).
  195: 
  196: string3_test(doc) -> "";
  197: string3_test(suite) -> [];
  198: string3_test(Config) ->
  199:     do_test(string3_test, Config).
  200: 
  201: string4_test(doc) -> "";
  202: string4_test(suite) -> [];
  203: string4_test(Config) ->
  204:     do_test(string4_test, Config).
  205: 
  206: struct2_test(doc) -> "";
  207: struct2_test(suite) -> [];
  208: struct2_test(Config) ->
  209:     do_test(struct2_test, Config).
  210: 
  211: struct_test(doc) -> "";
  212: struct_test(suite) -> [];
  213: struct_test(Config) ->
  214:     do_test(struct_test, Config).
  215: 
  216: term_sequence_test(doc) -> "";
  217: term_sequence_test(suite) -> [];
  218: term_sequence_test(Config) ->
  219:     do_test(term_sequence_test, Config).
  220: 
  221: term_struct_test(doc) -> "";
  222: term_struct_test(suite) -> [];
  223: term_struct_test(Config) ->
  224:     do_test(term_struct_test, Config).
  225: 
  226: term_test(doc) -> "";
  227: term_test(suite) -> [];
  228: term_test(Config) ->
  229:     do_test(term_test, Config).
  230: 
  231: typedef_test(doc) -> "";
  232: typedef_test(suite) -> [];
  233: typedef_test(Config) ->
  234:     do_test(typedef_test, Config).
  235: 
  236: unsigned_long_long_test(doc) -> "";
  237: unsigned_long_long_test(suite) -> [];
  238: unsigned_long_long_test(Config) ->
  239:     do_test(unsigned_long_long_test, Config).
  240: 
  241: unsigned_long_test(doc) -> "";
  242: unsigned_long_test(suite) -> [];
  243: unsigned_long_test(Config) ->
  244:     do_test(unsigned_long_test, Config).
  245: 
  246: unsigned_short_test(doc) -> "";
  247: unsigned_short_test(suite) -> [];
  248: unsigned_short_test(Config) ->
  249:     do_test(unsigned_short_test, Config).
  250: 
  251: void_test(doc) -> "";
  252: void_test(suite) -> [];
  253: void_test(Config) ->
  254:     do_test(void_test, Config).
  255: 
  256: wchar_test(doc) -> "";
  257: wchar_test(suite) -> [];
  258: wchar_test(Config) ->
  259:     do_test(wchar_test, Config).
  260: 
  261: wstring1_test(doc) -> "";
  262: wstring1_test(suite) -> [];
  263: wstring1_test(Config) ->
  264:     do_test(wstring1_test, Config).
  265: 
  266: 
  267: %% It is here that all tests really are done.
  268: %%
  269:      
  270: do_test(Case, Config) ->
  271:     %% Trap exits
  272:     process_flag(trap_exit, true),
  273:     %% Start the server
  274:     {ok, _Pid} = m_i:oe_create_link([], {local, ?ERLANG_SERVER_NAME}),
  275:     Node = atom_to_list(node()),
  276:     %% [NodeName, HostName] = string:tokens(Node, "@"),
  277:     DataDir = ?config(data_dir, Config),
  278:     %% io:format("~p: data directory: ~p~n", [?MODULE, DataDir]),
  279:     Cookie = atom_to_list(erlang:get_cookie()),
  280:     %% Start C-client node as a port program.  
  281:     Cmd = filename:join([DataDir, "c_client"]) ++
  282: 	" -this-node-name " ++ atom_to_list(?C_CLIENT_NODE_NAME) ++
  283: 	" -peer-node " ++ Node ++
  284: 	" -peer-process-name " ++ atom_to_list(?ERLANG_SERVER_NAME) ++
  285: 	" -cookie " ++ Cookie ++ 
  286: 	" -test-case " ++ atom_to_list(Case),
  287:     Port = open_port({spawn, Cmd}, [exit_status, eof, stderr_to_stdout]),
  288:     Res = wait_for_completion(Port),
  289:     %% Kill off node if there was timeout
  290:     case Res of
  291: 	{error, timeout} ->
  292: 	    catch rpc:cast(?C_CLIENT_NODE_NAME, erlang, halt, [1]);
  293: 	_ ->
  294: 	    ok
  295:     end, 
  296:     process_flag(trap_exit, false),
  297:     catch m_i:stop(?ERLANG_SERVER_NAME),
  298:     ok = Res.
  299: 
  300: 
  301: %% Wait for eof *and* exit status, but return if exit status indicates
  302: %% an error, or we have been waiting more than PORT_TIMEOUT seconds.
  303: %%
  304: wait_for_completion(Port) ->
  305:     wait_for_completion(Port, 0).
  306: 
  307: wait_for_completion(Port, N) when N < 2 ->
  308:     receive
  309: 	{Port, {data, Bytes}} ->
  310: 	    %% Relay output
  311: 	    io:format("~s", [Bytes]),
  312: 	    wait_for_completion(Port, N);
  313: 	{Port, {exit_status, 0}} ->
  314: 	    wait_for_completion(Port, N + 1);
  315: 	{Port, {exit_status, Status}} ->
  316: 	    {error, Status};
  317: 	{Port, eof} ->
  318: 	    wait_for_completion(Port, N + 1);
  319: 	{'EXIT', Port, Reason} ->
  320: 	    io:format("Port exited with reason: ~w~n", [Reason]),
  321: 	    wait_for_completion(Port, N);
  322: 	{'EXIT', From, Reason} ->
  323: 	    io:format("Got unexpected exit: ~p~n", [{'EXIT', From, Reason}]),
  324: 	    wait_for_completion(Port, N)
  325:     after ?PORT_TIMEOUT ->
  326: 	    {error, timeout}
  327:     end;
  328: wait_for_completion(_, _) ->
  329:     ok.
  330: 
  331: 	    
  332: