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