LCOV - code coverage report
Current view: top level - pkcs11/gnome2-store - test-gnome2-file.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 329 329
Test Date: 2024-05-07 18:02:03 Functions: 100.0 % 28 28

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

Generated by: LCOV version 2.0-1