tracker-0.16.2/tests/gvdb/gvdb-test.c

No issues found

  1 #include <glib.h>
  2 #include "gvdb/gvdb-builder.h"
  3 #include "gvdb/gvdb-reader.h"
  4 
  5 static void
  6 remove_file (const gchar *filename)
  7 {
  8         GFile *f;
  9         f = g_file_new_for_path (filename);
 10         g_assert (g_file_delete (f, NULL, NULL));
 11 }
 12 
 13 static void
 14 walk_value_cb (G_GNUC_UNUSED const gchar *name,
 15                G_GNUC_UNUSED gsize name_len,
 16                G_GNUC_UNUSED GVariant *value,
 17                gpointer user_data)
 18 {
 19         gint *counter = (gint *)user_data;
 20         (*counter) += 1;
 21 }
 22 
 23 static gboolean
 24 walk_open_cb (G_GNUC_UNUSED const gchar *name,
 25               G_GNUC_UNUSED gsize name_len,
 26               G_GNUC_UNUSED gpointer user_data)
 27 {
 28         return TRUE;
 29 }
 30 
 31 static void
 32 walk_close_cb (G_GNUC_UNUSED gsize name_len,
 33                G_GNUC_UNUSED gpointer user_data)
 34 {
 35 }
 36 
 37 static void
 38 test_gvdb_walk (void)
 39 {
 40         GHashTable *root_table, *ns_table;
 41         GvdbItem   *root, *item;
 42         GvdbTable  *root_level, *ns_level;
 43         guint32     item_id;
 44         gchar      *key;
 45         gint        counter = 0;
 46 
 47         const gchar *DB_FILE = "./test_walk.gvdb";
 48 
 49         root_table = gvdb_hash_table_new (NULL, NULL);
 50 
 51         ns_table = gvdb_hash_table_new (root_table, "namespaces");
 52         root = gvdb_hash_table_insert (ns_table, "");
 53         for (item_id = 0; item_id < 3; item_id++) {
 54                 key = g_strdup_printf ("ns%d", item_id);
 55                 item = gvdb_hash_table_insert (ns_table, key);
 56                 gvdb_item_set_parent (item, root);
 57                 gvdb_item_set_value (item, g_variant_new_string ("http://some.cool.ns"));
 58                 g_free (key);
 59         }
 60 
 61         g_assert (gvdb_table_write_contents (root_table, DB_FILE, FALSE, NULL));
 62 
 63         g_hash_table_unref (ns_table);
 64         g_hash_table_unref (root_table);
 65 
 66 
 67         root_level = gvdb_table_new (DB_FILE, TRUE, NULL);
 68         ns_level = gvdb_table_get_table (root_level, "namespaces");
 69 
 70         gvdb_table_walk (ns_level, "", walk_open_cb, walk_value_cb, walk_close_cb, &counter);
 71         g_assert_cmpint (counter, ==, 3);
 72 
 73         gvdb_table_unref (root_level);
 74         gvdb_table_unref (ns_level);
 75 
 76         remove_file (DB_FILE);
 77 }
 78 
 79 static void
 80 test_gvdb_nested_keys (void)
 81 {
 82         GHashTable *root_table, *ns_table;
 83         GvdbItem   *root, *item;
 84         GvdbTable  *root_level, *ns_level;
 85         gchar     **keys;
 86         GVariant   *value;
 87         guint32     item_id;
 88         gchar      *key;
 89 
 90         const gchar *DB_FILE = "./test_nested_keys.gvdb";
 91 
 92         root_table = gvdb_hash_table_new (NULL, NULL);
 93 
 94         ns_table = gvdb_hash_table_new (root_table, "namespaces");
 95         root = gvdb_hash_table_insert (ns_table, "");
 96         for (item_id = 0; item_id < 3; item_id++) {
 97                 key = g_strdup_printf ("ns%d", item_id);
 98                 item = gvdb_hash_table_insert (ns_table, key);
 99                 gvdb_item_set_parent (item, root);
100                 gvdb_item_set_value (item, g_variant_new_string ("http://some.cool.ns"));
101                 g_free (key);
102         }
103 
104         g_assert (gvdb_table_write_contents (root_table, DB_FILE, FALSE, NULL));
105 
106         g_hash_table_unref (ns_table);
107         g_hash_table_unref (root_table);
108 
109         root_level = gvdb_table_new (DB_FILE, TRUE, NULL);
110         g_assert (root_level);
111 
112         ns_level = gvdb_table_get_table (root_level, "namespaces");
113         g_assert (ns_level);
114         
115         keys = gvdb_table_list (ns_level, "");
116         g_assert (keys);
117         g_assert_cmpint (g_strv_length (keys), ==, 3);
118         for (item_id = 0; item_id < 3; item_id++) {
119                 key = g_strdup_printf ("ns%d", item_id);
120                 g_assert (gvdb_table_has_value (ns_level, key));
121                 value = gvdb_table_get_raw_value (ns_level, key);
122                 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "http://some.cool.ns");
123                 g_free (key);
124         }
125         g_strfreev (keys);
126         
127         gvdb_table_unref (root_level);
128         gvdb_table_unref (ns_level);
129         remove_file (DB_FILE);
130 }
131 
132 static void
133 simple_test (const gchar *filename, gboolean use_byteswap) 
134 {
135         GHashTable *table;
136         GvdbTable  *read;
137         GVariant   *value;
138         GVariant   *expected;
139 
140         table = gvdb_hash_table_new (NULL, "level1");
141         gvdb_hash_table_insert_string (table, "key1", "here just a flat string");
142         g_assert (gvdb_table_write_contents (table, filename, use_byteswap, NULL));
143         g_hash_table_unref (table);
144 
145         read = gvdb_table_new (filename, TRUE, NULL);
146         g_assert (read);
147         g_assert (gvdb_table_is_valid (read));
148 
149         g_assert (gvdb_table_has_value (read, "key1"));
150         value = gvdb_table_get_value (read, "key1");
151         expected = g_variant_new_string ("here just a flat string");
152         g_assert (g_variant_equal (value, expected));
153 
154         g_variant_unref (expected);
155         g_variant_unref (value);
156 
157         gvdb_table_unref (read);
158 }
159 
160 static void
161 test_gvdb_byteswapped (void)
162 {
163         const gchar *DB_FILE = "./test_byteswpped.gvdb";
164 
165         simple_test (DB_FILE, TRUE);
166 
167         remove_file (DB_FILE);
168 }
169 
170 static void
171 test_gvdb_flat_strings (void)
172 {
173 
174         const gchar *DB_FILE = "./test_flat_strings.gvdb";
175         
176         simple_test (DB_FILE, FALSE);
177 
178         remove_file (DB_FILE);
179 }
180 
181 static void
182 test_gvdb_ref_unref (void)
183 {
184         GHashTable *table;
185         GvdbTable  *read, *read_ref;
186         GVariant   *value;
187         GVariant   *expected;
188 
189         const gchar *DB_FILE = "./test_ref_unref.gvdb";
190 
191         /* Create a table */
192         table = gvdb_hash_table_new (NULL, "level1");
193         gvdb_hash_table_insert_string (table, "key1", "whatever");
194         g_assert (gvdb_table_write_contents (table, DB_FILE, FALSE, NULL));
195         g_hash_table_unref (table);
196 
197         /* Read the table */
198         read = gvdb_table_new (DB_FILE, TRUE, NULL);
199         g_assert (read && gvdb_table_is_valid (read));
200 
201         /* Run the checks with a reference */
202         read_ref = gvdb_table_ref (read);
203         g_assert (gvdb_table_has_value (read_ref, "key1"));
204         value = gvdb_table_get_value (read_ref, "key1");
205         expected = g_variant_new_string ("whatever");
206         g_assert (g_variant_equal (value, expected));
207 
208         g_variant_unref (expected);
209         g_variant_unref (value);
210 
211         gvdb_table_unref (read);
212         gvdb_table_unref (read_ref);
213 
214         remove_file (DB_FILE);
215 }
216 
217 static void
218 test_gvdb_corrupted_file (void)
219 {
220         GError *error = NULL;
221 
222         g_file_set_contents ("./test_invalid.gvdb", 
223                              "Just a bunch of rubbish to fill a text file and try to open it"
224                              "as a gvdb and check the error is correctly reported",
225                              -1, NULL);
226 
227         gvdb_table_new ("./test_invalid.gvdb", TRUE, &error);
228         g_assert (error);
229 
230         remove_file ("./test_invalid.gvdb");
231 }
232 
233 
234 gint
235 main (gint argc, gchar **argv) 
236 {
237         g_test_init (&argc, &argv, NULL);
238 
239         g_test_add_func ("/gvdb/ref_unref", test_gvdb_ref_unref);
240         g_test_add_func ("/gvdb/flat_strings", test_gvdb_flat_strings);
241         g_test_add_func ("/gvdb/nested_keys", test_gvdb_nested_keys);
242         g_test_add_func ("/gvdb/walk", test_gvdb_walk);
243         g_test_add_func ("/gvdb/byteswapped", test_gvdb_byteswapped);
244         g_test_add_func ("/gvdb/corrupted_file", test_gvdb_corrupted_file);
245 
246         return g_test_run ();
247 }