blob: fc5ce754b6249dffd39b6b08d6f8e8a45a98d2ec [file] [log] [blame]
Neels Hofmeyrc6848f42020-09-18 18:00:50 +02001#include <stdio.h>
2
3#include <osmocom/core/utils.h>
4#include <osmocom/core/msgb.h>
5#include <osmocom/gsm/bsslap.h>
6
7struct 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 Weltee61d4592022-11-03 11:05:58 +010051void test_bsslap_enc_dec(void)
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020052{
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 Weltef4451502020-10-09 09:37:36 +020064 printf("[%td] %s: ERROR: failed to encode pdu\n", (pdu - bsslap_test_pdus),
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020065 osmo_bsslap_msgt_name(pdu->msg_type));
66 goto loop_end;
67 }
68 if (rc != msg->len) {
Harald Weltef4451502020-10-09 09:37:36 +020069 printf("[%td] %s: ERROR: osmo_bsslap_enc() returned length %d but msgb has %d bytes\n",
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020070 (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 Weltef4451502020-10-09 09:37:36 +020078 printf("[%td] %s: ERROR: failed to decode pdu: %s\n", (pdu - bsslap_test_pdus),
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020079 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 Weltef4451502020-10-09 09:37:36 +020085 printf("[%td] %s: ERROR: decoded PDU != encoded PDU\n", (pdu - bsslap_test_pdus),
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020086 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 Weltef4451502020-10-09 09:37:36 +020092 printf("[%td] %s: ok\n", (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type));
Neels Hofmeyrc6848f42020-09-18 18:00:50 +020093
94loop_end:
95 msgb_free(msg);
96 }
97}
98
Harald Weltee61d4592022-11-03 11:05:58 +010099int main(int argc, char **argv)
Neels Hofmeyrc6848f42020-09-18 18:00:50 +0200100{
101 test_bsslap_enc_dec();
102 return 0;
103}