Debian Codegraph @

src/libqmi-glib/test/test-message.c

raw
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) 2012-2014 Aleksander Morgado <[email protected]>
14 */
15
16 #include <config.h>
17 #include <glib-object.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 #include <libqmi-glib.h>
22
23 /*****************************************************************************/
24
25 static gchar *
26 __str_hex (gconstpointer mem,
27 gsize size,
28 gchar delimiter)
29 {
30 const guint8 *data = mem;
31 gsize i;
32 gsize j;
33 gsize new_str_length;
34 gchar *new_str;
35
36 /* Get new string length. If input string has N bytes, we need:
37 * - 1 byte for last NUL char
38 * - 2N bytes for hexadecimal char representation of each byte...
39 * - N-1 bytes for the separator ':'
40 * So... a total of (1+2N+N-1) = 3N bytes are needed... */
41 new_str_length = 3 * size;
42
43 /* Allocate memory for new array and initialize contents to NUL */
44 new_str = g_malloc0 (new_str_length);
45
46 /* Print hexadecimal representation of each byte... */
47 for (i = 0, j = 0; i < size; i++, j += 3) {
48 /* Print character in output string... */
49 snprintf (&new_str[j], 3, "%02X", data[i]);
50 /* And if needed, add separator */
51 if (i != (size - 1) )
52 new_str[j + 2] = delimiter;
53 }
54
55 /* Set output string */
56 return new_str;
57 }
58
59 static void
60 _g_assert_cmpmem (gconstpointer mem1,
61 gsize size1,
62 gconstpointer mem2,
63 gsize size2)
64 {
65 g_autofree gchar *str1 = NULL;
66 g_autofree gchar *str2 = NULL;
67
68 str1 = __str_hex (mem1, size1, ':');
69 str2 = __str_hex (mem2, size2, ':');
70 g_assert_cmpstr (str1, ==, str2);
71 }
72
73 /*****************************************************************************/
74
75 static void
76 test_message_parse_common (const guint8 *buffer,
77 guint buffer_len,
78 guint n_expected_messages)
79 {
80 g_autoptr(GByteArray) array = NULL;
81 guint n_messages = 0;
82
83 array = g_byte_array_sized_new (buffer_len);
84 g_byte_array_append (array, buffer, buffer_len);
85
86 do {
87 g_autoptr(GError) error = NULL;
88 g_autoptr(QmiMessage) message = NULL;
89 g_autofree gchar *printable = NULL;
90
91 message = qmi_message_new_from_raw (array, &error);
92 if (!message) {
93 if (error) {
94 if (n_messages < n_expected_messages)
95 g_printerr ("error creating message from raw data: '%s'\n", error->message);
96 else
97 g_debug ("error creating message from raw data: '%s'", error->message);
98 }
99 break;
100 }
101
102 printable = qmi_message_get_printable_full (message, NULL, "");
103 g_debug ("%s", printable);
104
105 n_messages++;
106 } while (array->len > 0);
107
108 g_assert_cmpuint (n_messages, ==, n_expected_messages);
109 }
110
111 static void
112 test_message_parse_wrong_qmux (void)
113 {
114 const guint8 buffer[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a };
115
116 test_message_parse_common (buffer, sizeof (buffer), 0);
117 }
118
119 static void
120 test_message_parse_tiny (void)
121 {
122 const guint8 buffer[] = { 0x01, 0x00, 0x02 };
123
124 test_message_parse_common (buffer, sizeof (buffer), 0);
125 }
126
127 static void
128 test_message_parse_short (void)
129 {
130 const guint8 buffer[] = {
131 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a,
132 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b,
133 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x66, 0x05
134 };
135
136 test_message_parse_common (buffer, sizeof (buffer), 0);
137 }
138
139 static void
140 test_message_parse_complete (void)
141 {
142 const guint8 buffer[] = {
143 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a,
144 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b,
145 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01,
146 0x00, 0x11, 0x05
147 };
148
149 test_message_parse_common (buffer, sizeof (buffer), 1);
150 }
151
152 static void
153 test_message_parse_complete_and_short (void)
154 {
155 const guint8 buffer[] = {
156 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a,
157 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b,
158 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01,
159 0x00, 0x11, 0x05, 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00,
160 0x20, 0x00, 0x1a, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
161 0x02, 0x00, 0x9b, 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x66, 0x05
162 };
163
164 test_message_parse_common (buffer, sizeof (buffer), 1);
165 }
166
167 static void
168 test_message_parse_complete_and_complete (void)
169 {
170 const guint8 buffer[] = {
171 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00, 0x20, 0x00, 0x1a,
172 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x9b,
173 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12, 0x04, 0x00, 0x01,
174 0x00, 0x11, 0x05, 0x01, 0x26, 0x00, 0x80, 0x03, 0x01, 0x02, 0x01, 0x00,
175 0x20, 0x00, 0x1a, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
176 0x02, 0x00, 0x9b, 0x05, 0x11, 0x04, 0x00, 0x01, 0x00, 0x65, 0x05, 0x12,
177 0x04, 0x00, 0x01, 0x00, 0x11, 0x05
178 };
179
180 test_message_parse_common (buffer, sizeof (buffer), 2);
181 }
182
183 static void
184 test_message_printable_common (const guint8 *buffer,
185 guint buffer_len,
186 guint16 vendor_id,
187 const gchar *expected_in_printable)
188 {
189 g_autoptr(QmiMessageContext) context = NULL;
190 g_autoptr(QmiMessage) message = NULL;
191 g_autoptr(GByteArray) array = NULL;
192 g_autoptr(GError) error = NULL;
193 g_autofree gchar *printable = NULL;
194
195 if (vendor_id != QMI_MESSAGE_VENDOR_GENERIC) {
196 context = qmi_message_context_new ();
197 qmi_message_context_set_vendor_id (context, vendor_id);
198 }
199
200 array = g_byte_array_sized_new (buffer_len);
201 g_byte_array_append (array, buffer, buffer_len);
202 message = qmi_message_new_from_raw (array, &error);
203 g_assert_no_error (error);
204 g_assert (message);
205
206 printable = qmi_message_get_printable_full (message, context, "");
207 g_debug ("%s", printable);
208 g_assert (strstr (printable, expected_in_printable));
209 }
210
211 static void
212 test_message_parse_wrong_tlv (void)
213 {
214 const guint8 buffer[] = {
215 0x01, 0x4F, 0x00, 0x80, 0x03, 0x03, 0x02, 0x01, 0x00, 0x24, 0x00, 0x43,
216 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x04, 0x00, 0x02,
217 0x03, 0x00, 0x00, 0x1D, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x02,
218 0x00, 0x00, 0x00, 0x15, 0x03, 0x00, 0x01, 0x05, 0x01, 0x12, 0x0E, 0x00,
219 0x36, 0x01, 0x04, 0x01, 0x09, 0x20, 0x54, 0x2D, 0x4D, 0x6F, 0x62, 0x69,
220 0x6C, 0x65, 0x11, 0x02, 0x00, 0x01, 0x05, 0x10, 0x01, 0x00, 0x01, 0x01,
221 0x06, 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, 0x05
222 };
223
224 test_message_printable_common (buffer, G_N_ELEMENTS (buffer), QMI_MESSAGE_VENDOR_GENERIC, "ERROR: Reading TLV would overflow");
225 }
226
227 #if defined HAVE_QMI_INDICATION_PDS_EVENT_REPORT
228
229 static void
230 test_message_parse_missing_size (void)
231 {
232 /* PDS Event Report indication: NMEA position */
233 const guint8 buffer[] = {
234 0x01, /* marker */
235 0x10, 0x00, /* qmux length */
236 0x80, /* qmux flags */
237 0x06, /* service: PDS */
238 0x03, /* client */
239 0x04, /* service flags: Indication */
240 0x01, 0x00, /* transaction */
241 0x01, 0x00, /* message: Event Report */
242 0x04, 0x00, /* all tlvs length: 4 bytes */
243 /* TLV */
244 0x11, /* type: Extended NMEA Position (1 guint8 and one 16-bit-sized string) */
245 0x01, 0x00, /* length: 1 byte (WE ONLY GIVE THE GUINT8!!!) */
246 0x01
247 };
248
249 test_message_printable_common (buffer, G_N_ELEMENTS (buffer), QMI_MESSAGE_VENDOR_GENERIC, "ERROR: Reading TLV would overflow");
250 }
251
252 #endif
253
254 #if defined HAVE_QMI_INDICATION_LOC_NMEA
255
256 static void
257 test_message_parse_string_with_crlf (void)
258 {
259 /* LOC indication: NMEA
260 * The NMEA trace comes suffixed with \r\n, this test makes sure the proper
261 * parsing is done on the string.
262 */
263 const guint8 buffer[] = {
264 0x01, 0x2D, 0x00, 0x80, 0x10, 0x01, 0x04, 0xB4, 0x00, 0x26, 0x00, 0x21,
265 0x00, 0x01, 0x1E, 0x00, 0x24, 0x47, 0x50, 0x47, 0x53, 0x41, 0x2C, 0x41,
266 0x2C, 0x31, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C,
267 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2A, 0x31, 0x45, 0x0D, 0x0A
268 };
269
270 test_message_printable_common (buffer, sizeof (buffer), QMI_MESSAGE_VENDOR_GENERIC, "$GPGSA,A,1,,,,,,,,,,,,,,,*1E");
271 }
272
273 #endif
274
275 #if defined HAVE_QMI_MESSAGE_DMS_SWI_GET_CURRENT_FIRMWARE
276
277 static void
278 test_message_parse_string_with_trailing_nul (void)
279 {
280 /* The Sierra Wireless (vid 0x1199) specific method to get current firmware
281 * info uses NUL-terminated strings.
282 */
283 const guint8 buffer[] = {
284 0x01, 0x82, 0x00, 0x80, 0x02, 0x03, 0x02, 0x01, 0x00, 0x56, 0x55, 0x76,
285 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x0C, 0x00, 0x30,
286 0x30, 0x32, 0x2E, 0x30, 0x37, 0x32, 0x5F, 0x30, 0x30, 0x30, 0x00, 0x17,
287 0x08, 0x00, 0x47, 0x45, 0x4E, 0x45, 0x52, 0x49, 0x43, 0x00, 0x16, 0x08,
288 0x00, 0x30, 0x30, 0x30, 0x2E, 0x30, 0x30, 0x35, 0x00, 0x15, 0x02, 0x00,
289 0x31, 0x00, 0x13, 0x08, 0x00, 0x31, 0x31, 0x30, 0x32, 0x34, 0x37, 0x36,
290 0x00, 0x12, 0x15, 0x00, 0x53, 0x57, 0x49, 0x39, 0x58, 0x33, 0x30, 0x43,
291 0x5F, 0x30, 0x32, 0x2E, 0x33, 0x33, 0x2E, 0x30, 0x33, 0x2E, 0x30, 0x30,
292 0x00, 0x11, 0x15, 0x00, 0x53, 0x57, 0x49, 0x39, 0x58, 0x33, 0x30, 0x43,
293 0x5F, 0x30, 0x32, 0x2E, 0x33, 0x33, 0x2E, 0x30, 0x33, 0x2E, 0x30, 0x30,
294 0x00, 0x10, 0x07, 0x00, 0x4D, 0x43, 0x37, 0x34, 0x35, 0x35, 0x00
295 };
296
297 test_message_printable_common (buffer, sizeof (buffer), 0x1199, "SWI9X30C_02.33.03.00");
298 }
299
300 #endif
301
302 #if defined HAVE_QMI_MESSAGE_DMS_GET_MODEL
303
304 static void
305 test_message_parse_string_with_trailing_tab (void)
306 {
307 /* Quectel EM12-AW model strint has a trailing TAB character (ASCII 0x09) */
308 const guint8 buffer[] = {
309 0x01, 0x1E, 0x00, 0x80, 0x02, 0x05, 0x02, 0x01, 0x00, 0x22, 0x00, 0x12,
310 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x45,
311 0x4D, 0x31, 0x32, 0x2D, 0x41, 0x57, 0x09
312 };
313
314 test_message_printable_common (buffer, sizeof (buffer), QMI_MESSAGE_VENDOR_GENERIC, "EM12-AW");
315 }
316
317 #endif
318
319 #if defined HAVE_QMI_MESSAGE_NAS_SWI_GET_STATUS
320
321 static void
322 test_message_parse_signed_int (void)
323 {
324 /* Temperature given as a signed int */
325 const guint8 buffer[] = {
326 0x01, 0x27, 0x00, 0x80, 0x03, 0x05, 0x02, 0x01, 0x00, 0x56, 0x55, 0x1B,
327 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x09, 0x00, 0x08,
328 0x01, 0xFC, 0x0D, 0xFF, 0xFF, 0x02, 0x00, 0x00, 0x01, 0x05, 0x00, 0xFB,
329 0x05, 0x09, 0x00, 0x00
330 };
331
332 test_message_printable_common (buffer, sizeof (buffer), 0x1199, "temperature = '-5'");
333 }
334
335 #endif
336
337 static void
338 test_message_parse_empty_fixed_string (void)
339 {
340 /* NAS response: Get System Info
341 * If the "Network ID Valid" field is false, then the MCC/MNC fields will
342 * contain all zeros. Make sure that's printed as an empty string and not
343 * an error.
344 */
345 const guint8 buffer[] = {
346 0x01, /* marker */
347 0x2C, 0x00, /* qmux length */
348 0x80, /* qmux flags */
349 0x03, /* service: NAS */
350 0x03, /* client */
351 0x02, /* service flags: Response */
352 0x01, 0x00, /* transaction */
353 0x4D, 0x00, /* message: Get System Info */
354 0x20, 0x00, /* all tlvs length: 4 bytes */
355 /* TLV */
356 0x19, /* type: LTE System Info v2 */
357 0x1D, 0x00, /* length: 29 bytes */
358 0x01, /* Domain Valid */
359 0x04, /* Domain */
360 0x01, /* Service Capability Valid */
361 0x02, /* Service Capability */
362 0x01, /* Roaming Status Valid */
363 0x00, /* Roaming Status */
364 0x01, /* Forbidden Valid */
365 0x00, /* Forbidden */
366 0x00, /* LAC Valid */
367 0x00,0x00, /* LAC */
368 0x01, /* CID Valid */
369 0x0C, 0xF6, 0xE7, 0x02, /* CID */
370 0x01, /* Registration Reject Info Valid */
371 0x02, /* Registration Reject Domain */
372 0x0F, /* Registration Reject Cause */
373 0x00, /* Network ID Valid */
374 0x00, 0x00, 0x00, /* MCC */
375 0x00, 0x00, 0x00, /* MNC */
376 0x01, /* TAC Valid */
377 0x4D, 0xBE /* TAC */
378 };
379
380 test_message_printable_common (buffer, sizeof (buffer), QMI_MESSAGE_VENDOR_GENERIC, "mcc = '' mnc = ''");
381 }
382
383
384 /*****************************************************************************/
385
386 static void
387 test_message_new_request (void)
388 {
389 static const guint8 expected_buffer [] = {
390 0x01, /* marker */
391 0x0C, 0x00, /* qmux length */
392 0x00, /* qmux flags */
393 0x02, /* service: DMS */
394 0x01, /* client id */
395 0x00, /* service flags */
396 0x02, 0x00, /* transaction */
397 0xFF, 0xFF, /* message id */
398 0x00, 0x00, /* all tlvs length */
399 };
400
401 g_autoptr(QmiMessage) self = NULL;
402 g_autoptr(GError) error = NULL;
403 const guint8 *buffer;
404 gsize buffer_length = 0;
405
406 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
407 g_assert (self);
408
409 buffer = qmi_message_get_raw (self, &buffer_length, &error);
410 g_assert_no_error (error);
411 g_assert (buffer != NULL);
412 g_assert_cmpuint (buffer_length, >, 0);
413
414 _g_assert_cmpmem (buffer, buffer_length, expected_buffer, sizeof (expected_buffer));
415 }
416
417 static void
418 test_message_new_request_from_data (void)
419 {
420 static const guint8 expected_buffer [] = {
421 0x00, /* service flags */
422 0x02, 0x00, /* transaction */
423 0xFF, 0xFF, /* message id */
424 0x00, 0x00, /* all tlvs length */
425 };
426
427 g_autoptr(GByteArray) qmi = NULL;
428 g_autoptr(QmiMessage) self = NULL;
429 g_autoptr(GError) error = NULL;
430 const guint8 *buffer;
431 gsize buffer_length = 0;
432
433 /* set up service header */
434 qmi = g_byte_array_new ();
435 g_byte_array_append (qmi, expected_buffer, sizeof (expected_buffer));
436
437 self = qmi_message_new_from_data (QMI_SERVICE_DMS, 0x01, qmi, &error);
438 g_assert_no_error (error);
439 g_assert (self);
440
441 /* check that the QMUX header contains the right values*/
442 g_assert_cmpuint (qmi_message_get_service (self), ==, QMI_SERVICE_DMS);
443 g_assert_cmpuint (qmi_message_get_client_id (self), ==, 0x01);
444
445 /* length (13) = qmux marker (1) + qmux header length (5) + qmi header length (7) */
446 g_assert_cmpuint (qmi_message_get_length (self), ==, 13);
447
448 /* check that transaction and message IDs line up */
449 g_assert_cmpuint (qmi_message_get_transaction_id (self), ==, 0x02);
450 g_assert_cmpuint (qmi_message_get_message_id (self), ==, 0xFFFF);
451
452 buffer = qmi_message_get_raw (self, &buffer_length, &error);
453 g_assert_no_error (error);
454 g_assert (buffer != NULL);
455 g_assert_cmpuint (buffer_length, >, 0);
456 /* check that we have a qmux marker so we don't break framing */
457 g_assert_cmpuint (buffer[0], ==, 0x01);
458
459 /* make sure the qmi portion of the message is what we expect */
460 buffer = qmi_message_get_data (self, &buffer_length, &error);
461 g_assert_no_error (error);
462 g_assert (buffer != NULL);
463 g_assert_cmpuint (buffer_length, >, 0);
464 _g_assert_cmpmem (buffer, buffer_length, expected_buffer, sizeof (expected_buffer));
465 }
466
467 static void
468 test_message_new_response_ok (void)
469 {
470 static const guint8 expected_buffer [] = {
471 0x01, /* marker */
472 0x13, 0x00, /* qmux length */
473 0x80, /* qmux flags */
474 0x02, /* service: DMS */
475 0x01, /* client id */
476 0x02, /* service flags */
477 0x02, 0x00, /* transaction */
478 0xFF, 0xFF, /* message id */
479 0x07, 0x00, /* all tlvs length */
480 /* TLV */
481 0x02, /* tlv type */
482 0x04, 0x00, /* tlv size */
483 0x00, 0x00, 0x00, 0x00
484 };
485
486 g_autoptr(QmiMessage) request = NULL;
487 g_autoptr(QmiMessage) response = NULL;
488 g_autoptr(GError) error = NULL;
489 const guint8 *buffer;
490 gsize buffer_length = 0;
491
492 request = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
493 g_assert (request);
494
495 response = qmi_message_response_new (request, QMI_PROTOCOL_ERROR_NONE);
496 g_assert (response);
497
498 buffer = qmi_message_get_raw (response, &buffer_length, &error);
499 g_assert_no_error (error);
500 g_assert (buffer != NULL);
501 g_assert_cmpuint (buffer_length, >, 0);
502
503 _g_assert_cmpmem (buffer, buffer_length, expected_buffer, sizeof (expected_buffer));
504 }
505
506 static void
507 test_message_new_response_error (void)
508 {
509 static const guint8 expected_buffer [] = {
510 0x01, /* marker */
511 0x13, 0x00, /* qmux length */
512 0x80, /* qmux flags */
513 0x02, /* service: DMS */
514 0x01, /* client id */
515 0x02, /* service flags */
516 0x02, 0x00, /* transaction */
517 0xFF, 0xFF, /* message id */
518 0x07, 0x00, /* all tlvs length */
519 /* TLV */
520 0x02, /* tlv type */
521 0x04, 0x00, /* tlv size */
522 0x01, 0x00, 0x03, 0x00
523 };
524
525 g_autoptr(QmiMessage) request = NULL;
526 g_autoptr(QmiMessage) response = NULL;
527 g_autoptr(GError) error = NULL;
528 const guint8 *buffer;
529 gsize buffer_length = 0;
530
531 request = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
532 g_assert (request);
533
534 response = qmi_message_response_new (request, QMI_PROTOCOL_ERROR_INTERNAL);
535 g_assert (response);
536
537 buffer = qmi_message_get_raw (response, &buffer_length, &error);
538 g_assert_no_error (error);
539 g_assert (buffer != NULL);
540 g_assert_cmpuint (buffer_length, >, 0);
541
542 _g_assert_cmpmem (buffer, buffer_length, expected_buffer, sizeof (expected_buffer));
543 }
544
545 /*****************************************************************************/
546
547 static void
548 test_message_tlv_write_empty (void)
549 {
550 g_autoptr(QmiMessage) self = NULL;
551 g_autoptr(GError) error = NULL;
552 gboolean ret;
553 gsize init_offset;
554
555 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
556
557 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
558 g_assert_no_error (error);
559 g_assert (init_offset > 0);
560
561 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
562 g_assert_no_error (error);
563 g_assert (ret);
564 }
565
566 static void
567 test_message_tlv_write_reset (void)
568 {
569 g_autoptr(QmiMessage) self = NULL;
570 g_autoptr(GError) error = NULL;
571 gboolean ret;
572 gsize init_offset;
573 gsize previous_size;
574
575 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
576 previous_size = qmi_message_get_length (self);
577
578 /* Test reset just after init */
579 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
580 g_assert_no_error (error);
581 g_assert (init_offset > 0);
582
583 qmi_message_tlv_write_reset (self, init_offset);
584 g_assert_cmpuint (previous_size, ==, qmi_message_get_length (self));
585
586 /* Test reset after adding variables */
587 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
588 g_assert_no_error (error);
589 g_assert (init_offset > 0);
590
591 ret = qmi_message_tlv_write_guint8 (self, 0x12, &error);
592 g_assert_no_error (error);
593 g_assert (ret);
594
595 qmi_message_tlv_write_reset (self, init_offset);
596 g_assert_cmpuint (previous_size, ==, qmi_message_get_length (self));
597 }
598
599 static void
600 test_message_tlv_rw_8 (void)
601 {
602 g_autoptr(QmiMessage) self = NULL;
603 g_autoptr(GError) error = NULL;
604 gboolean ret;
605 gsize init_offset;
606 gsize expected_tlv_payload_size = 0;
607 guint16 tlv_length = 0;
608 gsize offset;
609 guint8 uint8;
610 gint8 int8;
611
612 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
613
614 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
615 g_assert_no_error (error);
616 g_assert (init_offset > 0);
617
618 /* Add one of each */
619 expected_tlv_payload_size += 1;
620 ret = qmi_message_tlv_write_guint8 (self, 0x12, &error);
621 g_assert_no_error (error);
622 g_assert (ret);
623
624 expected_tlv_payload_size += 1;
625 ret = qmi_message_tlv_write_gint8 (self, 0 - 0x12, &error);
626 g_assert_no_error (error);
627 g_assert (ret);
628
629 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
630 g_assert_no_error (error);
631 g_assert (ret);
632
633 /* Now read */
634 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
635 g_assert_no_error (error);
636 g_assert (init_offset > 0);
637 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
638
639 offset = 0;
640
641 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &uint8, &error);
642 g_assert_no_error (error);
643 g_assert (ret);
644 g_assert_cmpuint (uint8, ==, 0x12);
645
646 ret = qmi_message_tlv_read_gint8 (self, init_offset, &offset, &int8, &error);
647 g_assert_no_error (error);
648 g_assert (ret);
649 g_assert_cmpuint (int8, ==, 0 - 0x12);
650 }
651
652 static void
653 test_message_tlv_rw_16 (void)
654 {
655 guint n_bytes_prefixed;
656
657 /* We'll add [0 or 1] bytes before the actual 16-bit values, so that we
658 * check all possible memory alignments. */
659 for (n_bytes_prefixed = 0; n_bytes_prefixed <= 1; n_bytes_prefixed++) {
660 g_autoptr(QmiMessage) self = NULL;
661 g_autoptr(GError) error = NULL;
662 gboolean ret;
663 gsize init_offset;
664 guint16 tlv_length = 0;
665 gsize offset;
666 gsize expected_tlv_payload_size = 0;
667 guint16 uint16;
668 gint16 int16;
669 guint i;
670
671 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
672
673 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
674 g_assert_no_error (error);
675 g_assert (init_offset > 0);
676
677 for (i = 0; i < n_bytes_prefixed; i++) {
678 expected_tlv_payload_size += 1;
679 ret = qmi_message_tlv_write_guint8 (self, 0xFF, &error);
680 g_assert_no_error (error);
681 g_assert (ret);
682 }
683
684 /* Add one of each */
685 expected_tlv_payload_size += 2;
686 ret = qmi_message_tlv_write_guint16 (self, QMI_ENDIAN_LITTLE, 0x1212, &error);
687 g_assert_no_error (error);
688 g_assert (ret);
689
690 expected_tlv_payload_size += 2;
691 ret = qmi_message_tlv_write_gint16 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212, &error);
692 g_assert_no_error (error);
693 g_assert (ret);
694
695 expected_tlv_payload_size += 2;
696 ret = qmi_message_tlv_write_guint16 (self, QMI_ENDIAN_BIG, 0x1212, &error);
697 g_assert_no_error (error);
698 g_assert (ret);
699
700 expected_tlv_payload_size += 2;
701 ret = qmi_message_tlv_write_gint16 (self, QMI_ENDIAN_BIG, 0 - 0x1212, &error);
702 g_assert_no_error (error);
703 g_assert (ret);
704
705 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
706 g_assert_no_error (error);
707 g_assert (ret);
708
709 /* Now read */
710 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
711 g_assert_no_error (error);
712 g_assert (init_offset > 0);
713 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
714
715 offset = 0;
716
717 for (i = 0; i < n_bytes_prefixed; i++) {
718 guint8 aux;
719
720 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &aux, &error);
721 g_assert_no_error (error);
722 g_assert (ret);
723 g_assert_cmpuint (aux, ==, 0xFF);
724 }
725
726 ret = qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint16, &error);
727 g_assert_no_error (error);
728 g_assert (ret);
729 g_assert_cmpuint (uint16, ==, 0x1212);
730
731 ret = qmi_message_tlv_read_gint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int16, &error);
732 g_assert_no_error (error);
733 g_assert (ret);
734 g_assert_cmpuint (int16, ==, 0 - 0x1212);
735
736 ret = qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_BIG, &uint16, &error);
737 g_assert_no_error (error);
738 g_assert (ret);
739 g_assert_cmpuint (uint16, ==, 0x1212);
740
741 ret = qmi_message_tlv_read_gint16 (self, init_offset, &offset, QMI_ENDIAN_BIG, &int16, &error);
742 g_assert_no_error (error);
743 g_assert (ret);
744 g_assert_cmpuint (int16, ==, 0 - 0x1212);
745 }
746 }
747
748 static void
749 test_message_tlv_rw_32 (void)
750 {
751 guint n_bytes_prefixed;
752
753 /* We'll add [0, 1, 2, 3] bytes before the actual 32-bit values, so that we
754 * check all possible memory alignments. */
755 for (n_bytes_prefixed = 0; n_bytes_prefixed <= 3; n_bytes_prefixed++) {
756 g_autoptr(QmiMessage) self = NULL;
757 g_autoptr(GError) error = NULL;
758 gboolean ret;
759 gsize init_offset;
760 guint16 tlv_length = 0;
761 gsize offset;
762 gsize expected_tlv_payload_size = 0;
763 guint32 uint32;
764 gint32 int32;
765 guint i;
766
767 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
768
769 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
770 g_assert_no_error (error);
771 g_assert (init_offset > 0);
772
773 for (i = 0; i < n_bytes_prefixed; i++) {
774 expected_tlv_payload_size += 1;
775 ret = qmi_message_tlv_write_guint8 (self, 0xFF, &error);
776 g_assert_no_error (error);
777 g_assert (ret);
778 }
779
780 /* Add one of each */
781 expected_tlv_payload_size += 4;
782 ret = qmi_message_tlv_write_guint32 (self, QMI_ENDIAN_LITTLE, 0x12121212, &error);
783 g_assert_no_error (error);
784 g_assert (ret);
785
786 expected_tlv_payload_size += 4;
787 ret = qmi_message_tlv_write_gint32 (self, QMI_ENDIAN_LITTLE, 0 - 0x12121212, &error);
788 g_assert_no_error (error);
789 g_assert (ret);
790
791 expected_tlv_payload_size += 4;
792 ret = qmi_message_tlv_write_guint32 (self, QMI_ENDIAN_BIG, 0x12121212, &error);
793 g_assert_no_error (error);
794 g_assert (ret);
795
796 expected_tlv_payload_size += 4;
797 ret = qmi_message_tlv_write_gint32 (self, QMI_ENDIAN_BIG, 0 - 0x12121212, &error);
798 g_assert_no_error (error);
799 g_assert (ret);
800
801 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
802 g_assert_no_error (error);
803 g_assert (ret);
804
805 /* Now read */
806 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
807 g_assert_no_error (error);
808 g_assert (init_offset > 0);
809 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
810
811 offset = 0;
812
813 for (i = 0; i < n_bytes_prefixed; i++) {
814 guint8 aux;
815
816 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &aux, &error);
817 g_assert_no_error (error);
818 g_assert (ret);
819 g_assert_cmpuint (aux, ==, 0xFF);
820 }
821
822 ret = qmi_message_tlv_read_guint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint32, &error);
823 g_assert_no_error (error);
824 g_assert (ret);
825 g_assert_cmpuint (uint32, ==, 0x12121212);
826
827 ret = qmi_message_tlv_read_gint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int32, &error);
828 g_assert_no_error (error);
829 g_assert (ret);
830 g_assert_cmpuint (int32, ==, 0 - 0x12121212);
831
832 ret = qmi_message_tlv_read_guint32 (self, init_offset, &offset, QMI_ENDIAN_BIG, &uint32, &error);
833 g_assert_no_error (error);
834 g_assert (ret);
835 g_assert_cmpuint (uint32, ==, 0x12121212);
836
837 ret = qmi_message_tlv_read_gint32 (self, init_offset, &offset, QMI_ENDIAN_BIG, &int32, &error);
838 g_assert_no_error (error);
839 g_assert (ret);
840 g_assert_cmpuint (int32, ==, 0 - 0x12121212);
841 }
842 }
843
844 static void
845 test_message_tlv_rw_64 (void)
846 {
847 guint n_bytes_prefixed;
848
849 /* We'll add [0, 1, 2, 3, 4, 5, 6, 7] bytes before the actual 64-bit values,
850 * so that we check all possible memory alignments. */
851 for (n_bytes_prefixed = 0; n_bytes_prefixed <= 7; n_bytes_prefixed++) {
852 g_autoptr(QmiMessage) self = NULL;
853 g_autoptr(GError) error = NULL;
854 gboolean ret;
855 gsize init_offset;
856 guint16 tlv_length = 0;
857 gsize offset;
858 gsize expected_tlv_payload_size = 0;
859 guint64 uint64;
860 gint64 int64;
861 guint i;
862
863 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
864
865 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
866 g_assert_no_error (error);
867 g_assert (init_offset > 0);
868
869 for (i = 0; i < n_bytes_prefixed; i++) {
870 expected_tlv_payload_size += 1;
871 ret = qmi_message_tlv_write_guint8 (self, 0xFF, &error);
872 g_assert_no_error (error);
873 g_assert (ret);
874 }
875
876 /* Add one of each */
877 expected_tlv_payload_size += 8;
878 ret = qmi_message_tlv_write_guint64 (self, QMI_ENDIAN_LITTLE, 0x1212121212121212ULL, &error);
879 g_assert_no_error (error);
880 g_assert (ret);
881
882 expected_tlv_payload_size += 8;
883 ret = qmi_message_tlv_write_gint64 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212121212121212LL, &error);
884 g_assert_no_error (error);
885 g_assert (ret);
886
887 expected_tlv_payload_size += 8;
888 ret = qmi_message_tlv_write_guint64 (self, QMI_ENDIAN_BIG, 0x1212121212121212ULL, &error);
889 g_assert_no_error (error);
890 g_assert (ret);
891
892 expected_tlv_payload_size += 8;
893 ret = qmi_message_tlv_write_gint64 (self, QMI_ENDIAN_BIG, 0 - 0x1212121212121212LL, &error);
894 g_assert_no_error (error);
895 g_assert (ret);
896
897 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
898 g_assert_no_error (error);
899 g_assert (ret);
900
901 /* Now read */
902 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
903 g_assert_no_error (error);
904 g_assert (init_offset > 0);
905 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
906
907 offset = 0;
908
909 for (i = 0; i < n_bytes_prefixed; i++) {
910 guint8 aux;
911
912 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &aux, &error);
913 g_assert_no_error (error);
914 g_assert (ret);
915 g_assert_cmpuint (aux, ==, 0xFF);
916 }
917
918 ret = qmi_message_tlv_read_guint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint64, &error);
919 g_assert_no_error (error);
920 g_assert (ret);
921 g_assert_cmpuint (uint64, ==, 0x1212121212121212ULL);
922
923 ret = qmi_message_tlv_read_gint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int64, &error);
924 g_assert_no_error (error);
925 g_assert (ret);
926 g_assert_cmpuint (int64, ==, 0 - 0x1212121212121212LL);
927
928 ret = qmi_message_tlv_read_guint64 (self, init_offset, &offset, QMI_ENDIAN_BIG, &uint64, &error);
929 g_assert_no_error (error);
930 g_assert (ret);
931 g_assert_cmpuint (uint64, ==, 0x1212121212121212ULL);
932
933 ret = qmi_message_tlv_read_gint64 (self, init_offset, &offset, QMI_ENDIAN_BIG, &int64, &error);
934 g_assert_no_error (error);
935 g_assert (ret);
936 g_assert_cmpuint (int64, ==, 0 - 0x1212121212121212LL);
937 }
938 }
939
940 static void
941 test_message_tlv_rw_sized (void)
942 {
943 guint sized[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
944 guint sized_i;
945
946 for (sized_i = 0; sized_i < G_N_ELEMENTS (sized); sized_i++) {
947 guint n_bytes_prefixed;
948
949 /* We'll add [0, 1, 2, 3, 4, 5, 6, 7] bytes before the actual N-bit values,
950 * so that we check all possible memory alignments. */
951 for (n_bytes_prefixed = 0; n_bytes_prefixed <= sized[sized_i] - 1; n_bytes_prefixed++) {
952 g_autoptr(QmiMessage) self = NULL;
953 g_autoptr(GError) error = NULL;
954 gboolean ret;
955 gsize init_offset;
956 guint16 tlv_length = 0;
957 gsize offset;
958 gsize expected_tlv_payload_size = 0;
959 guint64 uint64;
960 guint i;
961 guint64 value;
962 guint64 tmp;
963
964 value = 0x1212121212121212ULL;
965 tmp = 0xFF;
966 for (i = 1; i < sized[sized_i]; i++) {
967 tmp <<= 8;
968 tmp |= 0xFF;
969 }
970 value &= tmp;
971
972 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
973
974 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
975 g_assert_no_error (error);
976 g_assert (init_offset > 0);
977
978 for (i = 0; i < n_bytes_prefixed; i++) {
979 expected_tlv_payload_size += 1;
980 ret = qmi_message_tlv_write_guint8 (self, 0xFF, &error);
981 g_assert_no_error (error);
982 g_assert (ret);
983 }
984
985 /* Add one of each */
986 expected_tlv_payload_size += sized[sized_i];
987 ret = qmi_message_tlv_write_sized_guint (self, sized[sized_i], QMI_ENDIAN_LITTLE, value, &error);
988 g_assert_no_error (error);
989 g_assert (ret);
990
991 expected_tlv_payload_size += sized[sized_i];
992 ret = qmi_message_tlv_write_sized_guint (self, sized[sized_i], QMI_ENDIAN_BIG, value, &error);
993 g_assert_no_error (error);
994 g_assert (ret);
995
996 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
997 g_assert_no_error (error);
998 g_assert (ret);
999
1000 /* Now read */
1001 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1002 g_assert_no_error (error);
1003 g_assert (init_offset > 0);
1004 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
1005
1006 offset = 0;
1007
1008 for (i = 0; i < n_bytes_prefixed; i++) {
1009 guint8 aux;
1010
1011 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &aux, &error);
1012 g_assert_no_error (error);
1013 g_assert (ret);
1014 g_assert_cmpuint (aux, ==, 0xFF);
1015 }
1016
1017 qmi_message_tlv_read_sized_guint (self, init_offset, &offset, sized[sized_i], QMI_ENDIAN_LITTLE, &uint64, &error);
1018 g_assert_cmpuint (uint64, ==, value);
1019
1020 qmi_message_tlv_read_sized_guint (self, init_offset, &offset, sized[sized_i], QMI_ENDIAN_BIG, &uint64, &error);
1021 g_assert_cmpuint (uint64, ==, value);
1022 }
1023 }
1024 }
1025
1026 static void
1027 test_message_tlv_rw_strings (void)
1028 {
1029 g_autoptr(QmiMessage) self = NULL;
1030 g_autoptr(GError) error = NULL;
1031 gboolean ret;
1032 gsize init_offset;
1033 guint16 tlv_length = 0;
1034 gsize offset;
1035 gsize expected_tlv_payload_size = 0;
1036 gchar *str;
1037 gchar fixed_str[5];
1038
1039 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1040
1041 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1042 g_assert_no_error (error);
1043 g_assert (init_offset > 0);
1044
1045 /* FIXED SIZE */
1046 expected_tlv_payload_size += 4;
1047 ret = qmi_message_tlv_write_string (self, 0, "abcd", -1, &error);
1048 g_assert_no_error (error);
1049 g_assert (ret);
1050
1051 /* 1-BYTE PREFIX */
1052 expected_tlv_payload_size += 5;
1053 ret = qmi_message_tlv_write_string (self, 1, "abcd", -1, &error);
1054 g_assert_no_error (error);
1055 g_assert (ret);
1056
1057 /* 2-BYTE PREFIX */
1058 expected_tlv_payload_size += 6;
1059 ret = qmi_message_tlv_write_string (self, 2, "abcd", -1, &error);
1060 g_assert_no_error (error);
1061 g_assert (ret);
1062
1063 /* FIXED SIZE */
1064 expected_tlv_payload_size += 0;
1065 ret = qmi_message_tlv_write_string (self, 0, "", -1, &error);
1066 g_assert_no_error (error);
1067 g_assert (ret);
1068
1069 /* 1-BYTE PREFIX */
1070 expected_tlv_payload_size += 1;
1071 ret = qmi_message_tlv_write_string (self, 1, "", -1, &error);
1072 g_assert_no_error (error);
1073 g_assert (ret);
1074
1075 /* 2-BYTE PREFIX */
1076 expected_tlv_payload_size += 2;
1077 ret = qmi_message_tlv_write_string (self, 2, "", -1, &error);
1078 g_assert_no_error (error);
1079 g_assert (ret);
1080
1081 /* 0-BYTE PREFIX (same as fixed size, but LAST in TLV) */
1082 expected_tlv_payload_size += 4;
1083 ret = qmi_message_tlv_write_string (self, 0, "abcd", -1, &error);
1084 g_assert_no_error (error);
1085 g_assert (ret);
1086
1087 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1088 g_assert_no_error (error);
1089 g_assert (ret);
1090
1091 /* Now read */
1092 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1093 g_assert_no_error (error);
1094 g_assert (init_offset > 0);
1095 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
1096
1097 offset = 0;
1098
1099 ret = qmi_message_tlv_read_fixed_size_string (self, init_offset, &offset, 4, fixed_str, &error);
1100 g_assert_no_error (error);
1101 g_assert (ret);
1102 fixed_str[4] = '\0';
1103 g_assert_cmpstr (fixed_str, ==, "abcd");
1104
1105 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1106 g_assert_no_error (error);
1107 g_assert (ret);
1108 g_assert_cmpstr (str, ==, "abcd");
1109 g_free (str);
1110
1111 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 2, 0, &str, &error);
1112 g_assert_no_error (error);
1113 g_assert (ret);
1114 g_assert_cmpstr (str, ==, "abcd");
1115 g_free (str);
1116
1117 fixed_str[0] = 'z';
1118 ret = qmi_message_tlv_read_fixed_size_string (self, init_offset, &offset, 0, fixed_str, &error);
1119 g_assert_no_error (error);
1120 g_assert (ret);
1121 g_assert_cmpuint (fixed_str[0], ==, 'z');
1122
1123 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1124 g_assert_no_error (error);
1125 g_assert (ret);
1126 g_assert_cmpstr (str, ==, "");
1127 g_free (str);
1128
1129 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 2, 0, &str, &error);
1130 g_assert_no_error (error);
1131 g_assert (ret);
1132 g_assert_cmpstr (str, ==, "");
1133 g_free (str);
1134
1135 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 0, 0, &str, &error);
1136 g_assert_no_error (error);
1137 g_assert (ret);
1138 g_assert_cmpstr (str, ==, "abcd");
1139 g_free (str);
1140
1141 /* There's no other string added, but we can try to read a new one without
1142 * size prefix, and we should get a 0-length string */
1143 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 0, 0, &str, &error);
1144 g_assert_no_error (error);
1145 g_assert (ret);
1146 g_assert_cmpstr (str, ==, "");
1147 g_free (str);
1148 }
1149
1150 static void
1151 test_message_tlv_rw_mixed (void)
1152 {
1153 g_autoptr(QmiMessage) self = NULL;
1154 g_autoptr(GError) error = NULL;
1155 gboolean ret;
1156 gsize init_offset;
1157 guint16 tlv_length = 0;
1158 gsize offset;
1159 gsize expected_tlv_payload_size = 0;
1160 guint8 uint8;
1161 gint8 int8;
1162 guint16 uint16;
1163 gint16 int16;
1164 guint32 uint32;
1165 gint32 int32;
1166 guint64 uint64;
1167 gint64 int64;
1168
1169 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1170
1171 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1172 g_assert_no_error (error);
1173 g_assert (init_offset > 0);
1174
1175 /* Add one of each */
1176 expected_tlv_payload_size += 1;
1177 ret = qmi_message_tlv_write_guint8 (self, 0x12, &error);
1178 g_assert_no_error (error);
1179 g_assert (ret);
1180
1181 expected_tlv_payload_size += 1;
1182 ret = qmi_message_tlv_write_gint8 (self, 0 - 0x12, &error);
1183 g_assert_no_error (error);
1184 g_assert (ret);
1185
1186 expected_tlv_payload_size += 2;
1187 ret = qmi_message_tlv_write_guint16 (self, QMI_ENDIAN_LITTLE, 0x1212, &error);
1188 g_assert_no_error (error);
1189 g_assert (ret);
1190
1191 expected_tlv_payload_size += 2;
1192 ret = qmi_message_tlv_write_gint16 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212, &error);
1193 g_assert_no_error (error);
1194 g_assert (ret);
1195
1196 expected_tlv_payload_size += 4;
1197 ret = qmi_message_tlv_write_guint32 (self, QMI_ENDIAN_LITTLE, 0x12121212, &error);
1198 g_assert_no_error (error);
1199 g_assert (ret);
1200
1201 expected_tlv_payload_size += 4;
1202 ret = qmi_message_tlv_write_gint32 (self, QMI_ENDIAN_LITTLE, 0 - 0x12121212, &error);
1203 g_assert_no_error (error);
1204 g_assert (ret);
1205
1206 expected_tlv_payload_size += 8;
1207 ret = qmi_message_tlv_write_guint64 (self, QMI_ENDIAN_LITTLE, 0x1212121212121212ULL, &error);
1208 g_assert_no_error (error);
1209 g_assert (ret);
1210
1211 expected_tlv_payload_size += 8;
1212 ret = qmi_message_tlv_write_gint64 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212121212121212LL, &error);
1213 g_assert_no_error (error);
1214 g_assert (ret);
1215
1216 expected_tlv_payload_size += 4;
1217 ret = qmi_message_tlv_write_string (self, 0, "abcd", -1, &error);
1218 g_assert_no_error (error);
1219 g_assert (ret);
1220
1221 expected_tlv_payload_size += 5;
1222 ret = qmi_message_tlv_write_string (self, 1, "abcd", -1, &error);
1223 g_assert_no_error (error);
1224 g_assert (ret);
1225
1226 expected_tlv_payload_size += 6;
1227 ret = qmi_message_tlv_write_string (self, 2, "abcd", -1, &error);
1228 g_assert_no_error (error);
1229 g_assert (ret);
1230
1231 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1232 g_assert_no_error (error);
1233 g_assert (ret);
1234
1235 /* Now read */
1236 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1237 g_assert_no_error (error);
1238 g_assert (init_offset > 0);
1239 g_assert_cmpuint (tlv_length, ==, expected_tlv_payload_size);
1240
1241 offset = 0;
1242
1243 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &uint8, &error);
1244 g_assert_no_error (error);
1245 g_assert (ret);
1246 g_assert_cmpuint (uint8, ==, 0x12);
1247
1248 ret = qmi_message_tlv_read_gint8 (self, init_offset, &offset, &int8, &error);
1249 g_assert_no_error (error);
1250 g_assert (ret);
1251 g_assert_cmpuint (int8, ==, 0 - 0x12);
1252
1253 ret = qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint16, &error);
1254 g_assert_no_error (error);
1255 g_assert (ret);
1256 g_assert_cmpuint (uint16, ==, 0x1212);
1257
1258 ret = qmi_message_tlv_read_gint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int16, &error);
1259 g_assert_no_error (error);
1260 g_assert (ret);
1261 g_assert_cmpuint (int16, ==, 0 - 0x1212);
1262
1263 ret = qmi_message_tlv_read_guint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint32, &error);
1264 g_assert_no_error (error);
1265 g_assert (ret);
1266 g_assert_cmpuint (uint32, ==, 0x12121212);
1267
1268 ret = qmi_message_tlv_read_gint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int32, &error);
1269 g_assert_no_error (error);
1270 g_assert (ret);
1271 g_assert_cmpuint (int32, ==, 0 - 0x12121212);
1272
1273 ret = qmi_message_tlv_read_guint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint64, &error);
1274 g_assert_no_error (error);
1275 g_assert (ret);
1276 g_assert_cmpuint (uint64, ==, 0x1212121212121212ULL);
1277
1278 ret = qmi_message_tlv_read_gint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int64, &error);
1279 g_assert_no_error (error);
1280 g_assert (ret);
1281 g_assert_cmpuint (int64, ==, 0 - 0x1212121212121212LL);
1282 }
1283
1284 static void
1285 test_message_tlv_write_overflow (void)
1286 {
1287 g_autoptr(QmiMessage) self = NULL;
1288 g_autoptr(GError) error = NULL;
1289 gboolean ret;
1290 gsize init_offset;
1291
1292 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1293
1294 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1295 g_assert_no_error (error);
1296 g_assert (init_offset > 0);
1297
1298 /* Add guint8 values until we get to the maximum message size */
1299 while (qmi_message_get_length (self) < G_MAXUINT16) {
1300 ret = qmi_message_tlv_write_guint8 (self, 0x12, &error);
1301 g_assert_no_error (error);
1302 g_assert (ret);
1303 }
1304
1305 /* Message is max size now, don't allow more variables */
1306
1307 ret = qmi_message_tlv_write_guint8 (self, 0x12, &error);
1308 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1309 g_assert (!ret);
1310 g_clear_error (&error);
1311
1312 ret = qmi_message_tlv_write_gint8 (self, 0 - 0x12, &error);
1313 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1314 g_assert (!ret);
1315 g_clear_error (&error);
1316
1317 ret = qmi_message_tlv_write_guint16 (self, QMI_ENDIAN_LITTLE, 0x1212, &error);
1318 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1319 g_assert (!ret);
1320 g_clear_error (&error);
1321
1322 ret = qmi_message_tlv_write_gint16 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212, &error);
1323 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1324 g_assert (!ret);
1325 g_clear_error (&error);
1326
1327 ret = qmi_message_tlv_write_guint32 (self, QMI_ENDIAN_LITTLE, 0x12121212, &error);
1328 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1329 g_assert (!ret);
1330 g_clear_error (&error);
1331
1332 ret = qmi_message_tlv_write_gint32 (self, QMI_ENDIAN_LITTLE, 0 - 0x12121212, &error);
1333 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1334 g_assert (!ret);
1335 g_clear_error (&error);
1336
1337 ret = qmi_message_tlv_write_guint64 (self, QMI_ENDIAN_LITTLE, 0x1212121212121212ULL, &error);
1338 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1339 g_assert (!ret);
1340 g_clear_error (&error);
1341
1342 ret = qmi_message_tlv_write_gint64 (self, QMI_ENDIAN_LITTLE, 0 - 0x1212121212121212LL, &error);
1343 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1344 g_assert (!ret);
1345 g_clear_error (&error);
1346
1347 ret = qmi_message_tlv_write_sized_guint (self, 1, QMI_ENDIAN_LITTLE, 0x12, &error);
1348 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1349 g_assert (!ret);
1350 g_clear_error (&error);
1351
1352 ret = qmi_message_tlv_write_string (self, 0, "a", -1, &error);
1353 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1354 g_assert (!ret);
1355 g_clear_error (&error);
1356
1357 ret = qmi_message_tlv_write_string (self, 1, "a", -1, &error);
1358 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1359 g_assert (!ret);
1360 g_clear_error (&error);
1361
1362 ret = qmi_message_tlv_write_string (self, 2, "a", -1, &error);
1363 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1364 g_assert (!ret);
1365 g_clear_error (&error);
1366
1367 /* Writing an empty string with a 0-size prefix is actually valid :) */
1368 ret = qmi_message_tlv_write_string (self, 0, "", -1, &error);
1369 g_assert_no_error (error);
1370 g_assert (ret);
1371
1372 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1373 g_assert_no_error (error);
1374 g_assert (ret);
1375
1376 /* Message is max size now, don't allow more TLVs */
1377 init_offset = qmi_message_tlv_write_init (self, 0x02, &error);
1378 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1379 g_assert (init_offset == 0);
1380 g_clear_error (&error);
1381 }
1382
1383 static void
1384 test_message_tlv_read_overflow_message (void)
1385 {
1386 g_autoptr(QmiMessage) self = NULL;
1387 g_autoptr(GError) error = NULL;
1388 gboolean ret;
1389 gsize init_offset;
1390 guint16 tlv_length = 0;
1391 gsize offset;
1392 gchar *str;
1393 guint8 uint8;
1394 gint8 int8;
1395 guint16 uint16;
1396 gint16 int16;
1397 guint32 uint32;
1398 gint32 int32;
1399 guint64 uint64;
1400 gint64 int64;
1401
1402 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1403 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1404 g_assert_no_error (error);
1405 g_assert (init_offset > 0);
1406 /* Create a 1-byte size prefixed string manually, with a wrong length (out of message) */
1407 ret = qmi_message_tlv_write_guint8 (self, 5, &error);
1408 g_assert_no_error (error);
1409 g_assert (ret);
1410 ret = qmi_message_tlv_write_string (self, 0, "abcd", -1, &error);
1411 g_assert_no_error (error);
1412 g_assert (ret);
1413 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1414 g_assert_no_error (error);
1415 g_assert (ret);
1416 /* Now read */
1417 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1418 g_assert_no_error (error);
1419 g_assert (init_offset > 0);
1420 offset = 0;
1421 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1422 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1423 g_assert (!ret);
1424 g_clear_error (&error);
1425 g_clear_pointer (&self, qmi_message_unref);
1426
1427 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1428 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1429 g_assert_no_error (error);
1430 g_assert (init_offset > 0);
1431 ret = qmi_message_tlv_write_string (self, 1, "abcd", -1, &error);
1432 g_assert_no_error (error);
1433 g_assert (ret);
1434 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1435 g_assert_no_error (error);
1436 g_assert (ret);
1437 /* Now read */
1438 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1439 g_assert_no_error (error);
1440 g_assert (init_offset > 0);
1441 offset = 0;
1442 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1443 g_assert_no_error (error);
1444 g_assert (ret);
1445 g_assert_cmpstr (str, ==, "abcd");
1446 g_free (str);
1447 /* Reading more will fail */
1448 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1449 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1450 g_assert (!ret);
1451 g_clear_error (&error);
1452 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 2, 0, &str, &error);
1453 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1454 g_assert (!ret);
1455 g_clear_error (&error);
1456 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &uint8, &error);
1457 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1458 g_assert (!ret);
1459 g_clear_error (&error);
1460 ret = qmi_message_tlv_read_gint8 (self, init_offset, &offset, &int8, &error);
1461 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1462 g_assert (!ret);
1463 g_clear_error (&error);
1464 ret = qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint16, &error);
1465 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1466 g_assert (!ret);
1467 g_clear_error (&error);
1468 ret = qmi_message_tlv_read_gint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int16, &error);
1469 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1470 g_assert (!ret);
1471 g_clear_error (&error);
1472 ret = qmi_message_tlv_read_guint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint32, &error);
1473 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1474 g_assert (!ret);
1475 g_clear_error (&error);
1476 ret = qmi_message_tlv_read_gint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int32, &error);
1477 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1478 g_assert (!ret);
1479 g_clear_error (&error);
1480 ret = qmi_message_tlv_read_guint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint64, &error);
1481 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1482 g_assert (!ret);
1483 g_clear_error (&error);
1484 ret = qmi_message_tlv_read_gint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int64, &error);
1485 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1486 g_assert (!ret);
1487 g_clear_error (&error);
1488 g_clear_pointer (&self, qmi_message_unref);
1489 }
1490
1491 static void
1492 test_message_tlv_read_overflow_tlv (void)
1493 {
1494 g_autoptr(QmiMessage) self = NULL;
1495 g_autoptr(GError) error = NULL;
1496 gboolean ret;
1497 gsize init_offset;
1498 guint16 tlv_length = 0;
1499 gsize offset;
1500 gchar *str;
1501 guint8 uint8;
1502 gint8 int8;
1503 guint16 uint16;
1504 gint16 int16;
1505 guint32 uint32;
1506 gint32 int32;
1507 guint64 uint64;
1508 gint64 int64;
1509
1510 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1511 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1512 g_assert_no_error (error);
1513 g_assert (init_offset > 0);
1514 /* Create a 1-byte size prefixed string manually, with a wrong length (out of tlv) */
1515 ret = qmi_message_tlv_write_guint8 (self, 5, &error);
1516 g_assert_no_error (error);
1517 g_assert (ret);
1518 ret = qmi_message_tlv_write_string (self, 0, "abcd", -1, &error);
1519 g_assert_no_error (error);
1520 g_assert (ret);
1521 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1522 g_assert_no_error (error);
1523 g_assert (ret);
1524 init_offset = qmi_message_tlv_write_init (self, 0x02, &error);
1525 g_assert_no_error (error);
1526 g_assert (init_offset > 0);
1527 ret = qmi_message_tlv_write_string (self, 1, "efgh", -1, &error);
1528 g_assert_no_error (error);
1529 g_assert (ret);
1530 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1531 g_assert_no_error (error);
1532 g_assert (ret);
1533 /* Now read */
1534 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1535 g_assert_no_error (error);
1536 g_assert (init_offset > 0);
1537 offset = 0;
1538 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1539 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1540 g_assert (!ret);
1541 g_clear_error (&error);
1542 g_clear_pointer (&self, qmi_message_unref);
1543
1544 self = qmi_message_new (QMI_SERVICE_DMS, 0x01, 0x02, 0xFFFF);
1545 init_offset = qmi_message_tlv_write_init (self, 0x01, &error);
1546 g_assert_no_error (error);
1547 g_assert (init_offset > 0);
1548 ret = qmi_message_tlv_write_string (self, 1, "abcd", -1, &error);
1549 g_assert_no_error (error);
1550 g_assert (ret);
1551 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1552 g_assert_no_error (error);
1553 g_assert (ret);
1554 init_offset = qmi_message_tlv_write_init (self, 0x02, &error);
1555 g_assert_no_error (error);
1556 g_assert (init_offset > 0);
1557 ret = qmi_message_tlv_write_string (self, 1, "efgh", -1, &error);
1558 g_assert_no_error (error);
1559 g_assert (ret);
1560 ret = qmi_message_tlv_write_complete (self, init_offset, &error);
1561 g_assert_no_error (error);
1562 g_assert (ret);
1563 /* Now read */
1564 init_offset = qmi_message_tlv_read_init (self, 0x01, &tlv_length, &error);
1565 g_assert_no_error (error);
1566 g_assert (init_offset > 0);
1567 offset = 0;
1568 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1569 g_assert_no_error (error);
1570 g_assert (ret);
1571 g_assert_cmpstr (str, ==, "abcd");
1572 g_free (str);
1573 /* Reading more will fail */
1574 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 1, 0, &str, &error);
1575 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1576 g_assert (!ret);
1577 g_clear_error (&error);
1578 ret = qmi_message_tlv_read_string (self, init_offset, &offset, 2, 0, &str, &error);
1579 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1580 g_assert (!ret);
1581 g_clear_error (&error);
1582 ret = qmi_message_tlv_read_guint8 (self, init_offset, &offset, &uint8, &error);
1583 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1584 g_assert (!ret);
1585 g_clear_error (&error);
1586 ret = qmi_message_tlv_read_gint8 (self, init_offset, &offset, &int8, &error);
1587 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1588 g_assert (!ret);
1589 g_clear_error (&error);
1590 ret = qmi_message_tlv_read_guint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint16, &error);
1591 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1592 g_assert (!ret);
1593 g_clear_error (&error);
1594 ret = qmi_message_tlv_read_gint16 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int16, &error);
1595 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1596 g_assert (!ret);
1597 g_clear_error (&error);
1598 ret = qmi_message_tlv_read_guint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint32, &error);
1599 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1600 g_assert (!ret);
1601 g_clear_error (&error);
1602 ret = qmi_message_tlv_read_gint32 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int32, &error);
1603 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1604 g_assert (!ret);
1605 g_clear_error (&error);
1606 ret = qmi_message_tlv_read_guint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &uint64, &error);
1607 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1608 g_assert (!ret);
1609 g_clear_error (&error);
1610 ret = qmi_message_tlv_read_gint64 (self, init_offset, &offset, QMI_ENDIAN_LITTLE, &int64, &error);
1611 g_assert_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TLV_TOO_LONG);
1612 g_assert (!ret);
1613 g_clear_error (&error);
1614 g_clear_pointer (&self, qmi_message_unref);
1615 }
1616
1617 /*****************************************************************************/
1618
1619 static void
1620 test_message_set_transaction_id_ctl (void)
1621 {
1622 g_autoptr(GByteArray) buffer = NULL;
1623 g_autoptr(QmiMessage) message = NULL;
1624 g_autoptr(GError) error = NULL;
1625
1626 guint8 ctl_message[] = {
1627 0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
1628 0xFF, /* TRID to update */
1629 0x22, 0x00, 0x04, 0x00, 0x01, 0x01, 0x00, 0x01
1630 };
1631
1632 /* CTL message */
1633 buffer = g_byte_array_append (g_byte_array_sized_new (G_N_ELEMENTS (ctl_message)), ctl_message, G_N_ELEMENTS (ctl_message));
1634 message = qmi_message_new_from_raw (buffer, &error);
1635 g_assert_no_error (error);
1636 g_assert (message);
1637
1638 qmi_message_set_transaction_id (message, 0x55);
1639 g_assert_cmpuint (qmi_message_get_transaction_id (message), ==, 0x55);
1640 }
1641
1642 static void
1643 test_message_set_transaction_id_services (void)
1644 {
1645 g_autoptr(GByteArray) buffer = NULL;
1646 g_autoptr(QmiMessage) message = NULL;
1647 g_autoptr(GError) error = NULL;
1648
1649 guint8 dms_message[] = {
1650 0x01, 0x0C, 0x00, 0x00, 0x02, 0x01, 0x00,
1651 0xFF, 0xFF, /* TRID to update */
1652 0x25, 0x00, 0x00, 0x00
1653 };
1654
1655 /* DMS message */
1656 buffer = g_byte_array_append (g_byte_array_sized_new (G_N_ELEMENTS (dms_message)), dms_message, G_N_ELEMENTS (dms_message));
1657 message = qmi_message_new_from_raw (buffer, &error);
1658 g_assert_no_error (error);
1659 g_assert (message);
1660
1661 qmi_message_set_transaction_id (message, 0x5566);
1662 g_assert_cmpuint (qmi_message_get_transaction_id (message), ==, 0x5566);
1663 }
1664
1665 /*****************************************************************************/
1666
1667 static void
1668 test_message_16bit_service_indication (void)
1669 {
1670 g_autoptr(GByteArray) buffer = NULL;
1671 g_autoptr(QmiMessage) message = NULL;
1672 g_autoptr(GError) error = NULL;
1673
1674 const guint8 ssc_message[] = {
1675 0x02, /* marker is the QRTR one because we have a 16bit service! */
1676 0x21, 0x00, /* message length: 33 bytes*/
1677 0x90, 0x01, /* service: SSC */
1678 0x03, /* client */
1679 0x04, /* service flags: Indication */
1680 0x01, 0x00, /* transaction */
1681 0x21, 0x00, /* message: Report Small */
1682 0x15, 0x00, /* all tlvs length: 21 bytes */
1683 /* TLV */
1684 0x01, /* type: client id */
1685 0x08, 0x00, /* length: 8 bytes */
1686 0x01, 0x00, 0x00, 0x00, /* 64bit uint value */
1687 0x00, 0x00, 0x00, 0x00,
1688 /* TLV */
1689 0x02, /* type: data */
1690 0x07, 0x00, /* length: 7 bytes */
1691 0x05, 0x00, /* array size */
1692 0x00, 0x01, 0x02, 0x03, 0x04, /* 5 bytes in data array */
1693 };
1694
1695 test_message_printable_common (ssc_message, sizeof (ssc_message), QMI_MESSAGE_VENDOR_GENERIC, "");
1696
1697 /* DMS message */
1698 buffer = g_byte_array_append (g_byte_array_sized_new (sizeof (ssc_message)), ssc_message, sizeof (ssc_message));
1699 message = qmi_message_new_from_raw (buffer, &error);
1700 g_assert_no_error (error);
1701 g_assert (message);
1702
1703 g_assert (qmi_message_is_indication (message));
1704 g_assert_cmpuint (qmi_message_get_service (message), ==, QMI_SERVICE_SSC);
1705 g_assert_cmpuint (qmi_message_get_message_id (message), ==, 0x0021);
1706
1707 #if defined HAVE_QMI_INDICATION_SSC_REPORT_SMALL
1708 {
1709 g_autoptr(QmiIndicationSscReportSmallOutput) output = NULL;
1710 gboolean tlv_exists;
1711 GArray *value_data;
1712 guint64 client_id;
1713 const guint8 expected_value_data[] = {
1714 0x00, 0x01, 0x02, 0x03, 0x04
1715 };
1716
1717 output = qmi_indication_ssc_report_small_indication_parse (message, &error);
1718 g_assert_no_error (error);
1719 g_assert (output);
1720
1721 tlv_exists = qmi_indication_ssc_report_small_output_get_client_id (output, &client_id, &error);
1722 g_assert_no_error (error);
1723 g_assert (tlv_exists);
1724 g_assert_cmpuint (client_id, ==, (guint64)0x01);
1725
1726 tlv_exists = qmi_indication_ssc_report_small_output_get_data (output, &value_data, &error);
1727 g_assert_no_error (error);
1728 g_assert (tlv_exists);
1729 _g_assert_cmpmem (value_data->data, value_data->len, expected_value_data, G_N_ELEMENTS (expected_value_data));
1730 }
1731 #endif
1732 }
1733
1734 /*****************************************************************************/
1735
1736 int main (int argc, char **argv)
1737 {
1738 g_test_init (&argc, &argv, NULL);
1739
1740 g_test_add_func ("/libqmi-glib/message/parse/wrong-qmux", test_message_parse_wrong_qmux);
1741 g_test_add_func ("/libqmi-glib/message/parse/tiny", test_message_parse_tiny);
1742 g_test_add_func ("/libqmi-glib/message/parse/short", test_message_parse_short);
1743 g_test_add_func ("/libqmi-glib/message/parse/complete", test_message_parse_complete);
1744 g_test_add_func ("/libqmi-glib/message/parse/complete-and-short", test_message_parse_complete_and_short);
1745 g_test_add_func ("/libqmi-glib/message/parse/complete-and-complete", test_message_parse_complete_and_complete);
1746 g_test_add_func ("/libqmi-glib/message/parse/wrong-tlv", test_message_parse_wrong_tlv);
1747 #if defined HAVE_QMI_INDICATION_PDS_EVENT_REPORT
1748 g_test_add_func ("/libqmi-glib/message/parse/missing-size", test_message_parse_missing_size);
1749 #endif
1750 #if defined HAVE_QMI_INDICATION_LOC_NMEA
1751 g_test_add_func ("/libqmi-glib/message/parse/string-with-crlf", test_message_parse_string_with_crlf);
1752 #endif
1753 #if defined HAVE_QMI_MESSAGE_DMS_SWI_GET_CURRENT_FIRMWARE
1754 g_test_add_func ("/libqmi-glib/message/parse/string-with-trailing-nul", test_message_parse_string_with_trailing_nul);
1755 #endif
1756 #if defined HAVE_QMI_MESSAGE_DMS_GET_MODEL
1757 g_test_add_func ("/libqmi-glib/message/parse/string-with-trailing-tab", test_message_parse_string_with_trailing_tab);
1758 #endif
1759 #if defined HAVE_QMI_MESSAGE_NAS_SWI_GET_STATUS
1760 g_test_add_func ("/libqmi-glib/message/parse/signed-int", test_message_parse_signed_int);
1761 #endif
1762 g_test_add_func ("/libqmi-glib/message/parse/empty-fixed-string", test_message_parse_empty_fixed_string);
1763
1764 g_test_add_func ("/libqmi-glib/message/new/request", test_message_new_request);
1765 g_test_add_func ("/libqmi-glib/message/new/request-from-data", test_message_new_request_from_data);
1766 g_test_add_func ("/libqmi-glib/message/new/response/ok", test_message_new_response_ok);
1767 g_test_add_func ("/libqmi-glib/message/new/response/error", test_message_new_response_error);
1768
1769 g_test_add_func ("/libqmi-glib/message/tlv-write/empty", test_message_tlv_write_empty);
1770 g_test_add_func ("/libqmi-glib/message/tlv-write/reset", test_message_tlv_write_reset);
1771 g_test_add_func ("/libqmi-glib/message/tlv-rw/8", test_message_tlv_rw_8);
1772 g_test_add_func ("/libqmi-glib/message/tlv-rw/16", test_message_tlv_rw_16);
1773 g_test_add_func ("/libqmi-glib/message/tlv-rw/32", test_message_tlv_rw_32);
1774 g_test_add_func ("/libqmi-glib/message/tlv-rw/64", test_message_tlv_rw_64);
1775 g_test_add_func ("/libqmi-glib/message/tlv-rw/sized", test_message_tlv_rw_sized);
1776 g_test_add_func ("/libqmi-glib/message/tlv-rw/strings", test_message_tlv_rw_strings);
1777 g_test_add_func ("/libqmi-glib/message/tlv-rw/mixed", test_message_tlv_rw_mixed);
1778 g_test_add_func ("/libqmi-glib/message/tlv-write/overflow", test_message_tlv_write_overflow);
1779 g_test_add_func ("/libqmi-glib/message/tlv-read/overflow-message", test_message_tlv_read_overflow_message);
1780 g_test_add_func ("/libqmi-glib/message/tlv-read/overflow-tlv", test_message_tlv_read_overflow_tlv);
1781
1782 g_test_add_func ("/libqmi-glib/message/set-transaction-id/ctl", test_message_set_transaction_id_ctl);
1783 g_test_add_func ("/libqmi-glib/message/set-transaction-id/services", test_message_set_transaction_id_services);
1784
1785 g_test_add_func ("/libqmi-glib/message/16bit-service/indication", test_message_16bit_service_indication);
1786
1787 return g_test_run ();
1788 }