blob: a742b0b11f7c1ead51097968f7e8b7675fce7d50 [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 Pedrol9c834712024-05-08 16:54:21 +020082/* Return true if str ends exactly with token: */
83function f_str_endswith(charstring str, charstring token) return boolean
84{
85 if (lengthof(str) < lengthof(token)) {
86 return false;
87 }
88 var charstring str_end := substr(str, lengthof(str) - lengthof(token), lengthof(token));
89 return str_end == token;
90}
91
92/* Replace all matches of token "find" with "repl" in "str" */
93function f_str_replace(charstring str, charstring find, charstring repl) return charstring
94{
95 var integer pos := f_strstr(str, find, 0);
96 if (pos < 0) {
97 return str;
98 }
99 var charstring prefix := substr(str, 0, pos);
100 var integer suffix_pos := pos + lengthof(find);
101 var charstring suffix := substr(str, suffix_pos, lengthof(str) - suffix_pos);
102
103 var charstring new_str := prefix & repl & f_str_replace(suffix, find, repl);
104 return new_str;
105}
106
Pau Espin Pedrol601b5cd2024-04-17 19:16:17 +0200107type record of charstring ro_charstring;
108function f_str_split(charstring str, charstring delim := "\n") return ro_charstring
109{
110 var integer pos := 0;
111 var ro_charstring parts := {};
112 var integer delim_pos;
113 var integer end := lengthof(str);
114 while (pos < end) {
115 delim_pos := f_strstr(str, delim, pos);
116 if (delim_pos < 0) {
117 delim_pos := end;
118 }
119 if (delim_pos > pos) {
120 parts := parts & { substr(str, pos, delim_pos - pos) };
121 }
Pau Espin Pedrolf0862ed2024-05-07 20:04:10 +0200122 pos := delim_pos + lengthof(delim);
Pau Espin Pedrol601b5cd2024-04-17 19:16:17 +0200123 }
124 return parts;
125}
126
Daniel Willmannd9304742018-10-23 20:29:59 +0200127}