~binli/ubuntu/vivid/modemmanager/lp1441095

« back to all changes in this revision

Viewing changes to src/tests/test-sms-part-cdma.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Guido Günther, Michael Biebl
  • Date: 2014-06-25 02:23:09 UTC
  • mfrom: (20.2.2 sid)
  • Revision ID: package-import@ubuntu.com-20140625022309-43papaenj5ikbx3y
Tags: 1.2.0-1
[ Guido Günther ]
* New upstream version 1.2.0 (Closes: #731851)
* Update patches
* Install locale files
* Require newer libqmi
* Update symbols file
* Ship gobject introspection data
* Ship vala bindings

[ Michael Biebl ]
* Use canonical URI for Vcs-Git
* Use gir dh addon
* Update extendend package description (Closes: #744180)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/*
 
3
 * This program is free software; you can redistribute it and/or modify
 
4
 * it under the terms of the GNU General Public License as published by
 
5
 * the Free Software Foundation; either version 2 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details:
 
12
 *
 
13
 * Copyright (C) 2013 Google, Inc.
 
14
 */
 
15
 
 
16
#include <glib.h>
 
17
#include <glib-object.h>
 
18
#include <string.h>
 
19
#include <stdio.h>
 
20
#include <locale.h>
 
21
 
 
22
#define _LIBMM_INSIDE_MM
 
23
#include <libmm-glib.h>
 
24
 
 
25
#include "mm-sms-part-cdma.h"
 
26
#include "mm-log.h"
 
27
 
 
28
/* If defined will print debugging traces */
 
29
#ifdef TEST_SMS_PART_ENABLE_TRACE
 
30
#define trace_pdu(pdu, pdu_len) do {      \
 
31
        guint i;                          \
 
32
                                          \
 
33
        g_print ("\n        ");           \
 
34
        for (i = 0; i < len; i++) {       \
 
35
            g_print ("  0x%02X", pdu[i]); \
 
36
            if (((i + 1) % 12) == 0)      \
 
37
                g_print ("\n        ");   \
 
38
        }                                 \
 
39
        g_print ("\n");                   \
 
40
    } while (0)
 
41
#else
 
42
#define trace_pdu(...)
 
43
#endif
 
44
 
 
45
/********************* PDU PARSER TESTS *********************/
 
46
 
 
47
static void
 
48
common_test_part_from_hexpdu (const gchar *hexpdu,
 
49
                              MMSmsCdmaTeleserviceId expected_teleservice_id,
 
50
                              MMSmsCdmaServiceCategory expected_service_category,
 
51
                              const gchar *expected_address,
 
52
                              guint8 expected_bearer_reply_option,
 
53
                              const gchar *expected_text)
 
54
{
 
55
    MMSmsPart *part;
 
56
    GError *error = NULL;
 
57
 
 
58
    mm_dbg (" ");
 
59
    part = mm_sms_part_cdma_new_from_pdu (0, hexpdu, &error);
 
60
    g_assert_no_error (error);
 
61
    g_assert (part != NULL);
 
62
 
 
63
    if (expected_teleservice_id != MM_SMS_CDMA_TELESERVICE_ID_UNKNOWN)
 
64
        g_assert_cmpuint (expected_teleservice_id, ==, mm_sms_part_get_cdma_teleservice_id (part));
 
65
    if (expected_service_category != MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN)
 
66
        g_assert_cmpuint (expected_service_category, ==, mm_sms_part_get_cdma_service_category (part));
 
67
    if (expected_address) {
 
68
        if (expected_address[0])
 
69
            g_assert_cmpstr (expected_address, ==, mm_sms_part_get_number (part));
 
70
        else
 
71
            g_assert (mm_sms_part_get_number (part) == NULL);
 
72
    }
 
73
    if (expected_bearer_reply_option)
 
74
        g_assert_cmpuint (expected_bearer_reply_option, ==, mm_sms_part_get_message_reference (part));
 
75
    if (expected_text)
 
76
        g_assert_cmpstr (expected_text, ==, mm_sms_part_get_text (part));
 
77
 
 
78
    mm_sms_part_free (part);
 
79
}
 
80
 
 
81
static void
 
82
common_test_part_from_pdu (const guint8 *pdu,
 
83
                           gsize pdu_size,
 
84
                           MMSmsCdmaTeleserviceId expected_teleservice_id,
 
85
                           MMSmsCdmaServiceCategory expected_service_category,
 
86
                           const gchar *expected_address,
 
87
                           guint8 expected_bearer_reply_option,
 
88
                           const gchar *expected_text)
 
89
{
 
90
    gchar *hexpdu;
 
91
 
 
92
    hexpdu = mm_utils_bin2hexstr (pdu, pdu_size);
 
93
    common_test_part_from_hexpdu (hexpdu,
 
94
                                  expected_teleservice_id,
 
95
                                  expected_service_category,
 
96
                                  expected_address,
 
97
                                  expected_bearer_reply_option,
 
98
                                  expected_text);
 
99
    g_free (hexpdu);
 
100
}
 
101
 
 
102
static void
 
103
common_test_invalid_part_from_hexpdu (const gchar *hexpdu)
 
104
{
 
105
    MMSmsPart *part;
 
106
    GError *error = NULL;
 
107
 
 
108
    mm_dbg (" ");
 
109
    part = mm_sms_part_cdma_new_from_pdu (0, hexpdu, &error);
 
110
    g_assert (part == NULL);
 
111
    /* We don't care for the specific error type */
 
112
    g_assert (error != NULL);
 
113
    g_error_free (error);
 
114
}
 
115
 
 
116
static void
 
117
common_test_invalid_part_from_pdu (const guint8 *pdu,
 
118
                                   gsize pdu_size)
 
119
{
 
120
    gchar *hexpdu;
 
121
 
 
122
    hexpdu = mm_utils_bin2hexstr (pdu, pdu_size);
 
123
    common_test_invalid_part_from_hexpdu (hexpdu);
 
124
    g_free (hexpdu);
 
125
}
 
126
 
 
127
static void
 
128
test_pdu1 (void)
 
129
{
 
130
    static const guint8 pdu[] = {
 
131
        /* message type */
 
132
        0x00,
 
133
        /* teleservice id */
 
134
        0x00, 0x02,
 
135
        0x10, 0x02,
 
136
        /* originating address */
 
137
        0x02, 0x07,
 
138
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
139
        /* bearer reply option */
 
140
        0x06, 0x01,
 
141
        0xFC,
 
142
        /* bearer data */
 
143
        0x08, 0x15,
 
144
        0x00, 0x03, 0x16, 0x8D, 0x30, 0x01, 0x06,
 
145
        0x10, 0x24, 0x18, 0x30, 0x60, 0x80, 0x03,
 
146
        0x06, 0x10, 0x10, 0x04, 0x04, 0x48, 0x47
 
147
    };
 
148
 
 
149
    common_test_part_from_pdu (
 
150
        pdu, sizeof (pdu),
 
151
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
152
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
153
        "3305773196",
 
154
        63,
 
155
        "AAAA");
 
156
}
 
157
 
 
158
static void
 
159
test_invalid_parameter_length (void)
 
160
{
 
161
    static const guint8 pdu[] = {
 
162
        /* message type */
 
163
        0x00,
 
164
        /* teleservice id */
 
165
        0x00, 0x02,
 
166
        0x10, 0x02,
 
167
        /* originating address */
 
168
        0x02, 0x07,
 
169
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
170
        /* bearer reply option */
 
171
        0x06, 0x01,
 
172
        0xFC,
 
173
        /* bearer data */
 
174
        0x08, 0x20, /* wrong parameter length! */
 
175
        0x00, 0x03, 0x16, 0x8D, 0x30, 0x01, 0x06,
 
176
        0x10, 0x24, 0x18, 0x30, 0x60, 0x80, 0x03,
 
177
        0x06, 0x10, 0x10, 0x04, 0x04, 0x48, 0x47
 
178
    };
 
179
 
 
180
    common_test_invalid_part_from_pdu (pdu, sizeof (pdu));
 
181
}
 
182
 
 
183
static void
 
184
test_invalid_address_length (void)
 
185
{
 
186
    static const guint8 pdu[] = {
 
187
        /* message type */
 
188
        0x00,
 
189
        /* teleservice id */
 
190
        0x00, 0x02,
 
191
        0x10, 0x02,
 
192
        /* originating address (wrong num_fields) */
 
193
        0x02, 0x07,
 
194
        0x03, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
195
        /* bearer reply option */
 
196
        0x06, 0x01,
 
197
        0xFC,
 
198
        /* bearer data */
 
199
        0x08, 0x15,
 
200
        0x00, 0x03, 0x16, 0x8D, 0x30, 0x01, 0x06,
 
201
        0x10, 0x24, 0x18, 0x30, 0x60, 0x80, 0x03,
 
202
        0x06, 0x10, 0x10, 0x04, 0x04, 0x48, 0x47
 
203
    };
 
204
 
 
205
    common_test_part_from_pdu (
 
206
        pdu, sizeof (pdu),
 
207
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
208
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
209
        "",
 
210
        63,
 
211
        NULL);
 
212
}
 
213
 
 
214
static void
 
215
test_created_by_us (void)
 
216
{
 
217
    static const guint8 pdu[] = {
 
218
        /* message type */
 
219
        0x00,
 
220
        /* teleservice id */
 
221
        0x00, 0x02,
 
222
        0x10, 0x02,
 
223
        /* destination address */
 
224
        0x04, 0x07,
 
225
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
226
        /* bearer data */
 
227
        0x08, 0x0D,
 
228
        0x00, 0x03, 0x20, 0x00, 0x00, /* message id */
 
229
        0x01, 0x06, 0x10, 0x24, 0x18, 0x30, 0x60, 0x80 /* user_data */
 
230
    };
 
231
 
 
232
    common_test_part_from_pdu (
 
233
        pdu, sizeof (pdu),
 
234
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
235
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
236
        "3305773196",
 
237
        0,
 
238
        "AAAA");
 
239
}
 
240
 
 
241
static void
 
242
test_latin_encoding (void)
 
243
{
 
244
    static const guint8 pdu[] = {
 
245
        /* message type */
 
246
        0x00,
 
247
        /* teleservice id */
 
248
        0x00, 0x02,
 
249
        0x10, 0x02,
 
250
        /* originating address */
 
251
        0x02, 0x07,
 
252
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
253
        /* bearer reply option */
 
254
        0x06, 0x01,
 
255
        0xFC,
 
256
        /* bearer data */
 
257
        0x08, 0x39,
 
258
            /* message id */
 
259
            0x00, 0x03,
 
260
            0x13, 0x8D, 0x20,
 
261
            /* user data */
 
262
            0x01, 0x27,
 
263
            0x41, 0x29, 0x19, 0x22, 0xE1, 0x19, 0x1A, 0xE1,
 
264
            0x1A, 0x01, 0x19, 0xA1, 0x19, 0xA1, 0xA9, 0xB1,
 
265
            0xB9, 0xE9, 0x53, 0x4B, 0x23, 0xAB, 0x53, 0x23,
 
266
            0xAB, 0x23, 0x2B, 0xAB, 0xAB, 0x2B, 0x23, 0xAB,
 
267
            0x53, 0x23, 0x2B, 0xAB, 0x53, 0xAB, 0x20,
 
268
            /* message center timestamp */
 
269
            0x03, 0x06,
 
270
            0x13, 0x10, 0x23, 0x20, 0x06, 0x37,
 
271
            /* priority indicator */
 
272
            0x08, 0x01,
 
273
            0x00
 
274
    };
 
275
 
 
276
    common_test_part_from_pdu (
 
277
        pdu, sizeof (pdu),
 
278
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
279
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
280
        "3305773196",
 
281
        63,
 
282
        /* this is ASCII-7 but message uses latin encoding */
 
283
        "#$\\##\\#@#4#4567=*idujdudeuuedujdeujud");
 
284
}
 
285
 
 
286
static void
 
287
test_latin_encoding_2 (void)
 
288
{
 
289
    static const guint8 pdu[] = {
 
290
        /* message type */
 
291
        0x00,
 
292
        /* teleservice id */
 
293
        0x00, 0x02,
 
294
        0x10, 0x02,
 
295
        /* originating address */
 
296
        0x02, 0x07,
 
297
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
298
        /* bearer reply option */
 
299
        0x06, 0x01,
 
300
        0xFC,
 
301
        /* bearer data */
 
302
        0x08, 0x1C,
 
303
            /* message id */
 
304
            0x00, 0x03,
 
305
            0x13, 0x8D, 0x20,
 
306
            /* user data */
 
307
            0x01, 0x0A,
 
308
            0x40, 0x42, 0x1B, 0x0B, 0x6B, 0x83, 0x2F, 0x9B,
 
309
            0x71, 0x08,
 
310
            /* message center timestamp */
 
311
            0x03, 0x06,
 
312
            0x13, 0x10, 0x23, 0x20, 0x06, 0x37,
 
313
            /* priority indicator */
 
314
            0x08, 0x01,
 
315
            0x00
 
316
    };
 
317
 
 
318
    common_test_part_from_pdu (
 
319
        pdu, sizeof (pdu),
 
320
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
321
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
322
        "3305773196",
 
323
        63,
 
324
        /* this is latin and message uses latin encoding */
 
325
        "Campeón!");
 
326
}
 
327
 
 
328
static void
 
329
test_unicode_encoding (void)
 
330
{
 
331
    static const guint8 pdu[] = {
 
332
        /* message type */
 
333
        0x00,
 
334
        /* teleservice id */
 
335
        0x00, 0x02,
 
336
        0x10, 0x02,
 
337
        /* originating address */
 
338
        0x02, 0x07,
 
339
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
340
        /* bearer reply option */
 
341
        0x06, 0x01,
 
342
        0xFC,
 
343
        /* bearer data */
 
344
        0x08, 0x28,
 
345
            /* message id */
 
346
            0x00, 0x03,
 
347
            0x1B, 0x73, 0xF0,
 
348
            /* user data */
 
349
            0x01, 0x16,
 
350
            0x20, 0x52, 0x71, 0x6A, 0xB8, 0x5A, 0xA7, 0x92,
 
351
            0xDB, 0xC3, 0x37, 0xC4, 0xB7, 0xDA, 0xDA, 0x82,
 
352
            0x98, 0xB4, 0x50, 0x42, 0x94, 0x18,
 
353
            /* message center timestamp */
 
354
            0x03, 0x06,
 
355
            0x13, 0x10, 0x24, 0x10, 0x45, 0x28,
 
356
            /* priority indicator */
 
357
            0x08, 0x01,
 
358
            0x00
 
359
    };
 
360
 
 
361
    common_test_part_from_pdu (
 
362
        pdu, sizeof (pdu),
 
363
        MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
364
        MM_SMS_CDMA_SERVICE_CATEGORY_UNKNOWN,
 
365
        "3305773196",
 
366
        63,
 
367
        "中國哲學書電子化計劃");
 
368
}
 
369
 
 
370
/********************* PDU CREATOR TESTS *********************/
 
371
 
 
372
static void
 
373
common_test_create_pdu (MMSmsCdmaTeleserviceId teleservice_id,
 
374
                        const gchar *number,
 
375
                        const gchar *text,
 
376
                        const guint8 *data,
 
377
                        gsize data_size,
 
378
                        const guint8 *expected,
 
379
                        gsize expected_size)
 
380
{
 
381
    MMSmsPart *part;
 
382
    guint8 *pdu;
 
383
    guint len = 0;
 
384
    GError *error = NULL;
 
385
 
 
386
    g_assert (number != NULL);
 
387
 
 
388
    part = mm_sms_part_new (0, MM_SMS_PDU_TYPE_CDMA_SUBMIT);
 
389
    mm_sms_part_set_cdma_teleservice_id (part, teleservice_id);
 
390
    mm_sms_part_set_number (part, number);
 
391
    if (text)
 
392
        mm_sms_part_set_text (part, text);
 
393
    else {
 
394
        GByteArray *data_bytearray;
 
395
 
 
396
        data_bytearray = g_byte_array_sized_new (data_size);
 
397
        g_byte_array_append (data_bytearray, data, data_size);
 
398
        mm_sms_part_take_data (part, data_bytearray);
 
399
    }
 
400
 
 
401
    pdu = mm_sms_part_cdma_get_submit_pdu (part, &len, &error);
 
402
 
 
403
    trace_pdu (pdu, len);
 
404
 
 
405
    g_assert_no_error (error);
 
406
    g_assert (pdu != NULL);
 
407
    g_assert_cmpuint (len, ==, expected_size);
 
408
    g_assert_cmpint (memcmp (pdu, expected, len), ==, 0);
 
409
 
 
410
    g_free (pdu);
 
411
}
 
412
 
 
413
static void
 
414
test_create_pdu_text_ascii_encoding (void)
 
415
{
 
416
    static const char *number = "3305773196";
 
417
    static const char *text = "AAAA";
 
418
    static const guint8 expected[] = {
 
419
        /* message type */
 
420
        0x00,
 
421
        /* teleservice id */
 
422
        0x00, 0x02,
 
423
        0x10, 0x02,
 
424
        /* destination address */
 
425
        0x04, 0x07,
 
426
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
427
        /* bearer data */
 
428
        0x08, 0x0D,
 
429
        0x00, 0x03, 0x20, 0x00, 0x00, /* message id */
 
430
        0x01, 0x06, 0x10, 0x24, 0x18, 0x30, 0x60, 0x80 /* user_data */
 
431
    };
 
432
 
 
433
    common_test_create_pdu (MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
434
                            number,
 
435
                            text,
 
436
                            NULL, 0,
 
437
                            expected, sizeof (expected));
 
438
}
 
439
 
 
440
static void
 
441
test_create_pdu_text_latin_encoding (void)
 
442
{
 
443
    static const char *number = "3305773196";
 
444
    static const char *text = "Campeón!";
 
445
    static const guint8 expected[] = {
 
446
        /* message type */
 
447
        0x00,
 
448
        /* teleservice id */
 
449
        0x00, 0x02,
 
450
        0x10, 0x02,
 
451
        /* destination address */
 
452
        0x04, 0x07,
 
453
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
454
        /* bearer data */
 
455
        0x08, 0x11,
 
456
            /* message id */
 
457
            0x00, 0x03,
 
458
            0x20, 0x00, 0x00,
 
459
            /* user data */
 
460
            0x01, 0x0A,
 
461
            0x40, 0x42, 0x1B, 0x0B, 0x6B, 0x83, 0x2F, 0x9B,
 
462
            0x71, 0x08
 
463
    };
 
464
 
 
465
    common_test_create_pdu (MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
466
                            number,
 
467
                            text,
 
468
                            NULL, 0,
 
469
                            expected, sizeof (expected));
 
470
}
 
471
 
 
472
static void
 
473
test_create_pdu_text_unicode_encoding (void)
 
474
{
 
475
    static const char *number = "3305773196";
 
476
    static const char *text = "中國哲學書電子化計劃";
 
477
    static const guint8 expected[] = {
 
478
        /* message type */
 
479
        0x00,
 
480
        /* teleservice id */
 
481
        0x00, 0x02,
 
482
        0x10, 0x02,
 
483
        /* destination address */
 
484
        0x04, 0x07,
 
485
        0x02, 0x8C, 0xE9, 0x5D, 0xCC, 0x65, 0x80,
 
486
        /* bearer data */
 
487
        0x08, 0x1D,
 
488
            /* message id */
 
489
            0x00, 0x03,
 
490
            0x20, 0x00, 0x00,
 
491
            /* user data */
 
492
            0x01, 0x16,
 
493
            0x20, 0x52, 0x71, 0x6A, 0xB8, 0x5A, 0xA7, 0x92,
 
494
            0xDB, 0xC3, 0x37, 0xC4, 0xB7, 0xDA, 0xDA, 0x82,
 
495
            0x98, 0xB4, 0x50, 0x42, 0x94, 0x18
 
496
    };
 
497
 
 
498
    common_test_create_pdu (MM_SMS_CDMA_TELESERVICE_ID_WMT,
 
499
                            number,
 
500
                            text,
 
501
                            NULL, 0,
 
502
                            expected, sizeof (expected));
 
503
}
 
504
 
 
505
/************************************************************/
 
506
 
 
507
void
 
508
_mm_log (const char *loc,
 
509
         const char *func,
 
510
         guint32 level,
 
511
         const char *fmt,
 
512
         ...)
 
513
{
 
514
#if defined ENABLE_TEST_MESSAGE_TRACES
 
515
    /* Dummy log function */
 
516
    va_list args;
 
517
    gchar *msg;
 
518
 
 
519
    va_start (args, fmt);
 
520
    msg = g_strdup_vprintf (fmt, args);
 
521
    va_end (args);
 
522
    g_print ("%s\n", msg);
 
523
    g_free (msg);
 
524
#endif
 
525
}
 
526
 
 
527
int main (int argc, char **argv)
 
528
{
 
529
    setlocale (LC_ALL, "");
 
530
 
 
531
    g_type_init ();
 
532
    g_test_init (&argc, &argv, NULL);
 
533
 
 
534
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/pdu1", test_pdu1);
 
535
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/invalid-parameter-length", test_invalid_parameter_length);
 
536
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/invalid-address-length", test_invalid_address_length);
 
537
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/created-by-us", test_created_by_us);
 
538
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/latin-encoding", test_latin_encoding);
 
539
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/latin-encoding-2", test_latin_encoding_2);
 
540
    g_test_add_func ("/MM/SMS/CDMA/PDU-Parser/unicode-encoding", test_unicode_encoding);
 
541
 
 
542
    g_test_add_func ("/MM/SMS/CDMA/PDU-Creator/ascii-encoding", test_create_pdu_text_ascii_encoding);
 
543
    g_test_add_func ("/MM/SMS/CDMA/PDU-Creator/latin-encoding", test_create_pdu_text_latin_encoding);
 
544
    g_test_add_func ("/MM/SMS/CDMA/PDU-Creator/unicode-encoding", test_create_pdu_text_unicode_encoding);
 
545
 
 
546
    return g_test_run ();
 
547
}