blob: cfe2140e300133beb1e9c4c5fd6149e675ab559f [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* some humble start of unit testing */
2
3/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Neels Hofmeyrd8013d12016-04-06 13:56:02 +020021#include "test_common.h"
Harald Welte77847ad2015-10-06 22:07:04 +020022
Neels Hofmeyr96979af2016-01-05 15:19:44 +010023#include <osmocom/ranap/iu_helpers.h>
Neels Hofmeyrd8013d12016-04-06 13:56:02 +020024#include <osmocom/ranap/ranap_common.h>
Harald Welte35cbc112015-09-11 17:36:59 +020025#include "asn1helpers.h"
26
27#include <assert.h>
28#define ASSERT(x) assert(x)
29
30#include <osmocom/core/utils.h>
Neels Hofmeyrd8013d12016-04-06 13:56:02 +020031#include <osmocom/gsm/gsm48.h>
32
33#include <osmocom/ranap/RANAP_LAI.h>
Harald Welte35cbc112015-09-11 17:36:59 +020034
Harald Welte77128082020-04-20 20:01:15 +020035extern void *talloc_asn1_ctx;
Harald Welte35cbc112015-09-11 17:36:59 +020036
37/* use odd number of digits */
38const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 };
39const char imsi_decoded[] = "012345678";
40
Daniel Willmann54a9a142015-11-23 14:01:25 +010041void test_iu_helpers(void)
Harald Welte35cbc112015-09-11 17:36:59 +020042{
43 char outstr[32];
44 uint8_t outbuf[16];
45 int rc;
46
Daniel Willmann54a9a142015-11-23 14:01:25 +010047 printf("Testing Iu helper functions\n");
48
Harald Welte35cbc112015-09-11 17:36:59 +020049 printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded,
50 sizeof(imsi_encoded)));
Harald Welte056984f2016-01-03 16:31:31 +010051 rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded,
Harald Welte35cbc112015-09-11 17:36:59 +020052 sizeof(imsi_encoded));
53 ASSERT(rc >= 0);
54 printf("decoded: %s\n", outstr);
55 ASSERT(!strcmp(outstr, imsi_decoded));
56
Harald Welte056984f2016-01-03 16:31:31 +010057 rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded);
Harald Welte35cbc112015-09-11 17:36:59 +020058 ASSERT(rc >= 0);
59 printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc));
60 ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded)));
Daniel Willmann54a9a142015-11-23 14:01:25 +010061}
62
Daniel Willmannb2548fb2015-11-30 16:24:57 +010063const uint32_t val1 = 0xdeadbeef;
Daniel Willmann54a9a142015-11-23 14:01:25 +010064
Daniel Willmannec0e50e2015-11-23 15:48:59 +010065const OCTET_STRING_t text1 = {
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +070066 .buf = (uint8_t *) "0123456789012345",
Daniel Willmannec0e50e2015-11-23 15:48:59 +010067 .size = 16,
68};
69
Daniel Willmann8ea918d2015-11-23 15:50:06 +010070const OCTET_STRING_t text2 = {
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +070071 .buf = (uint8_t *) "01234567890123456789012345678901234567890",
Daniel Willmann8ea918d2015-11-23 15:50:06 +010072 .size = 40,
73};
74
Daniel Willmann54a9a142015-11-23 14:01:25 +010075void test_asn1_helpers(void)
76{
Daniel Willmannec0e50e2015-11-23 15:48:59 +010077 int rc;
78
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010079 void *buffer;
Daniel Willmann54a9a142015-11-23 14:01:25 +010080 BIT_STRING_t enc;
Daniel Willmannb2548fb2015-11-30 16:24:57 +010081 uint32_t res, tmpval;
Daniel Willmannec0e50e2015-11-23 15:48:59 +010082 char text[32];
Daniel Willmann54a9a142015-11-23 14:01:25 +010083
84 printf("Testing asn.1 helper functions\n");
85
86 printf("Encoding 0x%x to asn.1 bitstring\n", val1);
Daniel Willmannb2548fb2015-11-30 16:24:57 +010087 asn1_u32_to_bitstring(&enc, &tmpval, val1);
Daniel Willmann54a9a142015-11-23 14:01:25 +010088
Daniel Willmann54a9a142015-11-23 14:01:25 +010089 ASSERT(enc.size == sizeof(uint32_t));
90 ASSERT(enc.bits_unused == 0);
91
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010092 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
93 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
94
Daniel Willmann54a9a142015-11-23 14:01:25 +010095 res = asn1bitstr_to_u32(&enc);
96
97 printf("Decoding back to uint32_t: 0x%x\n", res);
98 ASSERT(res == val1);
99
Vadim Yanitskiy81fd48d2020-04-07 21:05:31 +0700100 printf("Encoding 0x%x to 24-bit asn.1 bitstring\n", val1);
Daniel Willmannb2548fb2015-11-30 16:24:57 +0100101 asn1_u24_to_bitstring(&enc, &tmpval, val1);
102
103 ASSERT(enc.size == 24/8);
104 ASSERT(enc.bits_unused == 0);
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100105
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100106 talloc_free(buffer);
107
Daniel Willmann9a12a4b2015-11-30 16:27:11 +0100108 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
109 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
110
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100111 rc = asn1_strncpy(text, &text1, sizeof(text));
112 printf("Decoding string from asn.1: %s\n", text);
113
114 ASSERT(rc == 16);
115 ASSERT(!strcmp(text, (char *)text1.buf));
116
Daniel Willmann8ea918d2015-11-23 15:50:06 +0100117 rc = asn1_strncpy(text, &text2, sizeof(text));
118 printf("Decoding large string from asn1: %s\n", text);
119 ASSERT(rc == 31);
120
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100121 talloc_free(buffer);
Daniel Willmann54a9a142015-11-23 14:01:25 +0100122}
123
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200124void test_ranap_common(void)
125{
126 uint8_t plmnid_buf[] = { 0x21, 0xf3, 0x54 };
127 uint8_t lac_buf[] = { 0xab, 0xcd };
128
129 struct gprs_ra_id ra_id = {0};
130
131 int rc;
Pau Espin Pedrol6f66feb2020-05-18 10:33:50 +0200132
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200133 RANAP_LAI_t lai = {
134 .pLMNidentity = {
135 .buf = plmnid_buf,
136 .size = 3
137 },
138 .lAC = {
139 .buf = lac_buf,
140 .size = 2
141 }
142 };
143
144 printf("Testing ranap common functions\n");
145
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700146 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200147 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700148 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200149 lai.lAC.size));
150
151 rc = ranap_parse_lai(&ra_id, &lai);
152 printf(" rc == %d\n", rc);
153 OSMO_ASSERT(rc == 0);
154 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
155 OSMO_ASSERT(ra_id.mcc == 123);
156 OSMO_ASSERT(ra_id.mnc == 45);
157 printf(" lac == 0x%x\n", ra_id.lac);
158 OSMO_ASSERT(ra_id.lac == 0xabcd);
159
160
161 /* three digit MNC */
162 uint8_t plmnid_buf_mnc3[] = { 0x21, 0x43, 0x65 };
163 lai.pLMNidentity.buf = plmnid_buf_mnc3;
164
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700165 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200166 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700167 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200168 lai.lAC.size));
169
170 rc = ranap_parse_lai(&ra_id, &lai);
171 printf(" rc == %d\n", rc);
172 OSMO_ASSERT(rc == 0);
173 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
174 OSMO_ASSERT(ra_id.mcc == 123);
175 OSMO_ASSERT(ra_id.mnc == 456);
176 printf(" lac == 0x%x\n", ra_id.lac);
177 OSMO_ASSERT(ra_id.lac == 0xabcd);
178
179
180 /* wrong PLMN-Id size */
181 lai.pLMNidentity.size = 2;
182
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700183 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200184 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700185 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200186 lai.lAC.size));
187
188 rc = ranap_parse_lai(&ra_id, &lai);
189 printf(" rc == %d\n", rc);
190 OSMO_ASSERT(rc == -1);
191
192
193 /* wrong LAC size */
194 lai.pLMNidentity.size = 3;
195 lai.lAC.size = 1;
196
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700197 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200198 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700199 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200200 lai.lAC.size));
201
202 rc = ranap_parse_lai(&ra_id, &lai);
203 printf(" rc == %d\n", rc);
204 OSMO_ASSERT(rc == -1);
205}
206
Daniel Willmann54a9a142015-11-23 14:01:25 +0100207int main(int argc, char **argv)
208{
Pau Espin Pedrol4f356652020-05-18 10:34:43 +0200209 asn1_xer_print = 0;
210
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200211 test_common_init();
212
Daniel Willmann54a9a142015-11-23 14:01:25 +0100213 test_iu_helpers();
214 test_asn1_helpers();
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200215 test_ranap_common();
Harald Welte35cbc112015-09-11 17:36:59 +0200216
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100217 test_common_cleanup();
Harald Welte35cbc112015-09-11 17:36:59 +0200218 return 0;
219}