aboutsummaryrefslogtreecommitdiffstats
path: root/tests/misc/misc_test.c
blob: e590394fc804bad992d878db584649b0db068a35 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* testing misc code */

/* (C) 2011 by Holger Hans Peter Freyther
 * (C) 2014 by sysmocom s.f.m.c. GmbH
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmo-bts/bts.h>
#include <osmo-bts/msg_utils.h>
#include <osmo-bts/logging.h>

#include <osmocom/gsm/protocol/ipaccess.h>

#include <stdlib.h>
#include <stdio.h>

static const uint8_t ipa_rsl_connect[] = {
	0x00, 0x1c, 0xff, 0x10, 0x80, 0x00, 0x0a, 0x0d,
	0x63, 0x6f, 0x6d, 0x2e, 0x69, 0x70, 0x61, 0x63,
	0x63, 0x65, 0x73, 0x73, 0x00, 0xe0, 0x04, 0x00,
	0x00, 0xff, 0x85, 0x00, 0x81, 0x0b, 0xbb
};

static const uint8_t osmo_rsl_power[] = {
	0x00, 0x18, 0xff, 0x10, 0x80, 0x00, 0x07, 0x0c,
	0x6f, 0x72, 0x67, 0x2e, 0x6f, 0x73, 0x6d, 0x6f,
	0x63, 0x6f, 0x6d, 0x00, 0x44, 0x02, 0x00, 0x00,
	0xff, 0xfe, 0x04
};

static void test_msg_utils_ipa(void)
{
	struct msgb *msg;
	int rc, size;

	printf("Testing IPA structure\n");

	msg = msgb_alloc(sizeof(ipa_rsl_connect), "IPA test");
	msg->l1h = msgb_put(msg, sizeof(ipa_rsl_connect));
	memcpy(msg->l1h, ipa_rsl_connect, sizeof(ipa_rsl_connect));
	rc = msg_verify_ipa_structure(msg);
	OSMO_ASSERT(rc == 0);
	msgb_free(msg);

	/* test truncated messages and they should fail */
	for (size = sizeof(ipa_rsl_connect) - 1; size >= 0; --size) {
		msg = msgb_alloc(sizeof(ipa_rsl_connect) - 1, "IPA test");
		msg->l1h = msgb_put(msg, size);
		memcpy(msg->l1h, ipa_rsl_connect, size);
		rc = msg_verify_ipa_structure(msg);
		OSMO_ASSERT(rc == -1);
		msgb_free(msg);
	}

	/* change the type of the message */
	msg = msgb_alloc(sizeof(ipa_rsl_connect), "IPA test");
	msg->l1h = msgb_put(msg, sizeof(ipa_rsl_connect));
	memcpy(msg->l1h, ipa_rsl_connect, sizeof(ipa_rsl_connect));
	msg->l1h[2] = 0x23;
	rc = msg_verify_ipa_structure(msg);
	OSMO_ASSERT(rc == -1);
	msgb_free(msg);
}

static void test_oml_data(const uint8_t *data, const size_t len, const int exp)
{
	int rc;
	struct msgb *msg;

	msg = msgb_alloc(len, "IPA test");
	msg->l2h = msgb_put(msg, len);
	memcpy(msg->l2h, data, len);
	rc = msg_verify_oml_structure(msg);
	if (rc >= 0)
		OSMO_ASSERT(msg->l3h > msg->l2h);
	OSMO_ASSERT(rc == exp);
	msgb_free(msg);
}

static void test_msg_utils_oml(void)
{
	static const size_t hh_size = sizeof(struct ipaccess_head);
	int size;

	printf("Testing OML structure\n");

	/* test with IPA message */
	test_oml_data(ipa_rsl_connect + hh_size,
			sizeof(ipa_rsl_connect) - hh_size,
			OML_MSG_TYPE_IPA);

	/* test truncated messages and they should fail */
	for (size = sizeof(ipa_rsl_connect) - hh_size - 1; size >=0; --size)
		test_oml_data(ipa_rsl_connect + hh_size, size, -1);

	/* test with Osmo message */
	test_oml_data(osmo_rsl_power + hh_size,
			sizeof(osmo_rsl_power) - hh_size,
			OML_MSG_TYPE_OSMO);
	for (size = sizeof(osmo_rsl_power) - hh_size - 1; size >=0; --size)
		test_oml_data(osmo_rsl_power + hh_size, size, -1);
}

static void test_sacch_get(void)
{
	struct gsm_lchan lchan;
	int i, off;

	printf("Testing lchan_sacch_get\n");
	memset(&lchan, 0, sizeof(lchan));

	/* initialize the input. */
	for (i = 1; i < _MAX_SYSINFO_TYPE; ++i) {
		lchan.si.valid |= (1 << i);
		memset(&lchan.si.buf[i], i, sizeof(lchan.si.buf[i]));
	}

	/* It will start with '1' */
	for (i = 1, off = 0; i <= 32; ++i) {
		uint8_t *data = lchan_sacch_get(&lchan);
		off = (off + 1) % _MAX_SYSINFO_TYPE;
		if (off == 0)
			off += 1;

		//printf("i=%d (%%=%d) -> data[0]=%d\n", i, off, data[0]);
		OSMO_ASSERT(data[0] == off);
	}
}

int main(int argc, char **argv)
{
	bts_log_init(NULL);

	test_sacch_get();
	test_msg_utils_ipa();
	test_msg_utils_oml();
	return EXIT_SUCCESS;
}