blob: 08236fa47f8b6bebcc86779420ce33e6a0610fa5 [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 Welte056984f2016-01-03 16:31:31 +010035int asn1_xer_print = 0;
Harald Welte35cbc112015-09-11 17:36:59 +020036void *talloc_asn1_ctx;
37
38/* use odd number of digits */
39const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 };
40const char imsi_decoded[] = "012345678";
41
Daniel Willmann54a9a142015-11-23 14:01:25 +010042void test_iu_helpers(void)
Harald Welte35cbc112015-09-11 17:36:59 +020043{
44 char outstr[32];
45 uint8_t outbuf[16];
46 int rc;
47
Daniel Willmann54a9a142015-11-23 14:01:25 +010048 printf("Testing Iu helper functions\n");
49
Harald Welte35cbc112015-09-11 17:36:59 +020050 printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded,
51 sizeof(imsi_encoded)));
Harald Welte056984f2016-01-03 16:31:31 +010052 rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded,
Harald Welte35cbc112015-09-11 17:36:59 +020053 sizeof(imsi_encoded));
54 ASSERT(rc >= 0);
55 printf("decoded: %s\n", outstr);
56 ASSERT(!strcmp(outstr, imsi_decoded));
57
Harald Welte056984f2016-01-03 16:31:31 +010058 rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded);
Harald Welte35cbc112015-09-11 17:36:59 +020059 ASSERT(rc >= 0);
60 printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc));
61 ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded)));
Daniel Willmann54a9a142015-11-23 14:01:25 +010062}
63
Daniel Willmannb2548fb2015-11-30 16:24:57 +010064const uint32_t val1 = 0xdeadbeef;
Daniel Willmann54a9a142015-11-23 14:01:25 +010065
Daniel Willmannec0e50e2015-11-23 15:48:59 +010066const OCTET_STRING_t text1 = {
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +070067 .buf = (uint8_t *) "0123456789012345",
Daniel Willmannec0e50e2015-11-23 15:48:59 +010068 .size = 16,
69};
70
Daniel Willmann8ea918d2015-11-23 15:50:06 +010071const OCTET_STRING_t text2 = {
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +070072 .buf = (uint8_t *) "01234567890123456789012345678901234567890",
Daniel Willmann8ea918d2015-11-23 15:50:06 +010073 .size = 40,
74};
75
Daniel Willmann54a9a142015-11-23 14:01:25 +010076void test_asn1_helpers(void)
77{
Daniel Willmannec0e50e2015-11-23 15:48:59 +010078 int rc;
79
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010080 void *buffer;
Daniel Willmann54a9a142015-11-23 14:01:25 +010081 BIT_STRING_t enc;
Daniel Willmannb2548fb2015-11-30 16:24:57 +010082 uint32_t res, tmpval;
Daniel Willmannec0e50e2015-11-23 15:48:59 +010083 char text[32];
Daniel Willmann54a9a142015-11-23 14:01:25 +010084
85 printf("Testing asn.1 helper functions\n");
86
87 printf("Encoding 0x%x to asn.1 bitstring\n", val1);
Daniel Willmannb2548fb2015-11-30 16:24:57 +010088 asn1_u32_to_bitstring(&enc, &tmpval, val1);
Daniel Willmann54a9a142015-11-23 14:01:25 +010089
Daniel Willmann54a9a142015-11-23 14:01:25 +010090 ASSERT(enc.size == sizeof(uint32_t));
91 ASSERT(enc.bits_unused == 0);
92
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010093 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
94 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
95
Daniel Willmann54a9a142015-11-23 14:01:25 +010096 res = asn1bitstr_to_u32(&enc);
97
98 printf("Decoding back to uint32_t: 0x%x\n", res);
99 ASSERT(res == val1);
100
Vadim Yanitskiy81fd48d2020-04-07 21:05:31 +0700101 printf("Encoding 0x%x to 24-bit asn.1 bitstring\n", val1);
Daniel Willmannb2548fb2015-11-30 16:24:57 +0100102 asn1_u24_to_bitstring(&enc, &tmpval, val1);
103
104 ASSERT(enc.size == 24/8);
105 ASSERT(enc.bits_unused == 0);
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100106
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100107 talloc_free(buffer);
108
Daniel Willmann9a12a4b2015-11-30 16:27:11 +0100109 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
110 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
111
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100112 rc = asn1_strncpy(text, &text1, sizeof(text));
113 printf("Decoding string from asn.1: %s\n", text);
114
115 ASSERT(rc == 16);
116 ASSERT(!strcmp(text, (char *)text1.buf));
117
Daniel Willmann8ea918d2015-11-23 15:50:06 +0100118 rc = asn1_strncpy(text, &text2, sizeof(text));
119 printf("Decoding large string from asn1: %s\n", text);
120 ASSERT(rc == 31);
121
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100122 talloc_free(buffer);
Daniel Willmann54a9a142015-11-23 14:01:25 +0100123}
124
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200125void test_ranap_common(void)
126{
127 uint8_t plmnid_buf[] = { 0x21, 0xf3, 0x54 };
128 uint8_t lac_buf[] = { 0xab, 0xcd };
129
130 struct gprs_ra_id ra_id = {0};
131
132 int rc;
133
134 RANAP_LAI_t lai = {
135 .pLMNidentity = {
136 .buf = plmnid_buf,
137 .size = 3
138 },
139 .lAC = {
140 .buf = lac_buf,
141 .size = 2
142 }
143 };
144
145 printf("Testing ranap common functions\n");
146
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700147 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200148 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700149 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200150 lai.lAC.size));
151
152 rc = ranap_parse_lai(&ra_id, &lai);
153 printf(" rc == %d\n", rc);
154 OSMO_ASSERT(rc == 0);
155 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
156 OSMO_ASSERT(ra_id.mcc == 123);
157 OSMO_ASSERT(ra_id.mnc == 45);
158 printf(" lac == 0x%x\n", ra_id.lac);
159 OSMO_ASSERT(ra_id.lac == 0xabcd);
160
161
162 /* three digit MNC */
163 uint8_t plmnid_buf_mnc3[] = { 0x21, 0x43, 0x65 };
164 lai.pLMNidentity.buf = plmnid_buf_mnc3;
165
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700166 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200167 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700168 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200169 lai.lAC.size));
170
171 rc = ranap_parse_lai(&ra_id, &lai);
172 printf(" rc == %d\n", rc);
173 OSMO_ASSERT(rc == 0);
174 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
175 OSMO_ASSERT(ra_id.mcc == 123);
176 OSMO_ASSERT(ra_id.mnc == 456);
177 printf(" lac == 0x%x\n", ra_id.lac);
178 OSMO_ASSERT(ra_id.lac == 0xabcd);
179
180
181 /* wrong PLMN-Id size */
182 lai.pLMNidentity.size = 2;
183
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700184 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200185 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700186 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200187 lai.lAC.size));
188
189 rc = ranap_parse_lai(&ra_id, &lai);
190 printf(" rc == %d\n", rc);
191 OSMO_ASSERT(rc == -1);
192
193
194 /* wrong LAC size */
195 lai.pLMNidentity.size = 3;
196 lai.lAC.size = 1;
197
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700198 printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200199 lai.pLMNidentity.size));
Vadim Yanitskiy89062ae2020-04-07 21:01:12 +0700200 printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf,
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200201 lai.lAC.size));
202
203 rc = ranap_parse_lai(&ra_id, &lai);
204 printf(" rc == %d\n", rc);
205 OSMO_ASSERT(rc == -1);
206}
207
Daniel Willmann54a9a142015-11-23 14:01:25 +0100208int main(int argc, char **argv)
209{
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200210 test_common_init();
211
Daniel Willmann54a9a142015-11-23 14:01:25 +0100212 test_iu_helpers();
213 test_asn1_helpers();
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200214 test_ranap_common();
Harald Welte35cbc112015-09-11 17:36:59 +0200215
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100216 test_common_cleanup();
Harald Welte35cbc112015-09-11 17:36:59 +0200217 return 0;
218}