blob: 2a5d0e1c2b63e414637af45c0464ab12a92d709b [file] [log] [blame]
Jacob Erlbeck0acc0012014-12-03 13:05:16 +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/db.h>
23#include <openbsc/gsm_subscriber.h>
24#include <openbsc/gsm_04_11.h>
25
26#include <osmocom/core/application.h>
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <inttypes.h>
32
33static struct gsm_network dummy_net;
34static struct gsm_subscriber_group dummy_sgrp;
35
36static void test_subscr(void)
37{
Jacob Erlbeck0acc0012014-12-03 13:05:16 +010038 struct gsm_subscriber *subscr;
39 const char *imsi = "1234567890";
40
41 printf("Test subscriber allocation and deletion\n");
42
43 /* Don't keep subscr */
44
45 dummy_sgrp.keep_subscr = 0;
46
47 OSMO_ASSERT(llist_empty(&active_subscribers));
48
49 subscr = subscr_get_or_create(&dummy_sgrp, imsi);
50
51 OSMO_ASSERT(!llist_empty(&active_subscribers));
52 OSMO_ASSERT(subscr->use_count == 1);
53
54 subscr_put(subscr);
55
56 OSMO_ASSERT(llist_empty(&active_subscribers));
57
58 /* Keep subscr */
59
60 dummy_sgrp.keep_subscr = 1;
61
62 subscr = subscr_get_or_create(&dummy_sgrp, imsi);
63
64 OSMO_ASSERT(!llist_empty(&active_subscribers));
65 OSMO_ASSERT(subscr->use_count == 1);
66
67 subscr_put(subscr);
68 OSMO_ASSERT(!llist_empty(&active_subscribers));
69 OSMO_ASSERT(subscr->use_count == 0);
70
71 subscr_get(subscr);
72 OSMO_ASSERT(subscr->use_count == 1);
73
74 subscr_purge_inactive(&dummy_sgrp);
75
76 OSMO_ASSERT(!llist_empty(&active_subscribers));
77 OSMO_ASSERT(subscr->use_count == 1);
78
79 subscr_put(subscr);
80 OSMO_ASSERT(!llist_empty(&active_subscribers));
81 OSMO_ASSERT(subscr->use_count == 0);
82
83 subscr_purge_inactive(&dummy_sgrp);
84
85 OSMO_ASSERT(llist_empty(&active_subscribers));
Jacob Erlbeck70d8e312014-12-03 11:08:23 +010086
87 /* Test force_no_keep */
88
89 dummy_sgrp.keep_subscr = 0;
90
91 subscr = subscr_get_or_create(&dummy_sgrp, imsi);
92 OSMO_ASSERT(subscr);
93 subscr->keep_in_ram = 1;
94
95 OSMO_ASSERT(!llist_empty(&active_subscribers));
96 OSMO_ASSERT(subscr->use_count == 1);
97
98 subscr->keep_in_ram = 0;
99
100 subscr_put(subscr);
101 OSMO_ASSERT(llist_empty(&active_subscribers));
Jacob Erlbeck0acc0012014-12-03 13:05:16 +0100102}
103
104int main()
105{
Jacob Erlbeck0acc0012014-12-03 13:05:16 +0100106 printf("Testing subscriber core code.\n");
107 osmo_init_logging(&log_info);
108 log_set_print_filename(osmo_stderr_target, 0);
109
110 dummy_net.subscr_group = &dummy_sgrp;
111 dummy_sgrp.net = &dummy_net;
112
113 test_subscr();
114
115 printf("Done\n");
116 return 0;
117}