| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* unit-test-file-store.c: Test file store functionality |
| 3 |
|
| 4 |
Copyright (C) 2008 Stefan Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "gnome2-store/gkm-gnome2-file.h" |
| 26 |
|
| 27 |
#include "gkm/gkm-object.h" |
| 28 |
|
| 29 |
#include "egg/egg-libgcrypt.h" |
| 30 |
#include "egg/egg-secure-memory.h" |
| 31 |
|
| 32 |
#include <glib/gstdio.h> |
| 33 |
|
| 34 |
#include <fcntl.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <unistd.h> |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
GkmGnome2File *data_file; |
| 41 |
gchar *public_filename; |
| 42 |
gchar *private_filename; |
| 43 |
gchar *write_filename; |
| 44 |
int write_fd; |
| 45 |
int public_fd; |
| 46 |
int private_fd; |
| 47 |
GkmSecret *login; |
| 48 |
} Test; |
| 49 |
|
| 50 |
EGG_SECURE_DEFINE_GLIB_GLOBALS (); |
| 51 |
|
| 52 |
static void |
| 53 |
setup (Test *test, gconstpointer unused) |
| 54 |
{ |
| 55 |
test->public_filename = g_build_filename (SRCDIR "/pkcs11/gnome2-store/fixtures", "data-file-public.store", NULL); |
| 56 |
test->private_filename = g_build_filename (SRCDIR "/pkcs11/gnome2-store/fixtures", "data-file-private.store", NULL); |
| 57 |
test->write_filename = g_build_filename ("/tmp", "unit-test-file.store", NULL); |
| 58 |
|
| 59 |
test->data_file = gkm_gnome2_file_new (); |
| 60 |
|
| 61 |
test->public_fd = g_open (test->public_filename, O_RDONLY, 0); |
| 62 |
test->private_fd = g_open (test->private_filename, O_RDONLY, 0); |
| 63 |
test->write_fd = g_open (test->write_filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 64 |
g_assert (test->write_fd != -1); |
| 65 |
|
| 66 |
test->login = gkm_secret_new ((guchar*)"booo", 4); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
teardown (Test *test, gconstpointer unused) |
| 71 |
{ |
| 72 |
g_free (test->public_filename); |
| 73 |
g_free (test->private_filename); |
| 74 |
g_free (test->write_filename); |
| 75 |
|
| 76 |
g_object_unref (test->data_file); |
| 77 |
|
| 78 |
if (test->public_fd != -1) |
| 79 |
close (test->public_fd); |
| 80 |
if (test->private_fd != -1) |
| 81 |
close (test->private_fd); |
| 82 |
if (test->write_fd != -1) |
| 83 |
close (test->write_fd); |
| 84 |
test->public_fd = test->private_fd = test->write_fd = -1; |
| 85 |
|
| 86 |
g_object_unref (test->login); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
test_file_create (Test *test, gconstpointer unused) |
| 91 |
{ |
| 92 |
GkmDataResult res; |
| 93 |
|
| 94 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 95 |
g_assert (res == GKM_DATA_SUCCESS); |
| 96 |
|
| 97 |
/* Should be able to create private in a new file */ |
| 98 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 99 |
g_assert (res == GKM_DATA_SUCCESS); |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
test_file_write_value (Test *test, gconstpointer unused) |
| 104 |
{ |
| 105 |
GkmDataResult res; |
| 106 |
|
| 107 |
/* Can't write when no identifier present */ |
| 108 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); |
| 109 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 110 |
|
| 111 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 112 |
g_assert (res == GKM_DATA_SUCCESS); |
| 113 |
|
| 114 |
/* Should be able to write now */ |
| 115 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); |
| 116 |
g_assert (res == GKM_DATA_SUCCESS); |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
test_file_read_value (Test *test, gconstpointer unused) |
| 121 |
{ |
| 122 |
gconstpointer value = NULL; |
| 123 |
GkmDataResult res; |
| 124 |
gsize n_value; |
| 125 |
guint number = 7778; |
| 126 |
|
| 127 |
/* Write some stuff in */ |
| 128 |
res = gkm_gnome2_file_create_entry (test->data_file, "ident", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 129 |
g_assert (res == GKM_DATA_SUCCESS); |
| 130 |
res = gkm_gnome2_file_write_value (test->data_file, "ident", CKA_LABEL, "TWO-label", 10); |
| 131 |
g_assert (res == GKM_DATA_SUCCESS); |
| 132 |
res = gkm_gnome2_file_write_value (test->data_file, "ident", CKA_VALUE, &number, sizeof (number)); |
| 133 |
g_assert (res == GKM_DATA_SUCCESS); |
| 134 |
|
| 135 |
/* Read for an invalid item */ |
| 136 |
res = gkm_gnome2_file_read_value (test->data_file, "non-existant", CKA_LABEL, &value, &n_value); |
| 137 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 138 |
|
| 139 |
/* Read for an invalid attribute */ |
| 140 |
res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_ID, &value, &n_value); |
| 141 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 142 |
|
| 143 |
/* Read out a valid number */ |
| 144 |
res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_VALUE, &value, &n_value); |
| 145 |
g_assert (res == GKM_DATA_SUCCESS); |
| 146 |
g_assert (value); |
| 147 |
g_assert (n_value == sizeof (number)); |
| 148 |
g_assert_cmpuint (*((guint*)value), ==, number); |
| 149 |
|
| 150 |
/* Read out the valid string */ |
| 151 |
res = gkm_gnome2_file_read_value (test->data_file, "ident", CKA_LABEL, &value, &n_value); |
| 152 |
g_assert (res == GKM_DATA_SUCCESS); |
| 153 |
g_assert (value); |
| 154 |
g_assert (n_value == 10); |
| 155 |
g_assert_cmpstr ((const gchar*)value, ==, "TWO-label"); |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
test_file_read (Test *test, gconstpointer unused) |
| 160 |
{ |
| 161 |
GkmDataResult res; |
| 162 |
|
| 163 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 164 |
g_assert (res == GKM_DATA_SUCCESS); |
| 165 |
} |
| 166 |
|
| 167 |
static void |
| 168 |
test_file_lookup (Test *test, gconstpointer unused) |
| 169 |
{ |
| 170 |
GkmDataResult res; |
| 171 |
guint section; |
| 172 |
gboolean ret; |
| 173 |
|
| 174 |
/* Invalid shouldn't succeed */ |
| 175 |
ret = gkm_gnome2_file_lookup_entry (test->data_file, "non-existant", §ion); |
| 176 |
g_assert (ret == FALSE); |
| 177 |
|
| 178 |
/* Create a test item */ |
| 179 |
res = gkm_gnome2_file_create_entry (test->data_file, "test-ident", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 180 |
g_assert (res == GKM_DATA_SUCCESS); |
| 181 |
|
| 182 |
ret = gkm_gnome2_file_lookup_entry (test->data_file, "test-ident", §ion); |
| 183 |
g_assert (ret == TRUE); |
| 184 |
g_assert (section == GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 185 |
|
| 186 |
/* Should be able to call without asking for section */ |
| 187 |
ret = gkm_gnome2_file_lookup_entry (test->data_file, "test-ident", NULL); |
| 188 |
g_assert (ret == TRUE); |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
file_read_private_without_login (Test *test, gconstpointer unused) |
| 193 |
{ |
| 194 |
GkmDataResult res; |
| 195 |
guint section; |
| 196 |
gconstpointer value; |
| 197 |
gsize n_value; |
| 198 |
gboolean ret; |
| 199 |
|
| 200 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); |
| 201 |
g_assert (res == GKM_DATA_SUCCESS); |
| 202 |
|
| 203 |
/* Items from the private section should exist */ |
| 204 |
ret = gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", §ion); |
| 205 |
g_assert (ret); |
| 206 |
g_assert (section == GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 207 |
|
| 208 |
/* But we shouldn't be able to read values from those private items */ |
| 209 |
ret = gkm_gnome2_file_read_value (test->data_file, "identifier-private", CKA_LABEL, &value, &n_value); |
| 210 |
g_assert (ret == GKM_DATA_LOCKED); |
| 211 |
|
| 212 |
/* Shouldn't be able to create private items */ |
| 213 |
res = gkm_gnome2_file_create_entry (test->data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 214 |
g_assert (res == GKM_DATA_LOCKED); |
| 215 |
|
| 216 |
/* Shouldn't be able to write with another test->login */ |
| 217 |
res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, test->login); |
| 218 |
g_assert (res == GKM_DATA_LOCKED); |
| 219 |
|
| 220 |
/* Now load a public file without private bits*/ |
| 221 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 222 |
g_assert (res == GKM_DATA_SUCCESS); |
| 223 |
|
| 224 |
/* Now we should be able to load private stuff */ |
| 225 |
res = gkm_gnome2_file_create_entry (test->data_file, "dummy-private", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 226 |
g_assert (res == GKM_DATA_SUCCESS); |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
test_file_write (Test *test, gconstpointer unused) |
| 231 |
{ |
| 232 |
GkmDataResult res; |
| 233 |
|
| 234 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 235 |
g_assert (res == GKM_DATA_SUCCESS); |
| 236 |
|
| 237 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); |
| 238 |
g_assert (res == GKM_DATA_SUCCESS); |
| 239 |
|
| 240 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 241 |
g_assert (res == GKM_DATA_SUCCESS); |
| 242 |
|
| 243 |
res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, NULL); |
| 244 |
g_assert (res == GKM_DATA_SUCCESS); |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
test_cant_write_private_without_login (Test *test, gconstpointer unused) |
| 249 |
{ |
| 250 |
GkmDataResult res; |
| 251 |
|
| 252 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier_private", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 253 |
g_assert (res == GKM_DATA_SUCCESS); |
| 254 |
|
| 255 |
res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, NULL); |
| 256 |
g_assert (res == GKM_DATA_LOCKED); |
| 257 |
} |
| 258 |
|
| 259 |
static void |
| 260 |
test_write_private_with_login (Test *test, gconstpointer unused) |
| 261 |
{ |
| 262 |
GkmDataResult res; |
| 263 |
gulong value; |
| 264 |
|
| 265 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 266 |
g_assert (res == GKM_DATA_SUCCESS); |
| 267 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "public-label", 12); |
| 268 |
g_assert (res == GKM_DATA_SUCCESS); |
| 269 |
|
| 270 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-two", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 271 |
g_assert (res == GKM_DATA_SUCCESS); |
| 272 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-two", CKA_LABEL, "TWO-label", 9); |
| 273 |
g_assert (res == GKM_DATA_SUCCESS); |
| 274 |
value = 555; |
| 275 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-two", CKA_VALUE, &value, sizeof (value)); |
| 276 |
g_assert (res == GKM_DATA_SUCCESS); |
| 277 |
|
| 278 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-private", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 279 |
g_assert (res == GKM_DATA_SUCCESS); |
| 280 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-private", CKA_LABEL, "private-label", 13); |
| 281 |
g_assert (res == GKM_DATA_SUCCESS); |
| 282 |
|
| 283 |
res = gkm_gnome2_file_write_fd (test->data_file, test->write_fd, test->login); |
| 284 |
g_assert (res == GKM_DATA_SUCCESS); |
| 285 |
} |
| 286 |
|
| 287 |
static void |
| 288 |
test_read_private_with_login (Test *test, gconstpointer unused) |
| 289 |
{ |
| 290 |
GkmDataResult res; |
| 291 |
gconstpointer value; |
| 292 |
gsize n_value; |
| 293 |
|
| 294 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); |
| 295 |
g_assert (res == GKM_DATA_SUCCESS); |
| 296 |
|
| 297 |
/* Should be able to read private items */ |
| 298 |
res = gkm_gnome2_file_read_value (test->data_file, "identifier-private", CKA_LABEL, &value, &n_value); |
| 299 |
g_assert (res == GKM_DATA_SUCCESS); |
| 300 |
g_assert_cmpuint (n_value, ==, 13); |
| 301 |
g_assert (memcmp (value, "private-label", 13) == 0); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
test_destroy_entry (Test *test, gconstpointer unused) |
| 306 |
{ |
| 307 |
GkmDataResult res; |
| 308 |
|
| 309 |
res = gkm_gnome2_file_destroy_entry (test->data_file, "non-existant"); |
| 310 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 311 |
|
| 312 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 313 |
g_assert (res == GKM_DATA_SUCCESS); |
| 314 |
|
| 315 |
/* Make sure it's here */ |
| 316 |
g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-public", NULL)); |
| 317 |
|
| 318 |
res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-public"); |
| 319 |
g_assert (res == GKM_DATA_SUCCESS); |
| 320 |
|
| 321 |
/* Make sure it's gone */ |
| 322 |
g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "identifier-public", NULL)); |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
test_destroy_entry_by_loading (Test *test, gconstpointer unused) |
| 327 |
{ |
| 328 |
GkmDataResult res; |
| 329 |
|
| 330 |
/* Create some extra idenifiers */ |
| 331 |
res = gkm_gnome2_file_create_entry (test->data_file, "my-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 332 |
g_assert (res == GKM_DATA_SUCCESS); |
| 333 |
res = gkm_gnome2_file_create_entry (test->data_file, "my-private", GKM_GNOME2_FILE_SECTION_PRIVATE); |
| 334 |
g_assert (res == GKM_DATA_SUCCESS); |
| 335 |
|
| 336 |
/* Now read from the file */ |
| 337 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 338 |
g_assert (res == GKM_DATA_SUCCESS); |
| 339 |
|
| 340 |
/* Both should be gone */ |
| 341 |
g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "my-public", NULL)); |
| 342 |
g_assert (!gkm_gnome2_file_lookup_entry (test->data_file, "my-private", NULL)); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static void |
| 347 |
test_destroy_private_without_login (Test *test, gconstpointer unused) |
| 348 |
{ |
| 349 |
GkmDataResult res; |
| 350 |
|
| 351 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); |
| 352 |
g_assert (res == GKM_DATA_SUCCESS); |
| 353 |
|
| 354 |
/* Make sure it's here */ |
| 355 |
g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", NULL)); |
| 356 |
|
| 357 |
/* Shouldn't be able to destroy */ |
| 358 |
res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-private"); |
| 359 |
g_assert (res == GKM_DATA_LOCKED); |
| 360 |
|
| 361 |
/* Make sure it's still here */ |
| 362 |
g_assert (gkm_gnome2_file_lookup_entry (test->data_file, "identifier-private", NULL)); |
| 363 |
} |
| 364 |
|
| 365 |
static void |
| 366 |
entry_added_one (GkmGnome2File *df, const gchar *identifier, gboolean *added) |
| 367 |
{ |
| 368 |
g_assert (GKM_IS_GNOME2_FILE (df)); |
| 369 |
g_assert (identifier); |
| 370 |
g_assert (added); |
| 371 |
|
| 372 |
/* Should only be called once */ |
| 373 |
g_assert (!*added); |
| 374 |
*added = TRUE; |
| 375 |
} |
| 376 |
|
| 377 |
static void |
| 378 |
test_entry_added_signal (Test *test, gconstpointer unused) |
| 379 |
{ |
| 380 |
GkmDataResult res; |
| 381 |
gboolean added; |
| 382 |
|
| 383 |
g_signal_connect (test->data_file, "entry-added", G_CALLBACK (entry_added_one), &added); |
| 384 |
|
| 385 |
/* Should fire the signal */ |
| 386 |
added = FALSE; |
| 387 |
res = gkm_gnome2_file_create_entry (test->data_file, "identifier-public", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 388 |
g_assert (res == GKM_DATA_SUCCESS); |
| 389 |
g_assert (added == TRUE); |
| 390 |
|
| 391 |
/* Another one should be added when we load */ |
| 392 |
added = FALSE; |
| 393 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 394 |
g_assert (res == GKM_DATA_SUCCESS); |
| 395 |
g_assert (added == TRUE); |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
entry_changed_one (GkmGnome2File *df, const gchar *identifier, gulong type, gboolean *changed) |
| 400 |
{ |
| 401 |
g_assert (GKM_IS_GNOME2_FILE (df)); |
| 402 |
g_assert (identifier); |
| 403 |
g_assert (changed); |
| 404 |
g_assert (type == CKA_LABEL); |
| 405 |
|
| 406 |
/* Should only be called once */ |
| 407 |
g_assert (!*changed); |
| 408 |
*changed = TRUE; |
| 409 |
} |
| 410 |
|
| 411 |
static void |
| 412 |
test_entry_changed_signal (Test *test, gconstpointer unused) |
| 413 |
{ |
| 414 |
GkmDataResult res; |
| 415 |
gboolean changed; |
| 416 |
|
| 417 |
g_signal_connect (test->data_file, "entry-changed", G_CALLBACK (entry_changed_one), &changed); |
| 418 |
|
| 419 |
/* Loading shouldn't fire the signal */ |
| 420 |
changed = FALSE; |
| 421 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 422 |
g_assert (res == GKM_DATA_SUCCESS); |
| 423 |
g_assert (changed == FALSE); |
| 424 |
|
| 425 |
/* Shouldn't fire the signal on nonexistant */ |
| 426 |
changed = FALSE; |
| 427 |
res = gkm_gnome2_file_write_value (test->data_file, "non-existant", CKA_LABEL, "new-value", 10); |
| 428 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 429 |
g_assert (changed == FALSE); |
| 430 |
|
| 431 |
/* Should fire the signal */ |
| 432 |
changed = FALSE; |
| 433 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "new-value", 10); |
| 434 |
g_assert (res == GKM_DATA_SUCCESS); |
| 435 |
g_assert (changed == TRUE); |
| 436 |
|
| 437 |
/* Shouldn't fire the signal, same value again */ |
| 438 |
changed = FALSE; |
| 439 |
res = gkm_gnome2_file_write_value (test->data_file, "identifier-public", CKA_LABEL, "new-value", 10); |
| 440 |
g_assert (res == GKM_DATA_SUCCESS); |
| 441 |
g_assert (changed == FALSE); |
| 442 |
|
| 443 |
/* Reload file, should revert, fire signal */ |
| 444 |
changed = FALSE; |
| 445 |
g_assert (lseek (test->public_fd, 0, SEEK_SET) != -1); |
| 446 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 447 |
g_assert (res == GKM_DATA_SUCCESS); |
| 448 |
g_assert (changed == TRUE); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
entry_removed_one (GkmGnome2File *df, const gchar *identifier, gboolean *removed) |
| 453 |
{ |
| 454 |
g_assert (GKM_IS_GNOME2_FILE (df)); |
| 455 |
g_assert (identifier); |
| 456 |
g_assert (removed); |
| 457 |
|
| 458 |
/* Should only be called once */ |
| 459 |
g_assert (!*removed); |
| 460 |
*removed = TRUE; |
| 461 |
} |
| 462 |
|
| 463 |
static void |
| 464 |
test_entry_removed_signal (Test *test, gconstpointer unused) |
| 465 |
{ |
| 466 |
GkmDataResult res; |
| 467 |
gboolean removed; |
| 468 |
|
| 469 |
g_signal_connect (test->data_file, "entry-removed", G_CALLBACK (entry_removed_one), &removed); |
| 470 |
|
| 471 |
/* Loading shouldn't fire the signal */ |
| 472 |
removed = FALSE; |
| 473 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 474 |
g_assert (res == GKM_DATA_SUCCESS); |
| 475 |
g_assert (removed == FALSE); |
| 476 |
|
| 477 |
/* Shouldn't fire the signal on removing nonexistant */ |
| 478 |
removed = FALSE; |
| 479 |
res = gkm_gnome2_file_destroy_entry (test->data_file, "non-existant"); |
| 480 |
g_assert (res == GKM_DATA_UNRECOGNIZED); |
| 481 |
g_assert (removed == FALSE); |
| 482 |
|
| 483 |
/* Remove a real entry */ |
| 484 |
removed = FALSE; |
| 485 |
res = gkm_gnome2_file_destroy_entry (test->data_file, "identifier-public"); |
| 486 |
g_assert (res == GKM_DATA_SUCCESS); |
| 487 |
g_assert (removed == TRUE); |
| 488 |
|
| 489 |
/* Add a dummy entry */ |
| 490 |
res = gkm_gnome2_file_create_entry (test->data_file, "extra-dummy", GKM_GNOME2_FILE_SECTION_PUBLIC); |
| 491 |
g_assert (res == GKM_DATA_SUCCESS); |
| 492 |
|
| 493 |
/* That one should go away when we reload, fire signal */ |
| 494 |
removed = FALSE; |
| 495 |
g_assert (lseek (test->public_fd, 0, SEEK_SET) != -1); |
| 496 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 497 |
g_assert (res == GKM_DATA_SUCCESS); |
| 498 |
g_assert (removed == TRUE); |
| 499 |
} |
| 500 |
|
| 501 |
static void |
| 502 |
foreach_entry (GkmGnome2File *df, const gchar *identifier, gpointer data) |
| 503 |
{ |
| 504 |
GPtrArray *array = data; |
| 505 |
const gchar *ident; |
| 506 |
int i; |
| 507 |
|
| 508 |
g_assert (data); |
| 509 |
g_assert (identifier); |
| 510 |
g_assert (GKM_IS_GNOME2_FILE (df)); |
| 511 |
|
| 512 |
/* Check that this is unique */ |
| 513 |
for (i = 0; i < array->len; ++i) { |
| 514 |
ident = g_ptr_array_index (array, i); |
| 515 |
g_assert (ident); |
| 516 |
g_assert_cmpstr (ident, !=, identifier); |
| 517 |
} |
| 518 |
|
| 519 |
/* Add it */ |
| 520 |
g_ptr_array_add (array, g_strdup (identifier)); |
| 521 |
} |
| 522 |
|
| 523 |
static void |
| 524 |
test_data_file_foreach (Test *test, gconstpointer unused) |
| 525 |
{ |
| 526 |
GkmDataResult res; |
| 527 |
GPtrArray *array; |
| 528 |
|
| 529 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); |
| 530 |
g_assert (res == GKM_DATA_SUCCESS); |
| 531 |
|
| 532 |
array = g_ptr_array_new (); |
| 533 |
gkm_gnome2_file_foreach_entry (test->data_file, foreach_entry, array); |
| 534 |
g_assert (array->len == 4); |
| 535 |
|
| 536 |
g_ptr_array_add (array, NULL); |
| 537 |
g_strfreev ((gchar**)g_ptr_array_free (array, FALSE)); |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
test_unique_entry (Test *test, gconstpointer unused) |
| 542 |
{ |
| 543 |
GkmDataResult res; |
| 544 |
gchar *identifier; |
| 545 |
|
| 546 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 547 |
g_assert (res == GKM_DATA_SUCCESS); |
| 548 |
|
| 549 |
/* Should change an identifier that conflicts */ |
| 550 |
identifier = g_strdup ("identifier-public"); |
| 551 |
res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); |
| 552 |
g_assert (res == GKM_DATA_SUCCESS); |
| 553 |
g_assert_cmpstr (identifier, !=, "identifier-public"); |
| 554 |
g_free (identifier); |
| 555 |
|
| 556 |
/* Shouldn't change a unique identifier */ |
| 557 |
identifier = g_strdup ("identifier-unique"); |
| 558 |
res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); |
| 559 |
g_assert (res == GKM_DATA_SUCCESS); |
| 560 |
g_assert_cmpstr (identifier, ==, "identifier-unique"); |
| 561 |
g_free (identifier); |
| 562 |
|
| 563 |
/* Should be able to get from NULL */ |
| 564 |
identifier = NULL; |
| 565 |
res = gkm_gnome2_file_unique_entry (test->data_file, &identifier); |
| 566 |
g_assert (res == GKM_DATA_SUCCESS); |
| 567 |
g_assert (identifier != NULL); |
| 568 |
g_assert (identifier[0] != 0); |
| 569 |
g_free (identifier); |
| 570 |
} |
| 571 |
|
| 572 |
static void |
| 573 |
test_have_sections (Test *test, gconstpointer unused) |
| 574 |
{ |
| 575 |
GkmDataResult res; |
| 576 |
|
| 577 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, NULL); |
| 578 |
g_assert (res == GKM_DATA_SUCCESS); |
| 579 |
|
| 580 |
/* No private section */ |
| 581 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); |
| 582 |
g_assert (!gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); |
| 583 |
|
| 584 |
/* Read private stuff into file, without test->login */ |
| 585 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, NULL); |
| 586 |
g_assert (res == GKM_DATA_SUCCESS); |
| 587 |
|
| 588 |
/* Should have a private section even without test->login */ |
| 589 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); |
| 590 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); |
| 591 |
|
| 592 |
/* Read private stuff into file, with test->login */ |
| 593 |
g_assert (lseek (test->private_fd, 0, SEEK_SET) == 0); |
| 594 |
res = gkm_gnome2_file_read_fd (test->data_file, test->private_fd, test->login); |
| 595 |
g_assert (res == GKM_DATA_SUCCESS); |
| 596 |
|
| 597 |
/* Should have a private section now with test->login */ |
| 598 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); |
| 599 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); |
| 600 |
|
| 601 |
/* Read public stuff back into file*/ |
| 602 |
g_assert (lseek (test->public_fd, 0, SEEK_SET) == 0); |
| 603 |
res = gkm_gnome2_file_read_fd (test->data_file, test->public_fd, test->login); |
| 604 |
g_assert (res == GKM_DATA_SUCCESS); |
| 605 |
|
| 606 |
/* Shouldn't have a private section now */ |
| 607 |
g_assert (gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PUBLIC)); |
| 608 |
g_assert (!gkm_gnome2_file_have_section (test->data_file, GKM_GNOME2_FILE_SECTION_PRIVATE)); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
int |
| 613 |
main (int argc, char **argv) |
| 614 |
{ |
| 615 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 616 |
g_type_init (); |
| 617 |
#endif |
| 618 |
g_test_init (&argc, &argv, NULL); |
| 619 |
|
| 620 |
egg_libgcrypt_initialize (); |
| 621 |
|
| 622 |
g_test_add ("/gnome2-store/gnome2-file/file_create", Test, NULL, setup, test_file_create, teardown); |
| 623 |
g_test_add ("/gnome2-store/gnome2-file/file_write_value", Test, NULL, setup, test_file_write_value, teardown); |
| 624 |
g_test_add ("/gnome2-store/gnome2-file/file_read_value", Test, NULL, setup, test_file_read_value, teardown); |
| 625 |
g_test_add ("/gnome2-store/gnome2-file/file_read", Test, NULL, setup, test_file_read, teardown); |
| 626 |
g_test_add ("/gnome2-store/gnome2-file/file_lookup", Test, NULL, setup, test_file_lookup, teardown); |
| 627 |
g_test_add ("/gnome2-store/gnome2-file/file_read_private_without_login", Test, NULL, setup, file_read_private_without_login, teardown); |
| 628 |
g_test_add ("/gnome2-store/gnome2-file/file_write", Test, NULL, setup, test_file_write, teardown); |
| 629 |
g_test_add ("/gnome2-store/gnome2-file/cant_write_private_without_login", Test, NULL, setup, test_cant_write_private_without_login, teardown); |
| 630 |
g_test_add ("/gnome2-store/gnome2-file/write_private_with_login", Test, NULL, setup, test_write_private_with_login, teardown); |
| 631 |
g_test_add ("/gnome2-store/gnome2-file/read_private_with_login", Test, NULL, setup, test_read_private_with_login, teardown); |
| 632 |
g_test_add ("/gnome2-store/gnome2-file/destroy_entry", Test, NULL, setup, test_destroy_entry, teardown); |
| 633 |
g_test_add ("/gnome2-store/gnome2-file/destroy_entry_by_loading", Test, NULL, setup, test_destroy_entry_by_loading, teardown); |
| 634 |
g_test_add ("/gnome2-store/gnome2-file/destroy_private_without_login", Test, NULL, setup, test_destroy_private_without_login, teardown); |
| 635 |
g_test_add ("/gnome2-store/gnome2-file/entry_added_signal", Test, NULL, setup, test_entry_added_signal, teardown); |
| 636 |
g_test_add ("/gnome2-store/gnome2-file/entry_changed_signal", Test, NULL, setup, test_entry_changed_signal, teardown); |
| 637 |
g_test_add ("/gnome2-store/gnome2-file/entry_removed_signal", Test, NULL, setup, test_entry_removed_signal, teardown); |
| 638 |
g_test_add ("/gnome2-store/gnome2-file/data_file_foreach", Test, NULL, setup, test_data_file_foreach, teardown); |
| 639 |
g_test_add ("/gnome2-store/gnome2-file/unique_entry", Test, NULL, setup, test_unique_entry, teardown); |
| 640 |
g_test_add ("/gnome2-store/gnome2-file/have_sections", Test, NULL, setup, test_have_sections, teardown); |
| 641 |
|
| 642 |
return g_test_run (); |
| 643 |
} |