Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "rlc.h" |
| 5 | #include "gprs_debug.h" |
| 6 | #include <gprs_rlcmac.h> |
| 7 | #include "egprs_rlc_compression.h" |
| 8 | |
| 9 | extern "C" { |
| 10 | #include <osmocom/core/logging.h> |
| 11 | #include <osmocom/core/bitvec.h> |
| 12 | #include <osmocom/core/utils.h> |
| 13 | #include <osmocom/core/application.h> |
| 14 | } |
| 15 | |
| 16 | #define NEW 1 |
| 17 | #define MASK(n) (0xFF << (8-n)) |
| 18 | #define MAX_CRBB_LEN 23 |
| 19 | #define MAX_URBB_LEN 40 |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 20 | #define CEIL_DIV_8(x) (((x) + 7)/8) |
Neels Hofmeyr | dd1700a | 2017-03-26 23:21:16 +0200 | [diff] [blame] | 21 | #define _LOG(fmt, args...) \ |
| 22 | fprintf(stderr, fmt, ## args) |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 23 | |
| 24 | void *tall_pcu_ctx; |
| 25 | |
| 26 | struct test_data { |
| 27 | int8_t crbb_len; |
| 28 | uint8_t cc; |
| 29 | uint8_t crbb_data[MAX_CRBB_LEN]; /* compressed data */ |
| 30 | uint8_t ucmp_data[MAX_URBB_LEN]; /* uncompressed data */ |
| 31 | int ucmp_len; |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 32 | int expect_rc; |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 33 | } test[] = { |
| 34 | { .crbb_len = 67, .cc = 1, |
| 35 | .crbb_data = { |
| 36 | 0x02, 0x0c, 0xa0, 0x30, 0xcb, 0x1a, 0x0c, 0xe3, 0x6c |
| 37 | }, |
| 38 | .ucmp_data = { |
| 39 | 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x01, 0xff, 0xff, |
| 40 | 0xff, 0xf8, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, |
Neels Hofmeyr | 5382e0f | 2017-03-26 23:39:44 +0200 | [diff] [blame] | 41 | 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xc0 |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 42 | }, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 43 | .ucmp_len = 194, |
| 44 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 45 | }, |
| 46 | { .crbb_len = 40, .cc = 1, |
| 47 | .crbb_data = { |
| 48 | 0x53, 0x06, 0xc5, 0x40, 0x6d |
| 49 | }, |
| 50 | .ucmp_data = { |
| 51 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, |
| 52 | 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, |
Neels Hofmeyr | 5382e0f | 2017-03-26 23:39:44 +0200 | [diff] [blame] | 53 | 0x00, 0x00, 0x00, 0x00, 0x00 |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 54 | }, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 55 | .ucmp_len = 182, |
| 56 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 57 | }, |
| 58 | { .crbb_len = 8, .cc = 1, |
| 59 | .crbb_data = {0x02}, |
| 60 | .ucmp_data = {0xff, 0xff, 0xff, 0xf8}, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 61 | .ucmp_len = 29, |
| 62 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 63 | }, |
| 64 | { .crbb_len = 103, .cc = 1, |
| 65 | .crbb_data = { |
| 66 | 0x02, 0x0c, 0xe0, 0x41, 0xa0, 0x0c, 0x36, 0x0d, 0x03, |
| 67 | 0x71, 0xb0, 0x6e, 0x24 |
| 68 | }, |
| 69 | .ucmp_data = { |
| 70 | 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0xff, 0xff, 0xff, |
| 71 | 0xf8, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, |
| 72 | 0x0f, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x7f, 0xff, |
| 73 | 0xff, 0xff, 0x80, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff |
| 74 | }, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 75 | .ucmp_len = 288, |
| 76 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 77 | }, |
| 78 | /* Test vector from libosmocore test */ |
| 79 | { .crbb_len = 35, .cc = 0, |
| 80 | .crbb_data = {0xde, 0x88, 0x75, 0x65, 0x80}, |
| 81 | .ucmp_data = {0x37, 0x47, 0x81, 0xf0}, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 82 | .ucmp_len = 28, |
| 83 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 84 | }, |
| 85 | { .crbb_len = 18, .cc = 1, |
| 86 | .crbb_data = {0xdd, 0x41, 0x00}, |
| 87 | .ucmp_data = { |
| 88 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 89 | 0xff, 0x00, 0x00 |
| 90 | }, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 91 | .ucmp_len = 90, |
| 92 | .expect_rc = 0, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 93 | }, |
Alexander Couzens | 2d24eba | 2019-06-17 01:41:29 +0200 | [diff] [blame] | 94 | /* Invalid inputs */ |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 95 | { .crbb_len = 18, .cc = 1, |
| 96 | .crbb_data = {0x1E, 0x70, 0xc0}, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 97 | .ucmp_data = {0xb0, 0x00, 0x00}, |
| 98 | .ucmp_len = 19, |
Alexander Couzens | 2d24eba | 2019-06-17 01:41:29 +0200 | [diff] [blame] | 99 | .expect_rc = -1, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 100 | }, |
| 101 | { .crbb_len = 14, .cc = 1, |
| 102 | .crbb_data = {0x00, 0x1E, 0x7c}, |
| 103 | .ucmp_data = {0x0}, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 104 | .ucmp_len = 0, |
| 105 | .expect_rc = -1, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 106 | }, |
| 107 | { .crbb_len = 24, .cc = 0, |
| 108 | .crbb_data = {0x00, 0x00, 0x00}, |
| 109 | .ucmp_data = {0x0}, |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 110 | .ucmp_len = 0, |
| 111 | .expect_rc = -1, |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 115 | bool result_matches(const bitvec &bits, const uint8_t *exp_data, unsigned int exp_len) |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 116 | { |
| 117 | if (bits.cur_bit != exp_len) |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 118 | return false; |
Neels Hofmeyr | 5382e0f | 2017-03-26 23:39:44 +0200 | [diff] [blame] | 119 | return (memcmp(exp_data, bits.data, CEIL_DIV_8(exp_len)) == 0); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* To test decoding of compressed bitmap by Tree based method |
| 123 | * and to verify the result with expected result |
| 124 | * for invalid input verfication is suppressed |
| 125 | */ |
| 126 | static void test_EPDAN_decode_tree(void) |
| 127 | { |
| 128 | bitvec dest; |
Neels Hofmeyr | d34646a | 2017-02-08 17:07:40 +0100 | [diff] [blame] | 129 | unsigned int itr; |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 130 | int rc; |
| 131 | uint8_t bits_data[RLC_EGPRS_MAX_WS/8]; |
| 132 | |
| 133 | printf("=== start %s ===\n", __func__); |
| 134 | |
| 135 | for (itr = 0 ; itr < (sizeof(test) / sizeof(test_data)) ; itr++) { |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 136 | memset(bits_data, 0, sizeof(bits_data)); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 137 | dest.data = bits_data; |
| 138 | dest.data_len = sizeof(bits_data); |
| 139 | dest.cur_bit = 0; |
Neels Hofmeyr | dd1700a | 2017-03-26 23:21:16 +0200 | [diff] [blame] | 140 | _LOG("\nTest:%d\n" |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 141 | "Tree based decoding:\n" |
Alexander Couzens | 7fe3895 | 2019-06-17 01:42:56 +0200 | [diff] [blame] | 142 | "compressed data = %s\n" |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 143 | "len = %d\n", |
| 144 | itr + 1, |
| 145 | osmo_hexdump(test[itr].crbb_data, |
| 146 | CEIL_DIV_8(test[itr].crbb_len)), |
| 147 | test[itr].crbb_len); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 148 | rc = egprs_compress::decompress_crbb(test[itr].crbb_len, |
| 149 | test[itr].cc, test[itr].crbb_data, &dest); |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 150 | _LOG("rc = %d\n", rc); |
| 151 | OSMO_ASSERT(test[itr].expect_rc == rc); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 152 | if (rc < 0) { |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 153 | _LOG("Failed to decode CRBB: length %d, data %s\n", |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 154 | test[itr].crbb_len, |
| 155 | osmo_hexdump(test[itr].crbb_data, |
| 156 | CEIL_DIV_8(test[itr].crbb_len))); |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 157 | continue; |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 158 | } |
Neels Hofmeyr | e6d26ec | 2017-03-27 00:13:14 +0200 | [diff] [blame] | 159 | if (!result_matches(dest, test[itr].ucmp_data, |
| 160 | test[itr].ucmp_len)) { |
| 161 | _LOG("\nTree based decoding: Error\n" |
| 162 | "expected data = %s\n" |
| 163 | "expected len = %d\n", |
| 164 | osmo_hexdump(test[itr].ucmp_data, |
| 165 | CEIL_DIV_8(test[itr].ucmp_len)), |
| 166 | test[itr].ucmp_len); |
| 167 | _LOG("decoded data = %s\n" |
| 168 | "decoded len = %d\n", |
| 169 | osmo_hexdump(dest.data, |
| 170 | CEIL_DIV_8(dest.cur_bit)), |
| 171 | dest.cur_bit); |
| 172 | OSMO_ASSERT(0); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 173 | } |
Neels Hofmeyr | dd1700a | 2017-03-26 23:21:16 +0200 | [diff] [blame] | 174 | _LOG("\nexpected data = %s\n" |
| 175 | "expected len = %d\n", |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 176 | osmo_hexdump(test[itr].ucmp_data, |
| 177 | CEIL_DIV_8(test[itr].ucmp_len)), |
Neels Hofmeyr | dd1700a | 2017-03-26 23:21:16 +0200 | [diff] [blame] | 178 | test[itr].ucmp_len); |
| 179 | _LOG("decoded data = %s\n" |
| 180 | "decoded len = %d\n", |
Neels Hofmeyr | 7783964 | 2017-03-26 23:12:26 +0200 | [diff] [blame] | 181 | osmo_hexdump(dest.data, CEIL_DIV_8(dest.cur_bit)), |
| 182 | dest.cur_bit); |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | printf("=== end %s ===\n", __func__); |
| 186 | } |
| 187 | |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 188 | int main(int argc, char **argv) |
| 189 | { |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 190 | tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile bitcompTest context"); |
| 191 | if (!tall_pcu_ctx) |
| 192 | abort(); |
| 193 | |
Neels Hofmeyr | 42f2d61 | 2018-04-01 16:54:40 +0200 | [diff] [blame] | 194 | osmo_init_logging2(tall_pcu_ctx, &gprs_log_info); |
| 195 | log_set_use_color(osmo_stderr_target, 0); |
| 196 | log_set_print_filename(osmo_stderr_target, 0); |
| 197 | log_parse_category_mask(osmo_stderr_target, "DRLCMACUL,1"); |
| 198 | |
Pravin Kumarvel | 0a4a6c1 | 2016-10-17 11:00:57 +0530 | [diff] [blame] | 199 | test_EPDAN_decode_tree(); |
| 200 | |
| 201 | if (getenv("TALLOC_REPORT_FULL")) |
| 202 | talloc_report_full(tall_pcu_ctx, stderr); |
| 203 | talloc_free(tall_pcu_ctx); |
| 204 | return EXIT_SUCCESS; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * stubs that should not be reached |
| 209 | */ |
| 210 | extern "C" { |
| 211 | void l1if_pdch_req() { abort(); } |
| 212 | void l1if_connect_pdch() { abort(); } |
| 213 | void l1if_close_pdch() { abort(); } |
| 214 | void l1if_open_pdch() { abort(); } |
| 215 | } |
| 216 | |