aboutsummaryrefslogtreecommitdiffstats
path: root/tests/bsslap/bsslap_test.c
blob: fc5ce754b6249dffd39b6b08d6f8e8a45a98d2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>

#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/gsm/bsslap.h>

struct bsslap_pdu bsslap_test_pdus[] = {
	{
		.msg_type = BSSLAP_MSGT_TA_REQUEST,
	},
	{
		.msg_type = BSSLAP_MSGT_TA_RESPONSE,
		.ta_response = {
			.cell_id = 23,
			.ta = 42,
		},
	},
	{
		.msg_type = BSSLAP_MSGT_REJECT,
		.reject = BSSLAP_CAUSE_OTHER_RADIO_EVT_FAIL,
	},
	{
		.msg_type = BSSLAP_MSGT_RESET,
		.reset = {
			.cell_id = 23,
			.ta = 42,
			.chan_desc =  {
				.chan_nr = 23,
				.h0 = {
					.tsc = 5,
					.h = 1,
					.arfcn_high = 2,
					.arfcn_low = 3,
				},
			},
			.cause = BSSLAP_CAUSE_INTRA_BSS_HO,
		},
	},
	{
		.msg_type = BSSLAP_MSGT_ABORT,
		.abort = BSSLAP_CAUSE_LOSS_SIG_CONN_MS,
	},
	{
		.msg_type = BSSLAP_MSGT_TA_LAYER3,
		.ta_layer3 = {
			.ta = 23,
		},
	},
};

void test_bsslap_enc_dec(void)
{
	struct bsslap_pdu *pdu;
	printf("--- %s\n", __func__);

	for (pdu = bsslap_test_pdus; (pdu - bsslap_test_pdus) < ARRAY_SIZE(bsslap_test_pdus); pdu++) {
		struct msgb *msg = msgb_alloc(1024, __func__);
		struct bsslap_pdu dec_pdu;
		struct osmo_bsslap_err *err;
		int rc;
		void *loop_ctx = msg;
		rc = osmo_bsslap_enc(msg, pdu);
		if (rc <= 0) {
			printf("[%td] %s: ERROR: failed to encode pdu\n", (pdu - bsslap_test_pdus),
			       osmo_bsslap_msgt_name(pdu->msg_type));
			goto loop_end;
		}
		if (rc != msg->len) {
			printf("[%td] %s: ERROR: osmo_bsslap_enc() returned length %d but msgb has %d bytes\n",
			       (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type),
			       rc, msg->len);
			goto loop_end;
		}

		memset(&dec_pdu, 0xff, sizeof(dec_pdu));
		rc = osmo_bsslap_dec(&dec_pdu, &err, loop_ctx, msg->data, msg->len);
		if (rc) {
			printf("[%td] %s: ERROR: failed to decode pdu: %s\n", (pdu - bsslap_test_pdus),
			       osmo_bsslap_msgt_name(pdu->msg_type), err->logmsg);
			printf("     encoded data: %s\n", osmo_hexdump(msg->data, msg->len));
			goto loop_end;
		}

		if (memcmp(pdu, &dec_pdu, sizeof(dec_pdu))) {
			printf("[%td] %s: ERROR: decoded PDU != encoded PDU\n", (pdu - bsslap_test_pdus),
			       osmo_bsslap_msgt_name(pdu->msg_type));
			printf("     original struct: %s\n", osmo_hexdump((void*)pdu, sizeof(*pdu)));
			printf("      decoded struct: %s\n", osmo_hexdump((void*)&dec_pdu, sizeof(dec_pdu)));
			goto loop_end;
		}

		printf("[%td] %s: ok\n", (pdu - bsslap_test_pdus), osmo_bsslap_msgt_name(pdu->msg_type));

loop_end:
		msgb_free(msg);
	}
}

int main(int argc, char **argv)
{
	test_bsslap_enc_dec();
	return 0;
}