hnb: Introduce HNB_Tests testsuite

A new Iuh CodecPort + Emulation is introduced to (de)mux RANAP and RUA
in the same SCTP socket.
The Iuh_CodecPort.ttcn file has currently a hack to be able to test
HNBAP, since titan seem to be reporting sinfo_ppid=0 when in fact it
received sinfo_ppid=20 (HNBAP).

A couple tests are added to validate HNBAP HNBRegister Request  + Accept
or Reject. In current osmo-hnodeb state, both tests pass if run
separately, but fail if run sequentially since osmo-hnodeb still doesn't
re-connect properly after first test finishes and connection is dropped.

Related: SYS#5516
Change-Id: I7227917148e98a2c777f4b05d8d2eca6e9c121b7
diff --git a/library/Iuh_CodecPort.ttcn b/library/Iuh_CodecPort.ttcn
new file mode 100644
index 0000000..569a27f
--- /dev/null
+++ b/library/Iuh_CodecPort.ttcn
@@ -0,0 +1,141 @@
+module Iuh_CodecPort {
+
+/* Simple Iuh Codec Port, translating between raw SCTP primitives with
+ * octetstring payload towards the IPL4asp provider, and Iuh primitives
+ * which carry the decoded Iuh data types as payload.
+ *
+ * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * Author: Pau Espin Pedrol <pespin@sysmocom.de>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+	import from IPL4asp_PortType all;
+	import from IPL4asp_Types all;
+	import from HNBAP_PDU_Descriptions all;
+	import from HNBAP_Types all;
+	import from RUA_PDU_Descriptions all;
+	import from RUA_Types all;
+	import from Iuh_Types all;
+
+	type record Iuh_RecvFrom {
+		ConnectionId	connId,
+		HostName	remName,
+		PortNumber	remPort,
+		HostName	locName,
+		PortNumber	locPort,
+		Iuh_PDU	msg
+	};
+
+	template Iuh_RecvFrom t_Iuh_RecvFrom(template Iuh_PDU msg) := {
+		connId := ?,
+		remName := ?,
+		remPort := ?,
+		locName := ?,
+		locPort := ?,
+		msg := msg
+	}
+
+	template Iuh_RecvFrom t_Iuh_RecvFrom_HNBAP(template HNBAP_PDU hnbap_msg := ?) := {
+		connId := ?,
+		remName := ?,
+		remPort := ?,
+		locName := ?,
+		locPort := ?,
+		msg := {
+			hnbap := hnbap_msg
+		}
+	}
+
+	template Iuh_RecvFrom t_Iuh_RecvFrom_RUA(template RUA_PDU rua_msg := ?) := {
+		connId := ?,
+		remName := ?,
+		remPort := ?,
+		locName := ?,
+		locPort := ?,
+		msg := {
+			rua := rua_msg
+		}
+	}
+
+	type record Iuh_Send {
+		ConnectionId	connId,
+		Iuh_PDU	msg
+	};
+
+	template Iuh_Send t_Iuh_Send_HNBAP(template ConnectionId connId, template HNBAP_PDU hnbap_msg) := {
+		connId := connId,
+		msg := {
+			hnbap := hnbap_msg
+		}
+	}
+
+	template Iuh_Send t_Iuh_Send_RUA(template ConnectionId connId, template RUA_PDU rua_msg) := {
+		connId := connId,
+		msg := {
+			rua := rua_msg
+		}
+	}
+
+	private function IPL4_to_Iuh_RecvFrom(in ASP_RecvFrom pin, out Iuh_RecvFrom pout) {
+		pout.connId := pin.connId;
+		pout.remName := pin.remName;
+		pout.remPort := pin.remPort;
+		pout.locName := pin.locName;
+		pout.locPort := pin.locPort;
+		select (pin.proto.sctp.sinfo_ppid) {
+			case (19) {
+				pout.msg.rua := dec_RUA_PDU(pin.msg);
+			}
+			case (20) {
+				pout.msg.hnbap := dec_HNBAP_PDU(pin.msg);
+			}
+			case (0) {
+				/* FIXME: lower layers report sinfo_ppid=0: */
+				pout.msg.hnbap := dec_HNBAP_PDU(pin.msg);
+			}
+			case else {
+				pout.msg.payload := pin.msg;
+			}
+		}
+	} with { extension "prototype(fast)" };
+
+	private function Iuh_to_IPL4_Send(in Iuh_Send pin, out ASP_Send pout) {
+		var integer sctp_ppid;
+		if (ischosen(pin.msg.rua)) {
+			sctp_ppid := 19;
+			pout.msg := enc_RUA_PDU(pin.msg.rua);
+		} else if (ischosen(pin.msg.hnbap)) {
+			sctp_ppid := 20;
+			pout.msg := enc_HNBAP_PDU(pin.msg.hnbap);
+		} else { /*TODO: abort?*/
+			sctp_ppid := 0;
+			pout.msg := pin.msg.payload;
+		}
+		pout.connId := pin.connId;
+		pout.proto := {
+			sctp := {
+				sinfo_stream := omit,
+				sinfo_ppid := sctp_ppid,
+				remSocks := omit,
+				assocId := omit
+			}
+		};
+	} with { extension "prototype(fast)" };
+
+	type port Iuh_CODEC_PT message {
+		out	Iuh_Send;
+		in	Iuh_RecvFrom,
+			ASP_ConnId_ReadyToRelease,
+			ASP_Event;
+	} with { extension "user IPL4asp_PT
+		out(Iuh_Send -> ASP_Send:function(Iuh_to_IPL4_Send))
+		in(ASP_RecvFrom -> Iuh_RecvFrom: function(IPL4_to_Iuh_RecvFrom);
+		   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
+		   ASP_Event -> ASP_Event: simple)"
+	}
+}