smpp: Parse and use SMPP-provided validity period

Before this patch, we always ignored any SMPP-provided validity period
and used '0' which is now, and means it expires immediately.

As SMPP allows for validity_period of NULL, use 7 days as SMSC default
in such situations.

Change-Id: Iad9f2697f045ed3bc0eb74c3a9730861f82e6c48
Closes: OS#5567
diff --git a/tests/smpp/smpp_test.c b/tests/smpp/smpp_test.c
index d86da54..4640a5f 100644
--- a/tests/smpp/smpp_test.c
+++ b/tests/smpp/smpp_test.c
@@ -1,5 +1,6 @@
 /*
  * (C) 2013 by Holger Hans Peter Freyther
+ * (C) 2022 by Harald Welte <laforge@osmocom.org>
  * All Rights Reserved
  *
  * This program is free software; you can redistribute it and/or modify
@@ -62,6 +63,41 @@
 	}
 }
 
+static const char *smpp_time_tests[] = {
+	"\0",
+	"220517175524000+",
+	"220517175524000-",
+	"220517175524004+",	/* 1 hour advanced compared to GMT */
+	"220517175524004-",	/* 1 hour retarded compared to GMT */
+	"000000010000000R",	/* 1 hour */
+	"000001000000000R",	/* 1 day */
+};
+
+static void test_smpp_parse_time_format(void)
+{
+	time_t t_now = 1652745600;	/* 2022-05-17 00:00:00 UTC */
+	char *orig_tz;
+
+	printf("Testing SMPP time format parser\n");
+
+	/* relative time format conversion depends on the local time */
+	orig_tz = getenv("TZ");
+	setenv("TZ", "UTC", 1);
+
+	for (unsigned int i = 0; i < ARRAY_SIZE(smpp_time_tests); i++) {
+		time_t t = smpp_parse_time_format(smpp_time_tests[i], &t_now);
+		char buf[32];
+		strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", gmtime(&t));
+		printf("'%s': %ld == %s\n", smpp_time_tests[i], t, buf);
+	}
+
+	if (orig_tz)
+		setenv("TZ", orig_tz, 1);
+	else
+		unsetenv("TZ");
+
+}
+
 static const struct log_info_cat smpp_mirror_default_categories[] = {
 	[DSMPP] = {
 		.name = "DSMPP",
@@ -85,5 +121,7 @@
 	log_set_print_category_hex(osmo_stderr_target, 0);
 
 	test_coding_scheme();
+	test_smpp_parse_time_format();
+
 	return EXIT_SUCCESS;
 }