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 | |
| 21 | |
Neels Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 22 | #include <osmocom/ranap/iu_helpers.h> |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 23 | #include "asn1helpers.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #define ASSERT(x) assert(x) |
| 27 | |
| 28 | #include <osmocom/core/utils.h> |
| 29 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 30 | int asn1_xer_print = 0; |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 31 | void *talloc_asn1_ctx; |
| 32 | |
| 33 | /* use odd number of digits */ |
| 34 | const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 }; |
| 35 | const char imsi_decoded[] = "012345678"; |
| 36 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 37 | void test_iu_helpers(void) |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 38 | { |
| 39 | char outstr[32]; |
| 40 | uint8_t outbuf[16]; |
| 41 | int rc; |
| 42 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 43 | printf("Testing Iu helper functions\n"); |
| 44 | |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 45 | printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded, |
| 46 | sizeof(imsi_encoded))); |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 47 | rc = ranap_bcd_decode(outstr, sizeof(outstr), imsi_encoded, |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 48 | sizeof(imsi_encoded)); |
| 49 | ASSERT(rc >= 0); |
| 50 | printf("decoded: %s\n", outstr); |
| 51 | ASSERT(!strcmp(outstr, imsi_decoded)); |
| 52 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 53 | rc = ranap_imsi_encode(outbuf, sizeof(outbuf), imsi_decoded); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 54 | ASSERT(rc >= 0); |
| 55 | printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc)); |
| 56 | ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded))); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 59 | const uint32_t val1 = 0xdeadbeef; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 60 | |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 61 | const OCTET_STRING_t text1 = { |
| 62 | .buf = "0123456789012345", |
| 63 | .size = 16, |
| 64 | }; |
| 65 | |
Daniel Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 66 | const OCTET_STRING_t text2 = { |
| 67 | .buf = "01234567890123456789012345678901234567890", |
| 68 | .size = 40, |
| 69 | }; |
| 70 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 71 | void test_asn1_helpers(void) |
| 72 | { |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 73 | int rc; |
| 74 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 75 | void *buffer; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 76 | BIT_STRING_t enc; |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 77 | uint32_t res, tmpval; |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 78 | char text[32]; |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 79 | |
| 80 | printf("Testing asn.1 helper functions\n"); |
| 81 | |
| 82 | printf("Encoding 0x%x to asn.1 bitstring\n", val1); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 83 | asn1_u32_to_bitstring(&enc, &tmpval, val1); |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 84 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 85 | ASSERT(enc.size == sizeof(uint32_t)); |
| 86 | ASSERT(enc.bits_unused == 0); |
| 87 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 88 | rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer); |
| 89 | printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc)); |
| 90 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 91 | res = asn1bitstr_to_u32(&enc); |
| 92 | |
| 93 | printf("Decoding back to uint32_t: 0x%x\n", res); |
| 94 | ASSERT(res == val1); |
| 95 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 96 | printf("Encoding %s to 24-bit asn.1 bitstring\n", osmo_hexdump_nospc(&val1, 3)); |
| 97 | asn1_u24_to_bitstring(&enc, &tmpval, val1); |
| 98 | |
| 99 | ASSERT(enc.size == 24/8); |
| 100 | ASSERT(enc.bits_unused == 0); |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 101 | |
Daniel Willmann | 9a12a4b | 2015-11-30 16:27:11 +0100 | [diff] [blame] | 102 | rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer); |
| 103 | printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc)); |
| 104 | |
Daniel Willmann | ec0e50e | 2015-11-23 15:48:59 +0100 | [diff] [blame] | 105 | rc = asn1_strncpy(text, &text1, sizeof(text)); |
| 106 | printf("Decoding string from asn.1: %s\n", text); |
| 107 | |
| 108 | ASSERT(rc == 16); |
| 109 | ASSERT(!strcmp(text, (char *)text1.buf)); |
| 110 | |
Daniel Willmann | 8ea918d | 2015-11-23 15:50:06 +0100 | [diff] [blame] | 111 | rc = asn1_strncpy(text, &text2, sizeof(text)); |
| 112 | printf("Decoding large string from asn1: %s\n", text); |
| 113 | ASSERT(rc == 31); |
| 114 | |
Daniel Willmann | 54a9a14 | 2015-11-23 14:01:25 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int main(int argc, char **argv) |
| 118 | { |
| 119 | test_iu_helpers(); |
| 120 | test_asn1_helpers(); |
Harald Welte | 35cbc11 | 2015-09-11 17:36:59 +0200 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |