pcu: Introduce test TC_ul_tbf_finished_pkt_dl_ass_pch

Related: OS#5700
Change-Id: Id99b048e7c305fe616bb54437313a1e45449ff57
diff --git a/pcu/GPRS_Components.ttcn b/pcu/GPRS_Components.ttcn
index f9a39a6..e248860 100644
--- a/pcu/GPRS_Components.ttcn
+++ b/pcu/GPRS_Components.ttcn
@@ -1189,7 +1189,7 @@
 
 function f_rx_rlcmac_dl_block_exp_dummy(out RlcmacDlBlock dl_block,
 					template (value) TsTrxBtsNum nr := ts_TsTrxBtsNum)
-runs on MS_BTS_IFACE_CT {
+runs on MS_BTS_IFACE_CT return uint32_t {
 	var uint32_t dl_fn;
 
 	f_rx_rlcmac_dl_block(dl_block, dl_fn, nr := nr);
@@ -1197,6 +1197,7 @@
 		setverdict(fail, "Failed to match Packet DUMMY DL");
 		f_shutdown(__BFILE__, __LINE__);
 	}
+	return dl_fn;
 }
 
 function f_rx_rlcmac_dl_block_exp_pkt_pag_req(out RlcmacDlBlock dl_block,
diff --git a/pcu/PCU_Tests.ttcn b/pcu/PCU_Tests.ttcn
index 1648a92..8b3d1ea 100644
--- a/pcu/PCU_Tests.ttcn
+++ b/pcu/PCU_Tests.ttcn
@@ -3907,6 +3907,64 @@
 	f_tc_dl_data_no_llc_ui_dummy(bssgp_ms_racap_egprs_def);
 }
 
+/* Scenario: MS creates one phase access, does contention resolution CV>0 and
+ * finishes sending data (CV=0), which is forwarded to SGSN by PCU. PCU acks with
+ * FINAL_ACK=1 (hence UL TBF moves to FINISHED state). Then SGSN answers and PCU
+ * has to assign a DL TBF (through PCH because of FINISHED state, TS 44.060 9.3.3.3.2).
+ * Make sure the assignment is not done until we receive the PKT CTRL ACK from the MS
+ * (at that time we know the MS is listening on PCH again). OS#5700.
+ */
+testcase TC_ul_tbf_finished_pkt_dl_ass_pch() runs on RAW_PCU_Test_CT {
+	var RlcmacDlBlock dl_block;
+	var octetstring data := f_rnd_octstring(10);
+	var uint32_t sched_fn;
+	var uint32_t dl_fn;
+	var GprsMS ms;
+	timer T;
+	var octetstring payload;
+
+	/* Initialize NS/BSSGP side */
+	f_init_bssgp();
+	/* Initialize GPRS MS side */
+	f_init_gprs_ms();
+	ms := g_ms[0]; /* We only use first MS in this test */
+
+	/* Initialize the PCU interface abstraction */
+	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));
+
+	/* Establish BSSGP connection to the PCU */
+	f_bssgp_establish();
+	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);
+
+	/* Establish an Uplink TBF */
+	f_ms_establish_ul_tbf(ms);
+
+	/* Send one UL block (with TLLI since we are in One-Phase Access
+	   contention resolution) and make sure it is ACKED fine. */
+	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
+	dl_fn := f_rx_rlcmac_dl_block_exp_dummy(dl_block);
+	f_ms_tx_ul_data_block(ms, payload, cv := 1, with_tlli := true, fn := f_next_pdch_block(dl_fn));
+
+	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
+	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, false));
+	f_ms_tx_ul_data_block(ms, payload, cv := 0);
+	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
+
+	/* 1 UL block should be received in SGSN */
+	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));
+	/* Now SGSN sends some DL data, PCU will page on CCCH (PCH) */
+	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
+
+	/* UL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
+	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);
+	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);
+	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
+	f_sleep(X2002);
+	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);
+
+	f_shutdown(__BFILE__, __LINE__, final := true);
+}
+
 private function f_TC_egprs_pkt_chan_req(in EGPRSPktChRequest req,
 					 template GsmRrMessage t_imm_ass := ?,
 					 PCUIF_BurstType bt := BURST_TYPE_1)
@@ -6841,6 +6899,8 @@
 	execute( TC_dl_gprs_data_no_llc_ui_dummy() );
 	execute( TC_dl_egprs_data_no_llc_ui_dummy() );
 
+	execute( TC_ul_tbf_finished_pkt_dl_ass_pch() );
+
 	/* EGPRS specific test cases */
 	execute( TC_egprs_pkt_chan_req_signalling() );
 	execute( TC_egprs_pkt_chan_req_one_phase() );
diff --git a/pcu/expected-results.xml b/pcu/expected-results.xml
index 7f138cd..c666ff7 100644
--- a/pcu/expected-results.xml
+++ b/pcu/expected-results.xml
@@ -70,6 +70,7 @@
   <testcase classname='PCU_Tests' name='TC_bssgp_dl_unitdata_with_invalid_imsi' time='MASKED'/>
   <testcase classname='PCU_Tests' name='TC_dl_gprs_data_no_llc_ui_dummy' time='MASKED'/>
   <testcase classname='PCU_Tests' name='TC_dl_egprs_data_no_llc_ui_dummy' time='MASKED'/>
+  <testcase classname='PCU_Tests' name='TC_ul_tbf_finished_pkt_dl_ass_pch' time='MASKED'/>
   <testcase classname='PCU_Tests' name='TC_egprs_pkt_chan_req_signalling' time='MASKED'/>
   <testcase classname='PCU_Tests' name='TC_egprs_pkt_chan_req_one_phase' time='MASKED'/>
   <testcase classname='PCU_Tests' name='TC_egprs_pkt_chan_req_two_phase' time='MASKED'/>