Neels Hofmeyr | b2553eb | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 1 | /* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <osmocom/core/logging.h> |
| 23 | #include <osmocom/core/utils.h> |
| 24 | #include <osmocom/core/application.h> |
| 25 | #include <osmocom/gsupclient/gsup_req.h> |
| 26 | |
| 27 | void *ctx = NULL; |
| 28 | |
| 29 | static void test_gsup_make_response(void) |
| 30 | { |
| 31 | char *source_name = "incoming-source-name"; |
| 32 | char *destination_name = "preset-destination-name"; |
| 33 | uint8_t sm_rp_mr = 23; |
| 34 | uint8_t other_sm_rp_mr = 17; |
| 35 | struct osmo_gsup_message rx = { |
| 36 | .message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST, |
| 37 | .imsi = "1234567", |
| 38 | .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT, |
| 39 | .source_name = (uint8_t*)source_name, |
| 40 | .source_name_len = strlen(source_name) + 1, |
| 41 | .sm_rp_mr = &sm_rp_mr, |
| 42 | .session_id = 42, |
| 43 | .session_state = OSMO_GSUP_SESSION_STATE_BEGIN, |
| 44 | }; |
| 45 | struct osmo_gsup_message nonempty = { |
| 46 | .message_type = OSMO_GSUP_MSGT_ROUTING_ERROR, |
| 47 | .imsi = "987654321", |
| 48 | .message_class = OSMO_GSUP_MESSAGE_CLASS_INTER_MSC, |
| 49 | .destination_name = (uint8_t*)destination_name, |
| 50 | .destination_name_len = strlen(destination_name) + 1, |
| 51 | .sm_rp_mr = &other_sm_rp_mr, |
| 52 | .session_id = 11, |
| 53 | .session_state = OSMO_GSUP_SESSION_STATE_END, |
| 54 | }; |
| 55 | void *name_ctx = talloc_named_const(ctx, 0, __func__); |
| 56 | int error; |
| 57 | int final; |
| 58 | char *nonempty_str; |
| 59 | int rc; |
| 60 | |
| 61 | printf("\n%s()\n", __func__); |
| 62 | printf("rx = %s\n", osmo_gsup_message_to_str_c(name_ctx, &rx)); |
| 63 | |
| 64 | printf("\nwriting to an empty struct osmo_gsup_message should populate values as needed:\n"); |
| 65 | for (error = 0; error <= 1; error++) { |
| 66 | for (final = 0; final <= 1; final++) { |
| 67 | struct osmo_gsup_message target = {}; |
| 68 | printf("- args (error=%d, final=%d)\n", error, final); |
| 69 | rc = osmo_gsup_make_response(&target, &rx, error, final); |
| 70 | printf(" %s\n", osmo_gsup_message_to_str_c(name_ctx, &target)); |
| 71 | printf(" rc = %d\n", rc); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | printf("\nwriting to an already populated struct osmo_gsup_message, should have no effect:\n"); |
| 76 | nonempty_str = osmo_gsup_message_to_str_c(name_ctx, &nonempty); |
| 77 | for (error = 0; error <= 1; error++) { |
| 78 | for (final = 0; final <= 1; final++) { |
| 79 | struct osmo_gsup_message target = nonempty; |
| 80 | char *result; |
| 81 | printf("- args (error=%d, final=%d)\n", error, final); |
| 82 | rc = osmo_gsup_make_response(&target, &rx, error, final); |
| 83 | result = osmo_gsup_message_to_str_c(name_ctx, &target); |
| 84 | printf(" %s\n", result); |
| 85 | if (strcmp(result, nonempty_str)) |
| 86 | printf(" ERROR: expected: %s\n", nonempty_str); |
| 87 | printf(" rc = %d\n", rc); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const struct log_info_cat default_categories[] = { |
| 93 | }; |
| 94 | |
| 95 | static struct log_info info = { |
| 96 | .cat = default_categories, |
| 97 | .num_cat = ARRAY_SIZE(default_categories), |
| 98 | }; |
| 99 | |
| 100 | int main(int argc, char **argv) |
| 101 | { |
| 102 | ctx = talloc_named_const(NULL, 0, "gsup_test"); |
| 103 | osmo_init_logging2(ctx, &info); |
| 104 | log_set_print_filename(osmo_stderr_target, 0); |
| 105 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 106 | log_set_use_color(osmo_stderr_target, 0); |
| 107 | log_set_print_category(osmo_stderr_target, 1); |
| 108 | |
| 109 | test_gsup_make_response(); |
| 110 | |
| 111 | printf("Done.\n"); |
| 112 | return EXIT_SUCCESS; |
| 113 | } |