gmm: Introduce APIs & unit test to enc/dec GPRS Timer IE

enc/dec implementation and unit test are imported from osmo-sgsn.git
b83aabaa95695c61a64e5e990944f8e55934a976.

Change-Id: Ia3b903e8bc7890489390dcc5b2d4b60efd55dc2c
This commit is contained in:
Pau Espin 2023-05-22 13:42:09 +02:00
parent a03f36654b
commit c6e7a3bd88
7 changed files with 214 additions and 0 deletions

View File

@ -122,6 +122,10 @@ int gprs_gmm_submit_gmmreg_attach_cnf(struct gprs_gmm_entity *gmme, bool accepte
int gprs_gmm_submit_gmmsm_establish_cnf(struct gprs_gmm_entity *gmme, bool accepted, uint8_t cause);
int gprs_gmm_submit_llgmm_assing_req(const struct gprs_gmm_entity *gmme);
/* misc.c */
int gprs_gmm_gprs_tmr_to_secs(uint8_t gprs_tmr);
uint8_t gprs_gmm_secs_to_gprs_tmr_floor(int secs);
#define LOGGMME(gmme, level, fmt, args...) \
LOGGMM(level, "GMME(IMSI-%s:PTMSI-%08x:TLLI-%08x) " fmt, \
gmme->imsi, \

View File

@ -20,6 +20,8 @@
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
#include <osmocom/gprs/gmm/gmm.h>
#include <osmocom/gprs/gmm/gmm_ms_fsm.h>
@ -32,3 +34,48 @@ void osmo_gprs_gmm_set_log_cat(enum osmo_gprs_gmm_log_cat logc, int logc_num)
gprs_gmm_ms_fsm_set_log_cat(logc_num);
}
/* 3GPP TS 24.008, 10.5.7.3 GPRS Timer */
int gprs_gmm_gprs_tmr_to_secs(uint8_t gprs_tmr)
{
switch (gprs_tmr & GPRS_TMR_UNIT_MASK) {
case GPRS_TMR_2SECONDS:
return 2 * (gprs_tmr & GPRS_TMR_FACT_MASK);
default:
case GPRS_TMR_MINUTE:
return 60 * (gprs_tmr & GPRS_TMR_FACT_MASK);
case GPRS_TMR_6MINUTE:
return 360 * (gprs_tmr & GPRS_TMR_FACT_MASK);
case GPRS_TMR_DEACTIVATED:
return -1;
}
}
/* This functions returns a tmr value such that
* - f is monotonic
* - f(s) <= s
* - f(s) == s if a tmr exists with s = gprs_tmr_to_secs(tmr)
* - the best possible resolution is used
* where
* f(s) = gprs_tmr_to_secs(gprs_secs_to_tmr_floor(s))
*/
uint8_t gprs_gmm_secs_to_gprs_tmr_floor(int secs)
{
if (secs < 0)
return GPRS_TMR_DEACTIVATED;
if (secs < 2 * 32)
return GPRS_TMR_2SECONDS | (secs / 2);
if (secs < 60 * 2)
/* Ensure monotonicity */
return GPRS_TMR_2SECONDS | GPRS_TMR_FACT_MASK;
if (secs < 60 * 32)
return GPRS_TMR_MINUTE | (secs / 60);
if (secs < 360 * 6)
/* Ensure monotonicity */
return GPRS_TMR_MINUTE | GPRS_TMR_FACT_MASK;
if (secs < 360 * 32)
return GPRS_TMR_6MINUTE | (secs / 360);
return GPRS_TMR_6MINUTE | GPRS_TMR_FACT_MASK;
}

View File

@ -13,14 +13,30 @@ AM_LDFLAGS = \
$(NULL)
check_PROGRAMS = \
gmm_misc_test \
gmm_prim_test \
$(NULL)
EXTRA_DIST = \
gmm_misc_test.err \
gmm_misc_test.ok \
gmm_prim_test.err \
gmm_prim_test.ok \
$(NULL)
# libosmo-gprs-gmm.a is used below to access non-exported private symbols used in the test:
gmm_misc_test_SOURCES = gmm_misc_test.c
gmm_misc_test_LDADD = \
$(top_builddir)/src/gmm/.libs/libosmo-gprs-gmm.a \
$(top_builddir)/src/llc/.libs/libosmo-gprs-llc.a \
$(LIBOSMOGSM_LIBS) \
$(LIBOSMOCORE_LIBS) \
$(NULL)
gmm_misc_test_LDFLAGS = \
-no-install \
$(NULL)
# libosmo-gprs-llc.a is used below to access non-exported private symbols used in the test:
gmm_prim_test_SOURCES = gmm_prim_test.c
gmm_prim_test_LDADD = \

130
tests/gmm/gmm_misc_test.c Normal file
View File

@ -0,0 +1,130 @@
/* gmm_prim tests
*
* (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Pau espin Pedrol <pespin@sysmocom.de>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <stdio.h>
#include <osmocom/core/application.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/fsm.h>
#include <osmocom/core/msgb.h>
#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
#include <osmocom/gprs/gmm/gmm_private.h>
static void *tall_ctx = NULL;
/* Test: TS 24.008 10.5.7.3 GPRS Timer */
static void test_gprs_timer_enc_dec(void)
{
int i, u, secs, tmr;
const int upper_secs_test_limit = 12000;
int dec_secs, last_dec_secs = -1;
printf("==== %s() [start] ====\n", __func__);
/* Check gprs_gmm_gprs_tmr_to_secs() with all 256 encoded values */
for (u = 0; u <= GPRS_TMR_DEACTIVATED; u += 32) {
fprintf(stderr, "Testing decoding with timer value unit %u\n",
u / 32);
for (i = 0; i < 32; i++) {
switch (u) {
case GPRS_TMR_2SECONDS:
OSMO_ASSERT(gprs_gmm_gprs_tmr_to_secs(u + i) == 2 * i);
break;
default:
case GPRS_TMR_MINUTE:
OSMO_ASSERT(gprs_gmm_gprs_tmr_to_secs(u + i) == 60 * i);
break;
case GPRS_TMR_6MINUTE:
OSMO_ASSERT(gprs_gmm_gprs_tmr_to_secs(u + i) == 360 * i);
break;
case GPRS_TMR_DEACTIVATED:
OSMO_ASSERT(gprs_gmm_gprs_tmr_to_secs(u + i) == -1);
break;
}
OSMO_ASSERT(gprs_gmm_gprs_tmr_to_secs(u + i) < upper_secs_test_limit);
}
}
/* Check gprs_secs_to_tmr_floor for secs that can exactly be
* represented as GPRS timer values */
for (i = 0; i < GPRS_TMR_DEACTIVATED; i++) {
int j;
secs = gprs_gmm_gprs_tmr_to_secs(i);
tmr = gprs_gmm_secs_to_gprs_tmr_floor(secs);
OSMO_ASSERT(secs == gprs_gmm_gprs_tmr_to_secs(tmr));
/* Check that the highest resolution is used */
for (j = 0; j < tmr; j++)
OSMO_ASSERT(secs != gprs_gmm_gprs_tmr_to_secs(j));
}
OSMO_ASSERT(GPRS_TMR_DEACTIVATED == gprs_gmm_secs_to_gprs_tmr_floor(-1));
/* Check properties of gprs_secs_to_tmr_floor */
for (secs = 0; secs <= upper_secs_test_limit; secs++) {
int tmr = gprs_gmm_secs_to_gprs_tmr_floor(secs);
int delta_secs = gprs_gmm_gprs_tmr_to_secs((tmr & ~0x1f) | 1);
dec_secs = gprs_gmm_gprs_tmr_to_secs(tmr);
/* Check floor */
OSMO_ASSERT(dec_secs <= secs);
/* Check monotonicity */
OSMO_ASSERT(dec_secs >= last_dec_secs);
/* Check max distance (<= resolution) */
OSMO_ASSERT(dec_secs - last_dec_secs <= delta_secs);
last_dec_secs = dec_secs;
}
printf("==== %s() [end] ====\n", __func__);
}
static const struct log_info_cat test_log_categories[] = { };
static const struct log_info test_log_info = {
.cat = test_log_categories,
.num_cat = ARRAY_SIZE(test_log_categories),
};
int main(int argc, char *argv[])
{
tall_ctx = talloc_named_const(NULL, 1, __FILE__);
osmo_init_logging2(tall_ctx, &test_log_info);
log_parse_category_mask(osmo_stderr_target, "DLGLOBAL,1:");
osmo_fsm_log_addr(false);
log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
log_set_print_category_hex(osmo_stderr_target, 0);
log_set_print_category(osmo_stderr_target, 1);
log_set_print_level(osmo_stderr_target, 1);
log_set_use_color(osmo_stderr_target, 0);
test_gprs_timer_enc_dec();
talloc_free(tall_ctx);
}

View File

@ -0,0 +1,8 @@
Testing decoding with timer value unit 0
Testing decoding with timer value unit 1
Testing decoding with timer value unit 2
Testing decoding with timer value unit 3
Testing decoding with timer value unit 4
Testing decoding with timer value unit 5
Testing decoding with timer value unit 6
Testing decoding with timer value unit 7

View File

@ -0,0 +1,2 @@
==== test_gprs_timer_enc_dec() [start] ====
==== test_gprs_timer_enc_dec() [end] ====

View File

@ -1,6 +1,13 @@
AT_INIT
AT_BANNER([Regression tests])
AT_SETUP([gmm/gmm_misc])
AT_KEYWORDS([gmm gmm_misc])
cat $abs_srcdir/gmm/gmm_misc_test.ok > expout
cat $abs_srcdir/gmm/gmm_misc_test.err > experr
AT_CHECK([$abs_top_builddir/tests/gmm/gmm_misc_test], [0], [expout], [experr])
AT_CLEANUP
AT_SETUP([gmm/gmm_prim])
AT_KEYWORDS([gmm gmm_prim])
cat $abs_srcdir/gmm/gmm_prim_test.ok > expout