3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License version 2 as
9
* published by the Free Software Foundation.
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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
const unsigned char invalid_gsm_extended[] = {
33
const unsigned char invalid_gsm_extended_len[] = {
37
unsigned short gsm_to_unicode_map[] =
179
#define UTF8_LENGTH(c) \
181
((c) < 0x800 ? 2 : 3))
183
static void test_invalid()
189
res = convert_gsm_to_utf8(invalid_gsm_extended, 0, &nread, &nwritten,
191
g_assert(res == NULL);
192
g_assert(nread == 0);
194
res = convert_gsm_to_utf8(invalid_gsm_extended,
195
sizeof(invalid_gsm_extended),
196
&nread, &nwritten, 0);
197
g_assert(res == NULL);
198
g_assert(nread == 1);
200
res = convert_gsm_to_utf8(invalid_gsm_extended_len,
201
sizeof(invalid_gsm_extended_len),
202
&nread, &nwritten, 0);
203
g_assert(res == NULL);
204
g_assert(nread == 3);
207
static void test_valid()
217
unsigned char buf[2];
219
static int map_size =
220
sizeof(gsm_to_unicode_map) / sizeof(unsigned short) / 2;
222
for (i = 0; i < map_size; i++) {
223
unsigned short c = gsm_to_unicode_map[i*2];
234
res = convert_gsm_to_utf8(buf, size, &nread, &nwritten, 0);
237
if (g_test_verbose())
238
g_print("size: %ld, nread:%ld, nwritten:%ld, %s\n",
239
size, nread, nwritten, res);
241
g_assert(nread == size);
243
verify = g_utf8_to_ucs4(res, -1, NULL, NULL, NULL);
245
g_assert(verify[0] == gsm_to_unicode_map[i*2+1]);
246
g_assert(verify[1] == 0);
248
g_assert(nwritten == UTF8_LENGTH(verify[0]));
250
back = convert_utf8_to_gsm(res, -1, &nread, &nwritten, 0);
254
g_assert(nwritten == size);
256
g_assert(back[0] == 0x1b);
257
g_assert(back[1] == (c & 0x7f));
259
g_assert(back[0] == (c & 0x7f));
268
static const char hex_packed[] = "493A283D0795C3F33C88FE06C9CB6132885EC6D34"
269
"1EDF27C1E3E97E7207B3A0C0A5241E377BB1D"
271
static const char expected[] = "It is easy to read text messages via AT commands.";
272
static int reported_text_size = 49;
274
static void test_decode_encode()
276
const char *sms = hex_packed;
277
unsigned char *decoded, *packed;
278
char *utf8, *hex_packed;
279
unsigned char *gsm, *gsm_encoded;
280
long hex_decoded_size;
281
long unpacked_size, packed_size;
282
long gsm_encoded_size;
285
if (g_test_verbose())
286
g_print("Size of the orig string: %u\n",
287
(unsigned int)strlen(sms));
289
decoded = decode_hex(sms, -1, &hex_decoded_size, 0);
291
g_assert(decoded != NULL);
293
if (g_test_verbose())
294
g_print("Decode to %ld bytes\n", hex_decoded_size);
296
if (g_test_verbose()) {
297
g_print("%s\n", sms);
299
for (i = 0; i < hex_decoded_size; i++)
300
g_print("%02X", decoded[i]);
304
gsm = unpack_7bit(decoded, hex_decoded_size, 0, FALSE,
305
reported_text_size, &unpacked_size, 0xff);
307
g_assert(gsm != NULL);
309
if (g_test_verbose())
310
g_print("String unpacked to %ld bytes\n", unpacked_size);
312
utf8 = convert_gsm_to_utf8(gsm, -1, NULL, NULL, 0xff);
314
g_assert(utf8 != NULL);
316
if (g_test_verbose())
317
g_print("String is: -->%s<--\n", utf8);
319
g_assert(strcmp(utf8, expected) == 0);
321
gsm_encoded = convert_utf8_to_gsm(utf8, -1, NULL,
322
&gsm_encoded_size, 0xff);
324
g_assert(gsm_encoded != NULL);
326
if (g_test_verbose())
327
g_print("Converted back to GSM string of %ld bytes\n",
330
g_assert(gsm_encoded[gsm_encoded_size] == 0xff);
331
g_assert(gsm_encoded_size == unpacked_size);
332
g_assert(memcmp(gsm_encoded, gsm, gsm_encoded_size) == 0);
337
packed = pack_7bit(gsm_encoded, -1, 0, FALSE, &packed_size, 0xff);
339
g_assert(gsm_encoded != NULL);
341
if (g_test_verbose())
342
g_print("Packed GSM to size of %ld bytes\n", packed_size);
344
if (g_test_verbose()) {
345
for (i = 0; i < packed_size; i++)
346
g_print("%02X", packed[i]);
350
g_assert(packed_size == hex_decoded_size);
351
g_assert(memcmp(packed, decoded, packed_size) == 0);
355
hex_packed = encode_hex(packed, packed_size, 0);
357
g_assert(hex_packed != NULL);
361
if (g_test_verbose())
362
g_print("Hex encoded packed to size %ld bytes\n",
363
(long)strlen(hex_packed));
365
g_assert(strlen(hex_packed) == strlen(sms));
366
g_assert(strcmp(hex_packed, sms) == 0);
371
static void test_pack_size()
373
unsigned char c1[] = { 'a' };
374
unsigned char c2[] = { 'a', 'b' };
375
unsigned char c3[] = { 'a', 'b', 'c' };
376
unsigned char c4[] = { 'a', 'b', 'c', 'd' };
377
unsigned char c5[] = { 'a', 'b', 'c', 'd', 'e' };
378
unsigned char c6[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
379
unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
380
unsigned char c8[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
382
unsigned char *packed;
385
packed = pack_7bit(c1, 1, 0, FALSE, &size, 0);
386
g_assert(packed != NULL);
390
packed = pack_7bit(c2, 2, 0, FALSE, &size, 0);
391
g_assert(packed != NULL);
395
packed = pack_7bit(c3, 3, 0, FALSE, &size, 0);
396
g_assert(packed != NULL);
400
packed = pack_7bit(c4, 4, 0, FALSE, &size, 0);
401
g_assert(packed != NULL);
405
packed = pack_7bit(c5, 5, 0, FALSE, &size, 0);
406
g_assert(packed != NULL);
410
packed = pack_7bit(c6, 6, 0, FALSE, &size, 0);
411
g_assert(packed != NULL);
415
packed = pack_7bit(c7, 7, 0, FALSE, &size, 0);
416
g_assert(packed != NULL);
418
g_assert((packed[6] & 0xfe) == 0);
421
packed = pack_7bit(c7, 7, 0, TRUE, &size, 0);
422
g_assert(packed != NULL);
424
g_assert(((packed[6] & 0xfe) >> 1) == '\r');
427
packed = pack_7bit(c8, 8, 0, FALSE, &size, 0);
428
g_assert(packed != NULL);
433
static void test_cr_handling()
435
unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
436
unsigned char c7_expected[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r' };
437
unsigned char c8[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r' };
438
unsigned char c8_expected[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', '\r', '\r' };
440
unsigned char *packed;
441
unsigned char *unpacked;
445
packed = pack_7bit(c8, 8, 0, TRUE, &packed_size, 0);
446
g_assert(packed != NULL);
447
g_assert(packed_size == 8);
448
g_assert(((packed[6] & 0xfe) >> 1) == '\r');
449
g_assert((packed[7] & 0x7f) == '\r');
451
unpacked = unpack_7bit(packed, 8, 0, TRUE, -1, &unpacked_size, 0);
452
if (g_test_verbose())
453
g_print("Unpacked to size: %ld\n", unpacked_size);
455
g_assert(unpacked != NULL);
456
g_assert(unpacked_size == 9);
457
g_assert(memcmp(c8_expected, unpacked, 9) == 0);
462
packed = pack_7bit(c7, 7, 0, TRUE, &packed_size, 0);
463
g_assert(packed != NULL);
464
g_assert(packed_size == 7);
465
g_assert(((packed[6] & 0xfe) >> 1) == '\r');
467
unpacked = unpack_7bit(packed, 7, 0, TRUE, -1, &unpacked_size, 0);
468
if (g_test_verbose())
469
g_print("Unpacked to size: %ld\n", unpacked_size);
471
g_assert(unpacked != NULL);
472
g_assert(unpacked_size == 7);
473
g_assert(memcmp(c7, unpacked, 7) == 0);
478
/* As above, but now unpack using SMS style, we should now have cr at
479
* the end of the stream
481
packed = pack_7bit(c7, 7, 0, TRUE, &packed_size, 0);
482
unpacked = unpack_7bit(packed, 7, 0, FALSE, 8, &unpacked_size, 0);
483
if (g_test_verbose())
484
g_print("Unpacked to size: %ld\n", unpacked_size);
486
g_assert(unpacked != NULL);
487
g_assert(unpacked_size == 8);
488
g_assert(memcmp(c7_expected, unpacked, 8) == 0);
494
static void test_sms_handling()
496
unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
498
unsigned char *packed;
499
unsigned char *unpacked;
503
packed = pack_7bit(c7, 7, 0, FALSE, &packed_size, 0);
504
g_assert(packed != NULL);
505
g_assert(packed_size == 7);
507
unpacked = unpack_7bit(packed, 7, 0, FALSE, 8, &unpacked_size, 0xff);
508
if (g_test_verbose())
509
g_print("Unpacked to size: %ld\n", unpacked_size);
511
g_assert(unpacked != NULL);
512
g_assert(unpacked_size == 8);
513
g_assert(unpacked[7] == 0);
514
g_assert(unpacked[8] == 0xff);
519
packed = pack_7bit(c7, 7, 0, FALSE, &packed_size, 0);
520
g_assert(packed != NULL);
521
g_assert(packed_size == 7);
523
unpacked = unpack_7bit(packed, 7, 0, FALSE, 7, &unpacked_size, 0xff);
524
if (g_test_verbose())
525
g_print("Unpacked to size: %ld\n", unpacked_size);
527
g_assert(unpacked != NULL);
528
g_assert(unpacked_size == 7);
529
g_assert(unpacked[7] == 0xff);
535
static void test_offset_handling()
537
unsigned char c7[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
538
unsigned char c8[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
539
unsigned char c9[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
540
unsigned char *packed;
541
unsigned char *unpacked;
545
/* Pack at offset = 2 bytes, e.g. starting with 21st bit */
546
packed = pack_7bit(c7, 6, 2, FALSE, &packed_size, 0);
548
if (g_test_verbose())
549
g_print("Packed to size: %ld\n", packed_size);
551
g_assert(packed != NULL);
552
g_assert(packed_size == 6);
554
unpacked = unpack_7bit(packed, 6, 2, FALSE, 6, &unpacked_size, 0xff);
555
if (g_test_verbose())
556
g_print("Unpacked to size: %ld\n", unpacked_size);
558
g_assert(unpacked != NULL);
559
g_assert(unpacked_size == 6);
560
g_assert(unpacked[6] == 0xff);
561
g_assert(unpacked[0] == 'a');
562
g_assert(unpacked[5] == 'f');
567
/* Pack at offset = 6 bytes, we should be able to fit one character
568
* into the first byte, and the other 7 characters into the following
569
* 7 bytes. The 7 MSB bits of the last byte should be 0 since
570
* we're not using CBS packing
572
packed = pack_7bit(c8, 8, 6, FALSE, &packed_size, 0);
574
if (g_test_verbose())
575
g_print("Packed to size: %ld\n", packed_size);
577
g_assert(packed != NULL);
578
g_assert(packed_size == 8);
580
unpacked = unpack_7bit(packed, 8, 6, FALSE, 8, &unpacked_size, 0xff);
581
if (g_test_verbose())
582
g_print("Unpacked to size: %ld\n", unpacked_size);
584
g_assert(unpacked != NULL);
585
g_assert(unpacked_size == 8);
586
g_assert(unpacked[8] == 0xff);
587
g_assert(unpacked[0] == 'a');
588
g_assert(unpacked[7] == 'h');
593
/* Same as above, but instead pack in 9 characters */
594
packed = pack_7bit(c9, 9, 6, FALSE, &packed_size, 0);
596
if (g_test_verbose())
597
g_print("Packed to size: %ld\n", packed_size);
599
g_assert(packed != NULL);
600
g_assert(packed_size == 8);
602
unpacked = unpack_7bit(packed, 8, 6, FALSE, 9, &unpacked_size, 0xff);
603
if (g_test_verbose())
604
g_print("Unpacked to size: %ld\n", unpacked_size);
606
g_assert(unpacked != NULL);
607
g_assert(unpacked_size == 9);
608
g_assert(unpacked[9] == 0xff);
609
g_assert(unpacked[0] == 'a');
610
g_assert(unpacked[8] == 'i');
616
int main(int argc, char **argv)
618
g_test_init(&argc, &argv, NULL);
620
g_test_add_func("/testutil/Invalid Conversions", test_invalid);
621
g_test_add_func("/testutil/Valid Conversions", test_valid);
622
g_test_add_func("/testutil/Decode Encode", test_decode_encode);
623
g_test_add_func("/testutil/Pack Size", test_pack_size);
624
g_test_add_func("/testutil/CBS CR Handling", test_cr_handling);
625
g_test_add_func("/testutil/SMS Handling", test_sms_handling);
626
g_test_add_func("/testutil/Offset Handling", test_offset_handling);