Debian Codegraph @

src/cd-self-test.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2011 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <limits.h>
25 #include <stdlib.h>
26
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <sqlite3.h>
30 #include <glib/gstdio.h>
31
32 #include "cd-common.h"
33 #include "cd-device-array.h"
34 #include "cd-device-db.h"
35 #include "cd-device.h"
36 #include "cd-mapping-db.h"
37 #include "cd-profile-array.h"
38 #include "cd-profile-db.h"
39 #include "cd-profile.h"
40
41 static void
42 colord_common_func (void)
43 {
44 gchar *tmp;
45
46 /* exact match in quirk db */
47 tmp = cd_quirk_vendor_name ("NIKON");
48 g_assert_cmpstr (tmp, ==, "Nikon");
49 g_free (tmp);
50
51 /* suffix that needs removing */
52 tmp = cd_quirk_vendor_name ("Hughski Ltd.");
53 g_assert_cmpstr (tmp, ==, "Hughski");
54 g_free (tmp);
55
56 /* suffix that needs removing */
57 tmp = cd_quirk_vendor_name ("Acme Inc");
58 g_assert_cmpstr (tmp, ==, "Acme");
59 g_free (tmp);
60 }
61
62 static void
63 colord_profile_func (void)
64 {
65 CdProfile *profile;
66
67 profile = cd_profile_new ();
68 g_assert (profile != NULL);
69
70 cd_profile_set_id (profile, "dave");
71 g_assert_cmpstr (cd_profile_get_id (profile), ==, "dave");
72 g_assert_cmpint (cd_profile_get_score (profile), ==, 1);
73
74 /* system-wide profiles have a larger importance */
75 cd_profile_set_is_system_wide (profile, TRUE);
76 g_assert_cmpint (cd_profile_get_score (profile), ==, 2);
77
78 g_object_unref (profile);
79 }
80
81 static void
82 colord_device_func (void)
83 {
84 CdDeviceDb *ddb;
85 CdDevice *device;
86 CdProfile *profile;
87 CdProfileArray *profile_array;
88 gboolean ret;
89 GError *error = NULL;
90
91 profile_array = cd_profile_array_new ();
92 device = cd_device_new ();
93 g_assert (device != NULL);
94
95 /* create device database */
96 ddb = cd_device_db_new ();
97 ret = cd_device_db_load (ddb, "/tmp/device.db", &error);
98 g_assert_no_error (error);
99 g_assert (ret);
100 ret = cd_device_db_empty (ddb, &error);
101 g_assert_no_error (error);
102 g_assert (ret);
103
104 cd_device_set_id (device, "dave");
105 g_assert_cmpstr (cd_device_get_id (device), ==, "dave");
106
107 profile = cd_profile_new ();
108 cd_profile_set_id (profile, "dave");
109 cd_profile_array_add (profile_array, profile);
110
111 /* add profile */
112 ret = cd_device_add_profile (device,
113 CD_DEVICE_RELATION_SOFT,
114 cd_profile_get_object_path (profile),
115 0,
116 &error);
117 g_assert_no_error (error);
118 g_assert (ret);
119
120 /* add profile again */
121 ret = cd_device_add_profile (device,
122 CD_DEVICE_RELATION_SOFT,
123 cd_profile_get_object_path (profile),
124 0,
125 &error);
126 g_assert_error (error, CD_DEVICE_ERROR, CD_DEVICE_ERROR_PROFILE_ALREADY_ADDED);
127 g_assert (!ret);
128 g_clear_error (&error);
129
130 /* add profile again */
131 ret = cd_device_add_profile (device,
132 CD_DEVICE_RELATION_HARD,
133 cd_profile_get_object_path (profile),
134 0,
135 &error);
136 g_assert_no_error (error);
137 g_assert (ret);
138
139 /* add profile that does not exist */
140 ret = cd_device_add_profile (device,
141 CD_DEVICE_RELATION_SOFT,
142 "/dave",
143 0,
144 &error);
145 g_assert_error (error, CD_DEVICE_ERROR, CD_DEVICE_ERROR_PROFILE_DOES_NOT_EXIST);
146 g_assert (!ret);
147 g_clear_error (&error);
148
149 g_object_unref (device);
150 g_object_unref (ddb);
151 g_object_unref (profile);
152 g_object_unref (profile_array);
153 }
154
155 static void
156 colord_device_array_func (void)
157 {
158 CdDeviceArray *device_array;
159 CdDeviceDb *ddb;
160 CdDevice *device;
161 gboolean ret;
162 GError *error = NULL;
163
164 /* create device database */
165 ddb = cd_device_db_new ();
166 ret = cd_device_db_load (ddb, "/tmp/device.db", &error);
167 g_assert_no_error (error);
168 g_assert (ret);
169 ret = cd_device_db_empty (ddb, &error);
170 g_assert_no_error (error);
171 g_assert (ret);
172
173 device_array = cd_device_array_new ();
174 g_assert (device_array != NULL);
175
176 device = cd_device_new ();
177 cd_device_set_id (device, "dave");
178 cd_device_array_add (device_array, device);
179 g_object_unref (device);
180
181 device = cd_device_array_get_by_id_owner (device_array, "does not exist", 0, CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
182 g_assert (device == NULL);
183
184 device = cd_device_array_get_by_id_owner (device_array, "dave", 999, CD_DEVICE_ARRAY_FLAG_NONE);
185 g_assert (device == NULL);
186
187 device = cd_device_array_get_by_id_owner (device_array, "dave", 0, CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
188 g_assert (device != NULL);
189 g_assert_cmpstr (cd_device_get_id (device), ==, "dave");
190 g_object_unref (device);
191
192 device = cd_device_array_get_by_object_path (device_array,
193 "/org/freedesktop/ColorManager/devices/dave");
194 g_assert (device != NULL);
195 g_assert_cmpstr (cd_device_get_id (device), ==, "dave");
196 g_object_unref (device);
197
198 g_object_unref (device_array);
199 g_object_unref (ddb);
200 }
201
202 static void
203 cd_mapping_db_alter_func (void)
204 {
205 CdMappingDb *mdb;
206 const gchar *db_filename = "/tmp/mapping.db";
207 const gchar *statement;
208 gboolean ret;
209 GError *error = NULL;
210 gint rc;
211 sqlite3 *db;
212
213 /* create */
214 mdb = cd_mapping_db_new ();
215 g_assert (mdb != NULL);
216
217 /* setup v0.1.0 database for altering */
218 (void)g_unlink (db_filename);
219 rc = sqlite3_open (db_filename, &db);
220 g_assert_cmpint (rc, ==, SQLITE_OK);
221 statement = "CREATE TABLE mappings ("
222 "device TEXT,"
223 "profile TEXT);";
224 sqlite3_exec (db, statement, NULL, NULL, NULL);
225 statement = "INSERT INTO mappings (device, profile) VALUES ('dev1', 'prof1')";
226 sqlite3_exec (db, statement, NULL, NULL, NULL);
227
228 /* connect */
229 ret = cd_mapping_db_load (mdb, db_filename, &error);
230 g_assert_no_error (error);
231 g_assert (ret);
232
233 /* check we ALTERed it correctly */
234 statement = "SELECT timestamp FROM mappings LIMIT 1";
235 rc = sqlite3_exec (db, statement, NULL, NULL, NULL);
236 g_assert_cmpint (rc, ==, SQLITE_OK);
237
238 sqlite3_close (db);
239 g_object_unref (mdb);
240 }
241
242 static gint
243 cd_mapping_db_test_cb (void *data, gint argc, gchar **argv, gchar **col_name)
244 {
245 gchar **tmp = (gchar **) data;
246 *tmp = g_strdup (argv[0]);
247 return 0;
248 }
249
250 static void
251 cd_mapping_db_convert_func (void)
252 {
253 CdMappingDb *mdb;
254 const gchar *db_filename = "/tmp/mapping.db";
255 const gchar *statement;
256 gboolean ret;
257 gchar *device_id = NULL;
258 GError *error = NULL;
259 gint rc;
260 sqlite3 *db;
261
262 /* create */
263 mdb = cd_mapping_db_new ();
264 g_assert (mdb != NULL);
265
266 /* setup v0.1.8 database for converting */
267 (void)g_unlink (db_filename);
268 rc = sqlite3_open (db_filename, &db);
269 g_assert_cmpint (rc, ==, SQLITE_OK);
270 statement = "CREATE TABLE mappings ("
271 "device TEXT,"
272 "profile TEXT,"
273 "timestamp INTEGER DEFAULT 0);";
274 sqlite3_exec (db, statement, NULL, NULL, NULL);
275 statement = "INSERT INTO mappings (device, profile, timestamp) VALUES ('dev1', 'prof1', 12345)";
276 sqlite3_exec (db, statement, NULL, NULL, NULL);
277
278 /* connect, which should convert it for us */
279 ret = cd_mapping_db_load (mdb, db_filename, &error);
280 g_assert_no_error (error);
281 g_assert (ret);
282
283 /* check we converted it correctly */
284 statement = "SELECT timestamp FROM mappings_v2 LIMIT 1";
285 rc = sqlite3_exec (db, statement,
286 cd_mapping_db_test_cb, &device_id, NULL);
287 g_assert_cmpint (rc, ==, SQLITE_OK);
288 g_assert_cmpstr (device_id, ==, "12345");
289 g_free (device_id);
290
291 sqlite3_close (db);
292 g_object_unref (mdb);
293 }
294
295 static void
296 cd_mapping_db_func (void)
297 {
298 CdMappingDb *mdb;
299 const gchar *db_filename = "/tmp/mapping.db";
300 gboolean ret;
301 GError *error = NULL;
302 GPtrArray *array;
303 guint64 timestamp;
304
305 /* create */
306 mdb = cd_mapping_db_new ();
307 g_assert (mdb != NULL);
308
309 /* connect, which should create a v2 table for us */
310 (void)g_unlink (db_filename);
311 ret = cd_mapping_db_load (mdb, db_filename, &error);
312 g_assert_no_error (error);
313 g_assert (ret);
314
315 /* ensure empty */
316 ret = cd_mapping_db_empty (mdb, &error);
317 g_assert_no_error (error);
318 g_assert (ret);
319
320 /* add a few entries */
321 ret = cd_mapping_db_add (mdb, "device1", "profile1", &error);
322 g_assert_no_error (error);
323 g_assert (ret);
324 ret = cd_mapping_db_add (mdb, "device1", "profile2", &error);
325 g_assert_no_error (error);
326 g_assert (ret);
327 ret = cd_mapping_db_add (mdb, "device1", "profile3", &error);
328 g_assert_no_error (error);
329 g_assert (ret);
330
331 /* remove one */
332 ret = cd_mapping_db_clear_timestamp (mdb, "device1", "profile2", &error);
333 g_assert_no_error (error);
334 g_assert (ret);
335
336 /* ensure timestamp really is zero */
337 timestamp = cd_mapping_db_get_timestamp (mdb,
338 "device1",
339 "profile2",
340 &error);
341 g_assert_no_error (error);
342 g_assert (timestamp != G_MAXUINT64);
343 g_assert_cmpint (timestamp, ==, 0);
344
345 /* get the profiles for a device */
346 array = cd_mapping_db_get_profiles (mdb, "device1", &error);
347 g_assert_no_error (error);
348 g_assert (array != NULL);
349 g_assert_cmpint (array->len, ==, 2);
350 g_assert_cmpstr (g_ptr_array_index (array, 0), ==, "profile1");
351 g_assert_cmpstr (g_ptr_array_index (array, 1), ==, "profile3");
352 g_ptr_array_unref (array);
353
354 /* get the devices for a profile */
355 array = cd_mapping_db_get_devices (mdb, "profile1", &error);
356 g_assert_no_error (error);
357 g_assert (array != NULL);
358 g_assert_cmpint (array->len, ==, 1);
359 g_assert_cmpstr (g_ptr_array_index (array, 0), ==, "device1");
360 g_ptr_array_unref (array);
361
362 g_object_unref (mdb);
363 }
364
365 static void
366 cd_device_db_func (void)
367 {
368 CdDeviceDb *ddb;
369 GError *error = NULL;
370 gboolean ret;
371 GPtrArray *array;
372 gchar *value;
373
374 /* create */
375 ddb = cd_device_db_new ();
376 g_assert (ddb != NULL);
377
378 /* connect, which should create it for us */
379 ret = cd_device_db_load (ddb, "/tmp/device.db", &error);
380 g_assert_no_error (error);
381 g_assert (ret);
382
383 /* ensure empty */
384 ret = cd_device_db_empty (ddb, &error);
385 g_assert_no_error (error);
386 g_assert (ret);
387
388 /* add a few entries */
389 ret = cd_device_db_add (ddb, "device1", &error);
390 g_assert_no_error (error);
391 g_assert (ret);
392 ret = cd_device_db_add (ddb, "device2", &error);
393 g_assert_no_error (error);
394 g_assert (ret);
395 ret = cd_device_db_add (ddb, "device3", &error);
396 g_assert_no_error (error);
397 g_assert (ret);
398
399 /* remove one */
400 ret = cd_device_db_remove (ddb, "device1", &error);
401 g_assert_no_error (error);
402 g_assert (ret);
403
404 /* get all the devices */
405 array = cd_device_db_get_devices (ddb, &error);
406 g_assert_no_error (error);
407 g_assert (array != NULL);
408 g_assert_cmpint (array->len, ==, 2);
409 g_assert_cmpstr (g_ptr_array_index (array, 0), ==, "device2");
410 g_assert_cmpstr (g_ptr_array_index (array, 1), ==, "device3");
411 g_ptr_array_unref (array);
412
413 /* set a property */
414 ret = cd_device_db_set_property (ddb,
415 "device2",
416 "kind",
417 "display",
418 &error);
419 g_assert_no_error (error);
420 g_assert (ret);
421
422 /* get a property that does not exist */
423 value = cd_device_db_get_property (ddb,
424 "device2",
425 "xxx",
426 &error);
427 g_assert_error (error, CD_CLIENT_ERROR, CD_CLIENT_ERROR_INTERNAL);
428 g_assert (value == NULL);
429 g_clear_error (&error);
430 g_free (value);
431
432 /* get a property that does exist */
433 value = cd_device_db_get_property (ddb,
434 "device2",
435 "kind",
436 &error);
437 g_assert_no_error (error);
438 g_assert_cmpstr (value, ==, "display");
439 g_free (value);
440
441 /* check the correct number of properties are stored */
442 array = cd_device_db_get_properties (ddb, "device2", &error);
443 g_assert_no_error (error);
444 g_assert (array != NULL);
445 g_assert_cmpint (array->len, ==, 1);
446 g_ptr_array_unref (array);
447
448 /* remove devices */
449 ret = cd_device_db_remove (ddb, "device2", &error);
450 g_assert_no_error (error);
451 g_assert (ret);
452 ret = cd_device_db_remove (ddb, "device3", &error);
453 g_assert_no_error (error);
454 g_assert (ret);
455
456 /* get all the devices */
457 array = cd_device_db_get_devices (ddb, &error);
458 g_assert_no_error (error);
459 g_assert (array != NULL);
460 g_assert_cmpint (array->len, ==, 0);
461 g_ptr_array_unref (array);
462
463 /* get a property that should be deleted */
464 value = cd_device_db_get_property (ddb,
465 "device2",
466 "kind",
467 &error);
468 g_assert_error (error, CD_CLIENT_ERROR, CD_CLIENT_ERROR_INTERNAL);
469 g_assert (value == NULL);
470 g_clear_error (&error);
471 g_free (value);
472
473 g_object_unref (ddb);
474 }
475
476 static void
477 cd_profile_db_func (void)
478 {
479 CdProfileDb *pdb;
480 GError *error = NULL;
481 gboolean ret;
482 gchar *value = NULL;
483
484 /* create */
485 pdb = cd_profile_db_new ();
486 g_assert (pdb != NULL);
487
488 /* connect, which should create it for us */
489 ret = cd_profile_db_load (pdb, "/tmp/profile.db", &error);
490 g_assert_no_error (error);
491 g_assert (ret);
492
493 /* ensure empty */
494 ret = cd_profile_db_empty (pdb, &error);
495 g_assert_no_error (error);
496 g_assert (ret);
497
498 /* set a property */
499 ret = cd_profile_db_set_property (pdb,
500 "profile-test",
501 "Title",
502 500,
503 "My Display Profile",
504 &error);
505 g_assert_no_error (error);
506 g_assert (ret);
507
508 /* get a property that does not exist */
509 ret = cd_profile_db_get_property (pdb,
510 "profile-test",
511 "Modified",
512 500,
513 &value,
514 &error);
515 g_assert_no_error (error);
516 g_assert (ret);
517 g_assert_cmpstr (value, ==, NULL);
518
519 /* get a property for a different user */
520 ret = cd_profile_db_get_property (pdb,
521 "profile-test",
522 "Title",
523 501,
524 &value,
525 &error);
526 g_assert_no_error (error);
527 g_assert (ret);
528 g_assert_cmpstr (value, ==, NULL);
529
530 /* get a property that does exist */
531 ret = cd_profile_db_get_property (pdb,
532 "profile-test",
533 "Title",
534 500,
535 &value,
536 &error);
537 g_assert_no_error (error);
538 g_assert (ret);
539 g_assert_cmpstr (value, ==, "My Display Profile");
540 g_free (value);
541
542 g_object_unref (pdb);
543 }
544
545 int
546 main (int argc, char **argv)
547 {
548 g_test_init (&argc, &argv, NULL);
549
550 /* only critical and error are fatal */
551 g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
552
553 /* tests go here */
554 g_test_add_func ("/colord/common", colord_common_func);
555 g_test_add_func ("/colord/mapping-db{alter}", cd_mapping_db_alter_func);
556 g_test_add_func ("/colord/mapping-db{convert}", cd_mapping_db_convert_func);
557 g_test_add_func ("/colord/mapping-db", cd_mapping_db_func);
558 g_test_add_func ("/colord/device-db", cd_device_db_func);
559 g_test_add_func ("/colord/profile", colord_profile_func);
560 g_test_add_func ("/colord/profile-db", cd_profile_db_func);
561 g_test_add_func ("/colord/device", colord_device_func);
562 g_test_add_func ("/colord/device-array", colord_device_array_func);
563 return g_test_run ();
564 }
565