blob: 3c94b8662c0df783d8490ae281d0a0e7adb05531 [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
Neels Hofmeyrc0164792017-09-04 15:15:32 +020021#include <osmocom/bsc/debug.h>
22#include <osmocom/bsc/bsc_subscriber.h>
Neels Hofmeyr6d804b12017-02-18 22:20:46 +010023
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
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100113static const struct log_info_cat log_categories[] = {
114 [DREF] = {
115 .name = "DREF",
116 .description = "Reference Counting",
117 .enabled = 1, .loglevel = LOGL_DEBUG,
118 },
119};
120
121static const struct log_info log_info = {
122 .cat = log_categories,
123 .num_cat = ARRAY_SIZE(log_categories),
124};
125
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100126int main()
127{
Neels Hofmeyre3416182018-03-05 05:31:14 +0100128 void *ctx = talloc_named_const(NULL, 0, "bsc_subscr_test");
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100129 printf("Testing BSC subscriber core code.\n");
Neels Hofmeyre3416182018-03-05 05:31:14 +0100130 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100131 log_set_print_filename(osmo_stderr_target, 0);
132 log_set_print_timestamp(osmo_stderr_target, 0);
133 log_set_use_color(osmo_stderr_target, 0);
134 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100135
Neels Hofmeyre3416182018-03-05 05:31:14 +0100136 bsc_subscribers = talloc_zero(ctx, struct llist_head);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +0100137 INIT_LLIST_HEAD(bsc_subscribers);
138
139 test_bsc_subscr();
140
141 printf("Done\n");
142 return 0;
143}