summaryrefslogtreecommitdiffstats
path: root/remsim/RemsimClient_Tests.ttcn
blob: 499fd3e8e803409f305541925bcf39706b5d8b8c (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
module RemsimClient_Tests {

/* Integration Tests for osmo-remsim-client
 * (C) 2019-2020 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.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This test suite tests osmo-remsim-client by attaching to the external interfaces.
 */

import from Native_Functions all;
import from Osmocom_Types all;
import from IPA_Emulation all;

/* the PIPEasp port allows us to interact with osmo-remsim-client-shell via stdin/stdout */
import from PIPEasp_PortType all;
import from PIPEasp_Types all;

import from RSPRO all;
import from RSPRO_Types all;
import from RSPRO_Server all;
import from REMSIM_Tests all;

modulepar {
	boolean mp_have_local_client := false; /* backwards compatible default */
	charstring mp_client_shell_cmd := "osmo-remsim-client-shell";
};

type component client_test_CT extends rspro_server_CT {
	port PIPEasp_PT PIPE;
	var ComponentIdentity g_srv_comp_id, g_bankd_comp_id;
};

private function f_init() runs on client_test_CT {
	g_srv_comp_id := valueof(ts_CompId(remsimServer, "ttcn-server"));
	g_bankd_comp_id := valueof(ts_CompId(remsimBankd, "ttcn-bankd"));

	f_rspro_srv_init(0, mp_server_ip, mp_server_port, g_srv_comp_id);
	f_rspro_srv_init(1, mp_bankd_ip, mp_bankd_port, g_bankd_comp_id, exp_connect := false);
}


/* ConnectClientReq from client to remsim-server */
testcase TC_srv_connectClient() runs on client_test_CT {
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();
	setverdict(pass);
	f_sleep(1.0);
}

/* ConnectClientReq from client to remsim-server */
testcase TC_srv_connectClient_reject() runs on client_test_CT {
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq(res := illegalClientId);
	/* expect disconnect by client */
	RSPRO_SRV[0].receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN));
	setverdict(pass);
	f_sleep(1.0);
}

/* ConnectClientReq from client to remsim-server */
testcase TC_srv_connectClient_configClientBank() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);
	setverdict(pass);
	f_sleep(1.0);
}

/* Test if client re-connects to server after connection is lost */
testcase TC_srv_reconnect() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();

	/* disconnect the client from server and expect re-establish + re-connect */
	f_rspro_srv_fini(0);
	f_rspro_srv_init(0, mp_server_ip, mp_server_port, g_srv_comp_id, exp_connect := true);
	/* expect inbound connectClientReq */
	as_connectClientReq(i := 0);

	setverdict(pass);
	f_sleep(1.0);
}

/* Test if client re-connects to bank after connection is lost */
testcase TC_bank_reconnect() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	/* disconnect the client from bankd and expect re-establish + re-connect */
	f_rspro_srv_fini(1);
	f_rspro_srv_init(1, mp_bankd_ip, mp_bankd_port, g_bankd_comp_id, exp_connect := true);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	setverdict(pass);
	f_sleep(1.0);
}

/* Test if client disconnects from bankd after slotmap delete on server */
testcase TC_bank_disconnect() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	f_sleep(1.0);

	/* configure client to disconnect from [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4("0.0.0.0"), 0));

	/* expect disconnect of client on simulated bankd side */
	RSPRO_SRV[1].receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN));
	setverdict(pass);
}

/* Test if client connects to bankd after disconnects from bankd after slotmap delete on server */
testcase TC_bank_disconnect_reconnect() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	f_init();
	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	f_sleep(1.0);

	/* configure client to disconnect from [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4("0.0.0.0"), 0));

	/* expect disconnect of client on simulated bankd side */
	RSPRO_SRV[1].receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN));

	/* re-start the IPA emulation (which terminated itself on the TCP disconnect */
	f_rspro_srv_init(1, mp_bankd_ip, mp_bankd_port, g_bankd_comp_id, exp_connect := false);

	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));

	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);

	/* expect inbound connect on simulated bankd */
	as_connectClientReq(i := 1);

	setverdict(pass);
}

/***********************************************************************
 * Tests interacting with local remsim-bankd-client via PIPE port
 ***********************************************************************/

template (value) ASP_PExecuteBackground ts_ExecBg(charstring cmd) := {
	command := cmd
}

template (present) ASP_PStdout tr_Stdout(template (present) charstring line) := {
	stdout := line
}

template (present) ASP_PStderr tr_Stderr(template (present) charstring line) := {
	stderr := line
}

template (value) ASP_PStdin ts_Stdin(template (value) charstring line) := {
	stdin :=line
}

/* osmo-remsim-client logs to stderr, so we need to ignore that log output */
private altstep as_ignore_stderr() runs on client_test_CT {
[] PIPE.receive(tr_Stderr(?)) { repeat; }
}

/* start a local osmo-remsim-client as sub-process and attach it to PIPE port */
private function f_init_client_pipe(ClientSlot cs) runs on client_test_CT {
	var charstring cmdline := mp_client_shell_cmd & " -i 127.0.0.1";
	map(self:PIPE, system:PIPE);
	PIPE.send(ts_ExecBg(cmdline & " -c " & int2str(cs.clientId)
				    & " -n " & int2str(cs.slotNr)));
	activate(as_ignore_stderr());
}

/* pretty-print an octet string as series of ASCII encoded hex bytes with spaces */
function f_osmo_hexdump(octetstring input) return charstring {
	var charstring ret := "";
	for (var integer i := 0; i < lengthof(input); i := i+1) {
		ret := ret & f_str_tolower(oct2str(input[i])) & " ";
	}
	return ret;
}

/* simulated bankd instructs client to "set ATR"; expect it to show up on stdout */
function f_set_atr(template (value) ClientSlot cs, octetstring atr,
		   template ResultCode exp_res := ok, integer i := 0)
runs on client_test_CT {
	RSPRO_SRV[i].send(ts_RSPRO_SetAtrReq(cs, atr));
	interleave {
	[] RSPRO_SRV[i].receive(tr_RSPRO_SetAtrRes(exp_res));
	[] PIPE.receive(tr_Stdout("SET_ATR: " & f_osmo_hexdump(atr)));
	}
}

/* send a C-APDU from simulated application to client stdin; expect it on simulated bankd */
function f_client2bank(template (present) ClientSlot cs, template (present) BankSlot bs,
			octetstring c_apdu, integer i:=0) runs on client_test_CT
{
	/* send C-APDU via STDIN of osmo-remsim-client-shell */
	PIPE.send(ts_Stdin(oct2str(c_apdu)));
	/* expect the same C-APDU to arrive via RSPRO tpduModemToCard */
	f_rspro_srv_exp(tr_RSPRO_TpduModemToCard(cs, bs, ?, c_apdu), i);
}

/* send a R-APDU from simulated bankd to client; expect it on simualated app (client stdout) */
function f_bank2client(template (present) BankSlot bs, template (present) ClientSlot cs,
			octetstring r_apdu, boolean exp_fail := false, integer i:=0)
runs on client_test_CT {
	var TpduFlags flags := {
		tpduHeaderPresent:=true,
		finalPart:=true,
		procByteContinueTx:=false,
		procByteContinueRx:=false
	}
	timer T := 10.0;

	RSPRO_SRV[i].send(ts_RSPRO_TpduCardToModem(bs, cs, flags, r_apdu));
	T.start;
	alt {
	[] PIPE.receive(tr_Stdout("R-APDU: " & f_osmo_hexdump(r_apdu))) {
		if (exp_fail) {
			setverdict(fail, "Expected R-APDU to fail but still received it");
			self.stop;
		} else {
			setverdict(pass);
		}
		}
	[] PIPE.receive(tr_Stdout(?)) {
		setverdict(fail, "Unexpected R-APDU on stdout of remsim-client-shell");
		self.stop;
		}
	[] T.timeout {
		if (exp_fail) {
			setverdict(pass);
		} else {
			setverdict(fail, "Timeout waiting for R-APDU on remsim-client-shell");
			self.stop;
		}
		}
	}
}

/* Transceive a C+R APDU from client (via pipe) to simulated bankd and back */
function f_xceive_apdus(ClientSlot cslot, BankSlot bslot,
			integer count := 100, integer i := 0) runs on client_test_CT
{
	for (var integer j := 0; j < count; j := j+1) {
		var octetstring c_apdu := f_rnd_octstring(f_rnd_int(270));
		var octetstring r_apdu := f_rnd_octstring(f_rnd_int(270));
		f_client2bank(cslot, bslot, c_apdu, i:=i);
		f_bank2client(bslot, cslot, r_apdu, i:=i);
	}
}

/* transceive APDUs using forked osmo-remsim-client-shell + stdio */
testcase TC_pipe_xceive_apdus() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	var ClientSlot cslot := { 321, 123 };
	f_init_client_pipe(cslot);
	f_init();

	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	f_set_atr(cslot, '3B9F96801FC78031A073BE21136743200718000001A5'O, i:=1);

	f_xceive_apdus(cslot, bslot, count := 100, i:=1);
}

/* Send R-APDU from correct bankId/slotNr but to wrong ClientId */
testcase TC_pipe_apdu_wrong_cslot() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	var ClientSlot cslot := { 321, 123 };
	f_init_client_pipe(cslot);
	f_init();

	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	f_set_atr(cslot, '3B9F96801FC78031A073BE21136743200718000001A5'O, i:=1);

	var octetstring c_apdu := f_rnd_octstring(f_rnd_int(270));
	var octetstring r_apdu := f_rnd_octstring(f_rnd_int(270));
	/* Send C-APDU from correct ClientId/Slot to simulated bankd */
	f_client2bank(cslot, bslot, c_apdu, i:=1);
	/* respond with R-APDU from correct bankId/Slot but stating wrong ClientId */
	cslot.clientId := 1023;
	f_bank2client(bslot, cslot, r_apdu, exp_fail:=true, i:=1);
}

/* Send R-APDU from wrong bankId/slotNr but to correct ClientId/Slot */
testcase TC_pipe_apdu_wrong_bslot() runs on client_test_CT {
	var BankSlot bslot := { 1, 0 };
	var ClientSlot cslot := { 321, 123 };
	f_init_client_pipe(cslot);
	f_init();

	/* expect inbound connectClientReq */
	as_connectClientReq();
	/* configure client to connect to [simulated] bankd */
	f_rspro_config_client_bank(bslot, ts_IpPort(ts_IPv4(mp_bankd_ip), mp_bankd_port));
	/* expect inbound connect on simulated bankd */
	f_rspro_srv_exp_connect(1);
	/* expect inbound connectClientReq on simulated bankd */
	as_connectClientReq(i := 1);

	f_set_atr(cslot, '3B9F96801FC78031A073BE21136743200718000001A5'O, i:=1);

	var octetstring c_apdu := f_rnd_octstring(f_rnd_int(270));
	var octetstring r_apdu := f_rnd_octstring(f_rnd_int(270));
	/* Send C-APDU from correct ClientId/Slot to simulated bankd */
	f_client2bank(cslot, bslot, c_apdu, i:=1);
	/* respond with R-APDU from wrong bankId but stating correct ClientId */
	bslot.bankId := 1023;
	f_bank2client(bslot, cslot, r_apdu, exp_fail:=true, i:=1);
}


/* TODO:
   * send a configClientBankIpReq and change the bank of an active client
   * send a configClientBankSlotReq and chagne the bank slot of an active client
   * test keepalive mechanism: do we get IPA PING?
   * test keepalive mechanism: do we see disconnect+reconnect if we don't respond to IPA PING?
   * test messages in invalid state, e.g. APDUs before we're connected to a bank
   * test messages on server connection which are only permitted on bankd connection
 */

control {
	execute( TC_srv_connectClient() );
	execute( TC_srv_connectClient_reject() );
	execute( TC_srv_connectClient_configClientBank() );
	execute( TC_srv_reconnect() );
	execute( TC_bank_reconnect() );
	execute( TC_bank_disconnect() );
	execute( TC_bank_disconnect_reconnect() );

	if (mp_have_local_client) {
		execute( TC_pipe_xceive_apdus() );
		execute( TC_pipe_apdu_wrong_cslot() );
		execute( TC_pipe_apdu_wrong_bslot() );
	}
}


}