blob: 9d87b5bc8f5678b0aa2741d1c6ab17aa8e354f51 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
Nico Golde28de0532010-07-09 17:19:12 +02003 * (C) 2010 by Nico Golde <nico@ngolde.de>
Harald Welteec8b4502010-02-20 20:34:29 +01004 * 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>
25#include <sys/types.h>
26#include <osmocore/msgb.h>
27#include <osmocore/gsm_utils.h>
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080028#include <osmocore/utils.h>
29
30struct test_case {
31 const uint8_t *input;
32 const uint16_t input_length;
33
34 const uint8_t *expected;
35 const uint16_t expected_length;
36};
37
38static const char simple_text[] = "test text";
39static const uint8_t simple_enc[] = {
Nico Goldec0ce9aa2010-07-20 15:43:58 +020040 0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080041};
42
43static const char escape_text[] = "!$ a more#^- complicated test@@?_\%! case";
44static const uint8_t escape_enc[] = {
45 0x21, 0x01, 0x28, 0x0c, 0x6a, 0xbf, 0xe5, 0xe5, 0xd1,
46 0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d,
47 0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03,
48 0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080049};
50
51static const struct test_case test_encode[] =
52{
53 {
54 .input = simple_text,
55 .expected = simple_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +020056 .expected_length = sizeof(simple_enc),
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080057 },
58 {
59 .input = escape_text,
60 .expected = escape_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +020061 .expected_length = sizeof(escape_enc),
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080062 },
63};
64
65static const struct test_case test_decode[] =
66{
67 {
68 .input = simple_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +020069 .input_length = sizeof(simple_enc),
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080070 .expected = simple_text,
71 },
72 {
73 .input = escape_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +020074 .input_length = sizeof(escape_enc),
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080075 .expected = escape_text,
76 },
77};
Harald Welteec8b4502010-02-20 20:34:29 +010078
79int main(int argc, char** argv)
80{
81 printf("SMS testing\n");
82 struct msgb *msg;
83 uint8_t *sms;
84 uint8_t i;
85
Harald Welteec8b4502010-02-20 20:34:29 +010086 uint8_t length;
87 uint8_t coded[256];
88 char result[256];
89
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080090 /* test 7-bit encoding */
Nico Goldec0ce9aa2010-07-20 15:43:58 +020091 for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
92 memset(coded, 0x42, sizeof(coded));
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080093 length = gsm_7bit_encode(coded, test_encode[i].input);
94
95 if (length != test_encode[i].expected_length) {
Nico Goldec0ce9aa2010-07-20 15:43:58 +020096 fprintf(stderr, "Failed to encode case %d. Got %d, expected %d\n",
97 i, length, test_encode[i].expected_length);
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080098 return -1;
99 }
100
101 if (memcmp(coded, test_encode[i].expected, length) != 0) {
102 fprintf(stderr, "Encoded content does not match for %d\n",
103 i);
104 return -1;
105 }
Harald Welteec8b4502010-02-20 20:34:29 +0100106 }
Nico Golde28de0532010-07-09 17:19:12 +0200107
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800108 /* test 7-bit decoding */
109 for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200110 memset(result, 0x42, sizeof(coded));
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800111 gsm_7bit_decode(result, test_decode[i].input,
112 test_decode[i].input_length);
113
114 if (strcmp(result, test_decode[i].expected) != 0) {
115 fprintf(stderr, "Test case %d failed to decode.\n", i);
116 return -1;
117 }
Nico Golde28de0532010-07-09 17:19:12 +0200118 }
119
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800120 printf("OK\n");
Nico Golde28de0532010-07-09 17:19:12 +0200121 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100122}