blob: 3e14529cc00bf7bde1661a730c2acff09cb0d72f [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
Vadim Yanitskiy44ac4f62021-01-30 00:27:06 +0100271static void test_enc_large_msg(void)
272{
273 uint8_t enc_buf[2048 * 7 / 8];
274 char large_msg[2048 + 1];
275 int i, j, nsep, noct = 0;
276
277 printf("\nRunning %s\n", __func__);
278
279 /* Expected chunks (repeated) in the output buffer */
280 const uint8_t exp_chunk[] = { 0xc1, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x83 };
281
282 /* Length variants to be tested */
283 static const size_t nlen[] = { 2048, 1024, 555, 512, 260, 255, 250 };
284
285 memset(&large_msg[0], (int) 'A', sizeof(large_msg) - 1);
286
287 for (i = 0; i < ARRAY_SIZE(nlen); i++) {
288 /* Clear the output buffer first */
289 memset(&enc_buf[0], 0x00, sizeof(enc_buf));
290 /* Limit length of the input string */
291 large_msg[nlen[i]] = '\0';
292
293 /* How many octets we expect to be used? */
294 int noct_exp = nlen[i] * 7 / 8;
295 if (nlen[i] % 8 != 0)
296 noct_exp++;
297
298 /* Encode a sequence of 'A' repeated nlen[i] times */
299 nsep = gsm_7bit_encode_n(&enc_buf[0], sizeof(enc_buf), large_msg, &noct);
300 printf("gsm_7bit_encode_n(len=%zu) processed %d septets (expected %zu): %s\n",
301 nlen[i], nsep, nlen[i], nsep == nlen[i] ? "OK" : "FAIL");
302 printf("gsm_7bit_encode_n(len=%zu) used %d octets in the buffer (expected %d): %s\n",
303 nlen[i], noct, noct_exp, noct == noct_exp ? "OK" : "FAIL");
304
305 /* The encoding result is expected to consist of repeated chunks */
306 for (j = 0; j < noct_exp; j += sizeof(exp_chunk)) {
307 size_t len = OSMO_MIN(noct_exp - j, sizeof(exp_chunk));
308 if (nlen[i] % 8 != 0) /* skip incomplete octets */
309 len--;
310 if (memcmp(&enc_buf[j], exp_chunk, len) != 0) {
311 printf("\tUnexpected chunk at enc_buf[%d:%zu]: %s\n",
312 j, len, osmo_hexdump(&enc_buf[j], len));
313 break; /* No need to show them all */
314 }
315 }
316 }
317}
318
Harald Welteec8b4502010-02-20 20:34:29 +0100319int main(int argc, char** argv)
320{
321 printf("SMS testing\n");
Harald Welteec8b4502010-02-20 20:34:29 +0100322 uint8_t i;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200323 uint16_t buffer_size;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200324 uint8_t octet_length;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200325 int octets_written;
326 uint8_t computed_octet_length;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200327 uint8_t septet_length;
Harald Welteec8b4502010-02-20 20:34:29 +0100328 uint8_t coded[256];
Dennis Wehrle291e6132011-07-24 20:14:13 +0200329 uint8_t tmp[160];
330 uint8_t septet_data[256];
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200331 int nchars;
Harald Welteec8b4502010-02-20 20:34:29 +0100332 char result[256];
Neels Hofmeyra829b452018-04-05 03:02:35 +0200333 void *ctx = talloc_named_const(NULL, 0, "sms_test");
Harald Welteec8b4502010-02-20 20:34:29 +0100334
Alexander Chemerise4c50d52014-03-07 20:42:03 +0100335 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200336 osmo_init_logging2(ctx, &fake_log_info);
Alexander Chemerise4c50d52014-03-07 20:42:03 +0100337
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800338 /* test 7-bit encoding */
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200339 for (i = 0; i < ARRAY_SIZE(test_encode); ++i) {
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100340 /* Test new function */
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200341 memset(coded, 0x42, sizeof(coded));
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200342 septet_length = gsm_7bit_encode_n(coded, sizeof(coded),
343 (const char *) test_encode[i].input,
344 &octets_written);
345 computed_octet_length = gsm_get_octet_len(septet_length);
346 printf("Encode case %d: "
347 "Octet length %d (expected %d, computed %d), "
348 "septet length %d (expected %d)\n"
349 , i
350 , octets_written, test_encode[i].expected_octet_length, computed_octet_length
351 , septet_length, test_encode[i].expected_septet_length
352 );
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800353
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200354 OSMO_ASSERT (octets_written == test_encode[i].expected_octet_length);
355 OSMO_ASSERT (octets_written == computed_octet_length);
356 OSMO_ASSERT (memcmp(coded, test_encode[i].expected, octets_written) == 0);
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100357 OSMO_ASSERT (septet_length == test_encode[i].expected_septet_length);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200358
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200359 /* check buffer limiting */
360 memset(coded, 0xaa, sizeof(coded));
361
362 for (buffer_size = 0;
363 buffer_size < test_encode[i].expected_octet_length + 1
364 && buffer_size < sizeof(coded) - 1;
365 ++buffer_size)
366 {
367 gsm_7bit_encode_n(coded, buffer_size,
368 (const char *) test_encode[i].input,
369 &octets_written);
370
371 OSMO_ASSERT(octets_written <= buffer_size);
372 OSMO_ASSERT(coded[buffer_size] == 0xaa);
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800373 }
Harald Welteec8b4502010-02-20 20:34:29 +0100374 }
Nico Golde28de0532010-07-09 17:19:12 +0200375
Dennis Wehrle291e6132011-07-24 20:14:13 +0200376
377 /* Test: encode multiple SMS */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200378 int number_of_septets = gsm_septet_encode(septet_data, (const char *) test_multiple_encode[0].input);
Jacob Erlbeck73ae7a92013-10-08 12:04:42 +0200379 (void) number_of_septets;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200380
381 /* SMS part 1 */
382 memset(tmp, 0x42, sizeof(tmp));
383 memset(coded, 0x42, sizeof(coded));
384 memcpy(tmp, septet_data, concatenated_part1_septet_length);
385
386 /* In our case: test_multiple_decode[0].ud_hdr_ind equals number of padding bits*/
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100387 octet_length = gsm_septet_pack(coded, tmp, concatenated_part1_septet_length, test_multiple_encode[0].ud_hdr_ind);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200388
389 /* copy header */
390 memset(tmp, 0x42, sizeof(tmp));
391 int udh_length = test_multiple_encode[0].expected[0] + 1;
392 memcpy(tmp, test_multiple_encode[0].expected, udh_length);
393 memcpy(tmp + udh_length, coded, octet_length);
394 memset(coded, 0x42, sizeof(coded));
395 memcpy(coded, tmp, octet_length + 6);
396
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200397 OSMO_ASSERT(memcmp(coded, test_multiple_encode[0].expected, octet_length) == 0);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200398
399 /* SMS part 2 */
400 memset(tmp, 0x42, sizeof(tmp));
401 memset(coded, 0x42, sizeof(coded));
402 memcpy(tmp, septet_data + concatenated_part1_septet_length, concatenated_part2_septet_length);
403
404 /* In our case: test_multiple_decode[1].ud_hdr_ind equals number of padding bits*/
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100405 octet_length = gsm_septet_pack(coded, tmp, concatenated_part2_septet_length, test_multiple_encode[1].ud_hdr_ind);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200406
407 /* copy header */
408 memset(tmp, 0x42, sizeof(tmp));
409 udh_length = test_multiple_encode[1].expected[0] + 1;
410 memcpy(tmp, test_multiple_encode[1].expected, udh_length);
411 memcpy(tmp + udh_length, coded, octet_length);
412 memset(coded, 0x42, sizeof(coded));
413 memcpy(coded, tmp, octet_length + 6);
414
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200415 OSMO_ASSERT(memcmp(coded, test_multiple_encode[1].expected, octet_length) == 0);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200416
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800417 /* test 7-bit decoding */
418 for (i = 0; i < ARRAY_SIZE(test_decode); ++i) {
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100419 /* Test new function */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200420 memset(result, 0x42, sizeof(result));
421 nchars = gsm_7bit_decode_n_hdr(result, sizeof(result), test_decode[i].input,
Dennis Wehrle291e6132011-07-24 20:14:13 +0200422 test_decode[i].expected_septet_length, test_decode[i].ud_hdr_ind);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +0000423 printf("Decode case %d: return value %d (expected %zu)\n", i, nchars, strlen(result));
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800424
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200425 OSMO_ASSERT(strcmp(result, (const char *) test_decode[i].expected) == 0);
426 OSMO_ASSERT(nchars == strlen(result));
427
428 /* check buffer limiting */
429 memset(result, 0xaa, sizeof(result));
430
431 for (buffer_size = 1;
Holger Hans Peter Freytherbb9f8972013-10-08 13:57:51 +0200432 buffer_size < test_decode[i].expected_septet_length + 1
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200433 && buffer_size < sizeof(result) - 1;
434 ++buffer_size)
435 {
436 nchars = gsm_7bit_decode_n_hdr(result, buffer_size, test_decode[i].input,
437 test_decode[i].expected_septet_length, test_decode[i].ud_hdr_ind);
438
439 OSMO_ASSERT(nchars <= buffer_size);
440 OSMO_ASSERT(result[buffer_size] == (char)0xaa);
441 OSMO_ASSERT(result[nchars] == '\0');
Dennis Wehrle291e6132011-07-24 20:14:13 +0200442 }
Nico Golde28de0532010-07-09 17:19:12 +0200443 }
444
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200445 test_octet_return();
Holger Hans Peter Freyther4d7e49b2013-05-02 22:37:16 +0200446 test_gen_oa();
Vadim Yanitskiy44ac4f62021-01-30 00:27:06 +0100447 test_enc_large_msg();
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200448
Holger Hans Peter Freyther31e97ea2010-07-20 02:46:56 +0800449 printf("OK\n");
Nico Golde28de0532010-07-09 17:19:12 +0200450 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100451}