Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <osmocom/core/utils.h> |
| 4 | #include <osmocom/core/msgb.h> |
| 5 | #include <osmocom/gsm/bsslap.h> |
| 6 | |
| 7 | struct bsslap_pdu bsslap_test_pdus[] = { |
| 8 | { |
| 9 | .msg_type = BSSLAP_MSGT_TA_REQUEST, |
| 10 | }, |
| 11 | { |
| 12 | .msg_type = BSSLAP_MSGT_TA_RESPONSE, |
| 13 | .ta_response = { |
| 14 | .cell_id = 23, |
| 15 | .ta = 42, |
| 16 | }, |
| 17 | }, |
| 18 | { |
| 19 | .msg_type = BSSLAP_MSGT_REJECT, |
| 20 | .reject = BSSLAP_CAUSE_OTHER_RADIO_EVT_FAIL, |
| 21 | }, |
| 22 | { |
| 23 | .msg_type = BSSLAP_MSGT_RESET, |
| 24 | .reset = { |
| 25 | .cell_id = 23, |
| 26 | .ta = 42, |
| 27 | .chan_desc = { |
| 28 | .chan_nr = 23, |
| 29 | .h0 = { |
| 30 | .tsc = 5, |
| 31 | .h = 1, |
| 32 | .arfcn_high = 2, |
| 33 | .arfcn_low = 3, |
| 34 | }, |
| 35 | }, |
| 36 | .cause = BSSLAP_CAUSE_INTRA_BSS_HO, |
| 37 | }, |
| 38 | }, |
| 39 | { |
| 40 | .msg_type = BSSLAP_MSGT_ABORT, |
| 41 | .abort = BSSLAP_CAUSE_LOSS_SIG_CONN_MS, |
| 42 | }, |
| 43 | { |
| 44 | .msg_type = BSSLAP_MSGT_TA_LAYER3, |
| 45 | .ta_layer3 = { |
| 46 | .ta = 23, |
| 47 | }, |
| 48 | }, |
| 49 | }; |
| 50 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 51 | void test_bsslap_enc_dec(void) |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 52 | { |
| 53 | struct bsslap_pdu *pdu; |
| 54 | printf("--- %s\n", __func__); |
| 55 | |
| 56 | for (pdu = bsslap_test_pdus; (pdu - bsslap_test_pdus) < ARRAY_SIZE(bsslap_test_pdus); pdu++) { |
| 57 | struct msgb *msg = msgb_alloc(1024, __func__); |
| 58 | struct bsslap_pdu dec_pdu; |
| 59 | struct osmo_bsslap_err *err; |
| 60 | int rc; |
| 61 | void *loop_ctx = msg; |
| 62 | rc = osmo_bsslap_enc(msg, pdu); |
| 63 | if (rc <= 0) { |
Harald Welte | f445150 | 2020-10-09 09:37:36 +0200 | [diff] [blame] | 64 | printf("[%td] %s: ERROR: failed to encode pdu\n", (pdu - bsslap_test_pdus), |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 65 | osmo_bsslap_msgt_name(pdu->msg_type)); |
| 66 | goto loop_end; |
| 67 | } |
| 68 | if (rc != msg->len) { |
Harald Welte | f445150 | 2020-10-09 09:37:36 +0200 | [diff] [blame] | 69 | printf("[%td] %s: ERROR: osmo_bsslap_enc() returned length %d but msgb has %d bytes\n", |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 70 | (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type), |
| 71 | rc, msg->len); |
| 72 | goto loop_end; |
| 73 | } |
| 74 | |
| 75 | memset(&dec_pdu, 0xff, sizeof(dec_pdu)); |
| 76 | rc = osmo_bsslap_dec(&dec_pdu, &err, loop_ctx, msg->data, msg->len); |
| 77 | if (rc) { |
Harald Welte | f445150 | 2020-10-09 09:37:36 +0200 | [diff] [blame] | 78 | printf("[%td] %s: ERROR: failed to decode pdu: %s\n", (pdu - bsslap_test_pdus), |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 79 | osmo_bsslap_msgt_name(pdu->msg_type), err->logmsg); |
| 80 | printf(" encoded data: %s\n", osmo_hexdump(msg->data, msg->len)); |
| 81 | goto loop_end; |
| 82 | } |
| 83 | |
| 84 | if (memcmp(pdu, &dec_pdu, sizeof(dec_pdu))) { |
Harald Welte | f445150 | 2020-10-09 09:37:36 +0200 | [diff] [blame] | 85 | printf("[%td] %s: ERROR: decoded PDU != encoded PDU\n", (pdu - bsslap_test_pdus), |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 86 | osmo_bsslap_msgt_name(pdu->msg_type)); |
| 87 | printf(" original struct: %s\n", osmo_hexdump((void*)pdu, sizeof(*pdu))); |
| 88 | printf(" decoded struct: %s\n", osmo_hexdump((void*)&dec_pdu, sizeof(dec_pdu))); |
| 89 | goto loop_end; |
| 90 | } |
| 91 | |
Harald Welte | f445150 | 2020-10-09 09:37:36 +0200 | [diff] [blame] | 92 | printf("[%td] %s: ok\n", (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type)); |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 93 | |
| 94 | loop_end: |
| 95 | msgb_free(msg); |
| 96 | } |
| 97 | } |
| 98 | |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 99 | int main(int argc, char **argv) |
Neels Hofmeyr | c6848f4 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 100 | { |
| 101 | test_bsslap_enc_dec(); |
| 102 | return 0; |
| 103 | } |