Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013 by Holger Hans Peter Freyther |
Harald Welte | e6f1160 | 2022-05-17 18:25:14 +0200 | [diff] [blame] | 3 | * (C) 2022 by Harald Welte <laforge@osmocom.org> |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 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 Affero 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 <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 24 | #include <osmocom/msc/debug.h> |
Max | 5346f69 | 2022-07-28 14:19:22 +0700 | [diff] [blame] | 25 | #include <osmocom/smpp/smpp_smsc.h> |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 26 | #include <osmocom/core/application.h> |
| 27 | #include <osmocom/core/backtrace.h> |
| 28 | |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 29 | struct coding_test { |
| 30 | uint8_t dcs; |
| 31 | uint8_t coding; |
| 32 | int mode; |
| 33 | int res; |
| 34 | }; |
| 35 | |
| 36 | static struct coding_test codecs[] = { |
| 37 | { .dcs = 0xf6 , .coding = 0x02, .mode = MODE_8BIT, .res = 0 }, |
| 38 | { .dcs = 0xf2 , .coding = 0x01, .mode = MODE_7BIT, .res = 0 }, |
| 39 | { .dcs = 0x02 , .coding = 0x01, .mode = MODE_7BIT, .res = 0 }, |
| 40 | { .dcs = 0x06 , .coding = 0x02, .mode = MODE_8BIT, .res = 0 }, |
| 41 | { .dcs = 0x0A , .coding = 0x08, .mode = MODE_8BIT, .res = 0 }, |
| 42 | { .dcs = 0x0E , .coding = 0xFF, .mode = 0xFF, .res = -1 }, |
| 43 | { .dcs = 0xE0 , .coding = 0xFF, .mode = 0xFF, .res = -1 }, |
| 44 | }; |
| 45 | |
| 46 | static void test_coding_scheme(void) |
| 47 | { |
| 48 | int i; |
| 49 | printf("Testing coding scheme support\n"); |
| 50 | |
| 51 | for (i = 0; i < ARRAY_SIZE(codecs); ++i) { |
| 52 | uint8_t coding; |
| 53 | int mode, res; |
| 54 | |
| 55 | res = smpp_determine_scheme(codecs[i].dcs, &coding, &mode); |
| 56 | OSMO_ASSERT(res == codecs[i].res); |
| 57 | if (res != -1) { |
| 58 | OSMO_ASSERT(mode == codecs[i].mode); |
| 59 | OSMO_ASSERT(coding == codecs[i].coding); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Harald Welte | e6f1160 | 2022-05-17 18:25:14 +0200 | [diff] [blame] | 64 | static const char *smpp_time_tests[] = { |
| 65 | "\0", |
| 66 | "220517175524000+", |
| 67 | "220517175524000-", |
| 68 | "220517175524004+", /* 1 hour advanced compared to GMT */ |
| 69 | "220517175524004-", /* 1 hour retarded compared to GMT */ |
| 70 | "000000010000000R", /* 1 hour */ |
| 71 | "000001000000000R", /* 1 day */ |
| 72 | }; |
| 73 | |
| 74 | static void test_smpp_parse_time_format(void) |
| 75 | { |
| 76 | time_t t_now = 1652745600; /* 2022-05-17 00:00:00 UTC */ |
| 77 | char *orig_tz; |
| 78 | |
| 79 | printf("Testing SMPP time format parser\n"); |
| 80 | |
| 81 | /* relative time format conversion depends on the local time */ |
| 82 | orig_tz = getenv("TZ"); |
| 83 | setenv("TZ", "UTC", 1); |
| 84 | |
| 85 | for (unsigned int i = 0; i < ARRAY_SIZE(smpp_time_tests); i++) { |
| 86 | time_t t = smpp_parse_time_format(smpp_time_tests[i], &t_now); |
| 87 | char buf[32]; |
| 88 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", gmtime(&t)); |
| 89 | printf("'%s': %ld == %s\n", smpp_time_tests[i], t, buf); |
| 90 | } |
| 91 | |
| 92 | if (orig_tz) |
| 93 | setenv("TZ", orig_tz, 1); |
| 94 | else |
| 95 | unsetenv("TZ"); |
| 96 | |
| 97 | } |
| 98 | |
Neels Hofmeyr | 6a8b9c7 | 2018-03-22 15:51:22 +0100 | [diff] [blame] | 99 | static const struct log_info_cat smpp_mirror_default_categories[] = { |
| 100 | [DSMPP] = { |
| 101 | .name = "DSMPP", |
| 102 | .description = "SMPP interface for external SMS apps", |
| 103 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 104 | }, |
| 105 | }; |
| 106 | |
| 107 | const struct log_info log_info = { |
| 108 | .cat = smpp_mirror_default_categories, |
| 109 | .num_cat = ARRAY_SIZE(smpp_mirror_default_categories), |
| 110 | }; |
| 111 | |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 112 | int main(int argc, char **argv) |
| 113 | { |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 114 | void *ctx = talloc_named_const(NULL, 0, "smpp_test"); |
| 115 | osmo_init_logging2(ctx, &log_info); |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 116 | log_set_use_color(osmo_stderr_target, 0); |
Pau Espin Pedrol | cad22fd | 2021-02-19 13:37:00 +0100 | [diff] [blame] | 117 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Pau Espin Pedrol | 8d02d99 | 2021-02-19 13:36:47 +0100 | [diff] [blame] | 118 | log_set_print_category(osmo_stderr_target, 0); |
| 119 | log_set_print_category_hex(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 120 | |
| 121 | test_coding_scheme(); |
Harald Welte | e6f1160 | 2022-05-17 18:25:14 +0200 | [diff] [blame] | 122 | test_smpp_parse_time_format(); |
| 123 | |
Holger Hans Peter Freyther | a7328a5 | 2013-07-13 17:09:56 +0200 | [diff] [blame] | 124 | return EXIT_SUCCESS; |
| 125 | } |