From f98c741d584d3167b860ea5c77c7525a0ef7ee02 Mon Sep 17 00:00:00 2001
From: Alexander Chemeris <Alexander.Chemeris@gmail.com>
Date: Fri, 7 Mar 2014 20:55:21 +0100
Subject: [PATCH 3/6] sms_test: Introduce tests for SMS Validity time relative
 and absolute formats.

This tests demonstrate issues with the current code.
---
 tests/sms/sms_test.c  |   88 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/sms/sms_test.ok |   30 +++++++++++++++++
 2 files changed, 118 insertions(+)

diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c
index cd21e92..5ca5f59 100644
--- a/tests/sms/sms_test.c
+++ b/tests/sms/sms_test.c
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include <osmocom/gsm/protocol/gsm_03_40.h>
+#include <osmocom/gsm/protocol/gsm_04_11.h>
 
 #include <osmocom/gsm/gsm_utils.h>
 #include <osmocom/gsm/gsm0411_utils.h>
@@ -267,6 +268,92 @@ static void test_gen_oa(void)
 	printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
 }
 
+#define PRINT_VALIDITY_TIMES(expected, decoded) \
+{ \
+	char _validity_timestamp[1024]; \
+	time_t _temp_time; \
+	_temp_time = (expected); \
+	strftime(_validity_timestamp, sizeof(_validity_timestamp), \
+			 "%F %T", gmtime(&_temp_time)); \
+	printf("Expected: %s\n", _validity_timestamp); \
+	_temp_time = (decoded); \
+	strftime(_validity_timestamp, sizeof(_validity_timestamp), \
+			 "%F %T", gmtime(&_temp_time)); \
+	printf("Decoded:  %s\n", _validity_timestamp); \
+} \
+
+static void test_validity_period(void)
+{
+	int i;
+	time_t valid_until;
+
+	time_t vp_rel_default = SMS_DEFAULT_VALIDITY_PERIOD;
+
+	/* Relative data */
+	struct {
+		uint8_t vp[1];
+		time_t  decoded;
+	} vp_rel_test[] = {
+		{{5}, (5 + 1) * 5 * 60},
+		{{150}, (12*60 + (150-143) * 30) * 60},
+		{{180}, ((180 - 166) * 60 * 24) * 60},
+		{{250}, ((250 - 192) * 60 * 24 * 7) * 60}
+	};
+
+	/* Absolute data */
+	struct {
+		uint8_t vp[8];
+		time_t  decoded;
+	} vp_abs_test[] = {
+		/* 2013-05-15 12:24:36 UTC+0 
+		 * Basic check - no timezone offset, summer time, year after 2000 */
+		{{0x31, 0x50, 0x51, 0x21, 0x42, 0x63, 0x00|0x0, 0x0}, 1368620676},
+		/* 1984-05-15 12:24:36 UTC+0
+		 * Test year before 2000 */
+		{{0x48, 0x50, 0x51, 0x21, 0x42, 0x63, 0x00|0x0, 0x0}, 453471876},
+		/* 2013-05-15 12:24:36 UTC+4
+		 * Test positive timezone offset*/
+		{{0x31, 0x50, 0x51, 0x21, 0x42, 0x63, 0x00|0x61, 0x0}, 1368606276},
+		/* 2013-12-24 12:24:36 UTC
+		 * Test winter time */
+		{{0x31, 0x21, 0x42, 0x21, 0x42, 0x63, 0x00|0x0, 0x0}, 1387887876},
+		/* 2013-05-15 12:24:36 UTC-4
+		 * Test negative timezone offset */
+		{{0x31, 0x50, 0x51, 0x21, 0x42, 0x63, 0x80|0x61, 0x0}, 1368635076}
+	};
+
+	printf("\nTesting default validity time\n");
+	valid_until = gsm340_validity_period(0, GSM340_TP_VPF_NONE, NULL);
+	PRINT_VALIDITY_TIMES(vp_rel_default, valid_until);
+	OSMO_ASSERT(valid_until == SMS_DEFAULT_VALIDITY_PERIOD);
+
+	printf("\nTesting relative validity time\n");
+	for (i=0; i<ARRAY_SIZE(vp_rel_test); i++)
+	{
+		valid_until = gsm340_validity_period(0, GSM340_TP_VPF_RELATIVE, vp_rel_test[i].vp);
+		PRINT_VALIDITY_TIMES(vp_rel_test[i].decoded, valid_until);
+		OSMO_ASSERT(valid_until == vp_rel_test[i].decoded);
+	}
+
+	/* Non-zero 'now' value should accordingly shift the decoded value */
+	valid_until = gsm340_validity_period(100, GSM340_TP_VPF_RELATIVE, vp_rel_test[0].vp);
+	PRINT_VALIDITY_TIMES(vp_rel_test[0].decoded+100, valid_until);
+	OSMO_ASSERT(valid_until == vp_rel_test[0].decoded+100);
+
+	printf("\nTesting absolute validity time\n");
+	for (i=0; i<ARRAY_SIZE(vp_abs_test); i++)
+	{
+		valid_until = gsm340_validity_period(0, GSM340_TP_VPF_ABSOLUTE, vp_abs_test[i].vp);
+		PRINT_VALIDITY_TIMES(vp_abs_test[i].decoded, valid_until);
+		OSMO_ASSERT(valid_until == vp_abs_test[i].decoded);
+	}
+
+	/* Adding current time should not change returned value, as it's absolute */
+	valid_until = gsm340_validity_period(1000, GSM340_TP_VPF_ABSOLUTE, vp_abs_test[0].vp);
+	PRINT_VALIDITY_TIMES(vp_abs_test[0].decoded, valid_until);
+	OSMO_ASSERT(valid_until == vp_abs_test[0].decoded);
+}
+
 int main(int argc, char** argv)
 {
 	printf("SMS testing\n");
@@ -413,6 +500,7 @@ int main(int argc, char** argv)
 
 	test_octet_return();
 	test_gen_oa();
+	test_validity_period();
 
 	printf("OK\n");
 	return 0;
diff --git a/tests/sms/sms_test.ok b/tests/sms/sms_test.ok
index fa536ea..a7de5c2 100644
--- a/tests/sms/sms_test.ok
+++ b/tests/sms/sms_test.ok
@@ -26,4 +26,34 @@ Result: len(12) data(14 a1 21 43 65 87 09 21 43 65 87 19 )
 Result: len(2) data(00 91 )
 Result: len(9) data(0e d0 4f 78 d9 2d 9c 0e 01 )
 Result: len(12) data(14 d0 4f 78 d9 2d 9c 0e c3 e2 31 19 )
+
+Testing default validity time
+Expected: 1970-01-03 00:00:00
+Decoded:  1970-01-03 00:00:00
+
+Testing relative validity time
+Expected: 1970-01-01 00:30:00
+Decoded:  1970-01-01 00:30:00
+Expected: 1970-01-01 15:30:00
+Decoded:  1970-01-01 15:30:00
+Expected: 1970-01-15 00:00:00
+Decoded:  1970-01-15 00:00:00
+Expected: 1971-02-11 00:00:00
+Decoded:  1971-02-11 00:00:00
+Expected: 1970-01-01 00:31:40
+Decoded:  1970-01-01 00:31:40
+
+Testing absolute validity time
+Expected: 2013-05-15 12:24:36
+Decoded:  2013-05-15 12:24:36
+Expected: 1984-05-15 12:24:36
+Decoded:  1984-05-15 12:24:36
+Expected: 2013-05-15 08:24:36
+Decoded:  2013-05-15 08:24:36
+Expected: 2013-12-24 12:24:36
+Decoded:  2013-12-24 12:24:36
+Expected: 2013-05-15 16:24:36
+Decoded:  2013-05-15 16:24:36
+Expected: 2013-05-15 12:24:36
+Decoded:  2013-05-15 12:24:36
 OK
-- 
1.7.9.5

