ussd: Add a test case, switch parsing to use a gsm48_hdr and len
[osmocom-bb.git] / tests / ussd / ussd_test.c
1 /*
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
22 #include <osmocore/gsm0480.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 static const uint8_t ussd_request[] = {
28         0x0b, 0x7b, 0x1c, 0x15, 0xa1, 0x13, 0x02, 0x01,
29         0x03, 0x02, 0x01, 0x3b, 0x30, 0x0b, 0x04, 0x01,
30         0x0f, 0x04, 0x06, 0x2a, 0xd5, 0x4c, 0x16, 0x1b,
31         0x01, 0x7f, 0x01, 0x00
32 };
33
34 static int parse_ussd(const uint8_t *_data, int len)
35 {
36         uint8_t *data;
37         int rc;
38         struct ussd_request req;
39         struct gsm48_hdr *hdr;
40
41         data = malloc(len);
42         memcpy(data, _data, len);
43         hdr = (struct gsm48_hdr *) &data[0];
44         rc = gsm0480_decode_ussd_request(hdr, len, &req);
45         free(data);
46
47         return rc;
48 }
49
50 int main(int argc, char **argv)
51 {
52         const int size = sizeof(ussd_request);
53         int i;
54
55         printf("Testing parsing a USSD request and truncated versions\n");
56
57         for (i = size; i > sizeof(struct gsm48_hdr); --i) {
58                 int rc = parse_ussd(&ussd_request[0], i);
59                 printf("Result for %d is %d\n", rc, i);
60         }
61
62         return 0;
63 }