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 | 601b5cd | 2024-04-17 19:16:17 +0200 | [diff] [blame] | 82 | type record of charstring ro_charstring; |
| 83 | function 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 Willmann | d930474 | 2018-10-23 20:29:59 +0200 | [diff] [blame] | 102 | } |