Initial test suite for osmo-pcap-client

Change-Id: If4a1072e75cb64f785d660e4c828c0f521d84b16
diff --git a/library/OPCAP_Adapter.ttcn b/library/OPCAP_Adapter.ttcn
new file mode 100644
index 0000000..8bcc3fd
--- /dev/null
+++ b/library/OPCAP_Adapter.ttcn
@@ -0,0 +1,105 @@
+module OPCAP_Adapter {
+
+/* OPCAP Adapter layer, sitting on top of OPCAP_CodecPort.
+ * test suites can 'inherit' in order to have a OPCAP connection to the IUT which they're testing
+ *
+ * (C) 2021 by Harald Welte <laforge@gnumonks.org>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ */
+
+
+import from Osmocom_Types all;
+import from General_Types all;
+import from OPCAP_Types all;
+import from OPCAP_Templates all;
+import from OPCAP_CodecPort all;
+import from OPCAP_CodecPort_CtrlFunct all;
+import from IPL4asp_Types all;
+import from IPL4asp_PortType all;
+import from Socket_API_Definitions all;
+
+const integer NUM_OPCAP := 3;
+
+type component OPCAP_Adapter_CT {
+	/* down-facing port to OPCAP Codec port */
+	port OPCAP_CODEC_PT OPCAP[NUM_OPCAP];
+	var IPL4asp_Types.ConnectionId g_opcap_conn_id[NUM_OPCAP] := { -1, -1, -1 };
+}
+
+private function f_set_tcp_segmentation(integer idx) runs on OPCAP_Adapter_CT {
+	/* Set function for dissecting the binary stream into packets */
+	var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen);
+	/* Offset: 2, size of length: 2, delta: 4, multiplier: 1, big-endian */
+	OPCAP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(OPCAP[idx], g_opcap_conn_id[idx], vl_f, {2, 2, 4, 1, 0});
+}
+
+function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port,
+		   charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
+runs on OPCAP_Adapter_CT {
+	var IPL4asp_Types.Result res;
+	map(self:OPCAP[idx], system:OPCAP);
+	if (g_opcap_conn_id[idx] != -1) {
+		OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
+		g_opcap_conn_id[idx] := -1;
+	}
+	res := OPCAP_CodecPort_CtrlFunct.f_IPL4_connect(OPCAP[idx], remote_host, remote_port,
+							local_host, local_port, 0, { tcp :={} });
+	if (not ispresent(res.connId)) {
+		setverdict(fail, "Could not connect to OPCAP port, check your configuration ",
+			"{remote ", remote_host, ":", remote_port, " local ", local_host, ":", local_port, "}");
+		mtc.stop;
+	}
+	g_opcap_conn_id[idx] := res.connId;
+
+	f_set_tcp_segmentation(idx);
+}
+
+/* Function to use to bind to a local port as IPA server, accepting remote clients */
+function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port, integer idx := 0)
+runs on OPCAP_Adapter_CT {
+	var IPL4asp_Types.Result res;
+	map(self:OPCAP[idx], system:OPCAP);
+	if (g_opcap_conn_id[idx] != -1) {
+		OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
+		g_opcap_conn_id[idx] := -1;
+	}
+	res := OPCAP_CodecPort_CtrlFunct.f_IPL4_listen(OPCAP[idx], local_host, local_port, { tcp:={} });
+	if (not ispresent(res.connId)) {
+		setverdict(fail, "Could not bind to OPCAP port, check your configuration ",
+			   "{local ", local_host, ":", local_port, "}");
+		mtc.stop;
+	}
+	g_opcap_conn_id[idx] := res.connId;
+
+	f_set_tcp_segmentation(idx);
+}
+
+function f_wait_client_connect(integer idx) runs on OPCAP_Adapter_CT {
+	var IPL4asp_Types.PortEvent rx_event;
+	OPCAP[idx].receive(IPL4asp_Types.PortEvent:{connOpened:=?}) -> value rx_event {
+		log("Connection from ", rx_event.connOpened.remName, ":", rx_event.connOpened.remPort);
+		/* we want to store the client's connId, not the 'bind socket' one */
+		g_opcap_conn_id[idx] := rx_event.connOpened.connId;
+	}
+}
+
+function f_disconnect(integer idx) runs on OPCAP_Adapter_CT {
+	OPCAP_CodecPort_CtrlFunct.f_IPL4_close(OPCAP[idx], g_opcap_conn_id[idx], {tcp := {}});
+	OPCAP[idx].clear;
+};
+
+function f_opcap_send(template (value) OPCAP_PDU pdu, integer idx := 0) runs on OPCAP_Adapter_CT {
+	OPCAP[idx].send(ts_OPCAP_Send(g_opcap_conn_id[idx], pdu));
+}
+
+function f_opcap_exp(template OPCAP_PDU exp, integer idx := 0) runs on OPCAP_Adapter_CT return OPCAP_PDU {
+	var OPCAP_RecvFrom rf;
+	OPCAP[idx].receive(tr_OPCAP_Recv(g_opcap_conn_id[idx], exp)) -> value rf;
+	return rf.msg;
+}
+
+
+}
diff --git a/library/OPCAP_CodecPort.ttcn b/library/OPCAP_CodecPort.ttcn
new file mode 100644
index 0000000..f2d215d
--- /dev/null
+++ b/library/OPCAP_CodecPort.ttcn
@@ -0,0 +1,64 @@
+module OPCAP_CodecPort {
+
+/* Simple OPCAP Codec Port, translating between raw TCP octetstring payload
+ * towards the IPL4asp port provider, and OPCAP primitives
+ * which carry the decoded OPCAP data types as payload.
+ *
+ * (C) 2021 by Harald Welte <laforge@gnumonks.org>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ */
+
+
+import from IPL4asp_PortType all;
+import from IPL4asp_Types all;
+import from OPCAP_Types all;
+
+type record OPCAP_RecvFrom {
+	ConnectionId	connId,
+	OPCAP_PDU	msg
+}
+
+type record OPCAP_Send {
+	ConnectionId	connId,
+	OPCAP_PDU	msg
+}
+
+template (value) OPCAP_Send ts_OPCAP_Send(ConnectionId conn_id, template (value) OPCAP_PDU msg) := {
+	connId := conn_id,
+	msg := msg
+}
+
+template OPCAP_RecvFrom tr_OPCAP_Recv(template ConnectionId conn_id, template OPCAP_PDU msg) := {
+	connId := conn_id,
+	msg := msg
+}
+
+private function IPL4_to_OPCAP_RecvFrom(in ASP_RecvFrom pin, out OPCAP_RecvFrom pout) {
+	pout.connId := pin.connId;
+	pout.msg := dec_OPCAP_PDU(pin.msg);
+} with { extension "prototype(fast)" }
+
+private function OPCAP_to_IPL4_Send(in OPCAP_Send pin, out ASP_Send pout) {
+	pout.connId := pin.connId;
+	pout.proto := { tcp := {} };
+	pout.msg := enc_OPCAP_PDU(pin.msg);
+} with { extension "prototype(fast)" }
+
+type port OPCAP_CODEC_PT message {
+	out	OPCAP_Send;
+	in	OPCAP_RecvFrom,
+		ASP_ConnId_ReadyToRelease,
+		ASP_Event;
+} with { extension "user IPL4asp_PT
+	out(OPCAP_Send -> ASP_Send: function(OPCAP_to_IPL4_Send))
+	in(ASP_RecvFrom -> OPCAP_RecvFrom: function(IPL4_to_OPCAP_RecvFrom);
+	   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
+	   ASP_Event -> ASP_Event: simple)"
+}
+
+
+
+}
diff --git a/library/OPCAP_CodecPort_CtrlFunct.ttcn b/library/OPCAP_CodecPort_CtrlFunct.ttcn
new file mode 100644
index 0000000..99a5fe7
--- /dev/null
+++ b/library/OPCAP_CodecPort_CtrlFunct.ttcn
@@ -0,0 +1,52 @@
+module OPCAP_CodecPort_CtrlFunct {
+
+  import from OPCAP_CodecPort all;
+  import from IPL4asp_Types all;
+
+  external function f_IPL4_listen(
+    inout OPCAP_CODEC_PT portRef,
+    in HostName locName,
+    in PortNumber locPort,
+    in ProtoTuple proto,
+    in OptionList options := {}
+  ) return Result;
+
+  external function f_IPL4_connect(
+    inout OPCAP_CODEC_PT portRef,
+    in HostName remName,
+    in PortNumber remPort,
+    in HostName locName,
+    in PortNumber locPort,
+    in ConnectionId connId,
+    in ProtoTuple proto,
+    in OptionList options := {}
+  ) return Result;
+
+  external function f_IPL4_close(
+    inout OPCAP_CODEC_PT portRef,
+    in ConnectionId id,
+    in ProtoTuple proto := { unspecified := {} }
+  ) return Result;
+
+  external function f_IPL4_setUserData(
+    inout OPCAP_CODEC_PT portRef,
+    in ConnectionId id,
+    in UserData userData
+  ) return Result;
+
+  external function f_IPL4_getUserData(
+    inout OPCAP_CODEC_PT portRef,
+    in ConnectionId id,
+    out UserData userData
+  ) return Result;
+
+  external function f_IPL4_setGetMsgLen(
+    inout OPCAP_CODEC_PT portRef,
+    in ConnectionId id,
+    inout f_IPL4_getMsgLen f,
+    in ro_integer msgLenArgs
+  );
+
+
+}
+
diff --git a/library/OPCAP_CodecPort_CtrlFunctdef.cc b/library/OPCAP_CodecPort_CtrlFunctdef.cc
new file mode 100644
index 0000000..b19138a
--- /dev/null
+++ b/library/OPCAP_CodecPort_CtrlFunctdef.cc
@@ -0,0 +1,66 @@
+#include "IPL4asp_PortType.hh"
+#include "OPCAP_CodecPort.hh"
+#include "IPL4asp_PT.hh"
+
+namespace OPCAP__CodecPort__CtrlFunct {
+
+  IPL4asp__Types::Result f__IPL4__listen(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef,
+    const IPL4asp__Types::HostName& locName,
+    const IPL4asp__Types::PortNumber& locPort,
+    const IPL4asp__Types::ProtoTuple& proto,
+    const IPL4asp__Types::OptionList& options)
+  {
+    return f__IPL4__PROVIDER__listen(portRef, locName, locPort, proto, options);
+  }
+  
+  IPL4asp__Types::Result f__IPL4__connect(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef,
+    const IPL4asp__Types::HostName& remName,
+    const IPL4asp__Types::PortNumber& remPort,
+    const IPL4asp__Types::HostName& locName,
+    const IPL4asp__Types::PortNumber& locPort,
+    const IPL4asp__Types::ConnectionId& connId,
+    const IPL4asp__Types::ProtoTuple& proto,
+    const IPL4asp__Types::OptionList& options)
+  {
+    return f__IPL4__PROVIDER__connect(portRef, remName, remPort,
+                                      locName, locPort, connId, proto, options);
+  }
+
+  IPL4asp__Types::Result f__IPL4__close(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef, 
+    const IPL4asp__Types::ConnectionId& connId, 
+    const IPL4asp__Types::ProtoTuple& proto)
+  {
+      return f__IPL4__PROVIDER__close(portRef, connId, proto);
+  }
+
+  IPL4asp__Types::Result f__IPL4__setUserData(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef,
+    const IPL4asp__Types::ConnectionId& connId,
+    const IPL4asp__Types::UserData& userData)
+  {
+    return f__IPL4__PROVIDER__setUserData(portRef, connId, userData);
+  }
+  
+  IPL4asp__Types::Result f__IPL4__getUserData(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef,
+    const IPL4asp__Types::ConnectionId& connId,
+    IPL4asp__Types::UserData& userData)
+  {
+    return f__IPL4__PROVIDER__getUserData(portRef, connId, userData);
+  }
+
+  void f__IPL4__setGetMsgLen(
+    OPCAP__CodecPort::OPCAP__CODEC__PT& portRef,
+    const IPL4asp__Types::ConnectionId& connId,
+    Socket__API__Definitions::f__getMsgLen& f,
+    const Socket__API__Definitions::ro__integer& msgLenArgs)
+  {
+    return f__IPL4__PROVIDER__setGetMsgLen(portRef, connId, f, msgLenArgs);
+  }
+
+
+}
+
diff --git a/library/OPCAP_Templates.ttcn b/library/OPCAP_Templates.ttcn
new file mode 100644
index 0000000..0a1c884
--- /dev/null
+++ b/library/OPCAP_Templates.ttcn
@@ -0,0 +1,54 @@
+module OPCAP_Templates {
+
+/* OPCAP_Templates, defining TTCN-3 templates for the osmo-pcap protocol.
+ *
+ * OPCAP is a non-standard protocol used between osmo-pcap-client and osmo-pcap-server.
+ *
+ * (C) 2021 by Harald Welte <laforge@gnumonks.org>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ */
+
+import from General_Types all;
+import from Osmocom_Types all;
+import from OPCAP_Types all;
+
+
+template (present) OPCAP_PDU tr_OPCAP_FILE_HDR(template (present) uint32_t linktype := ?) := {
+	msg_type := PKT_LINK_HDR,
+	spare := ?,
+	len := 24,
+	u := {
+		file := {
+			magic := PCAP_MAGIC,
+			version_major := PCAP_VERSION_MAJOR,
+			version_minor := PCAP_VERSION_MINOR,
+			thiszone := 0,
+			sigfigs := 0,
+			snaplen := 9000,
+			linktype := linktype
+		}
+	}
+}
+
+template (present) OPCAP_PDU tr_OPCAP_PKT(template (present) octetstring payload,
+					  template (present) uint32_t caplen := ?,
+					  template (present) uint32_t len := ?) := {
+	msg_type := PKT_LINK_DATA,
+	spare := ?,
+	len := ?,
+	u := {
+		packet := {
+			ts_sec := ?,
+			ts_usec := ?,
+			caplen := caplen,
+			len := len,
+			payload := payload
+		}
+	}
+}
+
+
+};
diff --git a/library/OPCAP_Types.ttcn b/library/OPCAP_Types.ttcn
new file mode 100644
index 0000000..d244fef
--- /dev/null
+++ b/library/OPCAP_Types.ttcn
@@ -0,0 +1,78 @@
+module OPCAP_Types {
+
+/* OPCAP_Types, defining abstract TTCN-3 data types for the osmo-pcap protocol.
+ *
+ * OPCAP is a non-standard protocol used between osmo-pcap-client and osmo-pcap-server.
+ *
+ * (C) 2021 by Harald Welte <laforge@gnumonks.org>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ */
+
+import from General_Types all;
+import from Osmocom_Types all;
+
+type enumerated OpcapMsgType {
+	PKT_LINK_HDR	(0),
+	PKT_LINK_DATA	(1)
+} with { variant "FIELDLENGTH(8)" };
+
+type record OPCAP_PDU {
+	OpcapMsgType msg_type,
+	uint8_t spare,
+	uint16_t len,
+	OpcapBodyUnion u
+} with {
+	variant (len) "LENGTHTO(u)"
+	variant (len) "BYTEORDER(last)"
+	variant (u) "CROSSTAG(
+		file,	msg_type = PKT_LINK_HDR;
+		packet,	msg_type = PKT_LINK_DATA;
+			)"
+};
+
+type union OpcapBodyUnion {
+	PcapFileHeader file,
+	OpcapPacket packet
+};
+
+/* header in front of a PKT_LINK_DATA */
+type record OpcapPacket {
+	uint32_t ts_sec,
+	uint32_t ts_usec,
+	uint32_t caplen,
+	uint32_t len,
+	octetstring payload
+} with {
+	variant (caplen) "LENGTHTO(payload)"
+};
+
+/* below definitions are from pcap/pcap.h */
+const uint16_t PCAP_VERSION_MAJOR := 2;
+const uint16_t PCAP_VERSION_MINOR := 4;
+const uint32_t PCAP_MAGIC := 2712847316; //0xA1B2C3D4;
+
+type record PcapFileHeader {
+	uint32_t magic,
+	uint16_t version_major,
+	uint16_t version_minor,
+	uint32_t thiszone,
+	uint32_t sigfigs,
+	uint32_t snaplen,
+	uint32_t linktype
+};
+
+/* below definitions are from pcap/dlt.h */
+const uint32_t DLT_LINUX_SLL := 113;
+const uint32_t DLT_EN10MB := 1;
+
+
+external function enc_OPCAP_PDU(in OPCAP_PDU msg) return octetstring
+	with { extension "prototype(convert) encode(RAW)" };
+
+external function dec_OPCAP_PDU(in octetstring msg) return OPCAP_PDU
+	with { extension "prototype(convert) decode(RAW)" };
+
+} with { encode "RAW"; variant "FIELDORDER(msb)"; variant "BYTEORDER(first)" };