Debian Codegraph @

src/libqmi-glib/test/test-fixture.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) 2014 Aleksander Morgado <[email protected]>
14 */
15
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <string.h>
19
20 #include "test-fixture.h"
21
22 #define VIRTUAL_SOCKET_PATH "virtual-socket-path"
23
24 static const QmiService services [] = {
25 #if defined HAVE_QMI_SERVICE_DMS
26 QMI_SERVICE_DMS,
27 #endif
28 #if defined HAVE_QMI_SERVICE_NAS
29 QMI_SERVICE_NAS,
30 #endif
31 #if defined HAVE_QMI_SERVICE_WDS
32 QMI_SERVICE_WDS,
33 #endif
34 #if defined HAVE_QMI_SERVICE_PDS
35 QMI_SERVICE_PDS
36 #endif
37 };
38
39 static void
40 device_allocate_client_ready (QmiDevice *device,
41 GAsyncResult *res,
42 TestFixture *fixture)
43 {
44 GError *error = NULL;
45 QmiClient *client;
46 QmiService service;
47
48 client = qmi_device_allocate_client_finish (device, res, &error);
49 g_assert_no_error (error);
50 g_assert (QMI_IS_CLIENT (client));
51
52 service = qmi_client_get_service (client);
53 g_assert (service > QMI_SERVICE_CTL);
54 g_assert ((unsigned)service < G_N_ELEMENTS (fixture->service_info));
55 fixture->service_info[service].client = client;
56 fixture->service_info[service].transaction_id = 0x0001;
57 test_fixture_loop_stop (fixture);
58 }
59
60 static void
61 device_open_ready (QmiDevice *device,
62 GAsyncResult *res,
63 TestFixture *fixture)
64 {
65 GError *error = NULL;
66 gboolean ret;
67
68 ret = qmi_device_open_finish (device, res, &error);
69 g_assert_no_error (error);
70 g_assert (ret);
71 test_fixture_loop_stop (fixture);
72 }
73
74 static void
75 device_virtual_new_ready (GObject *source,
76 GAsyncResult *res,
77 TestFixture *fixture)
78 {
79 GError *error = NULL;
80
81 fixture->device = qmi_device_new_finish (res, &error);
82 g_assert_no_error (error);
83 g_assert (QMI_IS_DEVICE (fixture->device));
84 test_fixture_loop_stop (fixture);
85 }
86
87 void
88 test_fixture_setup (TestFixture *fixture)
89 {
90 GFile *file;
91 guint i;
92 static guint32 num = 0;
93
94 g_debug ("[%lu,%p] fixture setup", (gulong) pthread_self (), g_main_context_get_thread_default ());
95
96 qmi_utils_set_traces_enabled (TRUE);
97
98 /* Create port name, and add process ID so that multiple runs of this test
99 * in the same system don't clash with each other */
100 fixture->path = g_strdup_printf ("/dev/qmi%08lu%04u", (gulong) getpid (), num++);
101 fixture->service_info[QMI_SERVICE_CTL].transaction_id = 0x0001;
102 fixture->ctx = test_port_context_new (fixture->path);
103 test_port_context_start (fixture->ctx);
104
105 /* Create device */
106 file = g_file_new_for_path (fixture->path);
107 g_async_initable_new_async (QMI_TYPE_DEVICE,
108 G_PRIORITY_DEFAULT,
109 NULL,
110 (GAsyncReadyCallback) device_virtual_new_ready,
111 fixture,
112 QMI_DEVICE_FILE, file,
113 QMI_DEVICE_NO_FILE_CHECK, TRUE,
114 QMI_DEVICE_PROXY_PATH, fixture->path,
115 NULL);
116 g_object_unref (file);
117 test_fixture_loop_run (fixture);
118
119 /* Open device */
120 {
121 guint8 expected[] = {
122 0x01, /* marker */
123 /* QMUX */
124 0x22, 0x00, /* length */
125 0x00, /* flags */
126 0x00, /* service CTL */
127 0x00, /* client */
128 /* QMI header */
129 0x00, /* flags */
130 0xFF, /* transaction */
131 0x00, 0xFF, /* message: Internal proxy open */
132 0x17, 0x00, /* tlv length */
133 /* TLV */
134 0x01, /* type */
135 0x14, 0x00, /* length */
136 0x2F, 0x64, 0x65, 0x76, 0x2F, 0x76, 0x69, 0x72, 0x74, 0x75, 0x61, 0x6C, 0x2F, 0x71, 0x6D, 0x69, 0x00, 0x00, 0x00, 0x00
137 };
138 guint8 response[] = {
139 0x01, /* marker */
140 /* QMUX */
141 0x12, 0x00, /* length */
142 0x00, /* flags */
143 0x00, /* service CTL */
144 0x00, /* client */
145 /* QMI header */
146 0x01, /* flags */
147 0xFF, /* transaction */
148 0x00, 0xFF, /* message: Internal proxy open */
149 0x07, 0x00, /* tlv length */
150 /* TLV */
151 0x02, /* type: Result */
152 0x04, 0x00, /* length */
153 0x00, 0x00, /* error status */
154 0x00, 0x00, /* error code */
155 };
156
157 g_assert_cmpuint (strlen (fixture->path), ==, 20);
158 memcpy (&expected[15], fixture->path, strlen (fixture->path));
159
160 test_port_context_set_command (fixture->ctx,
161 expected, G_N_ELEMENTS (expected),
162 response, G_N_ELEMENTS (response),
163 fixture->service_info[QMI_SERVICE_CTL].transaction_id++);
164 }
165 qmi_device_open (fixture->device, QMI_DEVICE_OPEN_FLAGS_PROXY, 1, NULL,
166 (GAsyncReadyCallback) device_open_ready,
167 fixture);
168 test_fixture_loop_run (fixture);
169
170 /* Allocate clients */
171 for (i = 0; i < G_N_ELEMENTS (services); i++) {
172 guint8 expected[] = {
173 0x01, /* marker */
174 /* QMUX */
175 0x0F, 0x00, /* length */
176 0x00, /* flags */
177 0x00, /* service CTL */
178 0x00, /* client */
179 /* QMI header */
180 0x00, /* flags */
181 0xFF, /* transaction */
182 0x22, 0x00, /* message: Allocate CID */
183 0x04, 0x00, /* tlv length */
184 /* TLV */
185 0x01, /* type */
186 0x01, 0x00, /* length */
187 0xFF /* UPDATE: service */
188 };
189 guint8 response[] = {
190 0x01, /* marker */
191 /* QMUX */
192 0x17, 0x00, /* length */
193 0x00, /* flags */
194 0x00, /* service */
195 0x00, /* client */
196 /* QMI header */
197 0x01, /* flags: Response */
198 0xFF, /* transaction */
199 0x22, 0x00, /* message */
200 0x0C, 0x00, /* tlv length */
201 /* TLV */
202 0x02, /* type: Result */
203 0x04, 0x00, /* length */
204 0x00, 0x00, /* error status */
205 0x00, 0x00, /* error code */
206 /* TLV */
207 0x01, /* type: Allocation info */
208 0x02, 0x00, /* length */
209 0xFF, /* UPDATE: service */
210 0x01, /* cid: 1 */
211 };
212
213 expected[15] = services[i];
214 response[22] = services[i];
215
216 test_port_context_set_command (fixture->ctx,
217 expected, G_N_ELEMENTS (expected),
218 response, G_N_ELEMENTS (response),
219 fixture->service_info[QMI_SERVICE_CTL].transaction_id++);
220 qmi_device_allocate_client (fixture->device, services[i], QMI_CID_NONE, 10, NULL,
221 (GAsyncReadyCallback) device_allocate_client_ready,
222 fixture);
223 test_fixture_loop_run (fixture);
224 }
225 }
226
227 static void
228 device_release_client_ready (QmiDevice *device,
229 GAsyncResult *res,
230 TestFixture *fixture)
231 {
232 GError *error = NULL;
233 gboolean st;
234
235 st = qmi_device_release_client_finish (device, res, &error);
236 g_assert_no_error (error);
237 g_assert (st);
238 test_fixture_loop_stop (fixture);
239 }
240
241 static void
242 device_close_ready (QmiDevice *device,
243 GAsyncResult *res,
244 TestFixture *fixture)
245 {
246 GError *error = NULL;
247 gboolean st;
248
249 st = qmi_device_close_finish (device, res, &error);
250 g_assert_no_error (error);
251 g_assert (st);
252 test_fixture_loop_stop (fixture);
253 }
254
255 void
256 test_fixture_teardown (TestFixture *fixture)
257 {
258 guint i;
259
260 for (i = 0; i < G_N_ELEMENTS (services); i++) {
261 guint8 expected[] = {
262 0x01, /* marker */
263 /* QMUX */
264 0x10, 0x00, /* length */
265 0x00, /* flags */
266 0x00, /* service CTL */
267 0x00, /* client */
268 /* QMI header */
269 0x00, /* flags */
270 0xFF, /* transaction */
271 0x23, 0x00, /* message: Release CID */
272 0x05, 0x00, /* tlv length: 5 bytes */
273 /* TLV */
274 0x01, /* type */
275 0x02, 0x00, /* length */
276 0xFF, /* UPDATE: service */
277 0x01
278 };
279 guint8 response[] = {
280 0x01, /* marker */
281 /* QMUX */
282 0x17, 0x00, /* length */
283 0x00, /* flags */
284 0x00, /* service */
285 0x00, /* client */
286 /* QMI header */
287 0x01, /* flags: Response */
288 0xFF, /* transaction */
289 0x23, 0x00, /* message */
290 0x0C, 0x00, /* tlv length */
291 /* TLV */
292 0x02, /* type: Result*/
293 0x04, 0x00, /* length */
294 0x00, 0x00, /* error status */
295 0x00, 0x00, /* error code */
296 /* TLV */
297 0x01, /* type: Allocation Info */
298 0x02, 0x00, /* length */
299 0xFF, /* UPDATE: service */
300 0x01, /* cid: 1 */
301 };
302
303 expected[15] = services[i];
304 response[22] = services[i];
305 test_port_context_set_command (fixture->ctx,
306 expected, G_N_ELEMENTS (expected),
307 response, G_N_ELEMENTS (response),
308 fixture->service_info[QMI_SERVICE_CTL].transaction_id++);
309
310 qmi_device_release_client (fixture->device, fixture->service_info[services[i]].client, QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID, 10, NULL,
311 (GAsyncReadyCallback) device_release_client_ready,
312 fixture);
313
314 test_fixture_loop_run (fixture);
315
316 g_clear_object (&fixture->service_info[services[i]].client);
317 fixture->service_info[services[i]].transaction_id = 0x0000;
318 }
319
320 if (fixture->device) {
321 qmi_device_close_async (fixture->device, 10, NULL,
322 (GAsyncReadyCallback) device_close_ready,
323 fixture);
324
325 test_fixture_loop_run (fixture);
326
327 g_object_unref (fixture->device);
328 }
329
330 g_free (fixture->path);
331
332 /* Stop port context */
333 test_port_context_stop (fixture->ctx);
334 test_port_context_free (fixture->ctx);
335 }
336
337 void
338 test_fixture_loop_stop (TestFixture *fixture)
339 {
340 g_assert (fixture->loop);
341 g_main_loop_quit (fixture->loop);
342 }
343
344 void
345 test_fixture_loop_run (TestFixture *fixture)
346 {
347 g_assert (!fixture->loop);
348 fixture->loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
349 g_main_loop_run (fixture->loop);
350 g_main_loop_unref (fixture->loop);
351 fixture->loop = NULL;
352 }