tests/a5: Add a5 regression testing

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
diff --git a/configure.ac b/configure.ac
index 69b8ff7..450ccd3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -164,6 +164,7 @@
 	tests/ussd/Makefile
 	tests/smscb/Makefile
 	tests/bits/Makefile
+	tests/a5/Makefile
 	utils/Makefile
 	Doxyfile.core
 	Doxyfile.gsm
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4ab9773..5ff82e1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,5 @@
 if ENABLE_TESTS
-SUBDIRS = timer sms ussd smscb bits
+SUBDIRS = timer sms ussd smscb bits a5
 if ENABLE_MSGFILE
 SUBDIRS += msgfile
 endif
diff --git a/tests/a5/Makefile.am b/tests/a5/Makefile.am
new file mode 100644
index 0000000..e93a4cf
--- /dev/null
+++ b/tests/a5/Makefile.am
@@ -0,0 +1,5 @@
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+noinst_PROGRAMS = a5_test
+
+a5_test_SOURCES = a5_test.c
+a5_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la
diff --git a/tests/a5/a5_test.c b/tests/a5/a5_test.c
new file mode 100644
index 0000000..14436f1
--- /dev/null
+++ b/tests/a5/a5_test.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <osmocom/core/bits.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/gsm/a5.h>
+
+static const uint8_t key[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
+static const uint32_t fn = 123456;
+static const uint8_t dl[] = {
+	/* A5/0 */
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+	/* A5/1 */
+	0xcb, 0xa2, 0x55, 0x76, 0x17, 0x5d, 0x3b, 0x1c,
+	0x7b, 0x2f, 0x29, 0xa8, 0xc1, 0xb6, 0x00,
+
+	/* A5/2 */
+	0x45, 0x9c, 0x88, 0xc3, 0x82, 0xb7, 0xff, 0xb3,
+	0x98, 0xd2, 0xf9, 0x6e, 0x0f, 0x14, 0x80,
+};
+static const uint8_t ul[] = {
+	/* A5/0 */
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+
+	/* A5/1 */
+	0xd9, 0x03, 0x5e, 0x0f, 0x2a, 0xec, 0x13, 0x9a,
+	0x05, 0xd4, 0xa8, 0x7b, 0xb1, 0x64, 0x80,
+
+	/* A5/2 */
+	0xf0, 0x3a, 0xac, 0xde, 0xe3, 0x5b, 0x5e, 0x65,
+	0x80, 0xba, 0xab, 0xc0, 0x59, 0x26, 0x40,
+};
+
+static const char *
+binstr(ubit_t *d, int n)
+{
+	static char str[256];
+	int i;
+
+	for (i=0; i<n; i++)
+		str[i] = d[i] ? '1' : '0';
+
+	str[i] = '\0';
+
+	return str;
+}
+
+int main(int argc, char **argv)
+{
+	ubit_t exp[114];
+	ubit_t out[114];
+	int n, i;
+
+	for (n=0; n<3; n++) {
+		/* "Randomize" */
+		for (i=0; i<114; i++)
+			out[i] = i & 1;
+
+		/* DL */
+		osmo_pbit2ubit(exp, &dl[15*n], 114);
+
+		osmo_a5(n, key, fn, out, NULL);
+
+		printf("A5/%d - DL: %s", n, binstr(out, 114));
+
+		if (!memcmp(exp, out, 114))
+			printf(" => OK\n");
+		else {
+			printf(" => BAD\n");
+			printf(" Expected: %s", binstr(out, 114));
+			fprintf(stderr, "[!] A5/%d DL failed", n);
+			exit(1);
+		}
+
+		/* UL */
+		osmo_pbit2ubit(exp, &ul[15*n], 114);
+
+		osmo_a5(n, key, fn, NULL, out);
+
+		printf("A5/%d - UL: %s", n, binstr(out, 114));
+
+		if (!memcmp(exp, out, 114))
+			printf(" => OK\n");
+		else {
+			printf(" => BAD\n");
+			printf(" Expected: %s", binstr(out, 114));
+			fprintf(stderr, "[!] A5/%d UL failed", n);
+			exit(1);
+		}
+	}
+
+	return 0;
+}
diff --git a/tests/a5/a5_test.ok b/tests/a5/a5_test.ok
new file mode 100644
index 0000000..4497e14
--- /dev/null
+++ b/tests/a5/a5_test.ok
@@ -0,0 +1,6 @@
+A5/0 - DL: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => OK
+A5/0 - UL: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => OK
+A5/1 - DL: 110010111010001001010101011101100001011101011101001110110001110001111011001011110010100110101000110000011011011000 => OK
+A5/1 - UL: 110110010000001101011110000011110010101011101100000100111001101000000101110101001010100001111011101100010110010010 => OK
+A5/2 - DL: 010001011001110010001000110000111000001010110111111111111011001110011000110100101111100101101110000011110001010010 => OK
+A5/2 - UL: 111100000011101010101100110111101110001101011011010111100110010110000000101110101010101111000000010110010010011001 => OK
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 88c3dcb..e65ca7c 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -3,6 +3,12 @@
 
 
 # todo.. create one macro for it
+AT_SETUP([a5])
+AT_KEYWORDS([a5])
+cat $abs_srcdir/a5/a5_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/a5/bitrev_test], [], [expout])
+AT_CLEANUP
+
 AT_SETUP([bits])
 AT_KEYWORDS([bits])
 cat $abs_srcdir/bits/bitrev_test.ok > expout