blob: f218a79bcb58e05dcef6016390bf0acff15026d0 [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 = {
67 .buf = "0123456789012345",
68 .size = 16,
69};
70
Daniel Willmann8ea918d2015-11-23 15:50:06 +010071const OCTET_STRING_t text2 = {
72 .buf = "01234567890123456789012345678901234567890",
73 .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
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200101 printf("Encoding %s to 24-bit asn.1 bitstring\n",
102 osmo_hexdump_nospc((unsigned char*)&val1, 3));
Daniel Willmannb2548fb2015-11-30 16:24:57 +0100103 asn1_u24_to_bitstring(&enc, &tmpval, val1);
104
105 ASSERT(enc.size == 24/8);
106 ASSERT(enc.bits_unused == 0);
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100107
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100108 talloc_free(buffer);
109
Daniel Willmann9a12a4b2015-11-30 16:27:11 +0100110 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
111 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
112
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100113 rc = asn1_strncpy(text, &text1, sizeof(text));
114 printf("Decoding string from asn.1: %s\n", text);
115
116 ASSERT(rc == 16);
117 ASSERT(!strcmp(text, (char *)text1.buf));
118
Daniel Willmann8ea918d2015-11-23 15:50:06 +0100119 rc = asn1_strncpy(text, &text2, sizeof(text));
120 printf("Decoding large string from asn1: %s\n", text);
121 ASSERT(rc == 31);
122
Neels Hofmeyrd9cb19a2017-11-18 18:47:55 +0100123 talloc_free(buffer);
Daniel Willmann54a9a142015-11-23 14:01:25 +0100124}
125
Neels Hofmeyrd8013d12016-04-06 13:56:02 +0200126void test_ranap_common(void)
127{
128 uint8_t plmnid_buf[] = { 0x21, 0xf3, 0x54 };
129 uint8_t lac_buf[] = { 0xab, 0xcd };
130
131 struct gprs_ra_id ra_id = {0};
132
133 int rc;
134
135 RANAP_LAI_t lai = {
136 .pLMNidentity = {
137 .buf = plmnid_buf,
138 .size = 3
139 },
140 .lAC = {
141 .buf = lac_buf,
142 .size = 2
143 }
144 };
145
146 printf("Testing ranap common functions\n");
147
148 printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf,
149 lai.pLMNidentity.size));
150 printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf,
151 lai.lAC.size));
152
153 rc = ranap_parse_lai(&ra_id, &lai);
154 printf(" rc == %d\n", rc);
155 OSMO_ASSERT(rc == 0);
156 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
157 OSMO_ASSERT(ra_id.mcc == 123);
158 OSMO_ASSERT(ra_id.mnc == 45);
159 printf(" lac == 0x%x\n", ra_id.lac);
160 OSMO_ASSERT(ra_id.lac == 0xabcd);
161
162
163 /* three digit MNC */
164 uint8_t plmnid_buf_mnc3[] = { 0x21, 0x43, 0x65 };
165 lai.pLMNidentity.buf = plmnid_buf_mnc3;
166
167 printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf,
168 lai.pLMNidentity.size));
169 printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf,
170 lai.lAC.size));
171
172 rc = ranap_parse_lai(&ra_id, &lai);
173 printf(" rc == %d\n", rc);
174 OSMO_ASSERT(rc == 0);
175 printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc);
176 OSMO_ASSERT(ra_id.mcc == 123);
177 OSMO_ASSERT(ra_id.mnc == 456);
178 printf(" lac == 0x%x\n", ra_id.lac);
179 OSMO_ASSERT(ra_id.lac == 0xabcd);
180
181
182 /* wrong PLMN-Id size */
183 lai.pLMNidentity.size = 2;
184
185 printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf,
186 lai.pLMNidentity.size));
187 printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf,
188 lai.lAC.size));
189
190 rc = ranap_parse_lai(&ra_id, &lai);
191 printf(" rc == %d\n", rc);
192 OSMO_ASSERT(rc == -1);
193
194
195 /* wrong LAC size */
196 lai.pLMNidentity.size = 3;
197 lai.lAC.size = 1;
198
199 printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf,
200 lai.pLMNidentity.size));
201 printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf,
202 lai.lAC.size));
203
204 rc = ranap_parse_lai(&ra_id, &lai);
205 printf(" rc == %d\n", rc);
206 OSMO_ASSERT(rc == -1);
207}
208
Daniel Willmann54a9a142015-11-23 14:01:25 +0100209int main(int argc, char **argv)
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}