aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fn/FnTest.cpp
blob: 279903c8f9f8be8644e83291c664a9c1bf88e676 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Frame number calculation test */

/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Philipp Maier
 *
 * 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 Affero 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 "bts.h"
#include <string.h>
#include <stdio.h>

extern "C" {
#include <osmocom/core/application.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/utils.h>
}

#define RFN_MODULUS 42432

/* globals used by the code */ void *tall_pcu_ctx;
int16_t spoof_mnc = 0, spoof_mcc = 0;

static uint32_t calc_fn(BTS * bts, uint32_t rfn)
{
	uint32_t fn;
	fn = bts->rfn_to_fn(rfn);
	printf("rfn=%i ==> fn=%i\n", rfn, fn);
	return fn;
}

static void set_fn(BTS * bts, uint32_t fn)
{
	printf("\n");
	bts->set_current_frame_number(fn);
	printf("bts: fn=%i\n", fn);
}

static void run_test()
{
	BTS bts;
	uint32_t fn;

	printf("RFN_MODULUS=%i\n",RFN_MODULUS);
	printf("GSM_MAX_FN=%i\n",GSM_MAX_FN);


	/* Test with a collection of real world examples,
	 * all all of them are not critical and do not
	 * assume the occurence of any race contions */
	set_fn(&bts, 1320462);
	fn = calc_fn(&bts, 5066);
	OSMO_ASSERT(fn == 1320458);

	set_fn(&bts, 8246);
	fn = calc_fn(&bts, 8244);
	OSMO_ASSERT(fn == 8244);

	set_fn(&bts, 10270);
	fn = calc_fn(&bts, 10269);
	OSMO_ASSERT(fn == 10269);

	set_fn(&bts, 311276);
	fn = calc_fn(&bts, 14250);
	OSMO_ASSERT(fn == 311274);


	/* Now lets assume a case where the frame number
	 * just wrapped over a little bit above the
	 * modulo 42432 raster, but the rach request
	 * occurred before the wrapping */
	set_fn(&bts, RFN_MODULUS + 30);
	fn = calc_fn(&bts, RFN_MODULUS - 10);
	OSMO_ASSERT(fn == 42422);

	set_fn(&bts, RFN_MODULUS + 1);
	fn = calc_fn(&bts, RFN_MODULUS - 1);
	OSMO_ASSERT(fn == 42431);

	set_fn(&bts, RFN_MODULUS * 123 + 16);
	fn = calc_fn(&bts, RFN_MODULUS - 4);
	OSMO_ASSERT(fn == 5219132);

	set_fn(&bts, RFN_MODULUS * 123 + 451);
	fn = calc_fn(&bts, RFN_MODULUS - 175);
	OSMO_ASSERT(fn == 5218961);


	/* Lets check a special cornercase. We assume that
	 * the BTS just wrapped its internal frame number
	 * but we still get rach requests with high relative
	 * frame numbers. */
	set_fn(&bts, 0);
	fn = calc_fn(&bts, RFN_MODULUS - 13);
	OSMO_ASSERT(fn == 2715635);

	set_fn(&bts, 453);
	fn = calc_fn(&bts, RFN_MODULUS - 102);
	OSMO_ASSERT(fn == 2715546);

	set_fn(&bts, 10);
	fn = calc_fn(&bts, RFN_MODULUS - 10);
	OSMO_ASSERT(fn == 2715638);

	set_fn(&bts, 23);
	fn = calc_fn(&bts, RFN_MODULUS - 42);
	OSMO_ASSERT(fn == 2715606);


	/* Also check with some corner case
	 * values where Fn and RFn reach its
	 * maximum/minimum valid range */
	set_fn(&bts, GSM_MAX_FN);
	fn = calc_fn(&bts, RFN_MODULUS-1);
	OSMO_ASSERT(fn == GSM_MAX_FN-1);

	set_fn(&bts, 0);
	fn = calc_fn(&bts, RFN_MODULUS-1);
	OSMO_ASSERT(fn == GSM_MAX_FN-1);

	set_fn(&bts, GSM_MAX_FN);
	fn = calc_fn(&bts, 0);
	OSMO_ASSERT(fn == GSM_MAX_FN);

	set_fn(&bts, 0);
	fn = calc_fn(&bts, 0);
	OSMO_ASSERT(fn == 0);
}

int main(int argc, char **argv)
{
	tall_pcu_ctx = talloc_named_const(NULL, 1, "fn test context");
	if (!tall_pcu_ctx)
		abort();

	msgb_talloc_ctx_init(tall_pcu_ctx, 0);
	osmo_init_logging(&gprs_log_info);
	log_set_use_color(osmo_stderr_target, 0);
	log_set_print_filename(osmo_stderr_target, 0);
	log_set_log_level(osmo_stderr_target, LOGL_DEBUG);

	run_test();
	return EXIT_SUCCESS;
}

/*
 * stubs that should not be reached
 */
extern "C" {
	void l1if_pdch_req() {
		abort();
	} void l1if_connect_pdch() {
		abort();
	}
	void l1if_close_pdch() {
		abort();
	}
	void l1if_open_pdch() {
		abort();
	}
}