summaryrefslogtreecommitdiff
path: root/src/libmbim-glib/test/test-fragment.c
blob: 680039a91890daeb69a8ef3b271f8129c7a1917d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2013 - 2014 Aleksander Morgado <aleksander@aleksander.es>
 */

#include <config.h>
#include <string.h>

#include "mbim-message.h"
#include "mbim-message-private.h"

static void
test_fragment_receive_single (void)
{
    MbimMessage *message;
    const guint8 *fragment_information_buffer;
    guint32 fragment_information_buffer_length;

    /* This buffer contains a single message composed of a single fragment.
     * We don't really care about the actual data included within the fragment. */
    const guint8 buffer [] =  {
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x01, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x01, 0x02, 0x03, 0x04, /* frament data */
        0x05, 0x06, 0x07, 0x08 };

    const guint8 data [] = {
        0x01, 0x02, 0x03, 0x04, /* same data as in the fragment */
        0x05, 0x06, 0x07, 0x08
    };

    message = mbim_message_new (buffer, sizeof (buffer));
    g_assert         (_mbim_message_is_fragment          (message));
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 1);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0);

    fragment_information_buffer = (_mbim_message_fragment_get_payload (
                                       message,
                                       &fragment_information_buffer_length));

    g_assert_cmpuint (fragment_information_buffer_length, ==, sizeof (data));
    g_assert (memcmp (fragment_information_buffer, data, fragment_information_buffer_length) == 0);

    mbim_message_unref (message);
}

static void
test_fragment_receive_multiple (void)
{
    GByteArray *bytearray;
    MbimMessage *message;
    GError *error = NULL;
    const guint8 *fragment_information_buffer;
    guint32 fragment_information_buffer_length;

    /* This buffer contains several fragments of a single message.
     * We don't really care about the actual data included within the fragments. */
    const guint8 buffer [] =  {
        /* First fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x00, 0x01, 0x02, 0x03, /* frament data */
        0x04, 0x05, 0x06, 0x07,
        /* Second fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x01, 0x00, 0x00, 0x00, /* current fragment */
        0x08, 0x09, 0x0A, 0x0B, /* frament data */
        0x0C, 0x0D, 0x0E, 0x0F,
        /* Third fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x02, 0x00, 0x00, 0x00, /* current fragment */
        0x10, 0x11, 0x12, 0x13, /* frament data */
        0x14, 0x15, 0x16, 0x17,
        /* Fourth fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x18, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x03, 0x00, 0x00, 0x00, /* current fragment */
        0x18, 0x19, 0x1A, 0x1B  /* frament data */
    };

    const guint8 data [] = {
        0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */
        0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0A, 0x0B,
        0x0C, 0x0D, 0x0E, 0x0F,
        0x10, 0x11, 0x12, 0x13,
        0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1A, 0x1B
    };

    bytearray = g_byte_array_new ();
    g_byte_array_append (bytearray, buffer, sizeof (buffer));

    /* First fragment creates the message */
    message = _mbim_message_fragment_collector_init ((const MbimMessage *)bytearray, &error);
    g_assert_no_error (error);
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 4);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0);
    g_assert         (_mbim_message_fragment_collector_complete (message) == FALSE);
    g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray));


    /* Add second fragment */
    g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error));
    g_assert_no_error (error);
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 4);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 1);
    g_assert         (_mbim_message_fragment_collector_complete (message) == FALSE);
    g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray));

    /* Add third fragment */
    g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error));
    g_assert_no_error (error);
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 4);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 2);
    g_assert         (_mbim_message_fragment_collector_complete (message) == FALSE);
    g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray));

    /* Add fourth fragment */
    g_assert (_mbim_message_fragment_collector_add (message, (const MbimMessage *)bytearray, &error));
    g_assert_no_error (error);
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 4);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 3);
    g_assert         (_mbim_message_fragment_collector_complete (message) == TRUE);
    g_assert_cmpuint (_mbim_message_fragment_get_total   (message), ==, 1);
    g_assert_cmpuint (_mbim_message_fragment_get_current (message), ==, 0);
    g_byte_array_remove_range (bytearray, 0, mbim_message_get_message_length ((const MbimMessage *)bytearray));

    /* Compare all compiled data */
    fragment_information_buffer = (_mbim_message_fragment_get_payload (
                                       message,
                                       &fragment_information_buffer_length));
    g_assert_cmpuint (fragment_information_buffer_length, ==, sizeof (data));
    g_assert (memcmp (fragment_information_buffer, data, fragment_information_buffer_length) == 0);

    mbim_message_unref (message);
}

static void
test_fragment_send_multiple_common (guint32       max_fragment_size,
                                    const guint8 *buffer,
                                    gsize         buffer_length,
                                    const guint8 *expected_buffer,
                                    gsize         expected_buffer_length)
{
    GByteArray *bytearray;
    struct fragment_info *fragments;
    guint n_fragments;
    guint i;
    GByteArray *output_bytearray;

    /* Setup message and split it into fragments */
    bytearray = g_byte_array_new ();
    g_byte_array_append (bytearray, buffer, buffer_length);
    fragments = _mbim_message_split_fragments ((const MbimMessage *)bytearray,
                                               max_fragment_size,
                                               &n_fragments);
    g_assert (fragments != NULL);

    /* Simulate that we write the fragments */
    output_bytearray = g_byte_array_new ();
    for (i = 0; i < n_fragments; i++) {
        /* Header */
        g_byte_array_append (output_bytearray,
                             (guint8 *)&fragments[i].header,
                             sizeof (fragments[i].header));
        /* Fragment header */
        g_byte_array_append (output_bytearray,
                             (guint8 *)&fragments[i].fragment_header,
                             sizeof (fragments[i].fragment_header));
        /* Data */
        g_byte_array_append (output_bytearray,
                             fragments[i].data,
                             fragments[i].data_length);
    }
    g_free (fragments);

    /* Compare with the expected buffer */
    g_assert_cmpuint (expected_buffer_length, == , output_bytearray->len);
    g_assert (memcmp (output_bytearray->data, expected_buffer, output_bytearray->len) == 0);

    g_byte_array_unref (output_bytearray);
}

static void
test_fragment_send_multiple_1 (void)
{
    const guint8 buffer [] = {
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x30, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x01, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */
        0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0A, 0x0B,
        0x0C, 0x0D, 0x0E, 0x0F,
        0x10, 0x11, 0x12, 0x13,
        0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1A, 0x1B
    };

    /* This buffer contains several fragments of a single message.
     * We don't really care about the actual data included within the fragments. */
    const guint8 expected_buffer [] = {
        /* First fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x00, 0x01, 0x02, 0x03, /* frament data */
        0x04, 0x05, 0x06, 0x07,
        /* Second fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x01, 0x00, 0x00, 0x00, /* current fragment */
        0x08, 0x09, 0x0A, 0x0B, /* frament data */
        0x0C, 0x0D, 0x0E, 0x0F,
        /* Third fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x02, 0x00, 0x00, 0x00, /* current fragment */
        0x10, 0x11, 0x12, 0x13, /* frament data */
        0x14, 0x15, 0x16, 0x17,
        /* Fourth fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x18, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x04, 0x00, 0x00, 0x00, /* total fragments */
        0x03, 0x00, 0x00, 0x00, /* current fragment */
        0x18, 0x19, 0x1A, 0x1B  /* frament data */
    };

    test_fragment_send_multiple_common (
        28,
        buffer, sizeof (buffer),
        expected_buffer, sizeof (expected_buffer));
}

static void
test_fragment_send_multiple_2 (void)
{
    const guint8 buffer [] = {
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x2C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x01, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x00, 0x01, 0x02, 0x03, /* same data as in the fragments */
        0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0A, 0x0B,
        0x0C, 0x0D, 0x0E, 0x0F,
        0x10, 0x11, 0x12, 0x13,
        0x14, 0x15, 0x16, 0x17
    };

    /* This buffer contains several fragments of a single message.
     * We don't really care about the actual data included within the fragments. */
    const guint8 expected_buffer [] = {
        /* First fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x03, 0x00, 0x00, 0x00, /* total fragments */
        0x00, 0x00, 0x00, 0x00, /* current fragment */
        0x00, 0x01, 0x02, 0x03, /* frament data */
        0x04, 0x05, 0x06, 0x07,
        /* Second fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x03, 0x00, 0x00, 0x00, /* total fragments */
        0x01, 0x00, 0x00, 0x00, /* current fragment */
        0x08, 0x09, 0x0A, 0x0B, /* frament data */
        0x0C, 0x0D, 0x0E, 0x0F,
        /* Third fragment */
        0x07, 0x00, 0x00, 0x80, /* indications have fragments */
        0x1C, 0x00, 0x00, 0x00, /* length of this fragment */
        0x01, 0x00, 0x00, 0x00, /* transaction id */
        0x03, 0x00, 0x00, 0x00, /* total fragments */
        0x02, 0x00, 0x00, 0x00, /* current fragment */
        0x10, 0x11, 0x12, 0x13, /* frament data */
        0x14, 0x15, 0x16, 0x17
    };

    test_fragment_send_multiple_common (
        28,
        buffer, sizeof (buffer),
        expected_buffer, sizeof (expected_buffer));
}

int main (int argc, char **argv)
{
    g_test_init (&argc, &argv, NULL);

    g_test_add_func ("/libmbim-glib/fragment/receive/single",   test_fragment_receive_single);
    g_test_add_func ("/libmbim-glib/fragment/receive/multiple", test_fragment_receive_multiple);
    g_test_add_func ("/libmbim-glib/fragment/send/multiple-1",  test_fragment_send_multiple_1);
    g_test_add_func ("/libmbim-glib/fragment/send/multiple-2",  test_fragment_send_multiple_2);

    return g_test_run ();
}