blob: a303c919b3b7c72cbd2d49a389cbaf69b5bc0efc [file] [log] [blame]
Maxf74cfd32017-03-24 20:16:33 +01001/*
2 * (C) 2012 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2017 by sysmocom s.m.f.c. GmbH <info@sysmocom.de>
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 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{
125 struct abis_nm_sw_desc sw = { 0 };
126 int res = abis_nm_get_sw_conf(data, len, &sw, 1);
127 uint16_t xlen = abis_nm_get_sw_desc_len(data, len);
128
129 printf("parsing chained %s <1st: %d, total: %d>\n\tSW Descr (%s)\n", osmo_hexdump(data, len), xlen, len, what);
130
131 if (res < 0)
132 printf("\tFAIL: %d\n", -res);
133 else {
134 printf("\tFILE ID: [%d] %s => OK\n", sw.file_id_len, osmo_hexdump(sw.file_id, sw.file_id_len));
135 printf("\tFILE VERSION: [%d] %s => OK\n", sw.file_version_len,
136 osmo_hexdump(sw.file_version, sw.file_version_len));
137 }
138
139 if (len != xlen)
140 chk_raw(" 2nd", data + xlen, len - xlen);
141}
142
143static inline void chk_descr(struct msgb *msg, const char *f_id, const char *f_ver, const char *desc, bool header)
144{
145 int res;
146 uint16_t len;
147 struct abis_nm_sw_desc sw = { 0 }, put = {
148 .file_id_len = strlen(f_id),
149 .file_version_len = strlen(f_ver),
150 };
151
152 memcpy(put.file_id, f_id, put.file_id_len);
153 memcpy(put.file_version, f_ver, put.file_version_len);
154 len = abis_nm_put_sw_file(msg, f_id, f_ver, header);
155
156 printf("msgb[%u] :: {msgb->len} %u == %u {len} - %s]:\n\tSW DESCR (%s)\n"
157 "\tlength: {extracted} %u = %u {expected} - %s, failsafe - %s\n",
158 msg->data_len, msg->len, len, len != msg->len ? "fail" : "ok", desc,
159 abis_nm_get_sw_desc_len(msgb_data(msg), msg->len), msg->len,
160 abis_nm_get_sw_desc_len(msgb_data(msg), msg->len) != msg->len ? "FAIL" : "OK",
161 len > put.file_version_len + put.file_id_len ? "OK" : "FAIL");
162
163 res = abis_nm_get_sw_conf(msgb_data(msg), msg->len, &sw, 1);
164 if (res < 0)
165 printf("\tSW DESCR (%s) parsing error code %d!\n", desc, -res);
166 else {
167 print_chk("ID", sw.file_id_len, put.file_id_len, sw.file_id, put.file_id);
168 print_chk("VERSION", sw.file_version_len, put.file_version_len, sw.file_version, put.file_version);
169 }
170}
171
172static void test_sw_descr()
173{
174 const char *f_id = "TEST.L0L", *f_ver = "0.1.666~deadbeeffacefeed-dirty";
175 uint8_t chain[] = { 0x42, 0x12, 0x00, 0x03, 0x01, 0x02, 0x03, 0x13, 0x00, 0x03, 0x03, 0x04, 0x05, 0x42, 0x12,
176 0x00, 0x03, 0x09, 0x07, 0x05, 0x13, 0x00, 0x03, 0x06, 0x07, 0x08 };
177 struct msgb *msg = msgb_alloc_headroom(4096, 128, "sw");
178
179 printf("Testing SW Description (de)serialization...\n");
180
181 /* check that parsing |SW|ID|VER| works: */
182 chk_descr(msg, f_id, f_ver, "with header", true);
183 msgb_reset(msg);
184
185 /* check that parsing |ID|VER| works: */
186 chk_descr(msg, f_id, f_ver, "without header", false);
187
188 /* check that parsing |ID|VER|SW|ID|VER| fails - notice the lack of msgb_reset() to create bogus msgb data: */
189 chk_descr(msg, f_id, f_ver, "expected failure", true);
190
191 /* check multiple, chained SW-descr: */
192 chk_raw("half", chain, sizeof(chain) / 2);
193 chk_raw("full", chain, sizeof(chain));
194}
195
196int main(int argc, char **argv)
197{
198 osmo_init_logging(&info);
199
200 test_sw_descr();
201 test_simple_sw_config();
202 test_simple_sw_short();
203 test_dual_sw_config();
204
205 printf("OK.\n");
206
207 return 0;
208}