aboutsummaryrefslogtreecommitdiffstats
path: root/library/Misc_Helpers.ttcn
blob: fd2e7bb991659092b256456b830ec0f37238b6a3 (plain)
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
module Misc_Helpers {

import from Native_Functions all;
import from TCCConversion_Functions all;

modulepar {
	charstring mp_osmo_repo := "nightly";
}

/* Test the Osmocom repository (nightly, latest, 2021q4, ...), from which the
 * SUT is running (OS#4878). Usage examples:
 * 	if (Misc_Helpers.f_osmo_repo_is("nightly")) {
 * 		...
 * 	}
 * 	if (Misc_Helpers.f_osmo_repo_is(("nightly", "2021q4"))) {
 * 		...
 * 	}
 */
function f_osmo_repo_is(template charstring ver) return boolean {
	return match(mp_osmo_repo, ver);
}

/* Try to properly shutdown a testcase.
 * The reliable method to stop a testcase without running into dynamic
 * testcase errors due to unconnected ports receiving messages is to call
 * all component.stop before terminating. However, this can only be called
 * from the mtc! So in case we want to terminate from a component that is not
 * the mtc we try to do the next best thing which is calling mtc.stop and
 * hoping for the best.
 */
function f_shutdown(charstring file, integer line, verdicttype verdict := none,
			charstring text := "") {
	if (verdict != none) {
		text := file & ":" & int2str(line) & " : " & text
		setverdict(verdict, text);
	}

	log("Stopping testcase execution from ", file, ":", line)
	if (self == mtc) {
		/* Properly stop all ports before disconnecting them. This avoids
		 * running into the dynamic testcase error due to messages arriving on
		 * unconnected ports. */
		all component.stop;
	}
	mtc.stop
}

function f_addr_is_ipv6(charstring addr) return boolean {
	for (var integer i := 0; i < lengthof(addr); i := i + 1) {
		if (addr[i] == ":") {
			return true;
		}
	}
	return false;
}

function f_addrstr2addr(charstring addr) return octetstring {
	if (f_addr_is_ipv6(addr)) {
		return f_inet6_addr(addr);
	} else {
		return f_inet_addr(addr);
	}
}

/* Return a count of how many times sub_str occurs in str. */
function f_strstr_count(in charstring str, in charstring sub_str) return integer
{
	var integer count := 0;
	var integer pos := 0;

	while (true) {
		var integer at := f_strstr(str, sub_str, pos);
		if (at < 0) {
			break;
		}
		count := count + 1;
		pos := at + 1;
	}
	return count;
}

type record of charstring ro_charstring;
function f_str_split(charstring str, charstring delim := "\n") return ro_charstring
{
	var integer pos := 0;
	var ro_charstring parts := {};
	var integer delim_pos;
	var integer end := lengthof(str);
	while (pos < end) {
		delim_pos := f_strstr(str, delim, pos);
		if (delim_pos < 0) {
			delim_pos := end;
		}
		if (delim_pos > pos) {
			parts := parts & { substr(str, pos, delim_pos - pos) };
		}
		pos := delim_pos + 1;
	}
	return parts;
}

}