Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 1 | /* Test de-/encoding of HBNAP messages */ |
| 2 | |
| 3 | /* (C) 2015 by Daniel Willmann <dwillmann@sysmocom.de> |
| 4 | * (C) 2015 by sysmocom s.f.m.c GmbH |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "iu_helpers.h" |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 23 | #include "asn1helpers.h" |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 24 | |
| 25 | #include "hnbap_common.h" |
| 26 | #include "hnbap_ies_defs.h" |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #define ASSERT(x) assert(x) |
| 30 | |
| 31 | #include <osmocom/core/utils.h> |
| 32 | #include <osmocom/core/logging.h> |
| 33 | |
| 34 | void *talloc_asn1_ctx; |
| 35 | |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 36 | static const unsigned char hnbap_reg_req[] = { |
| 37 | 0x00, 0x01, |
| 38 | 0x00, 0x57, 0x00, 0x00, 0x07, 0x00, 0x03, 0x00, |
| 39 | 0x15, 0x04, 0x80, 0x31, 0x30, 0x30, 0x30, 0x35, |
| 40 | 0x42, 0x39, 0x2d, 0x30, 0x30, 0x31, 0x30, 0x39, |
| 41 | 0x34, 0x32, 0x30, 0x35, 0x30, 0x40, 0x00, 0x08, |
| 42 | 0x00, 0x17, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 43 | 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 44 | 0x00, 0x11, 0x00, 0x05, 0x00, 0xc0, 0xa8, 0x00, |
| 45 | 0x32, 0x00, 0x09, 0x00, 0x03, 0x09, 0xf1, 0x99, |
| 46 | 0x00, 0x0b, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, |
| 47 | 0x00, 0x06, 0x00, 0x02, 0x40, 0x20, 0x00, 0x07, |
| 48 | 0x00, 0x01, 0x64, 0x00, 0x0a, 0x00, 0x02, 0x00, |
| 49 | 0x01 |
| 50 | }; |
| 51 | |
| 52 | static const unsigned char hnbap_reg_acc[] = { |
| 53 | 0x20, 0x01, |
| 54 | 0x00, 0x09, 0x00, 0x00, 0x01, 0x00, 0x0e, 0x00, |
| 55 | 0x02, 0x00, 0x00 |
| 56 | }; |
| 57 | |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 58 | static const unsigned char hnbap_ue_reg_req[] = { |
| 59 | 0x00, 0x03, |
| 60 | 0x00, 0x1a, 0x00, 0x00, 0x03, 0x00, 0x05, 0x00, |
| 61 | 0x09, 0x0a, 0x62, 0x02, 0x11, 0x32, 0x54, 0x76, |
| 62 | 0x98, 0xf0, 0x00, 0x0c, 0x40, 0x01, 0x40, 0x00, |
| 63 | 0x0d, 0x00, 0x01, 0x0d |
| 64 | }; |
| 65 | |
| 66 | static const unsigned char hnbap_ue_reg_acc[] = { |
| 67 | 0x20, 0x03, |
| 68 | 0x00, 0x17, 0x00, 0x00, 0x02, 0x00, 0x05, 0x00, |
| 69 | 0x09, 0x0a, 0x62, 0x02, 0x11, 0x32, 0x54, 0x76, |
| 70 | 0x98, 0xf0, 0x00, 0x04, 0x00, 0x03, 0x17, 0x00, |
| 71 | 0x00 |
| 72 | }; |
| 73 | |
| 74 | void test_asn1_decoding(void) |
| 75 | { |
| 76 | int rc; |
| 77 | |
| 78 | HNBAP_PDU_t _pdu, *pdu = &_pdu; |
| 79 | InitiatingMessage_t *im; |
Daniel Willmann | 1a86955 | 2015-11-24 18:11:03 +0100 | [diff] [blame] | 80 | SuccessfulOutcome_t *so; |
Daniel Willmann | 23c86e3 | 2015-11-30 16:03:36 +0100 | [diff] [blame] | 81 | UERegisterRequestIEs_t ue_req_ies; |
| 82 | UERegisterAcceptIEs_t ue_acc_ies; |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 83 | HNBRegisterRequestIEs_t hnb_ies; |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 84 | |
| 85 | char imsi[16]; |
| 86 | |
| 87 | asn_dec_rval_t dec_ret; |
| 88 | |
| 89 | memset(pdu, 0, sizeof(*pdu)); |
| 90 | printf("Testing asn.1 HNBAP decoding\n"); |
| 91 | |
| 92 | dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 93 | hnbap_reg_req, sizeof(hnbap_reg_req), 0, 0); |
| 94 | |
| 95 | |
| 96 | ASSERT(dec_ret.code == RC_OK); |
| 97 | ASSERT(pdu->present == HNBAP_PDU_PR_initiatingMessage); |
| 98 | |
| 99 | im = &pdu->choice.initiatingMessage; |
| 100 | |
| 101 | ASSERT(im->procedureCode == ProcedureCode_id_HNBRegister); |
| 102 | |
| 103 | rc = hnbap_decode_hnbregisterrequesties(&hnb_ies, &im->value); |
| 104 | ASSERT(rc >= 0); |
| 105 | |
| 106 | uint16_t lac, sac; |
| 107 | uint8_t rac; |
| 108 | uint32_t cid; |
| 109 | |
| 110 | lac = asn1str_to_u16(&hnb_ies.lac); |
| 111 | sac = asn1str_to_u16(&hnb_ies.sac); |
| 112 | rac = asn1str_to_u8(&hnb_ies.rac); |
Daniel Willmann | e295643 | 2015-11-30 16:05:51 +0100 | [diff] [blame] | 113 | cid = asn1bitstr_to_u32(&hnb_ies.cellIdentity); |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 114 | |
| 115 | ASSERT(lac == 0x4020); |
| 116 | ASSERT(sac == 0x0001); |
| 117 | ASSERT(rac == 0x64); |
Daniel Willmann | e295643 | 2015-11-30 16:05:51 +0100 | [diff] [blame] | 118 | ASSERT(cid == 0x0000000); |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 119 | |
Daniel Willmann | e295643 | 2015-11-30 16:05:51 +0100 | [diff] [blame] | 120 | ASSERT(!strcmp((char *) hnb_ies.hnB_Identity.hNB_Identity_Info.buf, "10005B9-0010942050@")); |
| 121 | |
| 122 | printf("HNBAP register request for HNB %s\n", (char *) hnb_ies.hnB_Identity.hNB_Identity_Info.buf); |
Daniel Willmann | ea4c088 | 2015-11-27 16:14:09 +0100 | [diff] [blame] | 123 | dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 124 | hnbap_ue_reg_req, sizeof(hnbap_ue_reg_req), 0, 0); |
| 125 | |
| 126 | |
| 127 | ASSERT(dec_ret.code == RC_OK); |
| 128 | ASSERT(pdu->present == HNBAP_PDU_PR_initiatingMessage); |
| 129 | |
| 130 | im = &pdu->choice.initiatingMessage; |
| 131 | ASSERT(im->procedureCode == ProcedureCode_id_UERegister); |
| 132 | |
Daniel Willmann | 23c86e3 | 2015-11-30 16:03:36 +0100 | [diff] [blame] | 133 | rc = hnbap_decode_ueregisterrequesties(&ue_req_ies, &im->value); |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 134 | ASSERT(rc >= 0); |
| 135 | |
Daniel Willmann | 23c86e3 | 2015-11-30 16:03:36 +0100 | [diff] [blame] | 136 | ASSERT(ue_req_ies.uE_Identity.present == UE_Identity_PR_iMSI); |
| 137 | decode_iu_bcd(imsi, sizeof(imsi), ue_req_ies.uE_Identity.choice.iMSI.buf, |
| 138 | ue_req_ies.uE_Identity.choice.iMSI.size); |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 139 | |
Daniel Willmann | 1a86955 | 2015-11-24 18:11:03 +0100 | [diff] [blame] | 140 | printf("HNBAP UE Register request from IMSI %s\n", imsi); |
| 141 | |
| 142 | memset(pdu, 0, sizeof(*pdu)); |
| 143 | dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, |
| 144 | hnbap_ue_reg_acc, sizeof(hnbap_ue_reg_acc), 0, 0); |
| 145 | |
| 146 | |
| 147 | ASSERT(dec_ret.code == RC_OK); |
| 148 | ASSERT(pdu->present == HNBAP_PDU_PR_successfulOutcome); |
| 149 | |
| 150 | so = &pdu->choice.successfulOutcome; |
| 151 | ASSERT(so->procedureCode == ProcedureCode_id_UERegister); |
| 152 | |
Daniel Willmann | 23c86e3 | 2015-11-30 16:03:36 +0100 | [diff] [blame] | 153 | rc = hnbap_decode_ueregisteraccepties(&ue_acc_ies, &so->value); |
Daniel Willmann | 1a86955 | 2015-11-24 18:11:03 +0100 | [diff] [blame] | 154 | ASSERT(rc >= 0); |
| 155 | |
Daniel Willmann | 23c86e3 | 2015-11-30 16:03:36 +0100 | [diff] [blame] | 156 | ASSERT(ue_acc_ies.uE_Identity.present == UE_Identity_PR_iMSI); |
| 157 | decode_iu_bcd(imsi, sizeof(imsi), ue_acc_ies.uE_Identity.choice.iMSI.buf, |
| 158 | ue_acc_ies.uE_Identity.choice.iMSI.size); |
Daniel Willmann | 1a86955 | 2015-11-24 18:11:03 +0100 | [diff] [blame] | 159 | |
| 160 | printf("HNBAP UE Register accept to IMSI %s\n", imsi); |
| 161 | |
Daniel Willmann | 39e643a | 2015-11-24 18:03:29 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | static const struct log_info osmo_log = { |
| 166 | .cat = NULL, |
| 167 | .num_cat = 0, |
| 168 | }; |
| 169 | |
| 170 | int main(int argc, char **argv) |
| 171 | { |
| 172 | int rc; |
| 173 | |
| 174 | rc = osmo_init_logging(&osmo_log); |
| 175 | if (rc < 0) { |
| 176 | printf("Error in init logging\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | test_asn1_decoding(); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |