blob: c0605c28413d64d287c33732a92918c19ce64335 [file] [log] [blame]
Maxf74cfd32017-03-24 20:16:33 +01001/*
2 * (C) 2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2017 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
Maxf74cfd32017-03-24 20:16:33 +01004 * 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 General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <osmocom/core/application.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/gsm/lapdm.h>
26#include <osmocom/gsm/rsl.h>
27#include <osmocom/gsm/protocol/gsm_12_21.h>
28
29#include <errno.h>
30#include <stdbool.h>
31#include <string.h>
32
33static struct log_info info = {};
34
35static const uint8_t simple_config[] = { 66, 18, 0, 3, 1, 2, 3, 19, 0, 3, 3, 4, 5 };
36
37static const uint8_t dual_config[] = {
38 66, 18, 0, 3, 1, 2, 3, 19, 0, 3, 3, 4, 5,
39 66, 18, 0, 3, 9, 7, 5, 19, 0, 3, 6, 7, 8,
40};
41
42static void test_simple_sw_config(void)
43{
44 struct abis_nm_sw_desc desc[1];
45 uint16_t len;
46 int rc;
47
48 rc = abis_nm_get_sw_conf(simple_config, ARRAY_SIZE(simple_config), &desc[0], ARRAY_SIZE(desc));
49 if (rc != 1) {
50 printf("%s(): FAILED to parse the File Id/File version: %d\n", __func__, rc);
51 abort();
52 }
53
54 len = abis_nm_sw_desc_len(&desc[0], true);
55 if (len != 13) {
56 printf("WRONG SIZE: %u\n", len);
57 abort();
58 }
59
60 printf("len: %u\n", len);
61 printf("file_id: %s\n", osmo_hexdump(desc[0].file_id, desc[0].file_id_len));
62 printf("file_ver: %s\n", osmo_hexdump(desc[0].file_version, desc[0].file_version_len));
63 printf("%s(): OK\n", __func__);
64}
65
66static void test_simple_sw_short(void)
67{
68 struct abis_nm_sw_desc desc[1];
69 int i;
70
71 for (i = 1; i < ARRAY_SIZE(simple_config); ++i) {
72 int rc = abis_nm_get_sw_conf(simple_config, ARRAY_SIZE(simple_config) - i, &desc[0], ARRAY_SIZE(desc));
73 if (rc >= 1) {
74 printf("SHOULD not have parsed: %d\n", rc);
75 abort();
76 }
77 }
78 printf("%s(): OK\n", __func__);
79}
80
81static void test_dual_sw_config(void)
82{
83 struct abis_nm_sw_desc desc[2];
84 uint16_t len0, len1;
85 int rc;
86
87 rc = abis_nm_get_sw_conf(dual_config, ARRAY_SIZE(dual_config), &desc[0], ARRAY_SIZE(desc));
88 if (rc != 2) {
89 printf("%s(): FAILED to parse the File Id/File version: %d (%d,%d)\n",
90 __func__, -rc, EBADF, EBADMSG);
91 abort();
92 }
93
94 len0 = abis_nm_sw_desc_len(&desc[0], true);
95 if (len0 != 13) {
96 printf("WRONG SIZE0: %u\n", len0);
97 abort();
98 }
99
100 len1 = abis_nm_sw_desc_len(&desc[1], true);
101 if (len1 != 13) {
102 printf("WRONG SIZE1: %u\n", len1);
103 abort();
104 }
105
106 printf("len: %u\n", len0);
107 printf("file_id: %s\n", osmo_hexdump(desc[0].file_id, desc[0].file_id_len));
108 printf("file_ver: %s\n", osmo_hexdump(desc[0].file_version, desc[0].file_version_len));
109
110 printf("len: %u\n", len1);
111 printf("file_id: %s\n", osmo_hexdump(desc[1].file_id, desc[1].file_id_len));
112 printf("file_ver: %s\n", osmo_hexdump(desc[1].file_version, desc[1].file_version_len));
113 printf("%s(): OK\n", __func__);
114}
115
116static inline void print_chk(const char *what, uint8_t len1, uint8_t len2, const uint8_t *x1, const uint8_t *x2)
117{
118 int cmp = memcmp(x1, x2, len2);
119 printf("\tFILE %s [%u == %u -> %d, %s] %d => %s\n", what, len1, len2, len1 == len2, len1 != len2 ? "fail" : "ok",
120 cmp, cmp != 0 ? "FAIL" : "OK");
121}
122
123static inline void chk_raw(const char *what, const uint8_t *data, uint16_t len)
124{
Pau Espin Pedrol039171a2017-06-18 11:21:25 +0200125 /* = { 0 } triggers a warning in some gcc versions due to a bug: gcc issue #53119 */
126 struct abis_nm_sw_desc sw = { {0} };
Maxf74cfd32017-03-24 20:16:33 +0100127 int res = abis_nm_get_sw_conf(data, len, &sw, 1);
128 uint16_t xlen = abis_nm_get_sw_desc_len(data, len);
129
130 printf("parsing chained %s <1st: %d, total: %d>\n\tSW Descr (%s)\n", osmo_hexdump(data, len), xlen, len, what);
131
132 if (res < 0)
133 printf("\tFAIL: %d\n", -res);
134 else {
135 printf("\tFILE ID: [%d] %s => OK\n", sw.file_id_len, osmo_hexdump(sw.file_id, sw.file_id_len));
136 printf("\tFILE VERSION: [%d] %s => OK\n", sw.file_version_len,
137 osmo_hexdump(sw.file_version, sw.file_version_len));
138 }
139
140 if (len != xlen)
141 chk_raw(" 2nd", data + xlen, len - xlen);
142}
143
144static inline void chk_descr(struct msgb *msg, const char *f_id, const char *f_ver, const char *desc, bool header)
145{
146 int res;
147 uint16_t len;
Pau Espin Pedrol039171a2017-06-18 11:21:25 +0200148 /* = { 0 } triggers a warning in some gcc versions due to a bug: gcc issue #53119 */
149 struct abis_nm_sw_desc sw = { {0} }, put = {
Maxf74cfd32017-03-24 20:16:33 +0100150 .file_id_len = strlen(f_id),
151 .file_version_len = strlen(f_ver),
152 };
153
154 memcpy(put.file_id, f_id, put.file_id_len);
155 memcpy(put.file_version, f_ver, put.file_version_len);
156 len = abis_nm_put_sw_file(msg, f_id, f_ver, header);
157
158 printf("msgb[%u] :: {msgb->len} %u == %u {len} - %s]:\n\tSW DESCR (%s)\n"
159 "\tlength: {extracted} %u = %u {expected} - %s, failsafe - %s\n",
160 msg->data_len, msg->len, len, len != msg->len ? "fail" : "ok", desc,
161 abis_nm_get_sw_desc_len(msgb_data(msg), msg->len), msg->len,
162 abis_nm_get_sw_desc_len(msgb_data(msg), msg->len) != msg->len ? "FAIL" : "OK",
163 len > put.file_version_len + put.file_id_len ? "OK" : "FAIL");
164
165 res = abis_nm_get_sw_conf(msgb_data(msg), msg->len, &sw, 1);
166 if (res < 0)
167 printf("\tSW DESCR (%s) parsing error code %d!\n", desc, -res);
168 else {
169 print_chk("ID", sw.file_id_len, put.file_id_len, sw.file_id, put.file_id);
170 print_chk("VERSION", sw.file_version_len, put.file_version_len, sw.file_version, put.file_version);
171 }
172}
173
174static void test_sw_descr()
175{
176 const char *f_id = "TEST.L0L", *f_ver = "0.1.666~deadbeeffacefeed-dirty";
177 uint8_t chain[] = { 0x42, 0x12, 0x00, 0x03, 0x01, 0x02, 0x03, 0x13, 0x00, 0x03, 0x03, 0x04, 0x05, 0x42, 0x12,
178 0x00, 0x03, 0x09, 0x07, 0x05, 0x13, 0x00, 0x03, 0x06, 0x07, 0x08 };
179 struct msgb *msg = msgb_alloc_headroom(4096, 128, "sw");
180
181 printf("Testing SW Description (de)serialization...\n");
182
183 /* check that parsing |SW|ID|VER| works: */
184 chk_descr(msg, f_id, f_ver, "with header", true);
185 msgb_reset(msg);
186
187 /* check that parsing |ID|VER| works: */
188 chk_descr(msg, f_id, f_ver, "without header", false);
189
190 /* check that parsing |ID|VER|SW|ID|VER| fails - notice the lack of msgb_reset() to create bogus msgb data: */
191 chk_descr(msg, f_id, f_ver, "expected failure", true);
192
193 /* check multiple, chained SW-descr: */
194 chk_raw("half", chain, sizeof(chain) / 2);
195 chk_raw("full", chain, sizeof(chain));
Neels Hofmeyr39720ac2017-11-16 16:24:36 +0100196
197 msgb_free(msg);
Maxf74cfd32017-03-24 20:16:33 +0100198}
199
200int main(int argc, char **argv)
201{
202 osmo_init_logging(&info);
203
204 test_sw_descr();
205 test_simple_sw_config();
206 test_simple_sw_short();
207 test_dual_sw_config();
208
209 printf("OK.\n");
210
211 return 0;
212}