aboutsummaryrefslogtreecommitdiffstats
path: root/ipad/IPAd_Tests.ttcn
blob: c5ee41271a4a77de4cf290752bba865c887c59fc (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* IPAd testsuite in TTCN-3
 *
 * Author: Philipp Maier <pmaier@sysmocom.de> / sysmocom - s.f.m.c. GmbH
 *
 * 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 IPAd_Tests {

import from Misc_Helpers all;
import from General_Types all;
import from Osmocom_Types all;

import from SGP32Definitions all;
import from SGP32Definitions_Types all;
import from SGP32Definitions_Templates all;

import from RSPDefinitions all;
import from RSPDefinitions_Types all;
import from RSPDefinitions_Templates all;

import from PKIX1Explicit88 all;
import from PKIX1Explicit88_Templates all;
import from PKIX1Explicit88_Types all;

import from HTTP_Server_Emulation all;
import from HTTPmsg_Types all;

import from VPCD_Types all;
import from VPCD_CodecPort all;
import from VPCD_Adapter all;

modulepar {
	/* emulated eIM HTTPs server */
	charstring mp_esipa_ip := "127.0.0.1";
	integer mp_esipa_port := 4430;
	boolean mp_esipa_disable_ssl := false;
	boolean mp_use_vpcd := true;
	float mp_restart_guardtime := 2.0
}

/* Altstep to handle card power up/down and ATR transmission */
private altstep as_vpcd_atr() runs on VPCD_Adapter_CT {
	[] VPCD.receive(tr_VPCD_Recv(g_vpcd_conn_id, tr_VPCD_CTRL_ATR)) {
		f_vpcd_send(ts_VPCD_DATA('3B9F96801FC78031A073BE21136743200718000001A5'O));
		repeat;
		}
	[] VPCD.receive(tr_VPCD_Recv(g_vpcd_conn_id, tr_VPCD_CTRL_OFF)) {
		repeat;
		}
	[] VPCD.receive(tr_VPCD_Recv(g_vpcd_conn_id, tr_VPCD_CTRL_ON)) {
		repeat;
		}
}

/* Helper template to format HTTP responses */
private template (value) HTTPMessage ts_http_resp(template (value) octetstring resp := ''O) := {
	response_binary := {
		client_id := omit,
		version_major := 1,
		version_minor := 1,
		statuscode := 200,
		statustext := "OK",
		/* See also SGP.32, section 6.1.1 */
		header := {
				{
					header_name := "X-Admin-Protocol",
					header_value := "gsma/rsp/v1.0.0"
				},
				{
					header_name := "Content-Type",
					header_value := "application/x-gsma-rsp-asn1"
				},
				{
					header_name := "Content-Length",
					header_value := int2str(lengthof(resp))
				}
		},
		body := resp
	}
}

type component MTC_CT {
	timer g_Tguard;

	/* HTTP server */
	var HTTP_Server_Emulation_CT vc_HTTP;
};

type component IPAd_ConnHdlr extends HTTP_ConnHdlr, VPCD_Adapter_CT {
	var IPAd_ConnHdlrPars g_pars;
};

type record IPAd_ConnHdlrPars {
     /* TODO: add some useful parameters */
};

private function f_init_pars()
runs on MTC_CT return IPAd_ConnHdlrPars {
	var IPAd_ConnHdlrPars pars := {
	    /* TODO: fill parameters with meaninful values */
	};
	return pars;
}

private altstep as_Tguard() runs on MTC_CT {
	[] g_Tguard.timeout {
		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Tguard timeout");
	}
}

private type function void_fn(charstring id) runs on IPAd_ConnHdlr;

private function f_init_handler(void_fn fn, charstring id, IPAd_ConnHdlrPars pars) runs on IPAd_ConnHdlr {
	g_pars := pars;

	/* Initialize VPDC (virtual smartcard) */
	if (mp_use_vpcd) {
		VPCD_Adapter.f_connect();
		activate(as_vpcd_atr());
	}

	fn.apply(id);
}

private function f_start_handler(void_fn fn, IPAd_ConnHdlrPars pars)
runs on MTC_CT return IPAd_ConnHdlr {
	var IPAd_ConnHdlr vc_conn;
	var charstring id := testcasename();

	vc_conn := IPAd_ConnHdlr.create(id);

	if (isbound(vc_HTTP)) {
		connect(vc_conn:HTTP_SRV, vc_HTTP:CLIENT);
		connect(vc_conn:HTTP_SRV_PROC, vc_HTTP:CLIENT_PROC);
	}

	vc_conn.start(f_init_handler(fn, id, pars));
	return vc_conn;
}

function f_init_esipa(charstring id) runs on MTC_CT {
	var HttpServerEmulationCfg http_cfg := {
		http_bind_ip := mp_esipa_ip,
		http_bind_port := mp_esipa_port,
		use_ssl := not mp_esipa_disable_ssl
	};

	vc_HTTP := HTTP_Server_Emulation_CT.create(id);
	vc_HTTP.start(HTTP_Server_Emulation.main(http_cfg));
}

private function f_init(charstring id, float t_guard := 40.0) runs on MTC_CT {
	/* Ensure a guard time inbetween tests. This is to make sure that the IPAd is able to finish its current poll
	 * cycle. In practice this means that the IPAd will notice that the connectivity towards the eIM is lost and
	 * since this is one of the conditions for ending the current poll cycle it will exit. A freshly restarted
	 * IPAd is a mandatory start condition for the tests since all tests expect the initialization procedure
	 * (selection of ISD-P etc.) that the IPAd executes on startup. */
	f_sleep(mp_restart_guardtime);

	g_Tguard.start(t_guard);
	activate(as_Tguard());
	f_init_esipa(id);
}

/* Expect a GetResponse request from IUT and transfer as many response bytes the IUT requests */
private function f_vpcd_get_response(octetstring response) runs on IPAd_ConnHdlr return integer {
	var octetstring sw;
	var VPCD_PDU req;
	var integer len;

	req := f_vpcd_exp(tr_VPCD_DATA(?));
	len := oct2int(req.u.data[4]);
	if (len == 0) {
		len := 256;
	}

	/* Make sure that the request APDU is actually a GetResponse request (on logical channel 2) */
	if (substr(req.u.data, 0, 4) != '01c00000'O) {
		setverdict(fail, "unexpected APDU, expecting GetResponse");
		return 0;
	}

	/* Compute status word, in case the requested data is shorter then the response data we intend to send, we must
	 * tell the IUT that there is still data available, so that a consecutive GetResponse request can be issued.
	 * (caller must check return code to determine if a consecutive GetResponse is needed/expected) */
	if (lengthof(response) > len) {
		if (lengthof(response) - len > 255) {
			sw := '6100'O;
		} else {
			sw := '61'O & int2oct(lengthof(response) - len, 1);
		}
	} else {
		sw := '9000'O;
	}

	/* Send response to IUT */
	f_vpcd_send(ts_VPCD_DATA(substr(response, 0, len) & sw));

	/* Return how many bytes have sent */
	return len;
}

/* Expect one or more GetResponse requests from IUT until the full response is transferred */
private function f_vpcd_get_response_multi(octetstring response) runs on IPAd_ConnHdlr {
	var integer bytes_sent := 0;
	var octetstring response_remainder := response;

	while (true) {
		response_remainder := substr(response_remainder, bytes_sent, lengthof(response_remainder) - bytes_sent);
		bytes_sent := f_vpcd_get_response(response_remainder);

		/* Check if we reached the last chunk */
		if (lengthof(response_remainder) <= bytes_sent) {
			return;
		}
	}
}

/* Expect one or more STORE DATA requests until the IUT has completed the transmision cycle */
private function f_vpcd_store_data(octetstring exp := ''O) runs on IPAd_ConnHdlr return octetstring {

	var VPCD_PDU req;
	var octetstring block;
	var integer len;
	var octetstring data := ''O;

	while (true) {
		req := f_vpcd_exp(tr_VPCD_DATA(?));

		/* Make sure that the request APDU is actually a STORE DATA request (on logical channel 1) */
		if (substr(req.u.data, 0, 3) != '81E291'O and
		    substr(req.u.data, 0, 3) != '81E211'O) {
			setverdict(fail, "unexpected APDU, expecting GetResponse");
			return ''O;
		}

		if (lengthof(req.u.data) - 5 > 255) {
			len := 255;
		} else {
			len := lengthof(req.u.data) - 5;
		}
		block := substr(req.u.data, 5, len);
		data := data & block;

		/* The final status word contains the length of the response. We can not send it right now
		 * since the caller must first process the received data block and compute a response. When
		 * the exact length of the response data is known. The final status word can be sent using
		 * f_vpcd_store_data_final_ack() */
		if (substr(req.u.data, 2, 1) == '91'O) {
			if (exp != ''O and block != exp) {
				setverdict(fail, "received block contains unexpected data (", block, " != ", exp, ")");
			}
			return block;
		}

		f_vpcd_send(ts_VPCD_DATA('9000'O));
	}

	setverdict(fail, "no data? (we should not reach this code path)");
	return ''O;
}

/* Send a final status word to acknowledge the last block of a STORE DATA transmission. The status word will tell
 * the IUT how many response bytes are available. (The IUT will immediately begin to fetch the response using
 * one or more GetResponse requests */
private function f_vpcd_store_data_final_ack(integer response_len) runs on IPAd_ConnHdlr {
	var octetstring second_sw_byte;
	var octetstring first_sw_byte;

	if (response_len > 255) {
		second_sw_byte := '00'O;
	} else {
		second_sw_byte := int2oct(response_len, 1);
	}

	if (response_len > 0) {
		first_sw_byte := '61'O; /* 61xx */
	} else {
		first_sw_byte := '90'O; /* 9000 */
	}

	f_vpcd_send(ts_VPCD_DATA(first_sw_byte & second_sw_byte));
}

/* Expect a pre-defined request (optional), and send a pre-defined response. This is a shortcut that only works in case
 * the response does not depend on the request. */
private function f_vpcd_transceive(octetstring response, octetstring expected_request := ''O) runs on IPAd_ConnHdlr {

	/* In case we do not use the VPCD (because we have some other kind of eUICC emulation or even a real card
	 * present), we just skip. */
	if (mp_use_vpcd == false) {
		return;
	}

	f_vpcd_store_data(expected_request);
	f_vpcd_store_data_final_ack(lengthof(response));
	if (response != ''O) {
		f_vpcd_get_response_multi(response);
	}
}

/* Handle the opening of logical channel 1 and the selection of the ISD-R */
private function f_es10x_init() runs on IPAd_ConnHdlr {
	var charstring eim_fqdn := mp_esipa_ip & ":" & int2str(mp_esipa_port);

	/* If we decide not to use vpcd, then we must not initialize anything here */
	if (mp_use_vpcd == false) {
		return;
	}

	/* Expect a TERMINAL CAPABILITIES request
	 * (see also: 3GPP TS 102.221, section 11.1.19.2.4 and SGP.22, section 3.4.2) */
	f_vpcd_exp(tr_VPCD_DATA('80AA000005A903830107'O));
	f_vpcd_send(ts_VPCD_DATA('9000'O));

	/* Expect a MANAGE CHANNEL request that opens logical channel 1 */
	f_vpcd_exp(tr_VPCD_DATA('0070000100'O));
	f_vpcd_send(ts_VPCD_DATA('9000'O));

	/* Expect selection of ISD-R request */
	f_vpcd_exp(tr_VPCD_DATA('01a4040410a0000005591010ffffffff8900000100'O));
	f_vpcd_send(ts_VPCD_DATA('6121'O)); /* 21 bytes of response, which are not requested by the ipad. */

	/* Expect the IPAd to query the eID from the eUICC */
	f_vpcd_transceive(enc_GetEuiccDataResponse(valueof(ts_getEuiccDataResponse)), 'BF3E035C015A'O);

	/* Expect the IPAd to query the eIM configuration data from the eUICC */
	f_vpcd_transceive(enc_GetEimConfigurationDataResponse(valueof(ts_getEimConfigurationDataResponse(eim_fqdn))), 'BF5500'O);
}

/* Handle the closing of logical channel 1 */
private function f_es10x_close() runs on IPAd_ConnHdlr {

	/* Expect a MANAGE CHANNEL request that closes logical channel 1 */
	f_vpcd_exp(tr_VPCD_DATA('0070800100'O));
	f_vpcd_send(ts_VPCD_DATA('9000'O));
}

/* Receive ESipa HTTP request */
private function f_esipa_receive() runs on IPAd_ConnHdlr return EsipaMessageFromIpaToEim {
	var HTTPMessage esipa_req;
	timer T := 10.0;
	var EsipaMessageFromIpaToEim request;

	T.start;
	alt {
	[] HTTP_SRV.receive({ request_binary := ? }) -> value esipa_req {
		request := dec_EsipaMessageFromIpaToEim(esipa_req.request_binary.body);
		}
	[] T.timeout {
		setverdict(fail, "no HTTP request received?");
		}
	}

	return request;
}

/* Send ESipa HTTP response */
private function f_esipa_send(EsipaMessageFromEimToIpa response) runs on IPAd_ConnHdlr  {
	var octetstring esipa_res;
	esipa_res := enc_EsipaMessageFromEimToIpa(response);
	HTTP_SRV.send(ts_http_resp(esipa_res));
}

/* Perform one ESipa HTTP request/response cycle */
private function f_esipa_transceive(EsipaMessageFromEimToIpa response) runs on IPAd_ConnHdlr return EsipaMessageFromIpaToEim {
	var EsipaMessageFromIpaToEim request;

	request := f_esipa_receive();
	f_esipa_send(response);

	return request;
}

/* Perform one ESipa HTTP request/response cycle but with an empty response */
private function f_esipa_transceive_empty_response() runs on IPAd_ConnHdlr return EsipaMessageFromIpaToEim {
	var EsipaMessageFromIpaToEim request;

	request := f_esipa_receive();
	HTTP_SRV.send(ts_http_resp(''O));
	return request;
}

/* Common Mutual Authentication Procedure, see also: GSMA SGP.22, section 3.0.1 */
private function f_proc_cmn_mtl_auth() runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;

	/* Step #1 */
	f_vpcd_transceive(enc_EUICCInfo1(valueof(ts_EUICCInfo1)), 'bf2000'O);

	/* Step #2-#4 */
	f_vpcd_transceive(enc_GetEuiccChallengeResponse(valueof(ts_GetEuiccChallengeResponse)), 'bf2e00'O);

	/* Step #5-#10 */
	esipa_req := f_esipa_receive();
	if (not match(esipa_req, tr_initiateAuthenticationRequestEsipa)) {
		setverdict(fail, "unexpected message from IPAd");
	}
	esipa_res := valueof(ts_initiateAuthenticationResponseEsipa(euiccChallenge := esipa_req.initiateAuthenticationRequestEsipa.euiccChallenge));
	f_esipa_send(esipa_res);

	/* Step #11-#14 */
	f_vpcd_transceive(enc_AuthenticateServerResponse(valueof(ts_authenticateServerResponse)));

	/* Step #15-#17 */
	esipa_req := f_esipa_transceive(valueof(ts_authenticateClientResponseEsipa_dpe));
	if (not match(esipa_req, tr_authenticateClientRequestEsipa)) {
		setverdict(fail, "unexpected message from IPAd");
	}
}

/* ********************************************* */
/* ********** BELOW ONLY TESTCASES! ************ */
/* ********************************************* */


/* A testcase to try out an indirect profile download,
 * See also: GSMA SGP.32, section 3.2.3.2: Indirect Profile Download */
private function f_TC_proc_indirect_prfle_dwnld(charstring id) runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;
	var integer i;
	var charstring eim_fqdn := mp_esipa_ip & ":" & int2str(mp_esipa_port);
	var BoundProfilePackage boundProfilePackage;

	f_es10x_init();
	f_http_register();

	/* Prepare indirect profile download by responding with a download trigger request */
	esipa_res := valueof(ts_getEimPackageResponse_dnlTrigReq);
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_getEimPackageRequest)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Expect the IPAd to query the eIM configuration data from the eUICC */
	f_vpcd_transceive(enc_GetEimConfigurationDataResponse(valueof(ts_getEimConfigurationDataResponse(eim_fqdn))), 'BF5500'O);

	f_proc_cmn_mtl_auth();

	f_vpcd_transceive(enc_PrepareDownloadResponse(valueof(ts_prepareDownloadResponse)));

	esipa_res := valueof(ts_getBoundProfilePackageResponseEsipa);
	esipa_req := f_esipa_transceive(esipa_res);
	boundProfilePackage := esipa_res.getBoundProfilePackageResponseEsipa.getBoundProfilePackageOkEsipa.boundProfilePackage;
	/* TODO: match response (we do not have a template yet) */

	/* initialiseSecureChannelRequest */
	f_vpcd_transceive(''O);

	/* Step #3 (ES8+.ConfigureISDP) */
	for (i := 0; i < sizeof(boundProfilePackage.firstSequenceOf87); i := i + 1) {
		f_vpcd_transceive(''O);
	}

	/* Step #4 (ES8+.StoreMetadata) */
	for (i := 0; i < sizeof(boundProfilePackage.sequenceOf88); i := i + 1) {
		f_vpcd_transceive(''O);
	}

	/* Step #5 (ES8+.ReplaceSessionKeys", optional, left out) */
	if (ispresent(boundProfilePackage.secondSequenceOf87)) {
		for (i := 0; i < sizeof(boundProfilePackage.secondSequenceOf87); i := i + 1) {
			f_vpcd_transceive(''O);
		}
	}

	/* Step #6 (ES8+.LoadProfileElements) */
	for (i := 0; i < sizeof(boundProfilePackage.sequenceOf86); i := i + 1) {
		if (i < sizeof(boundProfilePackage.sequenceOf86) - 1) {
			f_vpcd_transceive(''O);
		} else {
			/* In the last message we send the ProfileInstallationResult */
			f_vpcd_transceive(enc_ProfileInstallationResult(valueof(ts_profileInstallationResult)));
		}
	}

	/* Receive ProfileInstallationResult from iPAD->eIM */
	esipa_req := f_esipa_transceive_empty_response();
	/* TODO: match response (we do not have a template yet) */

	/* Receive RemoveNotificationFromList from iPAD->eUICC */
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));

	/* Wait some time until the the last HTTP response is actually delivered */
	f_sleep(2.0);

	f_es10x_close();

	setverdict(pass);
}
testcase TC_proc_indirect_prfle_dwnld() runs on MTC_CT {
	var charstring id := testcasename();
	var IPAd_ConnHdlrPars pars := f_init_pars();
	var IPAd_ConnHdlr vc_conn;
	f_init(id);
	vc_conn := f_start_handler(refers(f_TC_proc_indirect_prfle_dwnld), pars);
	vc_conn.done;
	setverdict(pass);
}


/* A testcase to try out an the Generic eUICC Package Download and Execution Procedure,
 * See also: GSMA SGP.32, section 3.3.1: Generic eUICC Package Download and Execution */
private function f_TC_proc_euicc_pkg_dwnld_exec(charstring id) runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;

	f_es10x_init();
	f_http_register();

	/* Step #1-#2 */
	esipa_res := valueof(ts_getEimPackageResponse_euiccPkgReq);
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_getEimPackageRequest)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Step #3-#8 */
	f_vpcd_transceive(enc_EuiccPackageResult(valueof(ts_euiccPackageResult)));

	/* Step #9 */
	f_vpcd_transceive(enc_RetrieveNotificationsListResponse(valueof(ts_retrieveNotificationsListResponse)));

	/* Step #10-14 */
	esipa_res := valueof(ts_provideEimPackageResultResponse_eimAck(eimAcknowledgements := {1,2,3,4}));
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_provideEimPackageResult_ePRAndNotif)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Step #15-17 */
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));

	/* Wait some time until the the last HTTP response is actually delivered */
	f_sleep(2.0);

	f_es10x_close();

	setverdict(pass);
}

testcase TC_proc_euicc_pkg_dwnld_exec() runs on MTC_CT {
	var charstring id := testcasename();
	var IPAd_ConnHdlrPars pars := f_init_pars();
	var IPAd_ConnHdlr vc_conn;
	f_init(id);
	vc_conn := f_start_handler(refers(f_TC_proc_euicc_pkg_dwnld_exec), pars);
	vc_conn.done;
	setverdict(pass);
}


/* A testcase to try out an the Generic eUICC Package Download and Execution Procedure,
 * but this time we force a rollback meneuver */
private function f_TC_proc_euicc_pkg_dwnld_exec_rollback(charstring id) runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;

	f_es10x_init();
	f_http_register();

	/* Step #1-#2 */
	esipa_res := valueof(ts_getEimPackageResponse_euiccPkgReq);
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_getEimPackageRequest)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Step #3-#8 */
	f_vpcd_transceive(enc_EuiccPackageResult(valueof(ts_euiccPackageResult)));

	/* Step #9 */
	f_vpcd_transceive(enc_RetrieveNotificationsListResponse(valueof(ts_retrieveNotificationsListResponse)));

	/* We now ignore the response from the IPAd. The IPAd will interpret this as a disturbed IP connection. */
	f_esipa_receive();

	/* To fix the problem, the IPAd will now try a profile rollback meneuver. */
	f_vpcd_transceive(enc_ProfileRollbackResponse(valueof(ts_profileRollbackResponse)),
			  enc_ProfileRollbackRequest(valueof(ts_profileRollbackRequest)));

	/* At this point the old profile is active again. The IPAd is now expected to start at Step #9 again
	 * to continue the procedure normally. */

	/* Step #9 */
	f_vpcd_transceive(enc_RetrieveNotificationsListResponse(valueof(ts_retrieveNotificationsListResponse)));

	/* Step #10-14 */
	esipa_res := valueof(ts_provideEimPackageResultResponse_eimAck(eimAcknowledgements := {1,2,3,4}));
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_provideEimPackageResult_ePRAndNotif(euiccPackageResult := ?))) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Step #15-17 */
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));
	f_vpcd_transceive(enc_NotificationSentResponse(valueof(ts_notificationSentResponse)));

	/* Wait some time until the the last HTTP response is actually delivered */
	f_sleep(2.0);

	f_es10x_close();

	setverdict(pass);
}

testcase TC_proc_euicc_pkg_dwnld_exec_rollback() runs on MTC_CT {
	var charstring id := testcasename();
	var IPAd_ConnHdlrPars pars := f_init_pars();
	var IPAd_ConnHdlr vc_conn;
	f_init(id);
	vc_conn := f_start_handler(refers(f_TC_proc_euicc_pkg_dwnld_exec_rollback), pars);
	vc_conn.done;
	setverdict(pass);
}


/* A testcase to try out an IpaEuiccDataRequest */
private function f_TC_proc_euicc_data_req(charstring id) runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;
	var charstring eim_fqdn := mp_esipa_ip & ":" & int2str(mp_esipa_port);

	f_es10x_init();
	f_http_register();

	/* IPAd requests a package, we tell it to execute an ipaEuiccDataRequest */
	esipa_res := valueof(ts_getEimPackageResponse_euiccDataReq);
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_getEimPackageRequest)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* IPAd will obtain the data from the eUICC */
	f_vpcd_transceive(enc_EuiccConfiguredAddressesResponse(valueof(ts_euiccConfiguredAddressesResponse)));
	f_vpcd_transceive(enc_EUICCInfo1(valueof(ts_EUICCInfo1)));
	f_vpcd_transceive(enc_EUICCInfo2(valueof(ts_EUICCInfo2)));
	f_vpcd_transceive(enc_GetEimConfigurationDataResponse(valueof(ts_getEimConfigurationDataResponse(eim_fqdn))));
	f_vpcd_transceive(enc_GetCertsResponse(valueof(ts_getCertsResponse)));
	f_vpcd_transceive(enc_RetrieveNotificationsListResponse(valueof(ts_retrieveNotificationsListResponse)));

	/* IPAd will return the data to us */
	esipa_res := valueof(ts_provideEimPackageResultResponse_eimAck(eimAcknowledgements := {1,2,3,4}));
	esipa_req := f_esipa_transceive(esipa_res);

	/* Wait some time until the the last HTTP response is actually delivered */
	f_sleep(2.0);

	f_es10x_close();

	setverdict(pass);
}
testcase TC_proc_euicc_data_req() runs on MTC_CT {
	var charstring id := testcasename();
	var IPAd_ConnHdlrPars pars := f_init_pars();
	var IPAd_ConnHdlr vc_conn;
	f_init(id);
	vc_conn := f_start_handler(refers(f_TC_proc_euicc_data_req), pars);
	vc_conn.done;
	setverdict(pass);
}

/* A testcase to try out what happens when the eIM package request is rejected */
private function f_TC_get_eim_pkg_req_rej(charstring id) runs on IPAd_ConnHdlr {
	var EsipaMessageFromIpaToEim esipa_req;
	var EsipaMessageFromEimToIpa esipa_res;

	f_es10x_init();
	f_http_register();

	/* IPAd requests a package, we respond with an eimPackageError code 127 (undefined error) */
	esipa_res := valueof(ts_getEimPackageResponse_eimPkgErrUndef);
	esipa_req := f_esipa_transceive(esipa_res);
	if (not match(esipa_req, tr_getEimPackageRequest)) {
		setverdict(fail, "unexpected message from IPAd");
	}

	/* Wait some time until the the last HTTP response is actually delivered */
	f_sleep(2.0);

	f_es10x_close();

	setverdict(pass);
}
testcase TC_get_eim_pkg_req_rej() runs on MTC_CT {
	var charstring id := testcasename();
	var IPAd_ConnHdlrPars pars := f_init_pars();
	var IPAd_ConnHdlr vc_conn;
	f_init(id);
	vc_conn := f_start_handler(refers(f_TC_get_eim_pkg_req_rej), pars);
	vc_conn.done;
	setverdict(pass);
}

control {
	execute ( TC_proc_indirect_prfle_dwnld() );
	execute ( TC_proc_euicc_pkg_dwnld_exec() );
	execute ( TC_proc_euicc_pkg_dwnld_exec_rollback() );
	execute ( TC_proc_euicc_data_req() );
	execute ( TC_get_eim_pkg_req_rej() );
}

}