blob: 60f3d3b3b5edae5e3e0da5692f93ef0f76ab44ee [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
21
Harald Welte35cbc112015-09-11 17:36:59 +020022#include "iu_helpers.h"
23#include "asn1helpers.h"
24
25#include <assert.h>
26#define ASSERT(x) assert(x)
27
28#include <osmocom/core/utils.h>
29
30void *talloc_asn1_ctx;
31
32/* use odd number of digits */
33const uint8_t imsi_encoded[] = { 0x10, 0x32, 0x54, 0x76, 0xF8 };
34const char imsi_decoded[] = "012345678";
35
Daniel Willmann54a9a142015-11-23 14:01:25 +010036void test_iu_helpers(void)
Harald Welte35cbc112015-09-11 17:36:59 +020037{
38 char outstr[32];
39 uint8_t outbuf[16];
40 int rc;
41
Daniel Willmann54a9a142015-11-23 14:01:25 +010042 printf("Testing Iu helper functions\n");
43
Harald Welte35cbc112015-09-11 17:36:59 +020044 printf("pre-encoded: %s\n", osmo_hexdump_nospc(imsi_encoded,
45 sizeof(imsi_encoded)));
46 rc = decode_iu_bcd(outstr, sizeof(outstr), imsi_encoded,
47 sizeof(imsi_encoded));
48 ASSERT(rc >= 0);
49 printf("decoded: %s\n", outstr);
50 ASSERT(!strcmp(outstr, imsi_decoded));
51
52 rc = encode_iu_imsi(outbuf, sizeof(outbuf), imsi_decoded);
53 ASSERT(rc >= 0);
54 printf("re-encoded: %s\n", osmo_hexdump_nospc(outbuf, rc));
55 ASSERT(!memcmp(outbuf, imsi_encoded, sizeof(imsi_encoded)));
Daniel Willmann54a9a142015-11-23 14:01:25 +010056}
57
Daniel Willmannb2548fb2015-11-30 16:24:57 +010058const uint32_t val1 = 0xdeadbeef;
Daniel Willmann54a9a142015-11-23 14:01:25 +010059
Daniel Willmannec0e50e2015-11-23 15:48:59 +010060const OCTET_STRING_t text1 = {
61 .buf = "0123456789012345",
62 .size = 16,
63};
64
Daniel Willmann8ea918d2015-11-23 15:50:06 +010065const OCTET_STRING_t text2 = {
66 .buf = "01234567890123456789012345678901234567890",
67 .size = 40,
68};
69
Daniel Willmann54a9a142015-11-23 14:01:25 +010070void test_asn1_helpers(void)
71{
Daniel Willmannec0e50e2015-11-23 15:48:59 +010072 int rc;
73
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010074 void *buffer;
Daniel Willmann54a9a142015-11-23 14:01:25 +010075 BIT_STRING_t enc;
Daniel Willmannb2548fb2015-11-30 16:24:57 +010076 uint32_t res, tmpval;
Daniel Willmannec0e50e2015-11-23 15:48:59 +010077 char text[32];
Daniel Willmann54a9a142015-11-23 14:01:25 +010078
79 printf("Testing asn.1 helper functions\n");
80
81 printf("Encoding 0x%x to asn.1 bitstring\n", val1);
Daniel Willmannb2548fb2015-11-30 16:24:57 +010082 asn1_u32_to_bitstring(&enc, &tmpval, val1);
Daniel Willmann54a9a142015-11-23 14:01:25 +010083
Daniel Willmann54a9a142015-11-23 14:01:25 +010084 ASSERT(enc.size == sizeof(uint32_t));
85 ASSERT(enc.bits_unused == 0);
86
Daniel Willmann9a12a4b2015-11-30 16:27:11 +010087 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
88 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
89
Daniel Willmann54a9a142015-11-23 14:01:25 +010090 res = asn1bitstr_to_u32(&enc);
91
92 printf("Decoding back to uint32_t: 0x%x\n", res);
93 ASSERT(res == val1);
94
Daniel Willmannb2548fb2015-11-30 16:24:57 +010095 printf("Encoding %s to 24-bit asn.1 bitstring\n", osmo_hexdump_nospc(&val1, 3));
96 asn1_u24_to_bitstring(&enc, &tmpval, val1);
97
98 ASSERT(enc.size == 24/8);
99 ASSERT(enc.bits_unused == 0);
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100100
Daniel Willmann9a12a4b2015-11-30 16:27:11 +0100101 rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
102 printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
103
Daniel Willmannec0e50e2015-11-23 15:48:59 +0100104 rc = asn1_strncpy(text, &text1, sizeof(text));
105 printf("Decoding string from asn.1: %s\n", text);
106
107 ASSERT(rc == 16);
108 ASSERT(!strcmp(text, (char *)text1.buf));
109
Daniel Willmann8ea918d2015-11-23 15:50:06 +0100110 rc = asn1_strncpy(text, &text2, sizeof(text));
111 printf("Decoding large string from asn1: %s\n", text);
112 ASSERT(rc == 31);
113
Daniel Willmann54a9a142015-11-23 14:01:25 +0100114}
115
116int main(int argc, char **argv)
117{
118 test_iu_helpers();
119 test_asn1_helpers();
Harald Welte35cbc112015-09-11 17:36:59 +0200120
121 return 0;
122}