Neels Hofmeyr | 7299215 | 2020-09-19 02:36:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * |
| 4 | * Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <osmocom/smlc/debug.h> |
| 24 | #include <osmocom/smlc/smlc_data.h> |
| 25 | #include <osmocom/smlc/smlc_subscr.h> |
| 26 | |
| 27 | #include <osmocom/core/application.h> |
| 28 | #include <osmocom/core/utils.h> |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <inttypes.h> |
| 34 | |
| 35 | struct smlc_state *g_smlc; |
| 36 | |
| 37 | #define VERBOSE_ASSERT(val, expect_op, fmt) \ |
| 38 | do { \ |
| 39 | printf(#val " == " fmt "\n", (val)); \ |
| 40 | OSMO_ASSERT((val) expect_op); \ |
| 41 | } while (0); |
| 42 | |
| 43 | #define USE_FOO "foo" |
| 44 | #define USE_BAR "bar" |
| 45 | |
| 46 | static void assert_smlc_subscr(const struct smlc_subscr *smlc_subscr, const struct osmo_mobile_identity *imsi) |
| 47 | { |
| 48 | struct smlc_subscr *sfound; |
| 49 | OSMO_ASSERT(smlc_subscr); |
| 50 | OSMO_ASSERT(osmo_mobile_identity_cmp(&smlc_subscr->imsi, imsi) == 0); |
| 51 | |
| 52 | sfound = smlc_subscr_find(imsi, __func__); |
| 53 | OSMO_ASSERT(sfound == smlc_subscr); |
| 54 | |
| 55 | smlc_subscr_put(sfound, __func__); |
| 56 | } |
| 57 | |
| 58 | static void test_smlc_subscr(void) |
| 59 | { |
| 60 | struct smlc_subscr *s1, *s2, *s3; |
| 61 | const struct osmo_mobile_identity imsi1 = { .type = GSM_MI_TYPE_IMSI, .imsi = "1234567890", }; |
| 62 | const struct osmo_mobile_identity imsi2 = { .type = GSM_MI_TYPE_IMSI, .imsi = "9876543210", }; |
| 63 | const struct osmo_mobile_identity imsi3 = { .type = GSM_MI_TYPE_IMSI, .imsi = "423423", }; |
| 64 | |
| 65 | printf("Test SMLC subscriber allocation and deletion\n"); |
| 66 | |
| 67 | /* Check for emptiness */ |
| 68 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 0, "%d"); |
| 69 | OSMO_ASSERT(smlc_subscr_find(&imsi1, "-") == NULL); |
| 70 | OSMO_ASSERT(smlc_subscr_find(&imsi2, "-") == NULL); |
| 71 | OSMO_ASSERT(smlc_subscr_find(&imsi3, "-") == NULL); |
| 72 | |
| 73 | /* Allocate entry 1 */ |
| 74 | s1 = smlc_subscr_find_or_create(&imsi1, USE_FOO); |
| 75 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 1, "%d"); |
| 76 | assert_smlc_subscr(s1, &imsi1); |
| 77 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 1, "%d"); |
| 78 | OSMO_ASSERT(smlc_subscr_find(&imsi2, "-") == NULL); |
| 79 | |
| 80 | /* Allocate entry 2 */ |
| 81 | s2 = smlc_subscr_find_or_create(&imsi2, USE_BAR); |
| 82 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 2, "%d"); |
| 83 | |
| 84 | /* Allocate entry 3 */ |
| 85 | s3 = smlc_subscr_find_or_create(&imsi3, USE_FOO); |
| 86 | smlc_subscr_get(s3, USE_BAR); |
| 87 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 3, "%d"); |
| 88 | |
| 89 | /* Check entries */ |
| 90 | assert_smlc_subscr(s1, &imsi1); |
| 91 | assert_smlc_subscr(s2, &imsi2); |
| 92 | assert_smlc_subscr(s3, &imsi3); |
| 93 | |
| 94 | /* Free entry 1 */ |
| 95 | smlc_subscr_put(s1, USE_FOO); |
| 96 | s1 = NULL; |
| 97 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 2, "%d"); |
| 98 | OSMO_ASSERT(smlc_subscr_find(&imsi1, "-") == NULL); |
| 99 | |
| 100 | assert_smlc_subscr(s2, &imsi2); |
| 101 | assert_smlc_subscr(s3, &imsi3); |
| 102 | |
| 103 | /* Free entry 2 */ |
| 104 | smlc_subscr_put(s2, USE_BAR); |
| 105 | s2 = NULL; |
| 106 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 1, "%d"); |
| 107 | OSMO_ASSERT(smlc_subscr_find(&imsi1, "-") == NULL); |
| 108 | OSMO_ASSERT(smlc_subscr_find(&imsi2, "-") == NULL); |
| 109 | assert_smlc_subscr(s3, &imsi3); |
| 110 | |
| 111 | /* Remove one use of entry 3 */ |
| 112 | smlc_subscr_put(s3, USE_BAR); |
| 113 | assert_smlc_subscr(s3, &imsi3); |
| 114 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 1, "%d"); |
| 115 | |
| 116 | /* Free entry 3 */ |
| 117 | smlc_subscr_put(s3, USE_FOO); |
| 118 | s3 = NULL; |
| 119 | VERBOSE_ASSERT(llist_count(&g_smlc->subscribers), == 0, "%d"); |
| 120 | OSMO_ASSERT(smlc_subscr_find(&imsi3, "-") == NULL); |
| 121 | |
| 122 | OSMO_ASSERT(llist_empty(&g_smlc->subscribers)); |
| 123 | } |
| 124 | |
| 125 | static const struct log_info_cat log_categories[] = { |
| 126 | [DREF] = { |
| 127 | .name = "DREF", |
| 128 | .description = "Reference Counting", |
| 129 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 130 | }, |
| 131 | }; |
| 132 | |
| 133 | static const struct log_info log_info = { |
| 134 | .cat = log_categories, |
| 135 | .num_cat = ARRAY_SIZE(log_categories), |
| 136 | }; |
| 137 | |
| 138 | int main() |
| 139 | { |
| 140 | void *ctx = talloc_named_const(NULL, 0, "smlc_subscr_test"); |
| 141 | |
| 142 | osmo_init_logging2(ctx, &log_info); |
| 143 | log_set_print_filename(osmo_stderr_target, 0); |
| 144 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 145 | log_set_use_color(osmo_stderr_target, 0); |
| 146 | log_set_print_category(osmo_stderr_target, 1); |
| 147 | |
| 148 | g_smlc = smlc_state_alloc(ctx); |
| 149 | |
| 150 | printf("Testing SMLC subscriber code.\n"); |
| 151 | |
| 152 | test_smlc_subscr(); |
| 153 | |
| 154 | printf("Done\n"); |
| 155 | return 0; |
| 156 | } |
| 157 | |