1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 1998-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: %% File: ic_register_SUITE.erl
   22: %% 
   23: %% Description:
   24: %% Test suite for the IFR object registration
   25: %%
   26: %%-----------------------------------------------------------------
   27: -module(ic_register_SUITE).
   28: 
   29: -include_lib("test_server/include/test_server.hrl").
   30: -include_lib("orber/include/corba.hrl").
   31: %%-----------------------------------------------------------------
   32: %% External exports
   33: %%-----------------------------------------------------------------
   34: -export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2, 
   35: 	 init_per_suite/1, end_per_suite/1, ifr_reg_unreg/1]).
   36: -export([ifr_inheritence_reg/1,ifr_reg_unreg_with_inheritence/1]).
   37: -export([ifr_reg_unreg_with_inheritence_bad_order/1]).
   38: 
   39: %%-----------------------------------------------------------------
   40: %% Internal exports
   41: %%-----------------------------------------------------------------
   42: -export([]).
   43: 
   44: %%-----------------------------------------------------------------
   45: %% Macros
   46: %%-----------------------------------------------------------------
   47: -define(REMAP_EXCEPT(F), case catch F of
   48: 			     {'EXCEPTION', E} -> exit(E);
   49: 			     R -> R
   50: 			 end).
   51: %% Standard options to the ic compiler, NOTE unholy use of OutDir
   52: 
   53: -define(OUT(X), filename:join([?config(priv_dir, Config), gen, to_list(X)])).
   54: 
   55: 
   56: %%-----------------------------------------------------------------
   57: %% Func: all/1
   58: %% Args: 
   59: %% Returns: 
   60: %%-----------------------------------------------------------------
   61: suite() -> [{ct_hooks,[ts_install_cth]}].
   62: 
   63: all() -> 
   64:     cases().
   65: 
   66: groups() -> 
   67:     [].
   68: 
   69: init_per_group(_GroupName, Config) ->
   70:     Config.
   71: 
   72: end_per_group(_GroupName, Config) ->
   73:     Config.
   74: 
   75: 
   76: cases() -> 
   77:     [ifr_reg_unreg, ifr_reg_unreg_with_inheritence,
   78:      ifr_reg_unreg_with_inheritence_bad_order,
   79:      ifr_inheritence_reg].
   80: 
   81: %%-----------------------------------------------------------------
   82: %% Init and cleanup functions.
   83: %%-----------------------------------------------------------------
   84: 
   85: init_per_suite(Config) ->
   86:     io:format("Setting up.....~n"),
   87:     mnesia:stop(),
   88:     mnesia:delete_schema([node()]),
   89:     mnesia:create_schema([node()]),
   90:     mnesia:start(),
   91:     orber:install([node()]),
   92:     orber:start(),
   93:     if
   94: 	is_list(Config) ->
   95: 	    Config;
   96: 	true ->
   97: 	    exit("Config not a list")
   98:     end.
   99: 
  100: end_per_suite(Config) ->
  101:     io:format("Setting down.....~n"),
  102:     orber:stop(),
  103:     orber:uninstall(),
  104:     mnesia:stop(),
  105:     mnesia:delete_schema([node()]),
  106:     Config.
  107: 
  108: 
  109: 
  110: %%-----------------------------------------------------------------
  111: %% Test Case: IFR type registration
  112: %%-----------------------------------------------------------------
  113: ifr_reg_unreg(doc) ->
  114:     ["Checks that the generated register/unregister "
  115:      "code for the IFR is correct."];
  116: ifr_reg_unreg(suite) -> [];
  117: ifr_reg_unreg(Config) when is_list(Config) ->
  118:     ?REMAP_EXCEPT(ifr_reg_unregt_run(Config)).
  119: 
  120: ifr_reg_unregt_run(Config) ->
  121:     DataDir = ?config(data_dir, Config),
  122:     OutDir = ?OUT(ifr_reg_unreg),
  123:     File0 = filename:join(DataDir, reg_m8),
  124:     File1 = filename:join(DataDir, reg_m9),
  125:     File2 = filename:join(DataDir, reg_m10),
  126:     ?line ok = ic:gen(File0, stdopts(OutDir)++[{preproc_flags,
  127: 						"-I" ++ DataDir}] ),
  128:     ?line {ok, []} = ic:gen(File0, stdopts(OutDir)++[silent2, {preproc_flags,
  129: 					       "-I" ++ DataDir}]),
  130:     ?line ok = ic:gen(File1, stdopts(OutDir)++[{preproc_flags,
  131: 						"-I" ++ DataDir}] ),
  132:     ?line {ok, []} = ic:gen(File1, stdopts(OutDir)++[silent2, {preproc_flags,
  133: 					       "-I" ++ DataDir}]),
  134:     ?line ok = ic:gen(File2, stdopts(OutDir)++[{preproc_flags,
  135: 						"-I" ++ DataDir}] ),
  136:     ?line {ok, []} = ic:gen(File2, stdopts(OutDir)++[silent2, {preproc_flags,
  137: 					       "-I" ++ DataDir}]),
  138:     ?line ok = compile(OutDir, ifr_reg_unreg_files()),
  139:     code:add_pathz(OutDir),
  140:     ?line ok = 'oe_reg_m8':'oe_register'(),
  141:     ?line ok = 'oe_reg_m9':'oe_register'(),
  142:     ?line ok = 'oe_reg_m10':'oe_register'(),
  143:     ?line ok = 'oe_reg_m10':'oe_unregister'(),
  144:     ?line ok = 'oe_reg_m9':'oe_unregister'(),
  145:     ?line ok = 'oe_reg_m8':'oe_unregister'(),
  146:     code:del_path(OutDir),
  147:     ok.
  148: 
  149: ifr_reg_unreg_files() -> ['oe_reg_m8', 'oe_reg_m9', 'oe_reg_m10'].
  150: 
  151: 
  152: 
  153: %%-----------------------------------------------------------------
  154: %% Test Case: IFR registration when object inheritence 
  155: %%            is applied and registered.
  156: %%-----------------------------------------------------------------
  157: ifr_reg_unreg_with_inheritence(doc) ->
  158:     ["Checks that the generated register/unregister "
  159:      "code for the IFR is correct, and works even when"
  160:      "the object inheritence is registered. This fixes"
  161:      "two bugs in ifr that caused crash when trying to"
  162:      "use OE_register/OE_unregister in a sequence of"
  163:      "compiled files that contained interfaces who"
  164:      "inherited others in sequence."];
  165: ifr_reg_unreg_with_inheritence(suite) -> [];
  166: ifr_reg_unreg_with_inheritence(Config) when is_list(Config) ->
  167:     ?REMAP_EXCEPT(ifr_reg_unreg_with_inheritence_run(Config)).
  168: 
  169: ifr_reg_unreg_with_inheritence_run(Config) ->
  170:     DataDir = ?config(data_dir, Config),
  171:     OutDir = ?OUT(ifr_reg_unreg),
  172:     File0 = filename:join(DataDir, reg_m8),
  173:     File1 = filename:join(DataDir, reg_m9),
  174:     File2 = filename:join(DataDir, reg_m10),
  175:     File3 = filename:join(DataDir, reg_m11),
  176:     File4 = filename:join(DataDir, reg_m12),
  177:     ?line ok = ic:gen(File0, stdopts(OutDir)++[{preproc_flags,
  178: 						"-I" ++ DataDir}] ),
  179:     ?line {ok, []} = ic:gen(File0, stdopts(OutDir)++[silent2, {preproc_flags,
  180: 					       "-I" ++ DataDir}]),
  181:     ?line ok = ic:gen(File1, stdopts(OutDir)++[{preproc_flags,
  182: 						"-I" ++ DataDir}] ),
  183:     ?line {ok, []} = ic:gen(File1, stdopts(OutDir)++[silent2, {preproc_flags,
  184: 					       "-I" ++ DataDir}]),
  185:     ?line ok = ic:gen(File2, stdopts(OutDir)++[{preproc_flags,
  186: 						"-I" ++ DataDir}] ),
  187:     ?line {ok, []} = ic:gen(File2, stdopts(OutDir)++[silent2, {preproc_flags,
  188: 					       "-I" ++ DataDir}]),
  189:     ?line ok = ic:gen(File3, stdopts(OutDir)++[{preproc_flags,
  190: 						"-I" ++ DataDir}] ),
  191:     ?line {ok, []} = ic:gen(File3, stdopts(OutDir)++[silent2, {preproc_flags,
  192: 						"-I" ++ DataDir}]),
  193:     ?line ok = ic:gen(File4, stdopts(OutDir)++[{preproc_flags,
  194: 						"-I" ++ DataDir}] ),
  195:     ?line {ok, []} = ic:gen(File4, stdopts(OutDir)++[silent2, {preproc_flags,
  196: 						"-I" ++ DataDir}]),
  197:     ?line ok = compile(OutDir, ifr_reg_unreg_with_inheritence_files()),
  198:     code:add_pathz(OutDir),
  199:     ?line ok = 'oe_reg_m8':'oe_register'(),
  200:     ?line ok = 'oe_reg_m9':'oe_register'(),
  201:     ?line ok = 'oe_reg_m10':'oe_register'(),
  202:     ?line ok = 'oe_reg_m11':'oe_register'(),
  203:     ?line ok = 'oe_reg_m12':'oe_register'(),
  204:     ?line ok = 'oe_reg_m8':'oe_unregister'(),
  205:     ?line ok = 'oe_reg_m9':'oe_unregister'(),
  206:     ?line ok = 'oe_reg_m10':'oe_unregister'(),
  207:     ?line ok = 'oe_reg_m11':'oe_unregister'(),
  208:     ?line ok = 'oe_reg_m12':'oe_unregister'(),
  209:     code:del_path(OutDir),
  210:     ok.
  211: 
  212: ifr_reg_unreg_with_inheritence_files() -> 
  213:     ['oe_reg_m8', 'oe_reg_m9', 'oe_reg_m10', 'oe_reg_m11', 'oe_reg_m12'].
  214: 
  215: 
  216: 
  217: 
  218: 
  219: %%-----------------------------------------------------------------
  220: %% Test Case: IFR registration when object inheritence 
  221: %%            is applied and registered in a bad order.
  222: %%            Modules included and used from an ifr object
  223: %%            are not allready registered when the current 
  224: %%            object is getting registered.
  225: %%-----------------------------------------------------------------
  226: ifr_reg_unreg_with_inheritence_bad_order(doc) ->
  227:     ["This tests that ifr registration is done with
  228:       the right write order."
  229:      "Modules included and used from an ifr object"
  230:      "are tested if allready registered when the "
  231:      "current object is getting registered."];
  232: ifr_reg_unreg_with_inheritence_bad_order(suite) -> [];
  233: ifr_reg_unreg_with_inheritence_bad_order(Config) when is_list(Config) ->
  234:     ?REMAP_EXCEPT(ifr_reg_unreg_with_inheritence_bad_order_run(Config)).
  235: 
  236: ifr_reg_unreg_with_inheritence_bad_order_run(Config) ->
  237:     DataDir = ?config(data_dir, Config),
  238:     OutDir = ?OUT(ifr_reg_unreg),
  239:     File1 = filename:join(DataDir, reg_m9),
  240:     File2 = filename:join(DataDir, reg_m10),
  241:     File4 = filename:join(DataDir, reg_m12),
  242:     ?line ok = ic:gen(File1, stdopts(OutDir)++[{preproc_flags,
  243: 						"-I" ++ DataDir}] ),
  244:     ?line {ok, []} = ic:gen(File1, stdopts(OutDir)++[silent2, {preproc_flags,
  245: 					       "-I" ++ DataDir}]),
  246:     ?line ok = ic:gen(File2, stdopts(OutDir)++[{preproc_flags,
  247: 						"-I" ++ DataDir}] ),
  248:     ?line {ok, []} = ic:gen(File2, stdopts(OutDir)++[silent2, {preproc_flags,
  249: 					       "-I" ++ DataDir}]),
  250:     ?line ok = ic:gen(File4, stdopts(OutDir)++[{preproc_flags,
  251: 						"-I" ++ DataDir}] ),
  252:     ?line {ok, []} = ic:gen(File4, stdopts(OutDir)++[silent2, {preproc_flags,
  253: 						"-I" ++ DataDir}]),
  254:     ?line ok = compile(OutDir, ifr_reg_unreg_with_inheritence_files()),
  255:     code:add_pathz(OutDir),
  256:     case catch 'oe_reg_m12':'oe_register'() of
  257: 	{'EXIT',Reason1} ->
  258: 	    io:format("IFR object missing detected : ~p~n",[Reason1]),
  259: 	    true;
  260: 	_ ->
  261: 	    test_server:fail("Failed to detect object missing : IDL:M1:1.0~n")
  262:     end,
  263:     ?line ok = 'oe_reg_m9':'oe_register'(),
  264:     case catch 'oe_reg_m10':'oe_register'() of
  265: 	{'EXIT',Reason2} ->
  266: 	    io:format("IFR object missing detected : ~p~n",[Reason2]),
  267: 	    true;
  268: 	_ ->
  269: 	    test_server:fail("Failed to detect object missing : IDL:M0:1.0~n")
  270:     end,
  271:     ?line ok = 'oe_reg_m9':'oe_unregister'(),
  272:     code:del_path(OutDir),
  273:     ok.
  274: 
  275: 
  276: 
  277: %%-----------------------------------------------------------------
  278: %% Test Case: IFR registration with inheritence
  279: %%-----------------------------------------------------------------
  280: ifr_inheritence_reg(doc) ->
  281:     ["Checks that IFR object inheritence is correctly registered."];
  282: ifr_inheritence_reg(suite) -> [];
  283: ifr_inheritence_reg(Config) when is_list(Config) ->
  284:     ?REMAP_EXCEPT(ifr_inh_reg_run(Config)).
  285: 
  286: ifr_inh_reg_run(Config) ->
  287:     DataDir = ?config(data_dir, Config),
  288:     OutDir = ?OUT(ifr_reg_unreg),
  289:     File0 = filename:join(DataDir, reg_m8),
  290:     File1 = filename:join(DataDir, reg_m9),
  291:     File2 = filename:join(DataDir, reg_m10),
  292:     File3 = filename:join(DataDir, reg_m11),
  293:     File4 = filename:join(DataDir, reg_m12),
  294:     ?line ok = ic:gen(File0, stdopts(OutDir)++[{preproc_flags,
  295: 						"-I" ++ DataDir}] ),
  296:     ?line {ok, []} = ic:gen(File0, stdopts(OutDir)++[silent2, {preproc_flags,
  297: 					       "-I" ++ DataDir}]),
  298:     ?line ok = ic:gen(File1, stdopts(OutDir)++[{preproc_flags,
  299: 						"-I" ++ DataDir}] ),
  300:     ?line {ok, []} = ic:gen(File1, stdopts(OutDir)++[silent2, {preproc_flags,
  301: 					       "-I" ++ DataDir}]),
  302:     ?line ok = ic:gen(File2, stdopts(OutDir)++[{preproc_flags,
  303: 						"-I" ++ DataDir}] ),
  304:     ?line {ok, []} = ic:gen(File2, stdopts(OutDir)++[silent2, {preproc_flags,
  305: 					       "-I" ++ DataDir}]),
  306:     ?line ok = ic:gen(File3, stdopts(OutDir)++[{preproc_flags,
  307: 						"-I" ++ DataDir}] ),
  308:     ?line {ok, []} = ic:gen(File3, stdopts(OutDir)++[silent2, {preproc_flags,
  309: 						"-I" ++ DataDir}]),
  310:     ?line ok = ic:gen(File4, stdopts(OutDir)++[{preproc_flags,
  311: 						"-I" ++ DataDir}] ),
  312:     ?line {ok, []} = ic:gen(File4, stdopts(OutDir)++[silent2, {preproc_flags,
  313: 						"-I" ++ DataDir}]),
  314:     ?line ok = compile(OutDir, ifr_reg_unreg_with_inheritence_files()),
  315:     code:add_pathz(OutDir),
  316:     %% OE_register for all files
  317:     ?line ok = 'oe_reg_m8':'oe_register'(),
  318:     ?line ok = 'oe_reg_m9':'oe_register'(),
  319:     ?line ok = 'oe_reg_m10':'oe_register'(),
  320:     ?line ok = 'oe_reg_m11':'oe_register'(),
  321:     ?line ok = 'oe_reg_m12':'oe_register'(),
  322:     
  323:     %% Inheritence registration test
  324:     OE_IFR = orber_ifr:find_repository(),
  325:     %% Interfaces that not inherit from other interfaces
  326:     ?line [] = get_inh(OE_IFR, "IDL:m0/i0:1.0"),
  327:     ?line [] = get_inh(OE_IFR, "IDL:m1/i1:1.0"),
  328:     ?line [] = get_inh(OE_IFR, "IDL:m3/i3:1.0"),
  329:     %% Interfaces that inherit from other interfaces
  330:     ?line ["IDL:m1/i1:1.0"] = get_inh(OE_IFR, "IDL:m2/i2:1.0"),
  331:     ?line ["IDL:m1/i1:1.0","IDL:m2/i2:1.0"] = get_inh(OE_IFR, "IDL:m4/i4:1.0"),
  332:     ?line ["IDL:m3/i3:1.0"] = get_inh(OE_IFR, "IDL:m4/i5:1.0"),
  333:     
  334:     %% OE_unregister for all files
  335:     ?line ok = 'oe_reg_m8':'oe_unregister'(),
  336:     ?line ok = 'oe_reg_m9':'oe_unregister'(),
  337:     ?line ok = 'oe_reg_m10':'oe_unregister'(),
  338:     ?line ok = 'oe_reg_m11':'oe_unregister'(),
  339:     ?line ok = 'oe_reg_m12':'oe_unregister'(),
  340:     code:del_path(OutDir),
  341:     ok.
  342: 
  343: 
  344: get_inh(OE_IFR,ID) ->
  345:     OE_CURRENT = orber_ifr:lookup_id(OE_IFR,ID),
  346:     INH_LIST = orber_ifr:get_base_interfaces(OE_CURRENT),
  347:     case INH_LIST of
  348: 	[] ->
  349: 	    io:format("~nInterface ~p inherits from nobody.~n",[ID]),
  350: 	    [];
  351: 	_ ->
  352: 	    print_inh_list_ids(ID, INH_LIST, [])
  353:     end.
  354: 
  355: print_inh_list_ids(_ID, [], Acc) ->
  356:     lists:reverse(Acc);
  357: print_inh_list_ids(ID, [H|T], Acc) ->
  358:     io:format("~n"),
  359:     Parent = orber_ifr:get_id(H),
  360:     io:format("Interface ~p inherits from ~p.~n", [ID, Parent]),
  361:     print_inh_list_ids(ID, T, [Parent|Acc]).
  362: 
  363: 
  364: 
  365: 
  366: stdopts(OutDir) ->
  367:     [{outdir, OutDir}, {maxerrs, infinity}].
  368: 
  369: 
  370: compile(Dir, Files) ->
  371:     compile(Dir, Files, []).
  372: 
  373: compile(Dir, Files, Opts) ->
  374:     {ok, Cwd} = file:get_cwd(),
  375:     file:set_cwd(Dir),
  376:     io:format("Changing to ~p~n", [Dir]),
  377:     case catch do_compile(Files, Opts) of
  378: 	ok ->
  379: 	    file:set_cwd(Cwd);
  380: 	Err ->
  381: 	    file:set_cwd(Cwd),
  382: 	    test_server:fail(Err)
  383:     end.
  384: 
  385: do_compile([], _Opts) -> ok;
  386: do_compile([F | Fs], Opts) ->
  387:     io:format("Compiling ~p", [F]),
  388:     case compile:file(F, Opts) of
  389: 	ok ->
  390: 	    io:format(" ok~n", []),
  391: 	    do_load(F, Opts),
  392: 	    do_compile(Fs, Opts);
  393: 	{ok, _} ->
  394: 	    io:format(" ok~n", []),
  395: 	    do_load(F, Opts),
  396: 	    do_compile(Fs, Opts);
  397: 	{ok, _, _} ->
  398: 	    io:format(" ok~n", []),
  399: 	    do_load(F, Opts),
  400: 	    do_compile(Fs, Opts);
  401: 	Err -> 
  402: 	    io:format(" error: ~p~n", [Err]),
  403: 	    Err
  404:     end.
  405: 
  406: do_load(File, Opts) ->
  407:     case lists:member(load, Opts) of
  408: 	true ->
  409: 	    io:format("Loading file ~p", [File]),
  410: 	    code:purge(File),
  411: 	    R = code:load_abs(File),
  412: 	    io:format("Loaded: ~p", [R]);
  413: 	false ->
  414: 	    ok
  415:     end.
  416: 
  417: 
  418: to_list(X) when is_atom(X) -> atom_to_list(X);
  419: to_list(X) -> X.
  420: 
  421: 
  422: 
  423: 
  424: 
  425: 
  426: 
  427: 
  428: 
  429: 
  430: 
  431: 
  432: 
  433: 
  434: 
  435: 
  436: 
  437: