Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <osmocom/core/logging.h> |
| 4 | #include <osmocom/core/utils.h> |
| 5 | #include <osmocom/core/application.h> |
| 6 | #include <osmocom/gsm/gsup.h> |
| 7 | |
| 8 | #define VERBOSE_FPRINTF(...) |
| 9 | |
| 10 | /* Tests for osmo_gsup_messages.c */ |
| 11 | |
| 12 | #define TEST_IMSI_IE 0x01, 0x08, 0x21, 0x43, 0x65, 0x87, 0x09, 0x21, 0x43, 0xf5 |
| 13 | #define TEST_IMSI_STR "123456789012345" |
| 14 | |
| 15 | static void test_gsup_messages_dec_enc(void) |
| 16 | { |
| 17 | int test_idx; |
| 18 | int rc; |
| 19 | uint8_t buf[1024]; |
| 20 | |
| 21 | static const uint8_t send_auth_info_req[] = { |
| 22 | 0x08, |
| 23 | TEST_IMSI_IE |
| 24 | }; |
| 25 | |
| 26 | static const uint8_t send_auth_info_err[] = { |
| 27 | 0x09, |
| 28 | TEST_IMSI_IE, |
| 29 | 0x02, 0x01, 0x07 /* GPRS no allowed */ |
| 30 | }; |
| 31 | |
| 32 | static const uint8_t send_auth_info_res[] = { |
| 33 | 0x0a, |
| 34 | TEST_IMSI_IE, |
| 35 | 0x03, 0x22, /* Auth tuple */ |
| 36 | 0x20, 0x10, |
| 37 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 38 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 39 | 0x21, 0x04, |
| 40 | 0x21, 0x22, 0x23, 0x24, |
| 41 | 0x22, 0x08, |
| 42 | 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 43 | 0x03, 0x22, /* Auth tuple */ |
| 44 | 0x20, 0x10, |
| 45 | 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, |
| 46 | 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, |
| 47 | 0x21, 0x04, |
| 48 | 0xa1, 0xa2, 0xa3, 0xa4, |
| 49 | 0x22, 0x08, |
| 50 | 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, |
| 51 | }; |
| 52 | |
| 53 | static const uint8_t update_location_req[] = { |
| 54 | 0x04, |
| 55 | TEST_IMSI_IE, |
| 56 | }; |
| 57 | |
| 58 | static const uint8_t update_location_err[] = { |
| 59 | 0x05, |
| 60 | TEST_IMSI_IE, |
| 61 | 0x02, 0x01, 0x07 /* GPRS no allowed */ |
| 62 | }; |
| 63 | |
| 64 | static const uint8_t update_location_res[] = { |
| 65 | 0x06, |
| 66 | TEST_IMSI_IE, |
| 67 | 0x08, 0x07, /* MSISDN of the subscriber */ |
| 68 | 0x91, 0x94, 0x61, 0x46, 0x32, 0x24, 0x43, |
| 69 | 0x09, 0x07, /* HLR-Number of the subscriber */ |
| 70 | 0x91, 0x83, 0x52, 0x38, 0x48, 0x83, 0x93, |
| 71 | 0x04, 0x00, /* PDP info complete */ |
| 72 | 0x05, 0x15, |
| 73 | 0x10, 0x01, 0x01, |
| 74 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 75 | 0x12, 0x09, 0x04, 't', 'e', 's', 't', 0x03, 'a', 'p', 'n', |
| 76 | 0x13, 0x01, 0x02, |
| 77 | 0x05, 0x11, |
| 78 | 0x10, 0x01, 0x02, |
| 79 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 80 | 0x12, 0x08, 0x03, 'f', 'o', 'o', 0x03, 'a', 'p', 'n', |
| 81 | }; |
| 82 | |
| 83 | static const uint8_t location_cancellation_req[] = { |
| 84 | 0x1c, |
| 85 | TEST_IMSI_IE, |
| 86 | 0x06, 0x01, 0x00, |
| 87 | }; |
| 88 | |
| 89 | static const uint8_t location_cancellation_err[] = { |
| 90 | 0x1d, |
| 91 | TEST_IMSI_IE, |
| 92 | 0x02, 0x01, 0x03 /* Illegal MS */ |
| 93 | }; |
| 94 | |
| 95 | static const uint8_t location_cancellation_res[] = { |
| 96 | 0x1e, |
| 97 | TEST_IMSI_IE, |
| 98 | }; |
| 99 | |
| 100 | static const uint8_t purge_ms_req[] = { |
| 101 | 0x0c, |
| 102 | TEST_IMSI_IE, |
| 103 | }; |
| 104 | |
| 105 | static const uint8_t purge_ms_err[] = { |
| 106 | 0x0d, |
| 107 | TEST_IMSI_IE, |
| 108 | 0x02, 0x01, 0x03, /* Illegal MS */ |
| 109 | }; |
| 110 | |
| 111 | static const uint8_t purge_ms_res[] = { |
| 112 | 0x0e, |
| 113 | TEST_IMSI_IE, |
| 114 | 0x07, 0x00, |
| 115 | }; |
| 116 | |
| 117 | static const struct test { |
| 118 | char *name; |
| 119 | const uint8_t *data; |
| 120 | size_t data_len; |
| 121 | } test_messages[] = { |
| 122 | {"Send Authentication Info Request", |
| 123 | send_auth_info_req, sizeof(send_auth_info_req)}, |
| 124 | {"Send Authentication Info Error", |
| 125 | send_auth_info_err, sizeof(send_auth_info_err)}, |
| 126 | {"Send Authentication Info Result", |
| 127 | send_auth_info_res, sizeof(send_auth_info_res)}, |
| 128 | {"Update Location Request", |
| 129 | update_location_req, sizeof(update_location_req)}, |
| 130 | {"Update Location Error", |
| 131 | update_location_err, sizeof(update_location_err)}, |
| 132 | {"Update Location Result", |
| 133 | update_location_res, sizeof(update_location_res)}, |
| 134 | {"Location Cancellation Request", |
| 135 | location_cancellation_req, sizeof(location_cancellation_req)}, |
| 136 | {"Location Cancellation Error", |
| 137 | location_cancellation_err, sizeof(location_cancellation_err)}, |
| 138 | {"Location Cancellation Result", |
| 139 | location_cancellation_res, sizeof(location_cancellation_res)}, |
| 140 | {"Purge MS Request", |
| 141 | purge_ms_req, sizeof(purge_ms_req)}, |
| 142 | {"Purge MS Error", |
| 143 | purge_ms_err, sizeof(purge_ms_err)}, |
| 144 | {"Purge MS Result", |
| 145 | purge_ms_res, sizeof(purge_ms_res)}, |
| 146 | }; |
| 147 | |
| 148 | printf("Test GSUP message decoding/encoding\n"); |
| 149 | |
| 150 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 151 | const struct test *t = &test_messages[test_idx]; |
| 152 | struct osmo_gsup_message gm = {0}; |
| 153 | struct msgb *msg = msgb_alloc(4096, "gsup_test"); |
| 154 | |
| 155 | printf(" Testing %s\n", t->name); |
| 156 | |
| 157 | rc = osmo_gsup_decode(t->data, t->data_len, &gm); |
| 158 | OSMO_ASSERT(rc >= 0); |
| 159 | |
| 160 | osmo_gsup_encode(msg, &gm); |
| 161 | |
| 162 | fprintf(stderr, " generated message: %s\n", msgb_hexdump(msg)); |
| 163 | fprintf(stderr, " original message: %s\n", osmo_hexdump(t->data, t->data_len)); |
| 164 | fprintf(stderr, " IMSI: %s\n", gm.imsi); |
| 165 | OSMO_ASSERT(strcmp(gm.imsi, TEST_IMSI_STR) == 0); |
| 166 | OSMO_ASSERT(msgb_length(msg) == t->data_len); |
| 167 | OSMO_ASSERT(memcmp(msgb_data(msg), t->data, t->data_len) == 0); |
| 168 | |
| 169 | msgb_free(msg); |
| 170 | } |
| 171 | |
| 172 | /* simple truncation test */ |
| 173 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 174 | int j; |
| 175 | const struct test *t = &test_messages[test_idx]; |
| 176 | int ie_end = t->data_len; |
| 177 | struct osmo_gsup_message gm = {0}; |
| 178 | int counter = 0; |
| 179 | int parse_err = 0; |
| 180 | |
| 181 | for (j = t->data_len - 1; j >= 0; --j) { |
| 182 | rc = osmo_gsup_decode(t->data, j, &gm); |
| 183 | counter += 1; |
| 184 | |
| 185 | VERBOSE_FPRINTF(stderr, |
| 186 | " partial message decoding: " |
| 187 | "orig_len = %d, trunc = %d, rc = %d, ie_end = %d\n", |
| 188 | t->data_len, j, rc, ie_end); |
| 189 | if (rc >= 0) { |
| 190 | VERBOSE_FPRINTF(stderr, |
| 191 | " remaing partial message: %s\n", |
| 192 | osmo_hexdump(t->data + j, ie_end - j)); |
| 193 | |
| 194 | OSMO_ASSERT(j <= ie_end - 2); |
| 195 | OSMO_ASSERT(t->data[j+0] <= OSMO_GSUP_KC_IE); |
| 196 | OSMO_ASSERT(t->data[j+1] <= ie_end - j - 2); |
| 197 | |
| 198 | ie_end = j; |
| 199 | } else { |
| 200 | parse_err += 1; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | fprintf(stderr, |
| 205 | " message %d: tested %d truncations, %d parse failures\n", |
| 206 | test_idx, counter, parse_err); |
| 207 | } |
| 208 | |
| 209 | /* message modification test (relies on ASAN or valgrind being used) */ |
| 210 | for (test_idx = 0; test_idx < ARRAY_SIZE(test_messages); test_idx++) { |
| 211 | int j; |
| 212 | const struct test *t = &test_messages[test_idx]; |
| 213 | struct osmo_gsup_message gm = {0}; |
| 214 | uint8_t val; |
| 215 | int counter = 0; |
| 216 | int parse_err = 0; |
| 217 | |
| 218 | OSMO_ASSERT(sizeof(buf) >= t->data_len); |
| 219 | |
| 220 | for (j = t->data_len - 1; j >= 0; --j) { |
| 221 | memcpy(buf, t->data, t->data_len); |
| 222 | val = 0; |
| 223 | do { |
| 224 | VERBOSE_FPRINTF(stderr, |
| 225 | "t = %d, len = %d, val = %d\n", |
| 226 | test_idx, j, val); |
| 227 | buf[j] = val; |
| 228 | rc = osmo_gsup_decode(buf, t->data_len, &gm); |
| 229 | counter += 1; |
| 230 | if (rc < 0) |
| 231 | parse_err += 1; |
| 232 | |
| 233 | val += 1; |
| 234 | } while (val != (uint8_t)256); |
| 235 | } |
| 236 | |
| 237 | fprintf(stderr, |
| 238 | " message %d: tested %d modifications, %d parse failures\n", |
| 239 | test_idx, counter, parse_err); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | const struct log_info_cat default_categories[] = { |
| 244 | [DLGSUP] = { |
| 245 | .name = "DLGSUP", |
| 246 | .description = "Generic Subscriber Update Protocol", |
| 247 | .enabled = 0, .loglevel = LOGL_DEBUG, |
| 248 | }, |
| 249 | }; |
| 250 | |
| 251 | static struct log_info info = { |
| 252 | .cat = default_categories, |
| 253 | .num_cat = ARRAY_SIZE(default_categories), |
| 254 | }; |
| 255 | |
| 256 | int main(int argc, char **argv) |
| 257 | { |
| 258 | osmo_init_logging(&info); |
| 259 | |
| 260 | test_gsup_messages_dec_enc(); |
| 261 | |
| 262 | printf("Done.\n"); |
| 263 | return EXIT_SUCCESS; |
| 264 | } |