aboutsummaryrefslogtreecommitdiffstats
path: root/sccp/SCCP_Tests_RAW.ttcn
blob: 7d44eb42a07624a88165c693addec38e3b373343 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* (C) 2019 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * The idea is that these tests are executed against sccp_demo_user from
 * libosmo-sccp.git in server mode.
 *
 * 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
 */

module SCCP_Tests_RAW {

import from General_Types all;
import from Osmocom_Types all;

import from M3UA_Emulation all;

import from SCCP_Types all;
import from SCCPasp_Types all;
import from SCCP_Templates all;
import from SCCP_Emulation all;
import from SCCP_CodecPort all;

import from TELNETasp_PortType all;
import from Osmocom_VTY_Functions all;

import from SCCP_Tests all;

type component SCCP_Test_RAW_CT {
	/* VTY to sccp_demo_user (not used yet) */
	port TELNETasp_PT SCCP_DEMO_USER_VTY;

	/* SCCP raw port runs on top of M3UA Emulation.
	 * "System Under Test" is libosmo-sccp's sccp_demo_user example program. */
	var M3UA_CT vc_M3UA;
	port SCCP_CODEC_PT MTP3;

	var MSC_SCCP_MTP3_parameters g_param;

	var OCT3 g_own_lref := '000001'O

	/*Configure T(tias) over VTY, seconds */
	var integer g_demo_sccp_timer_ias :=  7 * 60;
	/*Configure T(tiar) over VTY, seconds */
	var integer g_demo_sccp_timer_iar := 15 * 60;
}

type record of charstring Commands;
private function f_cs7_inst_0_cfg(TELNETasp_PT pt, Commands cmds := {})
{
	f_vty_enter_cfg_cs7_inst(pt, 0);
	for (var integer i := 0; i < sizeof(cmds); i := i+1) {
		f_vty_transceive(pt, cmds[i]);
	}
	f_vty_transceive(pt, "end");
}

function f_init_vty() runs on SCCP_Test_RAW_CT {
	if (SCCP_DEMO_USER_VTY.checkstate("Mapped")) {
		/* skip initialization if already executed once */
		return;
	}
	map(self:SCCP_DEMO_USER_VTY, system:SCCP_DEMO_USER_VTY);
	f_vty_set_prompts(SCCP_DEMO_USER_VTY);
	f_vty_transceive(SCCP_DEMO_USER_VTY, "enable");
	f_cs7_inst_0_cfg(SCCP_DEMO_USER_VTY, {"sccp-timer ias " & int2str(g_demo_sccp_timer_ias),
			  "sccp-timer iar " & int2str(g_demo_sccp_timer_iar)});
}

private function f_init_raw(SCCP_Configuration cfg) runs on SCCP_Test_RAW_CT {
	g_param := {
		sio := {
			ni := substr(oct2bit(cfg.sio),0,2),
			prio := substr(oct2bit(cfg.sio),2,2),
			si := substr(oct2bit(cfg.sio),4,4)
		},
		opc := cfg.own_pc,
		dpc := cfg.peer_pc,
		sls := 0,
		sccp_serviceType := cfg.sccp_service_type,
		ssn := cfg.own_ssn
	};

	f_init_vty();

	/* Create and connect test components */
	vc_M3UA := M3UA_CT.create;
	connect(self:MTP3, vc_M3UA:MTP3_SP_PORT);
	map(vc_M3UA:SCTP_PORT, system:sctp);

	vc_M3UA.start(f_M3UA_Emulation(cfg.sctp_addr));
}

private function f_cleanup() runs on SCCP_Test_RAW_CT {
	all component.stop;
	unmap(vc_M3UA:SCTP_PORT, system:sctp);
	disconnect(vc_M3UA:MTP3_SP_PORT, self:MTP3);
	self.stop
}

private function f_send_sccp(template PDU_SCCP sccp) runs on SCCP_Test_RAW_CT {
	var SCCP_MTP3_TRANSFERreq tx := {
		sio := g_param.sio,
		opc := g_param.opc,
		dpc := g_param.dpc,
		sls := g_param.sls,
		data := valueof(sccp)
	};
	MTP3.send(tx);
}

private function tr_SCCP_MTP3_TRANSFERind(template PDU_SCCP sccp)
runs on SCCP_Test_RAW_CT return template SCCP_MTP3_TRANSFERind {

	var template SCCP_MTP3_TRANSFERind exp := {
		sio := g_param.sio,
		opc := g_param.dpc,
		dpc := g_param.opc,
		sls := g_param.sls,
		data := sccp
	};
	return exp;
}

private function f_exp_sccp(template PDU_SCCP sccp)
runs on SCCP_Test_RAW_CT return SCCP_MTP3_TRANSFERind {
	var template SCCP_MTP3_TRANSFERind exp := tr_SCCP_MTP3_TRANSFERind(sccp);
	var SCCP_MTP3_TRANSFERind rx;
	timer T := 10.0;
	T.start;
	alt {
	[] MTP3.receive(exp) -> value rx {
		return rx;
		}
	[] MTP3.receive {
		setverdict(fail, "Unexpected MTP/SCCP received");
		self.stop
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for ", exp);
		self.stop
		}
	}
	return rx;
}

private function f_establish_conn(SCCP_PAR_Address calling, SCCP_PAR_Address called)
runs on SCCP_Test_RAW_CT return OCT3 {
	var SCCP_MTP3_TRANSFERind mtp3_rx;

	f_send_sccp(ts_SCCP_CR(g_own_lref, calling, called));
	mtp3_rx := f_exp_sccp(tr_SCCP_CC(?, g_own_lref));

	return mtp3_rx.data.connconfirm.sourceLocRef;
}

private function f_tx_udt_exp(SCCP_PAR_Address calling, SCCP_PAR_Address called, octetstring data) runs on SCCP_Test_RAW_CT {

	f_send_sccp(ts_SCCP_UDT(calling, called, data));
	f_exp_sccp(tr_SCCP_UDT(called, calling, data));
}

/* Verify sccp_demo_user answers a CR with a CC for PC and SSN set up to echo back */
testcase TC_cr_cc() runs on SCCP_Test_RAW_CT {
	var SCCP_PAR_Address calling, called;

	f_init_raw(mp_sccp_cfg[0]);
	f_sleep(1.0);

	called := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].peer_pc, mp_sccp_cfg[0].peer_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	calling := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].own_pc, mp_sccp_cfg[0].own_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	f_establish_conn(calling, called);
	setverdict(pass);
}

/* Verify sccp_demo_user inactivty timers are not armed upon dealing with
/* connectionless data-unit messages. Since no connection exists. */
testcase TC_udt_without_cr_cc() runs on SCCP_Test_RAW_CT {
	var SCCP_PAR_Address calling, called;
	var SCCP_MTP3_TRANSFERind rx;
	var octetstring data := f_rnd_octstring(f_rnd_int(100));

	/* Keep recommended ratio of T(iar) >= T(ias)*2, but anyway no IT
	   should be received in this case. */
	g_demo_sccp_timer_ias := 1;
	g_demo_sccp_timer_iar := 3;
	f_init_raw(mp_sccp_cfg[0]);
	f_sleep(1.0);

	called := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].peer_pc, mp_sccp_cfg[0].peer_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	calling := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].own_pc, mp_sccp_cfg[0].own_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));

	f_tx_udt_exp(calling, called, data);

	/* Make sure no SCCP message is received at all, since no connection is active. */
	timer T := int2float(g_demo_sccp_timer_iar + 1);
	T.start;
	alt {
	[] MTP3.receive {
		setverdict(fail, "Unexpected MTP/SCCP received");
		self.stop;
		}
	[] T.timeout {}
	}
	setverdict(pass);
}

/* Verify T(iar) triggers and releases the channel */
testcase TC_tiar_timeout() runs on SCCP_Test_RAW_CT {
	var SCCP_PAR_Address calling, called;
	var OCT3 remote_lref;
	var octetstring data := f_rnd_octstring(f_rnd_int(100));

	/* Set T(iar) in sccp_demo_user low enough that it will trigger before other side
	   has time to keep alive with a T(ias). Keep recommended ratio of
	   T(iar) >= T(ias)*2 */
	g_demo_sccp_timer_ias := 2;
	g_demo_sccp_timer_iar := 5;
	f_init_raw(mp_sccp_cfg[0]);
	f_sleep(1.0);

	called := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].peer_pc, mp_sccp_cfg[0].peer_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	calling := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].own_pc, mp_sccp_cfg[0].own_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	remote_lref := f_establish_conn(calling, called);
	f_tx_udt_exp(calling, called, data);

	log("Waiting for first IT");
	f_exp_sccp(tr_SCCP_IT(remote_lref, g_own_lref));
	log("Waiting for second IT");
	f_exp_sccp(tr_SCCP_IT(remote_lref, g_own_lref));

	log("Waiting for RLSD");
	f_exp_sccp(tr_SCCP_RLSD(remote_lref, g_own_lref, hex2int('0D'H))); /* Cause: Expiration of Rx Inactivity Timer */
	f_send_sccp(ts_SCCP_RLC(g_own_lref, remote_lref));
	setverdict(pass);
}

/* Verify T(iar) triggers and releases the channel */
testcase TC_it_avoids_tiar() runs on SCCP_Test_RAW_CT {
	var SCCP_PAR_Address calling, called;
	var OCT3 remote_lref;
	var boolean it_received := false;

	g_demo_sccp_timer_ias := 1;
	g_demo_sccp_timer_iar := 3;
	f_init_raw(mp_sccp_cfg[0]);
	f_sleep(1.0);

	called := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].peer_pc, mp_sccp_cfg[0].peer_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	calling := valueof(ts_SccpAddr_PC_SSN(mp_sccp_cfg[0].own_pc, mp_sccp_cfg[0].own_ssn,
					     mp_sccp_cfg[0].sio, mp_sccp_cfg[0].sccp_service_type));
	remote_lref := f_establish_conn(calling, called);

	timer T_total := 7.0; /* Higher than g_demo_sccp_timer_iar */
	timer T_tias := 1.0; /* Lower than g_demo_sccp_timer_iar */
	T_total.start;
	T_tias.start;
	alt {
	[] MTP3.receive(tr_SCCP_MTP3_TRANSFERind(tr_SCCP_IT(remote_lref, g_own_lref))) {
		it_received := true;
		repeat;
		}
	[] MTP3.receive(tr_SCCP_MTP3_TRANSFERind(tr_SCCP_RLSD(remote_lref, g_own_lref, hex2int('0D'H)))) {
		setverdict(fail, "Unexpected SCCP RLSD received");
		self.stop;
		}
	[] MTP3.receive {
		setverdict(fail, "Unexpected MTP/SCCP received");
		self.stop;
		}
	[] T_tias.timeout {
		f_send_sccp(ts_SCCP_IT(g_own_lref, remote_lref));
		T_tias.start;
		repeat;
		}
	[] T_total.timeout {
		/* We kept the connection alive only with IT messages for a while, cool! */
		T_tias.stop;
		setverdict(pass);
		}
	}

	if (not it_received) {
		setverdict(fail, "Didn't receive any IT (Tias) from peer");
	}

	/* After we stop sending IT, we should be receiving an RLSD triggered from T(iar) */
	log("Waiting for RLSD");
	alt {
	[] MTP3.receive(tr_SCCP_MTP3_TRANSFERind(tr_SCCP_IT(remote_lref, g_own_lref))) {
		repeat;
		}
	[] MTP3.receive(tr_SCCP_MTP3_TRANSFERind(tr_SCCP_RLSD(remote_lref, g_own_lref, hex2int('0D'H)))) {
		f_send_sccp(ts_SCCP_RLC(g_own_lref, remote_lref));
		setverdict(pass);
		}
	[] MTP3.receive {
		setverdict(fail, "Unexpected MTP/SCCP received");
		self.stop;
		}
	}
}

control {
	execute( TC_cr_cc() );
	execute( TC_udt_without_cr_cc() );
	execute( TC_tiar_timeout() );
	execute( TC_it_avoids_tiar() );
}


}