blob: fd2e7bb991659092b256456b830ec0f37238b6a3 [file] [log] [blame]
Daniel Willmannd9304742018-10-23 20:29:59 +02001module Misc_Helpers {
2
Pau Espin Pedrola16a6b62024-03-21 16:19:07 +01003import from Native_Functions all;
Pau Espin Pedrol830061d2024-04-17 19:13:22 +02004import from TCCConversion_Functions all;
Pau Espin Pedrola16a6b62024-03-21 16:19:07 +01005
Oliver Smith8b6d1802021-11-26 13:30:13 +01006modulepar {
7 charstring mp_osmo_repo := "nightly";
8}
9
10/* Test the Osmocom repository (nightly, latest, 2021q4, ...), from which the
11 * SUT is running (OS#4878). Usage examples:
12 * if (Misc_Helpers.f_osmo_repo_is("nightly")) {
13 * ...
14 * }
15 * if (Misc_Helpers.f_osmo_repo_is(("nightly", "2021q4"))) {
16 * ...
17 * }
18 */
19function f_osmo_repo_is(template charstring ver) return boolean {
20 return match(mp_osmo_repo, ver);
21}
22
Daniel Willmannd9304742018-10-23 20:29:59 +020023/* Try to properly shutdown a testcase.
24 * The reliable method to stop a testcase without running into dynamic
25 * testcase errors due to unconnected ports receiving messages is to call
26 * all component.stop before terminating. However, this can only be called
27 * from the mtc! So in case we want to terminate from a component that is not
28 * the mtc we try to do the next best thing which is calling mtc.stop and
29 * hoping for the best.
30 */
31function f_shutdown(charstring file, integer line, verdicttype verdict := none,
32 charstring text := "") {
33 if (verdict != none) {
34 text := file & ":" & int2str(line) & " : " & text
35 setverdict(verdict, text);
36 }
37
38 log("Stopping testcase execution from ", file, ":", line)
39 if (self == mtc) {
40 /* Properly stop all ports before disconnecting them. This avoids
41 * running into the dynamic testcase error due to messages arriving on
42 * unconnected ports. */
43 all component.stop;
44 }
45 mtc.stop
46}
47
Pau Espin Pedrol6a846bc2020-09-04 13:01:22 +020048function f_addr_is_ipv6(charstring addr) return boolean {
49 for (var integer i := 0; i < lengthof(addr); i := i + 1) {
50 if (addr[i] == ":") {
51 return true;
52 }
53 }
54 return false;
55}
56
Pau Espin Pedrola16a6b62024-03-21 16:19:07 +010057function f_addrstr2addr(charstring addr) return octetstring {
58 if (f_addr_is_ipv6(addr)) {
59 return f_inet6_addr(addr);
60 } else {
61 return f_inet_addr(addr);
62 }
63}
64
Pau Espin Pedrol830061d2024-04-17 19:13:22 +020065/* Return a count of how many times sub_str occurs in str. */
66function f_strstr_count(in charstring str, in charstring sub_str) return integer
67{
68 var integer count := 0;
69 var integer pos := 0;
70
71 while (true) {
72 var integer at := f_strstr(str, sub_str, pos);
73 if (at < 0) {
74 break;
75 }
76 count := count + 1;
77 pos := at + 1;
78 }
79 return count;
80}
81
Pau Espin Pedrol601b5cd2024-04-17 19:16:17 +020082type record of charstring ro_charstring;
83function f_str_split(charstring str, charstring delim := "\n") return ro_charstring
84{
85 var integer pos := 0;
86 var ro_charstring parts := {};
87 var integer delim_pos;
88 var integer end := lengthof(str);
89 while (pos < end) {
90 delim_pos := f_strstr(str, delim, pos);
91 if (delim_pos < 0) {
92 delim_pos := end;
93 }
94 if (delim_pos > pos) {
95 parts := parts & { substr(str, pos, delim_pos - pos) };
96 }
97 pos := delim_pos + 1;
98 }
99 return parts;
100}
101
Daniel Willmannd9304742018-10-23 20:29:59 +0200102}