sgsn/test: Move message sending to send_0408_message

This replaces serveral occurences of duplicated code for message
creation and sending (passing to gsm0408_gprs_rcvmsg) into a single
function. In addition, the sgsn_tx_counter is always reset within
send_0408_message to simplify the code that checks for the number of
messages sent.

Sponsored-by: On-Waves ehf
diff --git a/openbsc/tests/sgsn/sgsn_test.c b/openbsc/tests/sgsn/sgsn_test.c
index 6762ef8..60e8bf2 100644
--- a/openbsc/tests/sgsn/sgsn_test.c
+++ b/openbsc/tests/sgsn/sgsn_test.c
@@ -100,6 +100,19 @@
 	return ctx;
 }
 
+static void send_0408_message(struct gprs_llc_llme *llme, uint32_t tlli,
+			      const uint8_t *data, size_t data_len)
+{
+	struct msgb *msg;
+
+	sgsn_tx_counter = 0;
+
+	msg = create_msg(data, data_len);
+	msgb_tlli(msg) = tlli;
+	gsm0408_gprs_rcvmsg(msg, llme);
+	msgb_free(msg);
+}
+
 static void test_llme(void)
 {
 	struct gprs_llc_lle *lle, *lle_copy;
@@ -142,8 +155,6 @@
 	struct gprs_ra_id raid = { 0, };
 	struct sgsn_mm_ctx *ctx, *ictx;
 	uint32_t local_tlli;
-	struct msgb *msg;
-	int sgsn_tx_counter_old;
 
 	printf("Testing GMM detach\n");
 
@@ -161,15 +172,12 @@
 	ctx = alloc_mm_ctx(local_tlli, &raid);
 
 	/* inject the detach */
-	sgsn_tx_counter_old = sgsn_tx_counter;
-	msg = create_msg(detach_req, ARRAY_SIZE(detach_req));
-	msgb_tlli(msg) = local_tlli;
-	gsm0408_gprs_rcvmsg(msg, ctx->llme);
-	msgb_free(msg);
+	send_0408_message(ctx->llme, local_tlli,
+			  detach_req, ARRAY_SIZE(detach_req));
 
 	/* verify that a single message (hopefully the Detach Accept) has been
 	 * sent by the SGSN */
-	OSMO_ASSERT(sgsn_tx_counter_old + 1 == sgsn_tx_counter);
+	OSMO_ASSERT(sgsn_tx_counter == 1);
 
 	/* verify that things are gone */
 	OSMO_ASSERT(count(gprs_llme_list()) == 0);
@@ -186,8 +194,6 @@
 	struct gprs_ra_id raid = { 0, };
 	struct sgsn_mm_ctx *ctx, *ictx;
 	uint32_t local_tlli;
-	struct msgb *msg;
-	int sgsn_tx_counter_old;
 
 	printf("Testing GMM detach (power off)\n");
 
@@ -205,15 +211,12 @@
 	ctx = alloc_mm_ctx(local_tlli, &raid);
 
 	/* inject the detach */
-	sgsn_tx_counter_old = sgsn_tx_counter;
-	msg = create_msg(detach_req, ARRAY_SIZE(detach_req));
-	msgb_tlli(msg) = local_tlli;
-	gsm0408_gprs_rcvmsg(msg, ctx->llme);
-	msgb_free(msg);
+	send_0408_message(ctx->llme, local_tlli,
+			  detach_req, ARRAY_SIZE(detach_req));
 
 	/* verify that no message (and therefore no Detach Accept) has been
 	 * sent by the SGSN */
-	OSMO_ASSERT(sgsn_tx_counter_old == sgsn_tx_counter);
+	OSMO_ASSERT(sgsn_tx_counter == 0);
 
 	/* verify that things are gone */
 	OSMO_ASSERT(count(gprs_llme_list()) == 0);
@@ -228,7 +231,6 @@
 {
 	struct gprs_llc_lle *lle;
 	uint32_t local_tlli;
-	struct msgb *msg;
 
 	printf("Testing GMM detach (no MMCTX)\n");
 
@@ -247,10 +249,8 @@
 	OSMO_ASSERT(count(gprs_llme_list()) == 1);
 
 	/* inject the detach */
-	msg = create_msg(detach_req, ARRAY_SIZE(detach_req));
-	msgb_tlli(msg) = local_tlli;
-	gsm0408_gprs_rcvmsg(msg, lle->llme);
-	msgb_free(msg);
+	send_0408_message(lle->llme, local_tlli,
+			  detach_req, ARRAY_SIZE(detach_req));
 
 	/* verify that the LLME is gone */
 	OSMO_ASSERT(count(gprs_llme_list()) == 0);
@@ -263,8 +263,6 @@
 {
 	struct gprs_llc_lle *lle;
 	uint32_t local_tlli;
-	struct msgb *msg;
-	int sgsn_tx_counter_old;
 
 	printf("Testing GMM Status (no MMCTX)\n");
 
@@ -281,14 +279,11 @@
 	OSMO_ASSERT(count(gprs_llme_list()) == 1);
 
 	/* inject the detach */
-	sgsn_tx_counter_old = sgsn_tx_counter;
-	msg = create_msg(gmm_status, ARRAY_SIZE(gmm_status));
-	msgb_tlli(msg) = local_tlli;
-	gsm0408_gprs_rcvmsg(msg, lle->llme);
-	msgb_free(msg);
+	send_0408_message(lle->llme, local_tlli,
+			  gmm_status, ARRAY_SIZE(gmm_status));
 
 	/* verify that no message has been sent by the SGSN */
-	OSMO_ASSERT(sgsn_tx_counter_old == sgsn_tx_counter);
+	OSMO_ASSERT(sgsn_tx_counter == 0);
 
 	/* verify that the LLME is gone */
 	OSMO_ASSERT(count(gprs_llme_list()) == 0);