1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2001-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: 
   26: -module(c_client_erl_server_SUITE).
   27: -include_lib("common_test/include/ct.hrl").
   28: 
   29: -export([init_per_testcase/2, end_per_testcase/2,
   30: 	 all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   31: 	 init_per_group/2,end_per_group/2, 
   32: 	 void_test/1, long_test/1, long_long_test/1,
   33: 	 unsigned_short_test/1, unsigned_long_test/1,
   34: 	 unsigned_long_long_test/1, double_test/1, char_test/1,
   35: 	 wchar_test/1, octet_test/1, bool_test/1, struct_test/1,
   36: 	 struct2_test/1, seq1_test/1, seq2_test/1, seq3_test/1,
   37: 	 seq4_test/1, seq5_test/1, array1_test/1, array2_test/1,
   38: 	 enum_test/1, string1_test/1, string2_test/1, string3_test/1,
   39: 	 string4_test/1, pid_test/1, port_test/1, ref_test/1, term_test/1,
   40: 	 typedef_test/1, inline_sequence_test/1, term_sequence_test/1,
   41: 	 term_struct_test/1, wstring1_test/1]).
   42: 
   43: -define(DEFAULT_TIMEOUT, 20000).
   44: -define(PORT_TIMEOUT, 15000).
   45: -define(ERLANG_SERVER_NAME, idl_erlang_server).
   46: -define(C_CLIENT_NODE_NAME, c_client_idl_test).
   47:   
   48: %% Add/remove code path and watchdog before/after each test case.
   49: %%
   50: init_per_testcase(_Case, Config) ->
   51:     DataDir = ?config(data_dir, Config),
   52:     code:add_patha(DataDir),
   53: 
   54:     %% Since other test suites use the module m_i, we have
   55:     %% to make sure we are using the right m_i module.
   56:     code:purge(m_i),
   57:     code:load_file(m_i),
   58: 
   59:     WatchDog = test_server:timetrap(?DEFAULT_TIMEOUT),
   60:     [{watchdog, WatchDog}| Config].
   61: 
   62: end_per_testcase(_Case, Config) ->
   63:     DataDir = ?config(data_dir, Config),
   64:     code:del_path(DataDir),
   65:     WatchDog = ?config(watchdog, Config),
   66:     test_server:timetrap_cancel(WatchDog).
   67: 
   68: suite() -> [{ct_hooks,[ts_install_cth]}].
   69: 
   70: all() -> 
   71:     [void_test, long_test, long_long_test,
   72:      unsigned_short_test, unsigned_long_test,
   73:      unsigned_long_long_test, double_test, char_test,
   74:      wchar_test, octet_test, bool_test, struct_test,
   75:      struct2_test, seq1_test, seq2_test, seq3_test,
   76:      seq4_test, seq5_test, array1_test, array2_test,
   77:      enum_test, string1_test, string2_test, string3_test,
   78:      string4_test, pid_test, port_test, ref_test, term_test,
   79:      typedef_test, inline_sequence_test, term_sequence_test,
   80:      term_struct_test, wstring1_test].
   81: 
   82: groups() -> 
   83:     [].
   84: 
   85: init_per_suite(Config) ->
   86:     Config.
   87: 
   88: end_per_suite(_Config) ->
   89:     ok.
   90: 
   91: init_per_group(_GroupName, Config) ->
   92:     Config.
   93: 
   94: end_per_group(_GroupName, Config) ->
   95:     Config.
   96: 
   97: array1_test(doc) -> "";
   98: array1_test(suite) -> [];
   99: array1_test(Config) ->
  100:     do_test(array1_test, Config). 
  101: 
  102: array2_test(doc) -> "";
  103: array2_test(suite) -> [];
  104: array2_test(Config) ->
  105:     do_test(array2_test, Config).
  106: 
  107: bool_test(doc) -> "";
  108: bool_test(suite) -> [];
  109: bool_test(Config) ->
  110:     do_test(bool_test, Config).
  111: 
  112: char_test(doc) -> "";
  113: char_test(suite) -> [];
  114: char_test(Config) ->
  115:     do_test(char_test, Config).
  116: 
  117: double_test(doc) -> "";
  118: double_test(suite) -> [];
  119: double_test(Config) ->
  120:     do_test(double_test, Config).
  121: 
  122: enum_test(doc) -> "";
  123: enum_test(suite) -> [];
  124: enum_test(Config) ->
  125:     do_test(enum_test, Config).
  126: 
  127: inline_sequence_test(doc) -> "";
  128: inline_sequence_test(suite) -> [];
  129: inline_sequence_test(Config) ->
  130:     do_test(inline_sequence_test, Config).
  131: 
  132: long_long_test(doc) -> "";
  133: long_long_test(suite) -> [];
  134: long_long_test(Config) ->
  135:     do_test(long_long_test, Config).
  136: 
  137: long_test(doc) -> "";
  138: long_test(suite) -> [];
  139: long_test(Config) ->
  140:     do_test(long_test, Config).
  141: 
  142: octet_test(doc) -> "";
  143: octet_test(suite) -> [];
  144: octet_test(Config) ->
  145:     do_test(octet_test, Config).
  146: 
  147: pid_test(doc) -> "";
  148: pid_test(suite) -> [];
  149: pid_test(Config) ->
  150:     do_test(pid_test, Config).
  151: 
  152: port_test(doc) -> "";
  153: port_test(suite) -> [];
  154: port_test(Config) ->
  155:     do_test(port_test, Config).
  156: 
  157: ref_test(doc) -> "";
  158: ref_test(suite) -> [];
  159: ref_test(Config) ->
  160:     do_test(ref_test, Config).
  161: 
  162: seq1_test(doc) -> "";
  163: seq1_test(suite) -> [];
  164: seq1_test(Config) ->
  165:     do_test(seq1_test, Config).
  166: 
  167: seq2_test(doc) -> "";
  168: seq2_test(suite) -> [];
  169: seq2_test(Config) ->
  170:     do_test(seq2_test, Config).
  171: 
  172: seq3_test(doc) -> "";
  173: seq3_test(suite) -> [];
  174: seq3_test(Config) ->
  175:     do_test(seq3_test, Config).
  176: 
  177: seq4_test(doc) -> "";
  178: seq4_test(suite) -> [];
  179: seq4_test(Config) ->
  180:     do_test(seq4_test, Config).
  181: 
  182: seq5_test(doc) -> "";
  183: seq5_test(suite) -> [];
  184: seq5_test(Config) ->
  185:     do_test(seq5_test, Config).
  186: 
  187: string1_test(doc) -> "";
  188: string1_test(suite) -> [];
  189: string1_test(Config) ->
  190:     do_test(string1_test, Config).
  191: 
  192: string2_test(doc) -> "";
  193: string2_test(suite) -> [];
  194: string2_test(Config) ->
  195:     do_test(string2_test, Config).
  196: 
  197: string3_test(doc) -> "";
  198: string3_test(suite) -> [];
  199: string3_test(Config) ->
  200:     do_test(string3_test, Config).
  201: 
  202: string4_test(doc) -> "";
  203: string4_test(suite) -> [];
  204: string4_test(Config) ->
  205:     do_test(string4_test, Config).
  206: 
  207: struct2_test(doc) -> "";
  208: struct2_test(suite) -> [];
  209: struct2_test(Config) ->
  210:     do_test(struct2_test, Config).
  211: 
  212: struct_test(doc) -> "";
  213: struct_test(suite) -> [];
  214: struct_test(Config) ->
  215:     do_test(struct_test, Config).
  216: 
  217: term_sequence_test(doc) -> "";
  218: term_sequence_test(suite) -> [];
  219: term_sequence_test(Config) ->
  220:     do_test(term_sequence_test, Config).
  221: 
  222: term_struct_test(doc) -> "";
  223: term_struct_test(suite) -> [];
  224: term_struct_test(Config) ->
  225:     do_test(term_struct_test, Config).
  226: 
  227: term_test(doc) -> "";
  228: term_test(suite) -> [];
  229: term_test(Config) ->
  230:     do_test(term_test, Config).
  231: 
  232: typedef_test(doc) -> "";
  233: typedef_test(suite) -> [];
  234: typedef_test(Config) ->
  235:     do_test(typedef_test, Config).
  236: 
  237: unsigned_long_long_test(doc) -> "";
  238: unsigned_long_long_test(suite) -> [];
  239: unsigned_long_long_test(Config) ->
  240:     do_test(unsigned_long_long_test, Config).
  241: 
  242: unsigned_long_test(doc) -> "";
  243: unsigned_long_test(suite) -> [];
  244: unsigned_long_test(Config) ->
  245:     do_test(unsigned_long_test, Config).
  246: 
  247: unsigned_short_test(doc) -> "";
  248: unsigned_short_test(suite) -> [];
  249: unsigned_short_test(Config) ->
  250:     do_test(unsigned_short_test, Config).
  251: 
  252: void_test(doc) -> "";
  253: void_test(suite) -> [];
  254: void_test(Config) ->
  255:     do_test(void_test, Config).
  256: 
  257: wchar_test(doc) -> "";
  258: wchar_test(suite) -> [];
  259: wchar_test(Config) ->
  260:     do_test(wchar_test, Config).
  261: 
  262: wstring1_test(doc) -> "";
  263: wstring1_test(suite) -> [];
  264: wstring1_test(Config) ->
  265:     do_test(wstring1_test, Config).
  266: 
  267: 
  268: %% It is here that all tests really are done.
  269: %%
  270:      
  271: do_test(Case, Config) ->
  272:     %% Trap exits
  273:     process_flag(trap_exit, true),
  274:     %% Start the server
  275:     {ok, _Pid} = m_i:oe_create_link([], {local, ?ERLANG_SERVER_NAME}),
  276:     Node = atom_to_list(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: