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 | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 35 | int asn1_xer_print = 0; |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 36 | void *talloc_asn1_ctx; |
| 37 | |
| 38 | /* use odd number of digits */ |
| 39 | const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 }; |
| 40 | const char imsi_decoded[] = "012345678"; |
| 41 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 42 | void test_iu_helpers(void) |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 43 | { |
| 44 | char outstr[32]; |
| 45 | uint8_t outbuf[16]; |
| 46 | int rc; |
| 47 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 48 | printf("Testing Iu helper functions\n"); |
| 49 | |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 50 | printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded, |
| 51 | sizeof(imsi_encoded))); |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 52 | rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded, |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 53 | sizeof(imsi_encoded)); |
| 54 | ASSERT(rc >= 0); |
| 55 | printf("decoded: %s\n", outstr); |
| 56 | ASSERT(!strcmp(outstr, imsi_decoded)); |
| 57 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 58 | rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 59 | ASSERT(rc >= 0); |
| 60 | printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc)); |
| 61 | ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded))); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 64 | const uint32_t val1 = 0xdeadbeef; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 65 | |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 66 | const OCTET_STRING_t text1 = { |
| 67 | .buf = "0123456789012345", |
| 68 | .size = 16, |
| 69 | }; |
| 70 | |
Daniel Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 71 | const OCTET_STRING_t text2 = { |
| 72 | .buf = "01234567890123456789012345678901234567890", |
| 73 | .size = 40, |
| 74 | }; |
| 75 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 76 | void test_asn1_helpers(void) |
| 77 | { |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 78 | int rc; |
| 79 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 80 | void *buffer; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 81 | BIT_STRING_t enc; |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 82 | uint32_t res, tmpval; |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 83 | char text[32]; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 84 | |
| 85 | printf("Testing asn.1 helper functions\n"); |
| 86 | |
| 87 | printf("Encoding 0x%x to asn.1 bitstring\n", val1); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 88 | asn1_u32_to_bitstring(&enc, &tmpval, val1); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 89 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 90 | ASSERT(enc.size == sizeof(uint32_t)); |
| 91 | ASSERT(enc.bits_unused == 0); |
| 92 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 93 | 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 Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 96 | res = asn1bitstr_to_u32(&enc); |
| 97 | |
| 98 | printf("Decoding back to uint32_t: 0x%x\n", res); |
| 99 | ASSERT(res == val1); |
| 100 | |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 101 | printf("Encoding %s to 24-bit asn.1 bitstring\n", |
| 102 | osmo_hexdump_nospc((unsigned char*)&val1, 3)); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 103 | asn1_u24_to_bitstring(&enc, &tmpval, val1); |
| 104 | |
| 105 | ASSERT(enc.size == 24/8); |
| 106 | ASSERT(enc.bits_unused == 0); |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 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 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 123 | void test_ranap_common(void) |
| 124 | { |
| 125 | uint8_t plmnid_buf[] = { 0x21, 0xf3, 0x54 }; |
| 126 | uint8_t lac_buf[] = { 0xab, 0xcd }; |
| 127 | |
| 128 | struct gprs_ra_id ra_id = {0}; |
| 129 | |
| 130 | int rc; |
| 131 | |
| 132 | RANAP_LAI_t lai = { |
| 133 | .pLMNidentity = { |
| 134 | .buf = plmnid_buf, |
| 135 | .size = 3 |
| 136 | }, |
| 137 | .lAC = { |
| 138 | .buf = lac_buf, |
| 139 | .size = 2 |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | printf("Testing ranap common functions\n"); |
| 144 | |
| 145 | printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf, |
| 146 | lai.pLMNidentity.size)); |
| 147 | printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf, |
| 148 | lai.lAC.size)); |
| 149 | |
| 150 | rc = ranap_parse_lai(&ra_id, &lai); |
| 151 | printf(" rc == %d\n", rc); |
| 152 | OSMO_ASSERT(rc == 0); |
| 153 | printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc); |
| 154 | OSMO_ASSERT(ra_id.mcc == 123); |
| 155 | OSMO_ASSERT(ra_id.mnc == 45); |
| 156 | printf(" lac == 0x%x\n", ra_id.lac); |
| 157 | OSMO_ASSERT(ra_id.lac == 0xabcd); |
| 158 | |
| 159 | |
| 160 | /* three digit MNC */ |
| 161 | uint8_t plmnid_buf_mnc3[] = { 0x21, 0x43, 0x65 }; |
| 162 | lai.pLMNidentity.buf = plmnid_buf_mnc3; |
| 163 | |
| 164 | printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf, |
| 165 | lai.pLMNidentity.size)); |
| 166 | printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf, |
| 167 | lai.lAC.size)); |
| 168 | |
| 169 | rc = ranap_parse_lai(&ra_id, &lai); |
| 170 | printf(" rc == %d\n", rc); |
| 171 | OSMO_ASSERT(rc == 0); |
| 172 | printf(" mcc == %d mnc == %d\n", ra_id.mcc, ra_id.mnc); |
| 173 | OSMO_ASSERT(ra_id.mcc == 123); |
| 174 | OSMO_ASSERT(ra_id.mnc == 456); |
| 175 | printf(" lac == 0x%x\n", ra_id.lac); |
| 176 | OSMO_ASSERT(ra_id.lac == 0xabcd); |
| 177 | |
| 178 | |
| 179 | /* wrong PLMN-Id size */ |
| 180 | lai.pLMNidentity.size = 2; |
| 181 | |
| 182 | printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf, |
| 183 | lai.pLMNidentity.size)); |
| 184 | printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf, |
| 185 | lai.lAC.size)); |
| 186 | |
| 187 | rc = ranap_parse_lai(&ra_id, &lai); |
| 188 | printf(" rc == %d\n", rc); |
| 189 | OSMO_ASSERT(rc == -1); |
| 190 | |
| 191 | |
| 192 | /* wrong LAC size */ |
| 193 | lai.pLMNidentity.size = 3; |
| 194 | lai.lAC.size = 1; |
| 195 | |
| 196 | printf("PLMN-Id [ %s]", osmo_hexdump((char*)lai.pLMNidentity.buf, |
| 197 | lai.pLMNidentity.size)); |
| 198 | printf(", LAC [ %s]\n", osmo_hexdump((char*)lai.lAC.buf, |
| 199 | lai.lAC.size)); |
| 200 | |
| 201 | rc = ranap_parse_lai(&ra_id, &lai); |
| 202 | printf(" rc == %d\n", rc); |
| 203 | OSMO_ASSERT(rc == -1); |
| 204 | } |
| 205 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 206 | int main(int argc, char **argv) |
| 207 | { |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 208 | test_common_init(); |
| 209 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 210 | test_iu_helpers(); |
| 211 | test_asn1_helpers(); |
Neels Hofmeyr | d8013d1 | 2016-04-06 13:56:02 +0200 | [diff] [blame] | 212 | test_ranap_common(); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |