Philipp | 22611be | 2016-08-10 12:08:03 +0200 | [diff] [blame] | 1 | /* 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 | |
| 22 | #include <openbsc/gprs_sndcp_xid.h> |
| 23 | #include <openbsc/debug.h> |
| 24 | |
| 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 */ |
| 34 | static 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 */ |
| 50 | comp_fields = gprs_sndcp_parse_xid(ctx, xid, sizeof(xid), NULL); |
| 51 | OSMO_ASSERT(comp_fields); |
| 52 | printf("Decoded:\n"); |
| 53 | gprs_sndcp_dump_comp_fields(comp_fields, DSNDCP); |
| 54 | |
| 55 | /* Encode comp-fields again */ |
| 56 | rc = gprs_sndcp_compile_xid(xid_r,sizeof(xid_r), comp_fields); |
| 57 | printf("Result length=%i\n",rc); |
| 58 | printf("Encoded: %s\n", osmo_hexdump_nospc(xid, sizeof(xid))); |
| 59 | printf("Rencoded: %s\n", osmo_hexdump_nospc(xid_r, rc)); |
| 60 | |
| 61 | OSMO_ASSERT(rc == 54); |
| 62 | OSMO_ASSERT(memcmp(xid, xid_r, sizeof(xid)) == 0); |
| 63 | |
| 64 | /* Free comp fields */ |
| 65 | talloc_free(comp_fields); |
| 66 | |
| 67 | printf("\n"); |
| 68 | } |
| 69 | |
| 70 | /* Encode and decode test with artificial test data */ |
| 71 | static void test_xid_encode_decode(const void *ctx) |
| 72 | { |
| 73 | printf("Testing SNDCP XID-Encoder/Decoder\n"); |
| 74 | |
| 75 | LLIST_HEAD(comp_fields); |
| 76 | struct gprs_sndcp_pcomp_rfc1144_params rfc1144_params; |
| 77 | struct gprs_sndcp_comp_field rfc1144_comp_field; |
| 78 | struct gprs_sndcp_pcomp_rfc2507_params rfc2507_params; |
| 79 | struct gprs_sndcp_comp_field rfc2507_comp_field; |
| 80 | struct gprs_sndcp_pcomp_rohc_params rohc_params; |
| 81 | struct gprs_sndcp_comp_field rohc_comp_field; |
| 82 | struct gprs_sndcp_dcomp_v42bis_params v42bis_params; |
| 83 | struct gprs_sndcp_comp_field v42bis_comp_field; |
| 84 | struct gprs_sndcp_dcomp_v44_params v44_params; |
| 85 | struct gprs_sndcp_comp_field v44_comp_field; |
| 86 | struct llist_head *comp_fields_dec; |
| 87 | |
| 88 | uint8_t xid[512]; |
| 89 | unsigned int xid_len = sizeof(xid); |
| 90 | int rc; |
| 91 | |
| 92 | memset(&rfc1144_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
| 93 | memset(&rfc2507_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
| 94 | memset(&rohc_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
| 95 | memset(&v42bis_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
| 96 | memset(&v44_comp_field, 0, sizeof(struct gprs_sndcp_comp_field)); |
| 97 | |
| 98 | /* Setup which NSAPIs shall make use of rfc1144 */ |
| 99 | rfc1144_params.nsapi[0] = 5; |
| 100 | rfc1144_params.nsapi_len = 1; |
| 101 | |
| 102 | /* Setup rfc1144 operating parameters */ |
| 103 | rfc1144_params.s01 = 7; |
| 104 | |
| 105 | /* Setup rfc1144 compression field */ |
| 106 | rfc1144_comp_field.p = 1; |
| 107 | rfc1144_comp_field.entity = 0; |
| 108 | rfc1144_comp_field.algo = RFC_1144; |
| 109 | rfc1144_comp_field.comp[RFC1144_PCOMP1] = 1; |
| 110 | rfc1144_comp_field.comp[RFC1144_PCOMP2] = 2; |
| 111 | rfc1144_comp_field.comp_len = RFC1144_PCOMP_NUM; |
| 112 | rfc1144_comp_field.rfc1144_params = &rfc1144_params; |
| 113 | |
| 114 | /* Setup which NSAPIs shall make use of rfc1144 */ |
| 115 | rfc2507_params.nsapi[0] = 6; |
| 116 | rfc2507_params.nsapi_len = 1; |
| 117 | |
| 118 | /* Setup rfc2507 operating parameters */ |
| 119 | rfc2507_params.f_max_period = 256; |
| 120 | rfc2507_params.f_max_time = 5; |
| 121 | rfc2507_params.max_header = 168; |
| 122 | rfc2507_params.tcp_space = 15; |
| 123 | rfc2507_params.non_tcp_space = 15; |
| 124 | |
| 125 | /* Setup rfc2507 compression field */ |
| 126 | rfc2507_comp_field.p = 1; |
| 127 | rfc2507_comp_field.entity = 1; |
| 128 | rfc2507_comp_field.algo = RFC_2507; |
| 129 | rfc2507_comp_field.comp[RFC2507_PCOMP1] = 3; |
| 130 | rfc2507_comp_field.comp[RFC2507_PCOMP2] = 4; |
| 131 | rfc2507_comp_field.comp[RFC2507_PCOMP3] = 5; |
| 132 | rfc2507_comp_field.comp[RFC2507_PCOMP4] = 6; |
| 133 | rfc2507_comp_field.comp[RFC2507_PCOMP5] = 7; |
| 134 | rfc2507_comp_field.comp_len = RFC2507_PCOMP_NUM; |
| 135 | rfc2507_comp_field.rfc2507_params = &rfc2507_params; |
| 136 | |
| 137 | /* Setup which NSAPIs shall make use of ROHC */ |
| 138 | rohc_params.nsapi[0] = 5; |
| 139 | rohc_params.nsapi[1] = 6; |
| 140 | rohc_params.nsapi[2] = 7; |
| 141 | rohc_params.nsapi[3] = 8; |
| 142 | rohc_params.nsapi[4] = 9; |
| 143 | rohc_params.nsapi[5] = 10; |
| 144 | rohc_params.nsapi[6] = 11; |
| 145 | rohc_params.nsapi[7] = 12; |
| 146 | rohc_params.nsapi[8] = 13; |
| 147 | rohc_params.nsapi[9] = 14; |
| 148 | rohc_params.nsapi[10] = 15; |
| 149 | rohc_params.nsapi_len = 11; |
| 150 | |
| 151 | /* Setup ROHC operating parameters */ |
| 152 | rohc_params.max_cid = 15; /* default */ |
| 153 | rohc_params.max_header = 168; /* default */ |
| 154 | rohc_params.profile[0] = ROHC_UNCOMPRESSED; |
| 155 | rohc_params.profile[1] = ROHC_RTP; |
| 156 | rohc_params.profile[2] = ROHCV2_RTP; |
| 157 | rohc_params.profile[3] = ROHC_UDP; |
| 158 | rohc_params.profile[4] = ROHCv2_UDP; |
| 159 | rohc_params.profile[5] = ROHC_ESP; |
| 160 | rohc_params.profile[6] = ROHCV2_ESP; |
| 161 | rohc_params.profile[7] = ROHC_IP; |
| 162 | rohc_params.profile[8] = ROHCV2_IP; |
| 163 | rohc_params.profile[9] = ROHC_LLA; |
| 164 | rohc_params.profile[10] = ROHC_LLA_WITH_R_MODE; |
| 165 | rohc_params.profile[11] = ROHC_TCP; |
| 166 | rohc_params.profile[12] = ROHC_RTP_UDP_LITE; |
| 167 | rohc_params.profile[13] = ROHCV2_RTP_UDP_LITE; |
| 168 | rohc_params.profile[14] = ROHC_UDP_LITE; |
| 169 | rohc_params.profile[15] = ROHCV2_UDP_LITE; |
| 170 | rohc_params.profile_len = 16; |
| 171 | |
| 172 | /* Setup ROHC compression field */ |
| 173 | rohc_comp_field.p = 1; |
| 174 | rohc_comp_field.entity = 2; |
| 175 | rohc_comp_field.algo = ROHC; |
| 176 | rohc_comp_field.comp[ROHC_PCOMP1] = 8; |
| 177 | rohc_comp_field.comp[ROHC_PCOMP2] = 9; |
| 178 | rohc_comp_field.comp_len = ROHC_PCOMP_NUM; |
| 179 | rohc_comp_field.rohc_params = &rohc_params; |
| 180 | |
| 181 | /* Setup which NSAPIs shall make use of v42bis */ |
| 182 | v42bis_params.nsapi[0] = 5; |
| 183 | v42bis_params.nsapi_len = 1; |
| 184 | |
| 185 | /* Setup v42bis operating parameters */ |
| 186 | v42bis_params.p0 = 3; |
| 187 | v42bis_params.p1 = 2048; |
| 188 | v42bis_params.p2 = 20; |
| 189 | |
| 190 | /* Setup v42bis compression field */ |
| 191 | v42bis_comp_field.p = 1; |
| 192 | v42bis_comp_field.entity = 3; |
| 193 | v42bis_comp_field.algo = V42BIS; |
| 194 | v42bis_comp_field.comp[V42BIS_DCOMP1] = 10; |
| 195 | v42bis_comp_field.comp_len = V42BIS_DCOMP_NUM; |
| 196 | v42bis_comp_field.v42bis_params = &v42bis_params; |
| 197 | |
| 198 | /* Setup which NSAPIs shall make use of v44 */ |
| 199 | v44_params.nsapi[0] = 5; |
| 200 | v44_params.nsapi_len = 1; |
| 201 | |
| 202 | /* Setup v44 operating parameters */ |
| 203 | v44_params.c0 = 0x80; |
| 204 | v44_params.p0 = 3; |
| 205 | v44_params.p1t = 300; |
| 206 | v44_params.p1r = 300; |
| 207 | v44_params.p3t = 600; |
| 208 | v44_params.p3r = 600; |
| 209 | |
| 210 | /* Setup v44 compression field */ |
| 211 | v44_comp_field.p = 1; |
| 212 | v44_comp_field.entity = 3; |
| 213 | v44_comp_field.algo = V44; |
| 214 | v44_comp_field.comp[V44_DCOMP1] = 10; |
| 215 | v44_comp_field.comp[V44_DCOMP2] = 11; |
| 216 | v44_comp_field.comp_len = V44_DCOMP_NUM; |
| 217 | v44_comp_field.v44_params = &v44_params; |
| 218 | |
| 219 | /* Add compression field(s) to list */ |
| 220 | llist_add(&v44_comp_field.list, &comp_fields); |
| 221 | llist_add(&v42bis_comp_field.list, &comp_fields); |
| 222 | llist_add(&rfc1144_comp_field.list, &comp_fields); |
| 223 | llist_add(&rfc2507_comp_field.list, &comp_fields); |
| 224 | llist_add(&rohc_comp_field.list, &comp_fields); |
| 225 | printf("Test input data:\n"); |
| 226 | gprs_sndcp_dump_comp_fields(&comp_fields, DSNDCP); |
| 227 | |
| 228 | /* Encode SNDCP-XID fields */ |
| 229 | rc = gprs_sndcp_compile_xid(xid, xid_len, &comp_fields); |
| 230 | OSMO_ASSERT(rc > 0); |
| 231 | |
| 232 | printf("Encoded: %s (%i bytes)\n", osmo_hexdump_nospc(xid, rc), rc); |
| 233 | |
| 234 | /* Parse and show contained comp fields */ |
| 235 | comp_fields_dec = gprs_sndcp_parse_xid(ctx, xid, rc, NULL); |
| 236 | OSMO_ASSERT(comp_fields_dec); |
| 237 | |
| 238 | printf("Decoded:\n"); |
| 239 | gprs_sndcp_dump_comp_fields(comp_fields_dec, DSNDCP); |
| 240 | |
| 241 | /* Free comp fields */ |
| 242 | talloc_free(comp_fields_dec); |
| 243 | } |
| 244 | |
| 245 | static struct log_info_cat gprs_categories[] = { |
| 246 | [DSNDCP] = { |
| 247 | .name = "DSNDCP", |
| 248 | .description = |
| 249 | "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 250 | .enabled = 1,.loglevel = LOGL_DEBUG, |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | static struct log_info info = { |
| 255 | .cat = gprs_categories, |
| 256 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 257 | }; |
| 258 | |
| 259 | int main(int argc, char **argv) |
| 260 | { |
| 261 | void *xid_ctx; |
| 262 | |
| 263 | osmo_init_logging(&info); |
| 264 | |
| 265 | xid_ctx = talloc_named_const(NULL, 0, "xid_ctx"); |
| 266 | |
| 267 | test_xid_decode_realworld(xid_ctx); |
| 268 | test_xid_encode_decode(xid_ctx); |
| 269 | |
| 270 | printf("Done\n"); |
| 271 | |
| 272 | talloc_report_full(xid_ctx, stderr); |
| 273 | OSMO_ASSERT(talloc_total_blocks(xid_ctx) == 1); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* stubs */ |
| 278 | struct osmo_prim_hdr; |
| 279 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 280 | { |
| 281 | abort(); |
| 282 | } |