Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* 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 Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 21 | #include "test_common.h" |
Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 22 | |
Neels Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 23 | #include <osmocom/ranap/iu_helpers.h> |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 24 | #include <osmocom/ranap/ranap_common.h> |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 25 | #include "asn1helpers.h" |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #define ASSERT(x) assert(x) |
| 29 | |
| 30 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm48.h> |
| 32 | |
| 33 | #include <osmocom/ranap/RANAP_LAI.h> |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 34 | |
Harald Welte | 7712808 | 2020-04-20 20:01:15 +0200 | [diff] [blame] | 35 | extern void *talloc_asn1_ctx; |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 36 | |
| 37 | /* use odd number of digits */ |
| 38 | const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 }; |
| 39 | const char imsi_decoded[] = "012345678"; |
| 40 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 41 | void test_iu_helpers(void) |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 42 | { |
| 43 | char outstr[32]; |
| 44 | uint8_t outbuf[16]; |
| 45 | int rc; |
| 46 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 47 | printf("Testing Iu helper functions\n"); |
| 48 | |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 49 | printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded, |
| 50 | sizeof(imsi_encoded))); |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 51 | rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded, |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 52 | sizeof(imsi_encoded)); |
| 53 | ASSERT(rc >= 0); |
| 54 | printf("decoded: %s\n", outstr); |
| 55 | ASSERT(!strcmp(outstr, imsi_decoded)); |
| 56 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 57 | rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 58 | ASSERT(rc >= 0); |
| 59 | printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc)); |
| 60 | ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded))); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 63 | const uint32_t val1 = 0xdeadbeef; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 64 | |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 65 | const OCTET_STRING_t text1 = { |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 66 | .buf = (uint8_t *) "0123456789012345", |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 67 | .size = 16, |
| 68 | }; |
| 69 | |
Daniel Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 70 | const OCTET_STRING_t text2 = { |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 71 | .buf = (uint8_t *) "01234567890123456789012345678901234567890", |
Daniel Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 72 | .size = 40, |
| 73 | }; |
| 74 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 75 | void test_asn1_helpers(void) |
| 76 | { |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 77 | int rc; |
| 78 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 79 | void *buffer; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 80 | BIT_STRING_t enc; |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 81 | uint32_t res, tmpval; |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 82 | char text[32]; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 83 | |
| 84 | printf("Testing asn.1 helper functions\n"); |
| 85 | |
| 86 | printf("Encoding 0x%x to asn.1 bitstring\n", val1); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 87 | asn1_u32_to_bitstring(&enc, &tmpval, val1); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 88 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 89 | ASSERT(enc.size == sizeof(uint32_t)); |
| 90 | ASSERT(enc.bits_unused == 0); |
| 91 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 92 | 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 Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 95 | res = asn1bitstr_to_u32(&enc); |
| 96 | |
| 97 | printf("Decoding back to uint32_t: 0x%x\n", res); |
| 98 | ASSERT(res == val1); |
| 99 | |
Vadim Yanitskiy | 81fd48d | 2020-04-07 21:05:31 +0700 | [diff] [blame] | 100 | printf("Encoding 0x%x to 24-bit asn.1 bitstring\n", val1); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 101 | asn1_u24_to_bitstring(&enc, &tmpval, val1); |
| 102 | |
| 103 | ASSERT(enc.size == 24/8); |
| 104 | ASSERT(enc.bits_unused == 0); |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 105 | |
Neels Hofmeyr | d9cb19a | 2017-11-18 18:47:55 +0100 | [diff] [blame] | 106 | talloc_free(buffer); |
| 107 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 108 | 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 Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 111 | 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 Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 117 | rc = asn1_strncpy(text, &text2, sizeof(text)); |
| 118 | printf("Decoding large string from asn1: %s\n", text); |
| 119 | ASSERT(rc == 31); |
| 120 | |
Neels Hofmeyr | d9cb19a | 2017-11-18 18:47:55 +0100 | [diff] [blame] | 121 | talloc_free(buffer); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 124 | void 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 Pedrol | 6f66feb | 2020-05-18 10:33:50 +0200 | [diff] [blame] | 132 | |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 133 | 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 Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 146 | printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 147 | lai.pLMNidentity.size)); |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 148 | printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 149 | 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 Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 165 | printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 166 | lai.pLMNidentity.size)); |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 167 | printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 168 | 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 Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 183 | printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 184 | lai.pLMNidentity.size)); |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 185 | printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 186 | 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 Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 197 | printf("PLMN-Id [ %s]", osmo_hexdump(lai.pLMNidentity.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 198 | lai.pLMNidentity.size)); |
Vadim Yanitskiy | 89062ae | 2020-04-07 21:01:12 +0700 | [diff] [blame] | 199 | printf(", LAC [ %s]\n", osmo_hexdump(lai.lAC.buf, |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 200 | 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 Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 207 | int main(int argc, char **argv) |
| 208 | { |
Pau Espin Pedrol | 4f35665 | 2020-05-18 10:34:43 +0200 | [diff] [blame] | 209 | asn1_xer_print = 0; |
| 210 | |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 211 | test_common_init(); |
| 212 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 213 | test_iu_helpers(); |
| 214 | test_asn1_helpers(); |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 215 | test_ranap_common(); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 216 | |
Neels Hofmeyr | d9cb19a | 2017-11-18 18:47:55 +0100 | [diff] [blame] | 217 | test_common_cleanup(); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 218 | return 0; |
| 219 | } |