Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 3 | * (C) 2010 by Nico Golde <nico@ngolde.de> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [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 General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 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 General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/gsm/gsm_utils.h> |
| 27 | #include <osmocom/core/utils.h> |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 28 | |
| 29 | struct test_case { |
| 30 | const uint8_t *input; |
| 31 | const uint16_t input_length; |
| 32 | |
| 33 | const uint8_t *expected; |
| 34 | const uint16_t expected_length; |
| 35 | }; |
| 36 | |
| 37 | static const char simple_text[] = "test text"; |
| 38 | static const uint8_t simple_enc[] = { |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 39 | 0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74 |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static const char escape_text[] = "!$ a more#^- complicated test@@?_\%! case"; |
| 43 | static const uint8_t escape_enc[] = { |
| 44 | 0x21, 0x01, 0x28, 0x0c, 0x6a, 0xbf, 0xe5, 0xe5, 0xd1, |
| 45 | 0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d, |
| 46 | 0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03, |
| 47 | 0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65, |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static const struct test_case test_encode[] = |
| 51 | { |
| 52 | { |
| 53 | .input = simple_text, |
| 54 | .expected = simple_enc, |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 55 | .expected_length = sizeof(simple_enc), |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | .input = escape_text, |
| 59 | .expected = escape_enc, |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 60 | .expected_length = sizeof(escape_enc), |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 61 | }, |
| 62 | }; |
| 63 | |
| 64 | static const struct test_case test_decode[] = |
| 65 | { |
| 66 | { |
| 67 | .input = simple_enc, |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 68 | .input_length = sizeof(simple_enc), |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 69 | .expected = simple_text, |
| 70 | }, |
| 71 | { |
| 72 | .input = escape_enc, |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 73 | .input_length = sizeof(escape_enc), |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 74 | .expected = escape_text, |
| 75 | }, |
| 76 | }; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | |
| 78 | int main(int argc, char** argv) |
| 79 | { |
| 80 | printf("SMS testing\n"); |
| 81 | struct msgb *msg; |
| 82 | uint8_t *sms; |
| 83 | uint8_t i; |
| 84 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 85 | uint8_t length; |
| 86 | uint8_t coded[256]; |
| 87 | char result[256]; |
| 88 | |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 89 | /* test 7-bit encoding */ |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 90 | for (i = 0; i < ARRAY_SIZE(test_encode); ++i) { |
| 91 | memset(coded, 0x42, sizeof(coded)); |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 92 | length = gsm_7bit_encode(coded, test_encode[i].input); |
| 93 | |
| 94 | if (length != test_encode[i].expected_length) { |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 95 | fprintf(stderr, "Failed to encode case %d. Got %d, expected %d\n", |
| 96 | i, length, test_encode[i].expected_length); |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | if (memcmp(coded, test_encode[i].expected, length) != 0) { |
| 101 | fprintf(stderr, "Encoded content does not match for %d\n", |
| 102 | i); |
| 103 | return -1; |
| 104 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 105 | } |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 106 | |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 107 | /* test 7-bit decoding */ |
| 108 | for (i = 0; i < ARRAY_SIZE(test_decode); ++i) { |
Nico Golde | c0ce9aa | 2010-07-20 15:43:58 +0200 | [diff] [blame] | 109 | memset(result, 0x42, sizeof(coded)); |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 110 | gsm_7bit_decode(result, test_decode[i].input, |
| 111 | test_decode[i].input_length); |
| 112 | |
| 113 | if (strcmp(result, test_decode[i].expected) != 0) { |
| 114 | fprintf(stderr, "Test case %d failed to decode.\n", i); |
| 115 | return -1; |
| 116 | } |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Holger Hans Peter Freyther | 31e97ea | 2010-07-20 02:46:56 +0800 | [diff] [blame] | 119 | printf("OK\n"); |
Nico Golde | 28de053 | 2010-07-09 17:19:12 +0200 | [diff] [blame] | 120 | return 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 121 | } |