blob: 56b97e336dd5065b011f4112548c740bd3c6fadf [file] [log] [blame]
Philipp59971b82016-08-10 12:08:03 +02001/* Test SNDCP-XID Encoding/Decoding */
2
3/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Philipp Maier
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020022#include <osmocom/sgsn/gprs_sndcp_xid.h>
23#include <osmocom/sgsn/debug.h>
Philipp59971b82016-08-10 12:08:03 +020024
25#include <osmocom/core/talloc.h>
26#include <osmocom/core/utils.h>
27
28#include <osmocom/core/application.h>
29
30#include <stdio.h>
31#include <string.h>
32
33/* Test SNDCP-XID decoding with a real world sample */
34static void test_xid_decode_realworld(const void *ctx)
35{
36 struct llist_head *comp_fields;
37 int rc;
38 printf("Testing SNDCP XID-Decoder/Encoder (real world data)\n");
39
40 /* Example of a real world SNDCP-XID message */
41 uint8_t xid[] =
42 { 0x00, 0x01, 0x00, 0x02, 0x31, 0x82, 0x02, 0x27, 0x89, 0xff, 0xe0,
43 0x00, 0x0f, 0x00, 0xa8, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x02,
44 0x01, 0x02, 0x00, 0x03, 0x01, 0x03, 0x00, 0x04, 0x01, 0x04, 0x00, 0x05,
45 0x01, 0x05, 0x00, 0x06, 0x00, 0x07, 0x01, 0x07, 0x00, 0x08, 0x01, 0x08,
46 0x80, 0x00, 0x04, 0x12, 0x00, 0x40, 0x07 };
47 uint8_t xid_r[512];
48
49 /* Parse and show contained comp fields */
Philipp9bf7dcb2016-12-22 14:15:20 +010050 comp_fields = gprs_sndcp_parse_xid(NULL, ctx, xid, sizeof(xid), NULL);
Philipp59971b82016-08-10 12:08:03 +020051 OSMO_ASSERT(comp_fields);
52 printf("Decoded:\n");
53 gprs_sndcp_dump_comp_fields(comp_fields, DSNDCP);
54
55 /* Encode comp-fields again */
Philipp9bf7dcb2016-12-22 14:15:20 +010056 rc = gprs_sndcp_compile_xid(xid_r,sizeof(xid_r), comp_fields,
57 DEFAULT_SNDCP_VERSION);
Philipp59971b82016-08-10 12:08:03 +020058 printf("Result length=%i\n",rc);
59 printf("Encoded: %s\n", osmo_hexdump_nospc(xid, sizeof(xid)));
60 printf("Rencoded: %s\n", osmo_hexdump_nospc(xid_r, rc));
61
62 OSMO_ASSERT(rc == 54);
63 OSMO_ASSERT(memcmp(xid, xid_r, sizeof(xid)) == 0);
64
65 /* Free comp fields */
66 talloc_free(comp_fields);
67
68 printf("\n");
69}
70
71/* Encode and decode test with artificial test data */
72static void test_xid_encode_decode(const void *ctx)
73{
74 printf("Testing SNDCP XID-Encoder/Decoder\n");
75
76 LLIST_HEAD(comp_fields);
77 struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params;
78 struct gprs_sndcp_comp_field rfc1144_comp_field;
79 struct gprs_sndcp_pcomp_rfc2507_params rfc2507_params;
80 struct gprs_sndcp_comp_field rfc2507_comp_field;
81 struct gprs_sndcp_pcomp_rohc_params rohc_params;
82 struct gprs_sndcp_comp_field rohc_comp_field;
83 struct gprs_sndcp_dcomp_v42bis_params v42bis_params;
84 struct gprs_sndcp_comp_field v42bis_comp_field;
85 struct gprs_sndcp_dcomp_v44_params v44_params;
86 struct gprs_sndcp_comp_field v44_comp_field;
87 struct llist_head *comp_fields_dec;
88
89 uint8_t xid[512];
90 unsigned int xid_len = sizeof(xid);
91 int rc;
92
93 memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
94 memset(&rfc2507_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
95 memset(&rohc_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
96 memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
97 memset(&v44_comp_field, 0, sizeof(struct gprs_sndcp_comp_field));
98
99 /* Setup which NSAPIs shall make use of rfc1144 */
100 rfc1144_params.nsapi[0] = 5;
101 rfc1144_params.nsapi_len = 1;
102
103 /* Setup rfc1144 operating parameters */
104 rfc1144_params.s01 = 7;
105
106 /* Setup rfc1144 compression field */
107 rfc1144_comp_field.p = 1;
108 rfc1144_comp_field.entity = 0;
Stefan Sperling7dd481c2018-11-07 16:33:39 +0100109 rfc1144_comp_field.algo.pcomp = RFC_1144;
Philipp59971b82016-08-10 12:08:03 +0200110 rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1;
111 rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2;
112 rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM;
113 rfc1144_comp_field.rfc1144_params = &rfc1144_params;
114
115 /* Setup which NSAPIs shall make use of rfc1144 */
116 rfc2507_params.nsapi[0] = 6;
117 rfc2507_params.nsapi_len = 1;
118
119 /* Setup rfc2507 operating parameters */
120 rfc2507_params.f_max_period = 256;
121 rfc2507_params.f_max_time = 5;
122 rfc2507_params.max_header = 168;
123 rfc2507_params.tcp_space = 15;
124 rfc2507_params.non_tcp_space = 15;
125
126 /* Setup rfc2507 compression field */
127 rfc2507_comp_field.p = 1;
128 rfc2507_comp_field.entity = 1;
Stefan Sperling7dd481c2018-11-07 16:33:39 +0100129 rfc2507_comp_field.algo.pcomp = RFC_2507;
Philipp59971b82016-08-10 12:08:03 +0200130 rfc2507_comp_field.comp[RFC2507_PCOMP1] = 3;
131 rfc2507_comp_field.comp[RFC2507_PCOMP2] = 4;
132 rfc2507_comp_field.comp[RFC2507_PCOMP3] = 5;
133 rfc2507_comp_field.comp[RFC2507_PCOMP4] = 6;
134 rfc2507_comp_field.comp[RFC2507_PCOMP5] = 7;
135 rfc2507_comp_field.comp_len = RFC2507_PCOMP_NUM;
136 rfc2507_comp_field.rfc2507_params = &rfc2507_params;
137
138 /* Setup which NSAPIs shall make use of ROHC */
139 rohc_params.nsapi[0] = 5;
140 rohc_params.nsapi[1] = 6;
141 rohc_params.nsapi[2] = 7;
142 rohc_params.nsapi[3] = 8;
143 rohc_params.nsapi[4] = 9;
144 rohc_params.nsapi[5] = 10;
145 rohc_params.nsapi[6] = 11;
146 rohc_params.nsapi[7] = 12;
147 rohc_params.nsapi[8] = 13;
148 rohc_params.nsapi[9] = 14;
149 rohc_params.nsapi[10] = 15;
150 rohc_params.nsapi_len = 11;
151
152 /* Setup ROHC operating parameters */
153 rohc_params.max_cid = 15; /* default */
154 rohc_params.max_header = 168; /* default */
155 rohc_params.profile[0] = ROHC_UNCOMPRESSED;
156 rohc_params.profile[1] = ROHC_RTP;
157 rohc_params.profile[2] = ROHCV2_RTP;
158 rohc_params.profile[3] = ROHC_UDP;
159 rohc_params.profile[4] = ROHCv2_UDP;
160 rohc_params.profile[5] = ROHC_ESP;
161 rohc_params.profile[6] = ROHCV2_ESP;
162 rohc_params.profile[7] = ROHC_IP;
163 rohc_params.profile[8] = ROHCV2_IP;
164 rohc_params.profile[9] = ROHC_LLA;
165 rohc_params.profile[10] = ROHC_LLA_WITH_R_MODE;
166 rohc_params.profile[11] = ROHC_TCP;
167 rohc_params.profile[12] = ROHC_RTP_UDP_LITE;
168 rohc_params.profile[13] = ROHCV2_RTP_UDP_LITE;
169 rohc_params.profile[14] = ROHC_UDP_LITE;
170 rohc_params.profile[15] = ROHCV2_UDP_LITE;
171 rohc_params.profile_len = 16;
172
173 /* Setup ROHC compression field */
174 rohc_comp_field.p = 1;
175 rohc_comp_field.entity = 2;
Stefan Sperling7dd481c2018-11-07 16:33:39 +0100176 rohc_comp_field.algo.pcomp = ROHC;
Philipp59971b82016-08-10 12:08:03 +0200177 rohc_comp_field.comp[ROHC_PCOMP1] = 8;
178 rohc_comp_field.comp[ROHC_PCOMP2] = 9;
179 rohc_comp_field.comp_len = ROHC_PCOMP_NUM;
180 rohc_comp_field.rohc_params = &rohc_params;
181
182 /* Setup which NSAPIs shall make use of v42bis */
183 v42bis_params.nsapi[0] = 5;
184 v42bis_params.nsapi_len = 1;
185
186 /* Setup v42bis operating parameters */
187 v42bis_params.p0 = 3;
188 v42bis_params.p1 = 2048;
189 v42bis_params.p2 = 20;
190
191 /* Setup v42bis compression field */
192 v42bis_comp_field.p = 1;
193 v42bis_comp_field.entity = 3;
Stefan Sperling7dd481c2018-11-07 16:33:39 +0100194 v42bis_comp_field.algo.dcomp = V42BIS;
Philipp59971b82016-08-10 12:08:03 +0200195 v42bis_comp_field.comp[V42BIS_DCOMP1] = 10;
196 v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM;
197 v42bis_comp_field.v42bis_params = &v42bis_params;
198
199 /* Setup which NSAPIs shall make use of v44 */
200 v44_params.nsapi[0] = 5;
201 v44_params.nsapi_len = 1;
202
203 /* Setup v44 operating parameters */
204 v44_params.c0 = 0x80;
205 v44_params.p0 = 3;
206 v44_params.p1t = 300;
207 v44_params.p1r = 300;
208 v44_params.p3t = 600;
209 v44_params.p3r = 600;
210
211 /* Setup v44 compression field */
212 v44_comp_field.p = 1;
213 v44_comp_field.entity = 3;
Stefan Sperling7dd481c2018-11-07 16:33:39 +0100214 v44_comp_field.algo.dcomp = V44;
Philipp59971b82016-08-10 12:08:03 +0200215 v44_comp_field.comp[V44_DCOMP1] = 10;
216 v44_comp_field.comp[V44_DCOMP2] = 11;
217 v44_comp_field.comp_len = V44_DCOMP_NUM;
218 v44_comp_field.v44_params = &v44_params;
219
220 /* Add compression field(s) to list */
221 llist_add(&v44_comp_field.list, &comp_fields);
222 llist_add(&v42bis_comp_field.list, &comp_fields);
223 llist_add(&rfc1144_comp_field.list, &comp_fields);
224 llist_add(&rfc2507_comp_field.list, &comp_fields);
225 llist_add(&rohc_comp_field.list, &comp_fields);
226 printf("Test input data:\n");
227 gprs_sndcp_dump_comp_fields(&comp_fields, DSNDCP);
228
229 /* Encode SNDCP-XID fields */
Philipp9bf7dcb2016-12-22 14:15:20 +0100230 rc = gprs_sndcp_compile_xid(xid, xid_len, &comp_fields,
231 DEFAULT_SNDCP_VERSION);
Philipp59971b82016-08-10 12:08:03 +0200232 OSMO_ASSERT(rc > 0);
233
234 printf("Encoded: %s (%i bytes)\n", osmo_hexdump_nospc(xid, rc), rc);
235
236 /* Parse and show contained comp fields */
Philipp9bf7dcb2016-12-22 14:15:20 +0100237 comp_fields_dec = gprs_sndcp_parse_xid(NULL, ctx, xid, rc, NULL);
Philipp59971b82016-08-10 12:08:03 +0200238 OSMO_ASSERT(comp_fields_dec);
239
240 printf("Decoded:\n");
241 gprs_sndcp_dump_comp_fields(comp_fields_dec, DSNDCP);
242
243 /* Free comp fields */
244 talloc_free(comp_fields_dec);
245}
246
247static struct log_info_cat gprs_categories[] = {
248 [DSNDCP] = {
249 .name = "DSNDCP",
250 .description =
251 "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
252 .enabled = 1,.loglevel = LOGL_DEBUG,
253 }
254};
255
256static struct log_info info = {
257 .cat = gprs_categories,
258 .num_cat = ARRAY_SIZE(gprs_categories),
259};
260
261int main(int argc, char **argv)
262{
263 void *xid_ctx;
Neels Hofmeyr8de6b862018-04-16 00:57:10 +0200264 void *log_ctx;
Philipp59971b82016-08-10 12:08:03 +0200265
266 xid_ctx = talloc_named_const(NULL, 0, "xid_ctx");
Neels Hofmeyr8de6b862018-04-16 00:57:10 +0200267 log_ctx = talloc_named_const(xid_ctx, 0, "log");
268 osmo_init_logging2(log_ctx, &info);
Philipp59971b82016-08-10 12:08:03 +0200269
270 test_xid_decode_realworld(xid_ctx);
271 test_xid_encode_decode(xid_ctx);
272
273 printf("Done\n");
274
275 talloc_report_full(xid_ctx, stderr);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +0200276 talloc_free(log_ctx);
Philipp59971b82016-08-10 12:08:03 +0200277 OSMO_ASSERT(talloc_total_blocks(xid_ctx) == 1);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +0200278 talloc_free(xid_ctx);
Philipp59971b82016-08-10 12:08:03 +0200279 return 0;
280}
281
282/* stubs */
283struct osmo_prim_hdr;
284int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
285{
286 abort();
287}