hnbgw: verify empty talloc asn1_context at end of tests

This allowed me to find massive memory leaks in osmo-hnbgw: at the end
of each test, run f_shutdown_helper(), and in it query 'show talloc' for
an empty asn1_context.

Hence verify that all the scenarios run in these ttcn3 tests have no
asn1 de/encoding memory leaks.

Change-Id: I2948ee6f167369a2252f85b493e9653b93c7e4e9
diff --git a/library/Osmocom_VTY_Functions.ttcn b/library/Osmocom_VTY_Functions.ttcn
index ed506a7..a448b8b 100644
--- a/library/Osmocom_VTY_Functions.ttcn
+++ b/library/Osmocom_VTY_Functions.ttcn
@@ -310,4 +310,57 @@
 	return parts;
 }
 
+public function f_verify_talloc_bytes(TELNETasp_PT pt, ro_charstring object_strs, integer expect_bytes := 0,
+				       integer attempts := 5, float wait_time := 3.0)
+{
+	var charstring show_cmd := "show talloc-context application brief";
+
+	while (attempts > 0) {
+		attempts := attempts - 1;
+		var charstring ret := f_vty_transceive_ret(pt, show_cmd);
+
+		var ro_charstring lines := f_str_split(ret);
+
+		var boolean ok := true;
+		for (var integer i := 0; i < lengthof(object_strs); i := i + 1) {
+			var charstring object_str := object_strs[i];
+
+			var integer bytes := 0;
+
+			for (var integer j := 0; j < lengthof(lines); j := j + 1) {
+				var charstring line := lines[j];
+				if (f_strstr(line, object_str) < 0) {
+					continue;
+				}
+				var ro_charstring tokens := f_str_split(line, " ");
+				if (lengthof(tokens) < 4) {
+					continue;
+				}
+				if (tokens[3] != "bytes") {
+					continue;
+				}
+				bytes := f_str2int(tokens[2]);
+			}
+
+			if (bytes != expect_bytes) {
+				ok := false;
+				log("ERROR: talloc reports ", object_str, " = ", bytes, " bytes, expecting ", expect_bytes,
+				    " from VTY response ", lines);
+			} else {
+				log("ok: talloc reports ", object_str, " = ", bytes, " bytes");
+			}
+		}
+		if (ok) {
+			return;
+		}
+		if (attempts == 0) {
+			break;
+		}
+		log("bytes count mismatch, retrying in ", wait_time);
+		f_sleep(wait_time);
+	}
+	setverdict(fail, "talloc bytes count mismatch");
+	mtc.stop;
+}
+
 }