Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 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 <stdio.h> |
| 22 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 23 | #include <osmocom/hlr/gsup_server.h> |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 24 | |
| 25 | #define comment_start() printf("\n===== %s\n", __func__) |
| 26 | #define comment_end() printf("===== %s: SUCCESS\n\n", __func__) |
| 27 | #define btw(fmt, args...) printf("\n" fmt "\n", ## args) |
| 28 | |
| 29 | #define VERBOSE_ASSERT(val, expect_op, fmt) \ |
| 30 | do { \ |
| 31 | printf(#val " == " fmt "\n", (val)); \ |
| 32 | OSMO_ASSERT((val) expect_op); \ |
| 33 | } while (0) |
| 34 | |
| 35 | void osmo_gsup_server_add_conn(struct llist_head *clients, |
| 36 | struct osmo_gsup_conn *conn); |
| 37 | |
| 38 | static void test_add_conn(void) |
| 39 | { |
| 40 | struct llist_head _list; |
| 41 | struct llist_head *clients = &_list; |
| 42 | struct osmo_gsup_conn conn_inst[23] = {}; |
| 43 | struct osmo_gsup_conn *conn; |
| 44 | unsigned int i; |
| 45 | |
| 46 | comment_start(); |
| 47 | |
| 48 | INIT_LLIST_HEAD(clients); |
| 49 | |
| 50 | btw("Add 10 items"); |
| 51 | for (i = 0; i < 10; i++) { |
| 52 | osmo_gsup_server_add_conn(clients, &conn_inst[i]); |
| 53 | printf("conn_inst[%u].auc_3g_ind == %u\n", i, conn_inst[i].auc_3g_ind); |
| 54 | OSMO_ASSERT(clients->next == &conn_inst[0].list); |
| 55 | } |
| 56 | |
| 57 | btw("Expecting a list of 0..9"); |
| 58 | i = 0; |
| 59 | llist_for_each_entry(conn, clients, list) { |
| 60 | printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind); |
| 61 | OSMO_ASSERT(conn->auc_3g_ind == i); |
| 62 | OSMO_ASSERT(conn == &conn_inst[i]); |
| 63 | i++; |
| 64 | } |
| 65 | |
| 66 | btw("Punch two holes in the sequence in arbitrary order," |
| 67 | " a larger one from 2..4 and a single one at 7."); |
| 68 | llist_del(&conn_inst[4].list); |
| 69 | llist_del(&conn_inst[2].list); |
| 70 | llist_del(&conn_inst[3].list); |
| 71 | llist_del(&conn_inst[7].list); |
| 72 | |
| 73 | btw("Expecting a list of 0,1, 5,6, 8,9"); |
| 74 | i = 0; |
| 75 | llist_for_each_entry(conn, clients, list) { |
| 76 | printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind); |
| 77 | i++; |
| 78 | } |
| 79 | |
| 80 | btw("Add conns, expecting them to take the open slots"); |
| 81 | osmo_gsup_server_add_conn(clients, &conn_inst[12]); |
| 82 | VERBOSE_ASSERT(conn_inst[12].auc_3g_ind, == 2, "%u"); |
| 83 | |
| 84 | osmo_gsup_server_add_conn(clients, &conn_inst[13]); |
| 85 | VERBOSE_ASSERT(conn_inst[13].auc_3g_ind, == 3, "%u"); |
| 86 | |
| 87 | osmo_gsup_server_add_conn(clients, &conn_inst[14]); |
| 88 | VERBOSE_ASSERT(conn_inst[14].auc_3g_ind, == 4, "%u"); |
| 89 | |
| 90 | osmo_gsup_server_add_conn(clients, &conn_inst[17]); |
| 91 | VERBOSE_ASSERT(conn_inst[17].auc_3g_ind, == 7, "%u"); |
| 92 | |
| 93 | osmo_gsup_server_add_conn(clients, &conn_inst[18]); |
| 94 | VERBOSE_ASSERT(conn_inst[18].auc_3g_ind, == 10, "%u"); |
| 95 | |
| 96 | btw("Expecting a list of 0..10"); |
| 97 | i = 0; |
| 98 | llist_for_each_entry(conn, clients, list) { |
| 99 | printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind); |
| 100 | OSMO_ASSERT(conn->auc_3g_ind == i); |
| 101 | i++; |
| 102 | } |
| 103 | |
| 104 | btw("Does it also work for the first item?"); |
| 105 | llist_del(&conn_inst[0].list); |
| 106 | |
| 107 | btw("Expecting a list of 1..10"); |
| 108 | i = 0; |
| 109 | llist_for_each_entry(conn, clients, list) { |
| 110 | printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind); |
| 111 | OSMO_ASSERT(conn->auc_3g_ind == i + 1); |
| 112 | i++; |
| 113 | } |
| 114 | |
| 115 | btw("Add another conn, should take auc_3g_ind == 0"); |
| 116 | osmo_gsup_server_add_conn(clients, &conn_inst[20]); |
| 117 | VERBOSE_ASSERT(conn_inst[20].auc_3g_ind, == 0, "%u"); |
| 118 | |
| 119 | btw("Expecting a list of 0..10"); |
| 120 | i = 0; |
| 121 | llist_for_each_entry(conn, clients, list) { |
| 122 | printf("conn[%u].auc_3g_ind == %u\n", i, conn->auc_3g_ind); |
| 123 | OSMO_ASSERT(conn->auc_3g_ind == i); |
| 124 | i++; |
| 125 | } |
| 126 | |
| 127 | btw("If a client reconnects, it will (likely) get the same auc_3g_ind"); |
| 128 | VERBOSE_ASSERT(conn_inst[5].auc_3g_ind, == 5, "%u"); |
| 129 | llist_del(&conn_inst[5].list); |
| 130 | conn_inst[5].auc_3g_ind = 423; |
| 131 | osmo_gsup_server_add_conn(clients, &conn_inst[5]); |
| 132 | VERBOSE_ASSERT(conn_inst[5].auc_3g_ind, == 5, "%u"); |
| 133 | |
| 134 | comment_end(); |
| 135 | } |
| 136 | |
| 137 | int main(int argc, char **argv) |
| 138 | { |
| 139 | printf("test_gsup_server.c\n"); |
| 140 | |
| 141 | test_add_conn(); |
| 142 | |
| 143 | printf("Done\n"); |
| 144 | return 0; |
| 145 | } |