blob: 60d687d584be28c47b3ec567b79fad6e0c3bcb6c [file] [log] [blame]
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
2 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2014 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <openbsc/debug.h>
22#include <openbsc/bsc_subscriber.h>
23
24#include <osmocom/core/application.h>
25#include <osmocom/core/utils.h>
26
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30#include <inttypes.h>
31
32struct llist_head *bsc_subscribers;
33
34#define VERBOSE_ASSERT(val, expect_op, fmt) \
35 do { \
36 printf(#val " == " fmt "\n", (val)); \
37 OSMO_ASSERT((val) expect_op); \
38 } while (0);
39
40static void assert_bsc_subscr(const struct bsc_subscr *bsub, const char *imsi)
41{
42 struct bsc_subscr *sfound;
43 OSMO_ASSERT(bsub);
44 OSMO_ASSERT(strcmp(bsub->imsi, imsi) == 0);
45
46 sfound = bsc_subscr_find_by_imsi(bsc_subscribers, imsi);
47 OSMO_ASSERT(sfound == bsub);
48
49 bsc_subscr_put(sfound);
50}
51
52static void test_bsc_subscr(void)
53{
54 struct bsc_subscr *s1, *s2, *s3;
55 const char *imsi1 = "1234567890";
56 const char *imsi2 = "9876543210";
57 const char *imsi3 = "5656565656";
58
59 printf("Test BSC subscriber allocation and deletion\n");
60
61 /* Check for emptiness */
62 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 0, "%d");
63 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi1) == NULL);
64 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi2) == NULL);
65 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi3) == NULL);
66
67 /* Allocate entry 1 */
68 s1 = bsc_subscr_find_or_create_by_imsi(bsc_subscribers, imsi1);
69 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 1, "%d");
70 assert_bsc_subscr(s1, imsi1);
71 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 1, "%d");
72 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi2) == NULL);
73
74 /* Allocate entry 2 */
75 s2 = bsc_subscr_find_or_create_by_imsi(bsc_subscribers, imsi2);
76 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 2, "%d");
77
78 /* Allocate entry 3 */
79 s3 = bsc_subscr_find_or_create_by_imsi(bsc_subscribers, imsi3);
80 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 3, "%d");
81
82 /* Check entries */
83 assert_bsc_subscr(s1, imsi1);
84 assert_bsc_subscr(s2, imsi2);
85 assert_bsc_subscr(s3, imsi3);
86
87 /* Free entry 1 */
88 bsc_subscr_put(s1);
89 s1 = NULL;
90 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 2, "%d");
91 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi1) == NULL);
92
93 assert_bsc_subscr(s2, imsi2);
94 assert_bsc_subscr(s3, imsi3);
95
96 /* Free entry 2 */
97 bsc_subscr_put(s2);
98 s2 = NULL;
99 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 1, "%d");
100 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi1) == NULL);
101 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi2) == NULL);
102 assert_bsc_subscr(s3, imsi3);
103
104 /* Free entry 3 */
105 bsc_subscr_put(s3);
106 s3 = NULL;
107 VERBOSE_ASSERT(llist_count(bsc_subscribers), == 0, "%d");
108 OSMO_ASSERT(bsc_subscr_find_by_imsi(bsc_subscribers, imsi3) == NULL);
109
110 OSMO_ASSERT(llist_empty(bsc_subscribers));
111}
112
113int main()
114{
115 printf("Testing BSC subscriber core code.\n");
116 osmo_init_logging(&log_info);
117 log_set_print_filename(osmo_stderr_target, 0);
118 log_set_print_timestamp(osmo_stderr_target, 0);
119 log_set_use_color(osmo_stderr_target, 0);
120 log_set_print_category(osmo_stderr_target, 1);
121 log_set_category_filter(osmo_stderr_target, DREF, 1, LOGL_DEBUG);
122
123 bsc_subscribers = talloc_zero(NULL, struct llist_head);
124 INIT_LLIST_HEAD(bsc_subscribers);
125
126 test_bsc_subscr();
127
128 printf("Done\n");
129 return 0;
130}