| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 3 |
/* |
| 4 |
* Copyright (C) 2013 - 2014 Aleksander Morgado <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <config.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "mbim-message.h" |
| 11 |
#include "mbim-message-private.h" |
| 12 |
|
| 13 |
static void |
| 14 |
test_fragment_receive_single (void) |
| 15 |
{ |
| 16 |
GError *error = NULL; |
| 17 |
g_autoptr(MbimMessage) message = NULL; |
| 18 |
const guint8 *fragment_information_buffer; |
| 19 |
guint32 fragment_information_buffer_length; |
| 20 |
|
| 21 |
/* This buffer contains a single message composed of a single fragment. |
| 22 |
* We don't really care about the actual data included within the fragment. */ |
| 23 |
const guint8 buffer [] = { |
| 24 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 25 |
0x2C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 26 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 27 |
0x01, 0x00, 0x00, 0x00, /* total fragments */ |
| 28 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 29 |
0x01, 0x02, 0x03, 0x04, /* fragment data (at least 24 bytes for an indication) */ |
| 30 |
0x05, 0x06, 0x07, 0x08, |
| 31 |
0x09, 0x0A, 0x0B, 0x0C, |
| 32 |
0x0D, 0x0E, 0x0F, 0x00, |
| 33 |
0x11, 0x12, 0x13, 0x14, |
| 34 |
0x00, 0x00, 0x00, 0x00, /* buffer length 0! */ |
| 35 |
}; |
| 36 |
|
| 37 |
const guint8 data [] = { |
| 38 |
0x01, 0x02, 0x03, 0x04, /* same data as in the fragment */ |
| 39 |
0x05, 0x06, 0x07, 0x08, |
| 40 |
0x09, 0x0A, 0x0B, 0x0C, |
| 41 |
0x0D, 0x0E, 0x0F, 0x00, |
| 42 |
0x11, 0x12, 0x13, 0x14, |
| 43 |
0x00, 0x00, 0x00, 0x00, |
| 44 |
}; |
| 45 |
|
| 46 |
message = mbim_message_new (buffer, sizeof (buffer)); |
| 47 |
g_assert (_mbim_message_is_fragment (message)); |
| 48 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 1); |
| 49 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0); |
| 50 |
|
| 51 |
g_assert (mbim_message_validate (message, &error)); |
| 52 |
g_assert_no_error (error); |
| 53 |
|
| 54 |
fragment_information_buffer = (_mbim_message_fragment_get_payload ( |
| 55 |
message, |
| 56 |
&fragment_information_buffer_length)); |
| 57 |
|
| 58 |
g_assert_cmpuint (fragment_information_buffer_length, ==, sizeof (data)); |
| 59 |
g_assert (memcmp (fragment_information_buffer, data, fragment_information_buffer_length) == 0); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
test_fragment_receive_multiple (void) |
| 64 |
{ |
| 65 |
g_autoptr(GByteArray) bytearray = NULL; |
| 66 |
g_autoptr(MbimMessage) message = NULL; |
| 67 |
GError *error = NULL; |
| 68 |
const guint8 *fragment_information_buffer; |
| 69 |
guint32 fragment_information_buffer_length; |
| 70 |
|
| 71 |
/* This buffer contains several fragments of a single message. |
| 72 |
* We don't really care about the actual data included within the fragments. */ |
| 73 |
const guint8 buffer [] = { |
| 74 |
/* First fragment */ |
| 75 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 76 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 77 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 78 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 79 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 80 |
0x00, 0x01, 0x02, 0x03, /* frament data */ |
| 81 |
0x04, 0x05, 0x06, 0x07, |
| 82 |
/* Second fragment */ |
| 83 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 84 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 85 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 86 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 87 |
0x01, 0x00, 0x00, 0x00, /* current fragment */ |
| 88 |
0x08, 0x09, 0x0A, 0x0B, /* frament data */ |
| 89 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 90 |
/* Third fragment */ |
| 91 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 92 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 93 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 94 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 95 |
0x02, 0x00, 0x00, 0x00, /* current fragment */ |
| 96 |
0x10, 0x11, 0x12, 0x13, /* frament data */ |
| 97 |
0x04, 0x00, 0x00, 0x00, |
| 98 |
/* Fourth fragment */ |
| 99 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 100 |
0x18, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 101 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 102 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 103 |
0x03, 0x00, 0x00, 0x00, /* current fragment */ |
| 104 |
0x18, 0x19, 0x1A, 0x1B /* frament data */ |
| 105 |
}; |
| 106 |
|
| 107 |
const guint8 data [] = { |
| 108 |
0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */ |
| 109 |
0x04, 0x05, 0x06, 0x07, |
| 110 |
0x08, 0x09, 0x0A, 0x0B, |
| 111 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 112 |
0x10, 0x11, 0x12, 0x13, |
| 113 |
0x04, 0x00, 0x00, 0x00, |
| 114 |
0x18, 0x19, 0x1A, 0x1B |
| 115 |
}; |
| 116 |
|
| 117 |
bytearray = g_byte_array_new (); |
| 118 |
g_byte_array_append (bytearray, buffer, sizeof (buffer)); |
| 119 |
g_assert (_mbim_message_validate_internal ((const MbimMessage *)bytearray, TRUE, &error)); |
| 120 |
g_assert_no_error (error); |
| 121 |
|
| 122 |
/* First fragment creates the message */ |
| 123 |
message = _mbim_message_fragment_collector_init ((const MbimMessage *)bytearray, &error); |
| 124 |
g_assert_no_error (error); |
| 125 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 4); |
| 126 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0); |
| 127 |
g_assert (_mbim_message_fragment_collector_complete (message) == FALSE); |
| 128 |
|
| 129 |
g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray)); |
| 130 |
g_assert (_mbim_message_validate_internal ((const MbimMessage *)bytearray, TRUE, &error)); |
| 131 |
g_assert_no_error (error); |
| 132 |
|
| 133 |
/* Add second fragment */ |
| 134 |
g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error)); |
| 135 |
g_assert_no_error (error); |
| 136 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 4); |
| 137 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 1); |
| 138 |
g_assert (_mbim_message_fragment_collector_complete (message) == FALSE); |
| 139 |
|
| 140 |
g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray)); |
| 141 |
g_assert (_mbim_message_validate_internal ((const MbimMessage *)bytearray, TRUE, &error)); |
| 142 |
g_assert_no_error (error); |
| 143 |
|
| 144 |
/* Add third fragment */ |
| 145 |
g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error)); |
| 146 |
g_assert_no_error (error); |
| 147 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 4); |
| 148 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 2); |
| 149 |
g_assert (_mbim_message_fragment_collector_complete (message) == FALSE); |
| 150 |
|
| 151 |
g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray)); |
| 152 |
g_assert (_mbim_message_validate_internal ((const MbimMessage *)bytearray, TRUE, &error)); |
| 153 |
g_assert_no_error (error); |
| 154 |
|
| 155 |
/* Add fourth fragment */ |
| 156 |
g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error)); |
| 157 |
g_assert_no_error (error); |
| 158 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 4); |
| 159 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 3); |
| 160 |
g_assert (_mbim_message_fragment_collector_complete (message) == TRUE); |
| 161 |
g_assert_cmpuint (_mbim_message_fragment_get_total (message), ==, 1); |
| 162 |
g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0); |
| 163 |
g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray)); |
| 164 |
|
| 165 |
/* Validate all compiled data */ |
| 166 |
|
| 167 |
g_assert (mbim_message_validate (message, &error)); |
| 168 |
g_assert_no_error (error); |
| 169 |
|
| 170 |
fragment_information_buffer = (_mbim_message_fragment_get_payload ( |
| 171 |
message, |
| 172 |
&fragment_information_buffer_length)); |
| 173 |
g_assert_cmpuint (fragment_information_buffer_length, ==, sizeof (data)); |
| 174 |
g_assert (memcmp (fragment_information_buffer, data, fragment_information_buffer_length) == 0); |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
test_fragment_send_multiple_common (guint32 max_fragment_size, |
| 179 |
const guint8 *buffer, |
| 180 |
gsize buffer_length, |
| 181 |
const guint8 *expected_buffer, |
| 182 |
gsize expected_buffer_length) |
| 183 |
{ |
| 184 |
GByteArray *bytearray; |
| 185 |
struct fragment_info *fragments; |
| 186 |
guint n_fragments; |
| 187 |
guint i; |
| 188 |
GByteArray *output_bytearray; |
| 189 |
|
| 190 |
/* Setup message and split it into fragments */ |
| 191 |
bytearray = g_byte_array_new (); |
| 192 |
g_byte_array_append (bytearray, buffer, buffer_length); |
| 193 |
fragments = _mbim_message_split_fragments ((const MbimMessage *)bytearray, |
| 194 |
max_fragment_size, |
| 195 |
&n_fragments); |
| 196 |
g_assert (fragments != NULL); |
| 197 |
|
| 198 |
/* Simulate that we write the fragments */ |
| 199 |
output_bytearray = g_byte_array_new (); |
| 200 |
for (i = 0; i < n_fragments; i++) { |
| 201 |
/* Header */ |
| 202 |
g_byte_array_append (output_bytearray, |
| 203 |
(guint8 *)&fragments[i].header, |
| 204 |
sizeof (fragments[i].header)); |
| 205 |
/* Fragment header */ |
| 206 |
g_byte_array_append (output_bytearray, |
| 207 |
(guint8 *)&fragments[i].fragment_header, |
| 208 |
sizeof (fragments[i].fragment_header)); |
| 209 |
/* Data */ |
| 210 |
g_byte_array_append (output_bytearray, |
| 211 |
fragments[i].data, |
| 212 |
fragments[i].data_length); |
| 213 |
} |
| 214 |
g_free (fragments); |
| 215 |
|
| 216 |
/* Compare with the expected buffer */ |
| 217 |
g_assert_cmpuint (expected_buffer_length, == , output_bytearray->len); |
| 218 |
g_assert (memcmp (output_bytearray->data, expected_buffer, output_bytearray->len) == 0); |
| 219 |
|
| 220 |
g_byte_array_unref (output_bytearray); |
| 221 |
} |
| 222 |
|
| 223 |
static void |
| 224 |
test_fragment_send_multiple_1 (void) |
| 225 |
{ |
| 226 |
const guint8 buffer [] = { |
| 227 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 228 |
0x30, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 229 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 230 |
0x01, 0x00, 0x00, 0x00, /* total fragments */ |
| 231 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 232 |
0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */ |
| 233 |
0x04, 0x05, 0x06, 0x07, |
| 234 |
0x08, 0x09, 0x0A, 0x0B, |
| 235 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 236 |
0x10, 0x11, 0x12, 0x13, |
| 237 |
0x14, 0x15, 0x16, 0x17, |
| 238 |
0x18, 0x19, 0x1A, 0x1B |
| 239 |
}; |
| 240 |
|
| 241 |
/* This buffer contains several fragments of a single message. |
| 242 |
* We don't really care about the actual data included within the fragments. */ |
| 243 |
const guint8 expected_buffer [] = { |
| 244 |
/* First fragment */ |
| 245 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 246 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 247 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 248 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 249 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 250 |
0x00, 0x01, 0x02, 0x03, /* frament data */ |
| 251 |
0x04, 0x05, 0x06, 0x07, |
| 252 |
/* Second fragment */ |
| 253 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 254 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 255 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 256 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 257 |
0x01, 0x00, 0x00, 0x00, /* current fragment */ |
| 258 |
0x08, 0x09, 0x0A, 0x0B, /* frament data */ |
| 259 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 260 |
/* Third fragment */ |
| 261 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 262 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 263 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 264 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 265 |
0x02, 0x00, 0x00, 0x00, /* current fragment */ |
| 266 |
0x10, 0x11, 0x12, 0x13, /* frament data */ |
| 267 |
0x14, 0x15, 0x16, 0x17, |
| 268 |
/* Fourth fragment */ |
| 269 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 270 |
0x18, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 271 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 272 |
0x04, 0x00, 0x00, 0x00, /* total fragments */ |
| 273 |
0x03, 0x00, 0x00, 0x00, /* current fragment */ |
| 274 |
0x18, 0x19, 0x1A, 0x1B /* frament data */ |
| 275 |
}; |
| 276 |
|
| 277 |
test_fragment_send_multiple_common ( |
| 278 |
28, |
| 279 |
buffer, sizeof (buffer), |
| 280 |
expected_buffer, sizeof (expected_buffer)); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
test_fragment_send_multiple_2 (void) |
| 285 |
{ |
| 286 |
const guint8 buffer [] = { |
| 287 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 288 |
0x2C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 289 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 290 |
0x01, 0x00, 0x00, 0x00, /* total fragments */ |
| 291 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 292 |
0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */ |
| 293 |
0x04, 0x05, 0x06, 0x07, |
| 294 |
0x08, 0x09, 0x0A, 0x0B, |
| 295 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 296 |
0x10, 0x11, 0x12, 0x13, |
| 297 |
0x14, 0x15, 0x16, 0x17 |
| 298 |
}; |
| 299 |
|
| 300 |
/* This buffer contains several fragments of a single message. |
| 301 |
* We don't really care about the actual data included within the fragments. */ |
| 302 |
const guint8 expected_buffer [] = { |
| 303 |
/* First fragment */ |
| 304 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 305 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 306 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 307 |
0x03, 0x00, 0x00, 0x00, /* total fragments */ |
| 308 |
0x00, 0x00, 0x00, 0x00, /* current fragment */ |
| 309 |
0x00, 0x01, 0x02, 0x03, /* frament data */ |
| 310 |
0x04, 0x05, 0x06, 0x07, |
| 311 |
/* Second fragment */ |
| 312 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 313 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 314 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 315 |
0x03, 0x00, 0x00, 0x00, /* total fragments */ |
| 316 |
0x01, 0x00, 0x00, 0x00, /* current fragment */ |
| 317 |
0x08, 0x09, 0x0A, 0x0B, /* frament data */ |
| 318 |
0x0C, 0x0D, 0x0E, 0x0F, |
| 319 |
/* Third fragment */ |
| 320 |
0x07, 0x00, 0x00, 0x80, /* indications have fragments */ |
| 321 |
0x1C, 0x00, 0x00, 0x00, /* length of this fragment */ |
| 322 |
0x01, 0x00, 0x00, 0x00, /* transaction id */ |
| 323 |
0x03, 0x00, 0x00, 0x00, /* total fragments */ |
| 324 |
0x02, 0x00, 0x00, 0x00, /* current fragment */ |
| 325 |
0x10, 0x11, 0x12, 0x13, /* frament data */ |
| 326 |
0x14, 0x15, 0x16, 0x17 |
| 327 |
}; |
| 328 |
|
| 329 |
test_fragment_send_multiple_common ( |
| 330 |
28, |
| 331 |
buffer, sizeof (buffer), |
| 332 |
expected_buffer, sizeof (expected_buffer)); |
| 333 |
} |
| 334 |
|
| 335 |
int main (int argc, char **argv) |
| 336 |
{ |
| 337 |
g_test_init (&argc, &argv, NULL); |
| 338 |
|
| 339 |
g_test_add_func ("/libmbim-glib/fragment/receive/single", test_fragment_receive_single); |
| 340 |
g_test_add_func ("/libmbim-glib/fragment/receive/multiple", test_fragment_receive_multiple); |
| 341 |
g_test_add_func ("/libmbim-glib/fragment/send/multiple-1", test_fragment_send_multiple_1); |
| 342 |
g_test_add_func ("/libmbim-glib/fragment/send/multiple-2", test_fragment_send_multiple_2); |
| 343 |
|
| 344 |
return g_test_run (); |
| 345 |
} |