Daniel Willmann | d930474 | 2018-10-23 20:29:59 +0200 | [diff] [blame] | 1 | module Misc_Helpers { |
| 2 | |
Pau Espin Pedrol | a16a6b6 | 2024-03-21 16:19:07 +0100 | [diff] [blame] | 3 | import from Native_Functions all; |
Pau Espin Pedrol | 830061d | 2024-04-17 19:13:22 +0200 | [diff] [blame] | 4 | import from TCCConversion_Functions all; |
Pau Espin Pedrol | a16a6b6 | 2024-03-21 16:19:07 +0100 | [diff] [blame] | 5 | |
Oliver Smith | 8b6d180 | 2021-11-26 13:30:13 +0100 | [diff] [blame] | 6 | modulepar { |
| 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 | */ |
| 19 | function f_osmo_repo_is(template charstring ver) return boolean { |
| 20 | return match(mp_osmo_repo, ver); |
| 21 | } |
| 22 | |
Daniel Willmann | d930474 | 2018-10-23 20:29:59 +0200 | [diff] [blame] | 23 | /* 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 | */ |
| 31 | function 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 Pedrol | 6a846bc | 2020-09-04 13:01:22 +0200 | [diff] [blame] | 48 | function 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 Pedrol | a16a6b6 | 2024-03-21 16:19:07 +0100 | [diff] [blame] | 57 | function 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 Pedrol | 830061d | 2024-04-17 19:13:22 +0200 | [diff] [blame] | 65 | /* Return a count of how many times sub_str occurs in str. */ |
| 66 | function 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 Pedrol | 9c83471 | 2024-05-08 16:54:21 +0200 | [diff] [blame] | 82 | /* Return true if str ends exactly with token: */ |
| 83 | function 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" */ |
| 93 | function 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 Pedrol | 601b5cd | 2024-04-17 19:16:17 +0200 | [diff] [blame] | 107 | type record of charstring ro_charstring; |
| 108 | function 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 Pedrol | f0862ed | 2024-05-07 20:04:10 +0200 | [diff] [blame] | 122 | pos := delim_pos + lengthof(delim); |
Pau Espin Pedrol | 601b5cd | 2024-04-17 19:16:17 +0200 | [diff] [blame] | 123 | } |
| 124 | return parts; |
| 125 | } |
| 126 | |
Daniel Willmann | d930474 | 2018-10-23 20:29:59 +0200 | [diff] [blame] | 127 | } |