blob: e3e8e08a10c04591da9b9e89f88e1e105a19892a [file] [log] [blame]
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +02001/*
2 * (C) 2010 by Holger Hans Peter Freyther
3 * (C) 2010 by On-Waves
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
Holger Hans Peter Freytherbf128002011-11-13 22:48:37 +010022#include <osmocom/core/application.h>
23#include <osmocom/core/logging.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/gsm/gsm0480.h>
Andreas Eversberg95975552013-08-08 12:38:53 +020025#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30static const uint8_t ussd_request[] = {
31 0x0b, 0x7b, 0x1c, 0x15, 0xa1, 0x13, 0x02, 0x01,
32 0x03, 0x02, 0x01, 0x3b, 0x30, 0x0b, 0x04, 0x01,
33 0x0f, 0x04, 0x06, 0x2a, 0xd5, 0x4c, 0x16, 0x1b,
34 0x01, 0x7f, 0x01, 0x00
35};
36
37static int parse_ussd(const uint8_t *_data, int len)
38{
39 uint8_t *data;
40 int rc;
41 struct ussd_request req;
42 struct gsm48_hdr *hdr;
43
44 data = malloc(len);
45 memcpy(data, _data, len);
46 hdr = (struct gsm48_hdr *) &data[0];
47 rc = gsm0480_decode_ussd_request(hdr, len, &req);
48 free(data);
49
50 return rc;
51}
52
Holger Hans Peter Freytherc88a44f2010-10-11 08:21:00 +020053static int parse_mangle_ussd(const uint8_t *_data, int len)
54{
55 uint8_t *data;
56 int rc;
57 struct ussd_request req;
58 struct gsm48_hdr *hdr;
59
60 data = malloc(len);
61 memcpy(data, _data, len);
62 hdr = (struct gsm48_hdr *) &data[0];
63 hdr->data[1] = len - sizeof(*hdr) - 2;
64 rc = gsm0480_decode_ussd_request(hdr, len, &req);
65 free(data);
66
67 return rc;
68}
69
Holger Hans Peter Freytherbf128002011-11-13 22:48:37 +010070struct log_info info = {};
71
Jacob Erlbeck8a1666b2013-08-08 12:38:54 +020072static void test_7bit_ussd(const char *text, const char *encoded_hex, const char *appended_after_decode)
Andreas Eversberg95975552013-08-08 12:38:53 +020073{
74 uint8_t coded[256];
75 char decoded[256];
76 int y;
77
78 printf("original = %s\n", osmo_hexdump((uint8_t *)text, strlen(text)));
79 gsm_7bit_encode_ussd(coded, text, &y);
80 printf("encoded = %s\n", osmo_hexdump(coded, y));
Jacob Erlbeck8a1666b2013-08-08 12:38:54 +020081
82 OSMO_ASSERT(strcmp(encoded_hex, osmo_hexdump_nospc(coded, y)) == 0);
83
Andreas Eversberg95975552013-08-08 12:38:53 +020084 gsm_7bit_decode_ussd(decoded, coded, y * 8 / 7);
85 y = strlen(decoded);
86 printf("decoded = %s\n\n", osmo_hexdump((uint8_t *)decoded, y));
Jacob Erlbeck8a1666b2013-08-08 12:38:54 +020087
88 OSMO_ASSERT(strncmp(text, decoded, strlen(text)) == 0);
89 OSMO_ASSERT(strcmp(appended_after_decode, decoded + strlen(text)) == 0);
Andreas Eversberg95975552013-08-08 12:38:53 +020090}
91
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020092int main(int argc, char **argv)
93{
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +020094 struct ussd_request req;
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020095 const int size = sizeof(ussd_request);
96 int i;
97
Holger Hans Peter Freytherbf128002011-11-13 22:48:37 +010098 osmo_init_logging(&info);
99
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +0200100 gsm0480_decode_ussd_request((struct gsm48_hdr *) ussd_request, size, &req);
101 printf("Tested if it still works. Text was: %s\n", req.text);
102
103
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200104 printf("Testing parsing a USSD request and truncated versions\n");
105
106 for (i = size; i > sizeof(struct gsm48_hdr); --i) {
107 int rc = parse_ussd(&ussd_request[0], i);
108 printf("Result for %d is %d\n", rc, i);
109 }
110
Holger Hans Peter Freytherc88a44f2010-10-11 08:21:00 +0200111 printf("Mangling the container now\n");
112 for (i = size; i > sizeof(struct gsm48_hdr) + 2; --i) {
113 int rc = parse_mangle_ussd(&ussd_request[0], i);
114 printf("Result for %d is %d\n", rc, i);
115 }
116
Andreas Eversberg95975552013-08-08 12:38:53 +0200117 printf("<CR> case test for 7 bit encode\n");
Jacob Erlbeck8a1666b2013-08-08 12:38:54 +0200118 test_7bit_ussd("01234567", "b0986c46abd96e", "");
119 test_7bit_ussd("0123456", "b0986c46abd91a", "");
120 test_7bit_ussd("01234567\r", "b0986c46abd96e0d", "");
121 /* The appended \r is compliant to GSM 03.38 section 6.1.2.3.1: */
122 test_7bit_ussd("0123456\r", "b0986c46abd91a0d", "\r");
123 test_7bit_ussd("012345\r", "b0986c46ab351a", "");
Andreas Eversberg95975552013-08-08 12:38:53 +0200124
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200125 return 0;
126}