blob: 06153964d77825372fef0bff620b2c77e3c6da1b [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>
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +020025
26#include <osmocom/gsm/protocol/gsm_03_40.h>
27
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +020029#include <osmocom/gsm/gsm0411_utils.h>
30
31#include <osmocom/core/msgb.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/core/utils.h>
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080033
Alexander Chemerise4c50d52014-03-07 20:42:03 +010034#include <osmocom/core/logging.h>
35#include <osmocom/core/application.h>
36
37struct log_info fake_log_info = {};
38
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080039struct test_case {
40 const uint8_t *input;
41 const uint16_t input_length;
42
43 const uint8_t *expected;
Dennis Wehrle291e6132011-07-24 20:14:13 +020044 const uint16_t expected_octet_length;
45 const uint16_t expected_septet_length;
46 const uint8_t ud_hdr_ind;
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080047};
48
49static const char simple_text[] = "test text";
Dennis Wehrle291e6132011-07-24 20:14:13 +020050#define simple_septet_length 9
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080051static const uint8_t simple_enc[] = {
Nico Goldec0ce9aa2010-07-20 15:43:58 +020052 0xf4, 0xf2, 0x9c, 0x0e, 0xa2, 0x97, 0xf1, 0x74
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080053};
54
Dennis Wehrle291e6132011-07-24 20:14:13 +020055static const char escape_text[] = "!$ a more#^- complicated test@@?_%! case";
56#define escape_septet_length 41 /* note: the ^ counts as two, because it is a extension character */
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080057static const uint8_t escape_enc[] = {
58 0x21, 0x01, 0x28, 0x0c, 0x6a, 0xbf, 0xe5, 0xe5, 0xd1,
59 0x86, 0xd2, 0x02, 0x8d, 0xdf, 0x6d, 0x38, 0x3b, 0x3d,
60 0x0e, 0xd3, 0xcb, 0x64, 0x10, 0xbd, 0x3c, 0xa7, 0x03,
61 0x00, 0xbf, 0x48, 0x29, 0x04, 0x1a, 0x87, 0xe7, 0x65,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +080062};
63
Dennis Wehrle291e6132011-07-24 20:14:13 +020064static const char enhanced_text[] = "enhanced ^ {][} test |+~ ^ test";
65#define enhanced_septet_length 39 /* note: the characters { } [ ] ^ | ~ count as two (each of them), because they are extension characters */
66static const uint8_t enhanced_enc[] = {
67 0x65, 0x37, 0x3A, 0xEC, 0x1E, 0x97, 0xC9, 0xA0, 0x0D,
68 0x05, 0xB4, 0x41, 0x6D, 0x7C, 0x1B, 0xDE, 0x26, 0x05,
69 0xA2, 0x97, 0xE7, 0x74, 0xD0, 0x06, 0xB8, 0xDA, 0xF4,
70 0x40, 0x1B, 0x0A, 0x88, 0x5E, 0x9E, 0xD3, 0x01,
71};
72
73static const char enhancedV2_text[] = "enhanced ^ {][} test |+~ ^ tests";
74#define enhancedV2_septet_length 40 /* note: number of octets are equal to the enhanced_text! */
75static const uint8_t enhancedV2_enc[] = {
76 0x65, 0x37, 0x3A, 0xEC, 0x1E, 0x97, 0xC9, 0xA0, 0x0D,
77 0x05, 0xB4, 0x41, 0x6D, 0x7C, 0x1B, 0xDE, 0x26, 0x05,
78 0xA2, 0x97, 0xE7, 0x74, 0xD0, 0x06, 0xB8, 0xDA, 0xF4,
79 0x40, 0x1B, 0x0A, 0x88, 0x5E, 0x9E, 0xD3, 0xE7,
80};
81
82
83
84static const char concatenated_text[] =
85 "this is a testmessage. this is a testmessage. this is a testmessage. this is a testmessage. "
86 "this is a testmessage. this is a testmessage. cut here .....: this is a second testmessage. end here.";
87
88static const char splitted_text_part1[] =
89 "this is a testmessage. this is a testmessage. this is a testmessage. this is a testmessage. "
90 "this is a testmessage. this is a testmessage. cut here .....:";
91#define concatenated_part1_septet_length_with_header 160
92#define concatenated_part1_septet_length 153
93static const uint8_t concatenated_part1_enc[] = {
94 0x05, 0x00, 0x03, 0x6f, 0x02, 0x01,
95 0xe8, 0xe8, 0xf4, 0x1c, 0x94, 0x9e, 0x83, 0xc2,
96 0x20, 0x7a, 0x79, 0x4e, 0x6f, 0x97, 0xe7, 0xf3,
97 0xf0, 0xb9, 0xec, 0x02, 0xd1, 0xd1, 0xe9, 0x39,
98 0x28, 0x3d, 0x07, 0x85, 0x41, 0xf4, 0xf2, 0x9c,
99 0xde, 0x2e, 0xcf, 0xe7, 0xe1, 0x73, 0xd9, 0x05,
100 0xa2, 0xa3, 0xd3, 0x73, 0x50, 0x7a, 0x0e, 0x0a,
101 0x83, 0xe8, 0xe5, 0x39, 0xbd, 0x5d, 0x9e, 0xcf,
102 0xc3, 0xe7, 0xb2, 0x0b, 0x44, 0x47, 0xa7, 0xe7,
103 0xa0, 0xf4, 0x1c, 0x14, 0x06, 0xd1, 0xcb, 0x73,
104 0x7a, 0xbb, 0x3c, 0x9f, 0x87, 0xcf, 0x65, 0x17,
105 0x88, 0x8e, 0x4e, 0xcf, 0x41, 0xe9, 0x39, 0x28,
106 0x0c, 0xa2, 0x97, 0xe7, 0xf4, 0x76, 0x79, 0x3e,
107 0x0f, 0x9f, 0xcb, 0x2e, 0x10, 0x1d, 0x9d, 0x9e,
108 0x83, 0xd2, 0x73, 0x50, 0x18, 0x44, 0x2f, 0xcf,
109 0xe9, 0xed, 0xf2, 0x7c, 0x1e, 0x3e, 0x97, 0x5d,
110 0xa0, 0x71, 0x9d, 0x0e, 0x42, 0x97, 0xe5, 0x65,
111 0x90, 0xcb, 0xe5, 0x72, 0xb9, 0x74,
112};
113
114static const char splitted_text_part2[] = " this is a second testmessage. end here.";
115#define concatenated_part2_septet_length_with_header 47
116#define concatenated_part2_septet_length 40
117static const uint8_t concatenated_part2_enc[] = {
118 0x05, 0x00, 0x03, 0x6f, 0x02, 0x02,
119 0x40, 0x74, 0x74, 0x7a, 0x0e, 0x4a, 0xcf, 0x41,
120 0x61, 0xd0, 0xbc, 0x3c, 0x7e, 0xbb, 0xc9, 0x20,
121 0x7a, 0x79, 0x4e, 0x6f, 0x97, 0xe7, 0xf3, 0xf0,
122 0xb9, 0xec, 0x02, 0x95, 0xdd, 0x64, 0x10, 0xba,
123 0x2c, 0x2f, 0xbb, 0x00,
124};
125
126static const struct test_case test_multiple_encode[] =
127{
128 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200129 .input = (const uint8_t *) concatenated_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200130 .expected = concatenated_part1_enc,
131 .expected_octet_length = sizeof(concatenated_part1_enc),
132 .expected_septet_length = concatenated_part1_septet_length,
133 .ud_hdr_ind = 1,
134 },
135 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200136 .input = (const uint8_t *) concatenated_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200137 .expected = concatenated_part2_enc,
138 .expected_octet_length = sizeof(concatenated_part2_enc),
139 .expected_septet_length = concatenated_part2_septet_length,
140 .ud_hdr_ind = 1,
141 },
142};
143
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800144static const struct test_case test_encode[] =
145{
146 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200147 .input = (const uint8_t *) simple_text,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800148 .expected = simple_enc,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200149 .expected_octet_length = sizeof(simple_enc),
150 .expected_septet_length = simple_septet_length,
151 .ud_hdr_ind = 0,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800152 },
153 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200154 .input = (const uint8_t *) escape_text,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800155 .expected = escape_enc,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200156 .expected_octet_length = sizeof(escape_enc),
157 .expected_septet_length = escape_septet_length,
158 .ud_hdr_ind = 0,
159 },
160 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200161 .input = (const uint8_t *) enhanced_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200162 .expected = enhanced_enc,
163 .expected_octet_length = sizeof(enhanced_enc),
164 .expected_septet_length = enhanced_septet_length,
165 .ud_hdr_ind = 0,
166 },
167 {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200168 .input = (const uint8_t *) enhancedV2_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200169 .expected = enhancedV2_enc,
170 .expected_octet_length = sizeof(enhancedV2_enc),
171 .expected_septet_length = enhancedV2_septet_length,
172 .ud_hdr_ind = 0,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800173 },
174};
175
176static const struct test_case test_decode[] =
177{
178 {
179 .input = simple_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200180 .input_length = sizeof(simple_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200181 .expected = (const uint8_t *) simple_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200182 .expected_septet_length = simple_septet_length,
183 .ud_hdr_ind = 0,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800184 },
185 {
186 .input = escape_enc,
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200187 .input_length = sizeof(escape_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200188 .expected = (const uint8_t *) escape_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200189 .expected_septet_length = escape_septet_length,
190 .ud_hdr_ind = 0,
191 },
192 {
193 .input = enhanced_enc,
194 .input_length = sizeof(enhanced_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200195 .expected = (const uint8_t *) enhanced_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200196 .expected_septet_length = enhanced_septet_length,
197 .ud_hdr_ind = 0,
198 },
199 {
200 .input = enhancedV2_enc,
201 .input_length = sizeof(enhancedV2_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200202 .expected = (const uint8_t *) enhancedV2_text,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200203 .expected_septet_length = enhancedV2_septet_length,
204 .ud_hdr_ind = 0,
205 },
206 {
207 .input = concatenated_part1_enc,
208 .input_length = sizeof(concatenated_part1_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200209 .expected = (const uint8_t *) splitted_text_part1,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200210 .expected_septet_length = concatenated_part1_septet_length_with_header,
211 .ud_hdr_ind = 1,
212 },
213 {
214 .input = concatenated_part2_enc,
215 .input_length = sizeof(concatenated_part2_enc),
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200216 .expected = (const uint8_t *) splitted_text_part2,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200217 .expected_septet_length = concatenated_part2_septet_length_with_header,
218 .ud_hdr_ind = 1,
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800219 },
220};
Harald Welteec8b4502010-02-20 20:34:29 +0100221
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200222static void test_octet_return()
223{
224 char out[256];
225 int oct, septets;
226
227 printf("Encoding some tests and printing number of septets/octets\n");
228
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200229 septets = gsm_7bit_encode_n((uint8_t *) out, sizeof(out), "test1234", &oct);
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200230 printf("SEPTETS: %d OCTETS: %d\n", septets, oct);
231
232 printf("Done\n");
233}
234
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +0200235static void test_gen_oa(void)
236{
237 uint8_t oa[12];
238 int len;
239
240 printf("Testing gsm340_gen_oa\n");
241
242 /* first try... */
243 len = gsm340_gen_oa(oa, ARRAY_SIZE(oa), GSM340_TYPE_UNKNOWN,
244 GSM340_PLAN_ISDN, "12345678901234567891");
245 OSMO_ASSERT(len == 12);
246 printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
247 len = gsm340_gen_oa(oa, ARRAY_SIZE(oa), GSM340_TYPE_NATIONAL,
248 GSM340_PLAN_ISDN, "12345678901234567891");
249 OSMO_ASSERT(len == 12);
250 printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
251
252 /* long input.. will fail and just prints the header*/
253 len = gsm340_gen_oa(oa, ARRAY_SIZE(oa), GSM340_TYPE_INTERNATIONAL,
254 GSM340_PLAN_ISDN, "123456789123456789120");
255 OSMO_ASSERT(len == 2);
256 printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
257
258 /* try the alpha numeric encoding */
259 len = gsm340_gen_oa(oa, ARRAY_SIZE(oa), GSM340_TYPE_ALPHA_NUMERIC,
260 GSM340_PLAN_UNKNOWN, "OpenBSC");
261 OSMO_ASSERT(len == 9);
262 printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
263
264 /* long alpha numeric text */
265 len = gsm340_gen_oa(oa, ARRAY_SIZE(oa), GSM340_TYPE_ALPHA_NUMERIC,
266 GSM340_PLAN_UNKNOWN, "OpenBSCabcdefghijklm");
267 OSMO_ASSERT(len == 12);
268 printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));
269}
270
Harald Welteec8b4502010-02-20 20:34:29 +0100271int main(int argc, char** argv)
272{
273 printf("SMS testing\n");
Harald Welteec8b4502010-02-20 20:34:29 +0100274 uint8_t i;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200275 uint16_t buffer_size;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200276 uint8_t octet_length;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200277 int octets_written;
278 uint8_t computed_octet_length;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200279 uint8_t septet_length;
Harald Welteec8b4502010-02-20 20:34:29 +0100280 uint8_t coded[256];
Dennis Wehrle291e6132011-07-24 20:14:13 +0200281 uint8_t tmp[160];
282 uint8_t septet_data[256];
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200283 int nchars;
Harald Welteec8b4502010-02-20 20:34:29 +0100284 char result[256];
Neels Hofmeyra829b452018-04-05 03:02:35 +0200285 void *ctx = talloc_named_const(NULL, 0, "sms_test");
Harald Welteec8b4502010-02-20 20:34:29 +0100286
Alexander Chemerise4c50d52014-03-07 20:42:03 +0100287 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200288 osmo_init_logging2(ctx, &fake_log_info);
Alexander Chemerise4c50d52014-03-07 20:42:03 +0100289
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800290 /* test 7-bit encoding */
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200291 for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100292 /* Test new function */
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200293 memset(coded, 0x42, sizeof(coded));
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200294 septet_length = gsm_7bit_encode_n(coded, sizeof(coded),
295 (const char *) test_encode[i].input,
296 &octets_written);
297 computed_octet_length = gsm_get_octet_len(septet_length);
298 printf("Encode case %d: "
299 "Octet length %d (expected %d, computed %d), "
300 "septet length %d (expected %d)\n"
301 , i
302 , octets_written, test_encode[i].expected_octet_length, computed_octet_length
303 , septet_length, test_encode[i].expected_septet_length
304 );
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800305
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200306 OSMO_ASSERT (octets_written == test_encode[i].expected_octet_length);
307 OSMO_ASSERT (octets_written == computed_octet_length);
308 OSMO_ASSERT (memcmp(coded, test_encode[i].expected, octets_written) == 0);
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100309 OSMO_ASSERT (septet_length == test_encode[i].expected_septet_length);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200310
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200311 /* check buffer limiting */
312 memset(coded, 0xaa, sizeof(coded));
313
314 for (buffer_size = 0;
315 buffer_size < test_encode[i].expected_octet_length + 1
316 && buffer_size < sizeof(coded) - 1;
317 ++buffer_size)
318 {
319 gsm_7bit_encode_n(coded, buffer_size,
320 (const char *) test_encode[i].input,
321 &octets_written);
322
323 OSMO_ASSERT(octets_written <= buffer_size);
324 OSMO_ASSERT(coded[buffer_size] == 0xaa);
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800325 }
Harald Welteec8b4502010-02-20 20:34:29 +0100326 }
Nico Golde28de0532010-07-09 17:19:12 +0200327
Dennis Wehrle291e6132011-07-24 20:14:13 +0200328
329 /* Test: encode multiple SMS */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200330 int number_of_septets = gsm_septet_encode(septet_data, (const char *) test_multiple_encode[0].input);
Jacob Erlbeck73ae7a92013-10-08 12:04:42 +0200331 (void) number_of_septets;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200332
333 /* SMS part 1 */
334 memset(tmp, 0x42, sizeof(tmp));
335 memset(coded, 0x42, sizeof(coded));
336 memcpy(tmp, septet_data, concatenated_part1_septet_length);
337
338 /* In our case: test_multiple_decode[0].ud_hdr_ind equals number of padding bits*/
339 octet_length = gsm_septets2octets(coded, tmp, concatenated_part1_septet_length, test_multiple_encode[0].ud_hdr_ind);
340
341 /* copy header */
342 memset(tmp, 0x42, sizeof(tmp));
343 int udh_length = test_multiple_encode[0].expected[0] + 1;
344 memcpy(tmp, test_multiple_encode[0].expected, udh_length);
345 memcpy(tmp + udh_length, coded, octet_length);
346 memset(coded, 0x42, sizeof(coded));
347 memcpy(coded, tmp, octet_length + 6);
348
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200349 OSMO_ASSERT(memcmp(coded, test_multiple_encode[0].expected, octet_length) == 0);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200350
351 /* SMS part 2 */
352 memset(tmp, 0x42, sizeof(tmp));
353 memset(coded, 0x42, sizeof(coded));
354 memcpy(tmp, septet_data + concatenated_part1_septet_length, concatenated_part2_septet_length);
355
356 /* In our case: test_multiple_decode[1].ud_hdr_ind equals number of padding bits*/
357 octet_length = gsm_septets2octets(coded, tmp, concatenated_part2_septet_length, test_multiple_encode[1].ud_hdr_ind);
358
359 /* copy header */
360 memset(tmp, 0x42, sizeof(tmp));
361 udh_length = test_multiple_encode[1].expected[0] + 1;
362 memcpy(tmp, test_multiple_encode[1].expected, udh_length);
363 memcpy(tmp + udh_length, coded, octet_length);
364 memset(coded, 0x42, sizeof(coded));
365 memcpy(coded, tmp, octet_length + 6);
366
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200367 OSMO_ASSERT(memcmp(coded, test_multiple_encode[1].expected, octet_length) == 0);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200368
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800369 /* test 7-bit decoding */
370 for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100371 /* Test new function */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200372 memset(result, 0x42, sizeof(result));
373 nchars = gsm_7bit_decode_n_hdr(result, sizeof(result), test_decode[i].input,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200374 test_decode[i].expected_septet_length, test_decode[i].ud_hdr_ind);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +0000375 printf("Decode case %d: return value %d (expected %zu)\n", i, nchars, strlen(result));
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800376
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200377 OSMO_ASSERT(strcmp(result, (const char *) test_decode[i].expected) == 0);
378 OSMO_ASSERT(nchars == strlen(result));
379
380 /* check buffer limiting */
381 memset(result, 0xaa, sizeof(result));
382
383 for (buffer_size = 1;
Holger Hans Peter Freytherbb9f8972013-10-08 13:57:51 +0200384 buffer_size < test_decode[i].expected_septet_length + 1
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200385 && buffer_size < sizeof(result) - 1;
386 ++buffer_size)
387 {
388 nchars = gsm_7bit_decode_n_hdr(result, buffer_size, test_decode[i].input,
389 test_decode[i].expected_septet_length, test_decode[i].ud_hdr_ind);
390
391 OSMO_ASSERT(nchars <= buffer_size);
392 OSMO_ASSERT(result[buffer_size] == (char)0xaa);
393 OSMO_ASSERT(result[nchars] == '\0');
Dennis Wehrle291e6132011-07-24 20:14:13 +0200394 }
Nico Golde28de0532010-07-09 17:19:12 +0200395 }
396
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200397 test_octet_return();
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +0200398 test_gen_oa();
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200399
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800400 printf("OK\n");
Nico Golde28de0532010-07-09 17:19:12 +0200401 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100402}