Philipp | fcb979a | 2016-08-10 12:14:57 +0200 | [diff] [blame] | 1 | /* Test LLC-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_llc_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 XID encoding */ |
| 34 | static void test_xid_encode(const void *ctx) |
| 35 | { |
| 36 | struct gprs_llc_xid_field xid_field_1; |
| 37 | struct gprs_llc_xid_field xid_field_2; |
| 38 | struct gprs_llc_xid_field xid_field_3; |
| 39 | struct gprs_llc_xid_field xid_field_4; |
| 40 | LLIST_HEAD(xid_fields); |
| 41 | uint8_t xid[255]; |
| 42 | uint8_t xid_expected[] = |
| 43 | { 0x10, 0x8c, 0x14, 0x43, 0x43, 0x43, 0x43, 0x43, 0x0b, 0x42, 0x42, |
| 44 | 0x42, 0x05, 0x41 }; |
| 45 | int rc; |
| 46 | |
| 47 | printf("Testing LLC XID-Encoder\n"); |
| 48 | |
| 49 | /* Setup some simple XID data */ |
| 50 | xid_field_1.type = 1; |
| 51 | xid_field_2.type = 2; |
| 52 | xid_field_3.type = 3; |
| 53 | xid_field_4.type = 4; |
| 54 | |
| 55 | xid_field_1.data = (uint8_t *) "A"; |
| 56 | xid_field_2.data = (uint8_t *) "BBB"; |
| 57 | xid_field_3.data = (uint8_t *) "CCCCC"; |
| 58 | xid_field_4.data = NULL; |
| 59 | |
| 60 | xid_field_1.data_len = 1; |
| 61 | xid_field_2.data_len = 3; |
| 62 | xid_field_3.data_len = 5; |
| 63 | xid_field_4.data_len = 0; |
| 64 | |
| 65 | llist_add(&xid_field_4.list, &xid_fields); |
| 66 | llist_add(&xid_field_3.list, &xid_fields); |
| 67 | llist_add(&xid_field_2.list, &xid_fields); |
| 68 | llist_add(&xid_field_1.list, &xid_fields); |
| 69 | |
| 70 | printf("Data to encode:\n"); |
| 71 | gprs_llc_dump_xid_fields(&xid_fields, DSNDCP); |
| 72 | |
| 73 | /* Encode data */ |
| 74 | rc = gprs_llc_compile_xid(xid, sizeof(xid), &xid_fields); |
| 75 | OSMO_ASSERT(rc == 14); |
| 76 | printf("Encoded: %s (%i bytes)\n", osmo_hexdump_nospc(xid, rc), rc); |
| 77 | printf("Expected: %s (%i bytes)\n", |
| 78 | osmo_hexdump_nospc(xid_expected, sizeof(xid_expected)), |
| 79 | (int)sizeof(xid_expected)); |
| 80 | |
| 81 | OSMO_ASSERT(memcmp(xid_expected, xid, sizeof(xid_expected)) == 0); |
| 82 | |
| 83 | printf("\n"); |
| 84 | } |
| 85 | |
| 86 | /* Test XID decoding */ |
| 87 | static void test_xid_decode(const void *ctx) |
| 88 | { |
| 89 | struct llist_head *xid_fields; |
| 90 | int rc; |
| 91 | |
| 92 | printf("Testing LLC XID-Decoder/Encoder\n"); |
| 93 | |
| 94 | /* Example of a real world LLC-XID message */ |
| 95 | uint8_t xid[] = |
| 96 | { 0x01, 0x00, 0x16, 0x05, 0xf0, 0x1a, 0x05, 0xf0, 0xac, 0xd8, 0x00, |
| 97 | 0x01, 0x00, 0x02, 0x31, 0x82, 0x02, 0x27, 0x89, 0xff, 0xe0, 0x00, 0x0f, |
| 98 | 0x00, 0xa8, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x02, 0x01, 0x02, |
| 99 | 0x00, 0x03, 0x01, 0x03, 0x00, 0x04, 0x01, 0x04, 0x00, 0x05, 0x01, 0x05, |
| 100 | 0x00, 0x06, 0x00, 0x07, 0x01, 0x07, 0x00, 0x08, 0x01, 0x08, 0x80, 0x00, |
| 101 | 0x04, 0x12, 0x00, 0x40, 0x07 }; |
| 102 | |
| 103 | uint8_t xid_r[512]; |
| 104 | |
| 105 | /* Decode and display XID fields */ |
| 106 | xid_fields = gprs_llc_parse_xid(ctx, xid, sizeof(xid)); |
| 107 | OSMO_ASSERT(xid_fields); |
| 108 | |
| 109 | printf("Decoded:\n"); |
| 110 | gprs_llc_dump_xid_fields(xid_fields, DSNDCP); |
| 111 | |
| 112 | |
| 113 | /* Encode xid-fields again */ |
| 114 | rc = gprs_llc_compile_xid(xid_r, sizeof(xid_r), xid_fields); |
| 115 | printf("Result length=%i\n",rc); |
| 116 | printf("Encoded: %s\n", osmo_hexdump_nospc(xid, sizeof(xid))); |
| 117 | printf("Rencoded: %s\n", osmo_hexdump_nospc(xid_r, rc)); |
| 118 | |
| 119 | OSMO_ASSERT(rc == 64); |
| 120 | OSMO_ASSERT(memcmp(xid, xid_r, sizeof(xid)) == 0); |
| 121 | |
| 122 | /* Free xid fields */ |
| 123 | talloc_free(xid_fields); |
| 124 | |
| 125 | printf("\n"); |
| 126 | } |
| 127 | |
| 128 | static struct log_info_cat gprs_categories[] = { |
| 129 | [DSNDCP] = { |
| 130 | .name = "DSNDCP", |
| 131 | .description = |
| 132 | "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 133 | .enabled = 1,.loglevel = LOGL_DEBUG, |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | static struct log_info info = { |
| 138 | .cat = gprs_categories, |
| 139 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 140 | }; |
| 141 | |
| 142 | int main(int argc, char **argv) |
| 143 | { |
| 144 | void *xid_ctx; |
| 145 | |
| 146 | osmo_init_logging(&info); |
| 147 | |
| 148 | xid_ctx = talloc_named_const(NULL, 0, "xid_ctx"); |
| 149 | |
| 150 | test_xid_decode(xid_ctx); |
| 151 | test_xid_encode(xid_ctx); |
| 152 | printf("Done\n"); |
| 153 | |
| 154 | talloc_report_full(xid_ctx, stderr); |
| 155 | OSMO_ASSERT(talloc_total_blocks(xid_ctx) == 1); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* stubs */ |
| 160 | struct osmo_prim_hdr; |
| 161 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 162 | { |
| 163 | abort(); |
| 164 | } |