1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
%%
%% %CopyrightBegin%
%%
%% SPDX-License-Identifier: Apache-2.0
%%
%% Copyright Ericsson AB 2010-2025. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%% Tests based on the contents of the diameter app file.
%%
-module(diameter_app_SUITE).
%% testcases, no common_test dependency
-export([run/0,
run/1]).
%% common_test wrapping
-export([
%% Framework functions
suite/0,
all/0,
init_per_suite/1,
end_per_suite/1,
init_per_testcase/2,
end_per_testcase/2
]).
%% testcases
-export([keys/1,
vsn/1,
modules/1,
exports/1,
release/1,
xref/1,
relup/1]).
-include_lib("kernel/include/file.hrl").
-include("diameter_util.hrl").
%% ===========================================================================
-define(A, list_to_atom).
%% Modules not in the app and that should not have dependencies on it
%% for build reasons.
-define(COMPILER_MODULES, [diameter_codegen,
diameter_dict_scanner,
diameter_dict_parser,
diameter_dict_util,
diameter_exprecs,
diameter_make]).
-define(INFO_MODULES, [diameter_dbg,
diameter_info]).
-define(AL(F), ?AL(F, [])).
-define(AL(F, A), ?LOG("DAPPS", F, A)).
%% ===========================================================================
suite() ->
[{timetrap, {seconds, 120}}].
all() ->
[keys,
vsn,
modules,
exports,
release,
xref,
relup].
init_per_suite(Config) ->
?DUTIL:init_per_suite(Config).
end_per_suite(Config) ->
?DUTIL:end_per_suite(Config).
%% This test case can take a *long* time, so if the machine is too slow, skip
init_per_testcase(xref = Case, Config) when is_list(Config) ->
?AL("init_per_testcase(~w) -> check factor", [Case]),
Key = dia_factor,
case lists:keysearch(Key, 1, Config) of
{value, {Key, Factor}} when (Factor > 10) ->
?AL("init_per_testcase(~w) -> Too slow (~w) => SKIP",
[Case, Factor]),
{skip, {machine_too_slow, Factor}};
_ ->
?AL("init_per_testcase(~w) -> run test", [Case]),
Config
end;
init_per_testcase(Case, Config) ->
?AL("init_per_testcase(~w) -> entry", [Case]),
Config.
end_per_testcase(Case, Config) when is_list(Config) ->
?AL("end_per_testcase(~w) -> entry", [Case]),
Config.
%% ===========================================================================
run() ->
run(all()).
run(List) ->
Tmp = ?MKTEMP("diameter_app"),
try
run([{priv_dir, Tmp}], List)
after
file:del_dir_r(Tmp)
end.
run(Config, List) ->
Timeout = factor2timeout(Config, 10000),
?AL("run -> use Timeout: ~w", [Timeout]),
[{application, diameter, App}] = ?CONSULT(diameter, app),
?RUN([{{?MODULE, F, [{App, Config}]}, Timeout} || F <- List]).
factor2timeout(Config, BaseTime) ->
Key = dia_factor,
case lists:keysearch(Key, 1, Config) of
{value, {Key, Factor}} when (Factor > 0) ->
BaseTime + (((Factor-1)*BaseTime) div 10);
_ ->
BaseTime
end.
%% ===========================================================================
%% # keys/1
%%
%% Ensure that the app file contains selected keys. Some of these would
%% also be caught by other testcases.
%% ===========================================================================
keys({App, _Config}) ->
[] = lists:filter(fun(K) -> not lists:keymember(K, 1, App) end,
[vsn, description, modules, registered, applications]);
keys(Config) ->
run(Config, [keys]).
%% ===========================================================================
%% # vsn/1
%%
%% Ensure that our app version sticks to convention.
%% ===========================================================================
vsn({App, _Config}) ->
true = is_vsn(fetch(vsn, App));
vsn(Config) ->
run(Config, [vsn]).
%% ===========================================================================
%% # modules/1
%%
%% Ensure that the app file modules and installed modules differ by
%% compiler/info modules.
%% ===========================================================================
modules({App, _Config}) ->
Mods = fetch(modules, App),
Installed = code_mods(),
Help = lists:sort(?INFO_MODULES ++ ?COMPILER_MODULES),
{[], Help} = {Mods -- Installed, lists:sort(Installed -- Mods)};
modules(Config) ->
run(Config, [modules]).
code_mods() ->
Dir = ?LIB_DIR(diameter, ebin),
{ok, Files} = file:list_dir(Dir),
[?A(lists:reverse(R)) || N <- Files, "maeb." ++ R <- [lists:reverse(N)]].
%% ===========================================================================
%% # exports/1
%%
%% Ensure that no module does export_all.
%% ===========================================================================
exports({App, _Config}) ->
Mods = fetch(modules, App),
[] = [M || M <- Mods, exports_all(M)];
exports(Config) ->
run(Config, [exports]).
exports_all(Mod) ->
Opts = fetch(options, Mod:module_info(compile)),
is_list(Opts) andalso lists:member(export_all, Opts).
%% ===========================================================================
%% # release/1
%%
%% Ensure that it's possible to build a minimal release with our app file.
%% ===========================================================================
release({App, Config}) ->
Rel = {release,
{"diameter test release", fetch(vsn, App)},
{erts, erlang:system_info(version)},
[{A, appvsn(A)} || A <- [sasl | fetch(applications, App)]]},
Dir = fetch(priv_dir, Config),
ok = write_file(filename:join([Dir, "diameter_test.rel"]), Rel),
{ok, _, []} = systools:make_script("diameter_test", [{path, [Dir]},
{outdir, Dir},
silent]);
release(Config) ->
run(Config, [release]).
%% sasl need to be included to avoid a missing_sasl warning, error
%% in the case of relup/1.
appvsn(Name) ->
[{application, Name, App}] = ?CONSULT(Name, app),
fetch(vsn, App).
%% ===========================================================================
%% # xref/1
%%
%% Ensure that no function in our application calls an undefined function
%% or one in an application we haven't declared as a dependency. (Almost.)
%% ===========================================================================
xref({App, _Config}) ->
?AL("xref -> entry with"
"~n App: ~p"
"~n Config: ~p", [App, _Config]),
Mods = fetch(modules, App), %% modules listed in the app file
%% List of application names extracted from runtime_dependencies.
?AL("xref -> get deps"),
Deps = lists:map(fun unversion/1, fetch(runtime_dependencies, App)),
?AL("xref -> start xref"),
{ok, XRef} = xref:start(make_name(xref_test_name)),
ok = xref:set_default(XRef, [{verbose, false}, {warnings, false}]),
%% Only add our application and those it's dependent on according
%% to the app file. Well, almost. erts beams are also required to
%% stop xref from complaining about calls to module erlang, which
%% was previously in kernel. Erts isn't an application however, in
%% the sense that there's no .app file, and isn't listed in
%% applications.
Apps = [diameter, erts | fetch(applications, App)],
?AL("xref -> add own and dep apps: "
"~n ~p", [Apps]),
ok = lists:foreach(fun(A) -> add_application(XRef, A) end, Apps),
?AL("xref -> analyze undefined_function_calls"),
{ok, Undefs} = xref:analyze(XRef, undefined_function_calls),
?AL("xref -> analyze module use: "
"~n For mods: ~p", [Mods]),
{ok, RTmods} = xref:analyze(XRef, {module_use, Mods}),
?AL("xref -> analyze (compiler) module use: "
"~n For mods: ~p", [?COMPILER_MODULES]),
{ok, CTmods} = xref:analyze(XRef, {module_use, ?COMPILER_MODULES}),
?AL("xref -> analyze module call: "
"~n For mods: ~p", [Mods]),
{ok, RTdeps} = xref:analyze(XRef, {module_call, Mods}),
?AL("xref -> stop xref"),
xref:stop(XRef),
?AL("xref -> get OTP release"),
Rel = release(), %% otp_release-ish
%% Only care about calls from our own application.
?AL("xref -> Only care about calls from our own application"),
[] = lists:filter(fun({{F,_,_} = From, {_,_,_} = To}) ->
lists:member(F, Mods)
andalso not ignored(From, To, Rel)
end,
Undefs),
%% Ensure that only runtime or info modules call runtime modules.
%% It's not strictly necessary that diameter compiler modules not
%% depend on other diameter modules but it's a simple source of
%% build errors if not properly encoded in the makefile so guard
%% against it.
?AL("xref -> ensure only runtime and info mod"),
[] = (RTmods -- Mods) -- ?INFO_MODULES,
%% Ensure that runtime modules don't call compiler modules.
?AL("xref -> ensure runtime mods don't call compiler mods"),
CTmods = CTmods -- Mods,
%% Ensure that runtime modules only call other runtime modules, or
%% applications declared in runtime_dependencies in the app file.
%% The declared application versions are ignored since we only
%% know what we see now.
?AL("xref -> ensure runtime mods only call runtime mods"),
[] = lists:filter(fun(M) -> not lists:member(app(M), Deps) end,
RTdeps -- Mods),
?AL("xref -> done"),
ok;
xref(Config) ->
?AL("xref -> entry with"
"~n Config: ~p", [Config]),
Res = run(Config, [xref]),
?AL("xref -> done when"
"~n Res: ~p", [Res]),
Res.
ignored({FromMod,_,_}, {ToMod,_,_} = To, Rel)->
%% diameter_tcp does call ssl despite the latter not being listed
%% as a dependency in the app file since ssl is only required for
%% TLS security: it's up to a client who wants TLS to start ssl.
%% The OTP 18 time api is also called if it exists, so that the
%% same code can be run on older releases.
{FromMod, ToMod} == {diameter_tcp, ssl}
orelse (FromMod == diameter_lib
andalso Rel < 18
andalso lists:member(To, time_api())).
%% New time api in OTP 18.
time_api() ->
[{erlang, F, A} || {F,A} <- [{convert_time_unit,3},
{monotonic_time,0},
{monotonic_time,1},
{system_time,0},
{system_time,1},
{time_offset,0},
{time_offset,1},
{timestamp,0},
{unique_integer,0},
{unique_integer,1}]]
++ [{os, system_time, 0},
{os, system_time, 1}].
release() ->
Rel = erlang:system_info(otp_release),
try list_to_integer(Rel) of
N -> N
catch
error:_ ->
0 %% aka < 17
end.
unversion(App) ->
{Name, [$-|Vsn]} = lists:splitwith(fun(C) -> C /= $- end, App),
true = is_app(Name), %% assert
Vsn = vsn_str(Vsn), %%
Name.
app('$M_EXPR') -> %% could be anything but assume it's ok
"erts";
app(Mod) ->
case code:which(Mod) of
preloaded ->
"erts";
Reason when is_atom(Reason) ->
error({Reason, Mod});
Path ->
%% match to identify an unexpectedly short path
{_, _, [_,_,_|_] = Split} = {Mod, Path, filename:split(Path)},
unversion(lists:nth(3, lists:reverse(Split)))
end.
add_application(XRef, App) ->
?AL("add_application -> get lib dir for app ~p", [App]),
LibDir = code:lib_dir(App),
?AL("add_application -> [xref] add lib dir:"
"~n ~p", [LibDir]),
{ok, App} = xref:add_application(XRef, LibDir, []).
make_name(Suf) ->
list_to_atom("diameter_" ++ atom_to_list(Suf)).
%% ===========================================================================
%% # relup/1
%%
%% Ensure that we can generate release upgrade files using our appup file.
%% ===========================================================================
relup({App, Config}) ->
i("relup -> entry with"
"~n App: ~p"
"~n Config: ~p", [App, Config]),
[{Vsn, Up, Down}] = ?CONSULT(diameter, appup),
true = is_vsn(Vsn),
i("relup -> "
"~n Vsn: ~p"
"~n Up: ~p"
"~n Down: ~p", [Vsn, Up, Down]),
Rel = [{erts, erlang:system_info(version)}
| [{A, appvsn(A)} || A <- [sasl | fetch(applications, App)]]],
i("relup -> "
"~n Rel: ~p", [Rel]),
Dir = fetch(priv_dir, Config),
i("relup -> verify path"
"~n Dir: ~p", [Dir]),
verify_path(Dir),
i("relup -> "
"~n Dir: "
"~n ~s"
"~n File info (dir): "
"~n ~s", [Dir, file_info(Dir)]),
Name = write_rel(Dir, Rel, Vsn),
i("relup -> written"
"~n Name: "
"~n ~s", [Name]),
UpFrom = acc_rel(Dir, Rel, Up),
i("relup -> "
"~n UpFrom: ~p", [UpFrom]),
DownTo = acc_rel(Dir, Rel, Down),
i("relup -> "
"~n DownTo: ~p", [DownTo]),
{[], []} = {UpFrom -- DownTo, DownTo -- UpFrom},
[[], []] = [S -- sets:to_list(sets:from_list(S))
|| S <- [UpFrom, DownTo]],
i("relup -> try make relup"),
{ok, _, _, []} = systools:make_relup(Name, UpFrom, DownTo, [{path, [Dir]},
{outdir, Dir},
silent]);
relup(Config) ->
run(Config, [relup]).
verify_path(Path) ->
Components = filename:split(filename:absname(Path)),
do_verify_path(Components).
do_verify_path([]) ->
exit(not_a_path);
do_verify_path([Root|Components]) ->
do_verify_path(Root, Components).
do_verify_path(Path, []) ->
case file:read_file_info(Path) of
{ok, #file_info{type = directory,
access = Access}} ->
i("do_verify_path -> (final) directory ok:"
"~n Path: ~p"
"~n Access: ~p", [Path, Access]),
ok;
{ok, #file_info{type = Type,
access = Access}} ->
i("do_verify_path -> (final) unexpected type: "
"~n Path: ~p"
"~n Type: ~p"
"~n Access: ~p", [Path, Type, Access]),
exit({not_dir, Path, Type, Access});
{error, Reason} ->
i("do_verify_path -> failed reading file info: "
"~n Path: ~p"
"~n Reason: ~p", [Path, Reason]),
exit({read_file_info, Path, Reason})
end;
do_verify_path(Path, [Component|Components]) ->
case file:read_file_info(Path) of
{ok, #file_info{type = directory,
access = Access}} ->
i("do_verify_path -> directory ok: "
"~n Path: ~p"
"~n Access: ~p", [Path, Access]),
do_verify_path(filename:absname_join(Path, Component),
Components);
{ok, #file_info{type = Type,
access = Access}} ->
i("do_verify_path -> unexpected type: "
"~n Path: ~p"
"~n Type: ~p"
"~n Access: ~p", [Path, Type, Access]),
exit({not_dir, Path, Type, Access});
{error, Reason} ->
i("do_verify_path -> failed reading file info: "
"~n Path: ~p"
"~n Reason: ~p", [Path, Reason]),
exit({read_file_info, Path, Reason})
end.
file_info(Path) ->
case file:read_file_info(Path) of
{ok, Info} ->
f("~p", [Info]);
{error, Reason} ->
f("error: ~p", [Reason])
end.
f(F, A) ->
lists:flatten(io_lib:format(F, A)).
acc_rel(Dir, Rel, List) ->
lists:map(fun({V,_}) -> write_rel(Dir, Rel, V) end, List).
%% Write a rel file and return its name.
write_rel(Dir, [Erts | Apps], Vsn) ->
VS = vsn_str(Vsn),
Name = "diameter_test_" ++ VS,
File = filename:join([Dir, Name ++ ".rel"]),
i("write_rel -> attempt write rel file:"
"~n Dir: ~p"
"~n File: ~p"
"~n File name length: ~p"
"~n Erts: ~p"
"~n Apps: ~p"
"~n Vsn: ~p (~p)",
[Dir, File, length(File), Erts, Apps, Vsn, VS]),
ok = write_file(File,
{release,
{"diameter " ++ VS ++ " test release", VS},
Erts,
Apps}),
Name.
%% ===========================================================================
%% ===========================================================================
fetch(Key, List) ->
{Key, {Key, Val}} = {Key, lists:keyfind(Key, 1, List)}, %% useful badmatch
Val.
write_file(Path, T) ->
file:write_file(Path, io_lib:format("~p.", [T])).
%% Is a version string of the expected form?
is_vsn(V) ->
V = vsn_str(V),
true.
%% Turn a from/to version in appup to a version string after ensuring
%% that it's valid version number of regexp. In the regexp case, the
%% regexp itself becomes the version string since there's no
%% requirement that a version in appup be anything but a string. The
%% restrictions placed on string-valued version numbers (that they be
%% '.'-separated integers) are our own.
vsn_str(S)
when is_list(S) ->
{_, match} = {S, match(S, "^(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*$")},
{_, nomatch} = {S, match(S, "\\.0\\.0$")},
S;
vsn_str(B)
when is_binary(B) ->
{ok, _} = re:compile(B),
%% Check if its a wildcard version: "1\\.."
Str = binary_to_list(B),
case lists:reverse(Str) of
[$*, $., $., $\\ | Rest] ->
lists:reverse(Rest);
_ ->
Str
end.
match(S, RE) ->
re:run(S, RE, [{capture, none}]).
%% Is an application name of the expected form?
is_app(S)
when is_list(S) ->
{_, match} = {S, match(S, "^([a-z]([a-z_]*|[a-zA-Z]*))$")},
true.
%% ===========================================================================
i(F) ->
i(F, []).
i(F, A) when is_list(F) andalso is_list(A) ->
io:format(F ++ "~n", A).
|