summaryrefslogtreecommitdiff
path: root/tests/gvdb/gvdb-test.c
blob: 14f8da2d3c342aa66b31363fd2b5872b25186675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <glib.h>
#include "gvdb/gvdb-builder.h"
#include "gvdb/gvdb-reader.h"

void
remove_file (const gchar *filename)
{
        GFile *f;
        f = g_file_new_for_path (filename);
        g_assert (g_file_delete (f, NULL, NULL));
}

void
walk_value_cb (G_GNUC_UNUSED const gchar *name,
               G_GNUC_UNUSED gsize name_len,
               G_GNUC_UNUSED GVariant *value,
               gpointer user_data)
{
        gint *counter = (gint *)user_data;
        (*counter) += 1;
}

gboolean
walk_open_cb (G_GNUC_UNUSED const gchar *name,
              G_GNUC_UNUSED gsize name_len,
              G_GNUC_UNUSED gpointer user_data)
{
        return TRUE;
}

void
walk_close_cb (G_GNUC_UNUSED gsize name_len,
               G_GNUC_UNUSED gpointer user_data)
{
}

void 
test_gvdb_walk ()
{
        GHashTable *root_table, *ns_table;
        GvdbItem   *root, *item;
        GvdbTable  *root_level, *ns_level;
        guint32     item_id;
        gchar      *key;
        gint        counter = 0;

        const gchar *DB_FILE = "./test_walk.gvdb";

        root_table = gvdb_hash_table_new (NULL, NULL);

        ns_table = gvdb_hash_table_new (root_table, "namespaces");
        root = gvdb_hash_table_insert (ns_table, "");
        for (item_id = 0; item_id < 3; item_id++) {
                key = g_strdup_printf ("ns%d", item_id);
                item = gvdb_hash_table_insert (ns_table, key);
                gvdb_item_set_parent (item, root);
                gvdb_item_set_value (item, g_variant_new_string ("http://some.cool.ns"));
                g_free (key);
        }

        g_assert (gvdb_table_write_contents (root_table, DB_FILE, FALSE, NULL));

        g_hash_table_unref (ns_table);
        g_hash_table_unref (root_table);


        root_level = gvdb_table_new (DB_FILE, TRUE, NULL);
        ns_level = gvdb_table_get_table (root_level, "namespaces");

        gvdb_table_walk (ns_level, "", walk_open_cb, walk_value_cb, walk_close_cb, &counter);
        g_assert_cmpint (counter, ==, 3);

        gvdb_table_unref (root_level);
        gvdb_table_unref (ns_level);

        remove_file (DB_FILE);
}

void
test_gvdb_nested_keys ()
{
        GHashTable *root_table, *ns_table;
        GvdbItem   *root, *item;
        GvdbTable  *root_level, *ns_level;
        gchar     **keys;
        GVariant   *value;
        guint32     item_id;
        gchar      *key;

        const gchar *DB_FILE = "./test_nested_keys.gvdb";

        root_table = gvdb_hash_table_new (NULL, NULL);

        ns_table = gvdb_hash_table_new (root_table, "namespaces");
        root = gvdb_hash_table_insert (ns_table, "");
        for (item_id = 0; item_id < 3; item_id++) {
                key = g_strdup_printf ("ns%d", item_id);
                item = gvdb_hash_table_insert (ns_table, key);
                gvdb_item_set_parent (item, root);
                gvdb_item_set_value (item, g_variant_new_string ("http://some.cool.ns"));
                g_free (key);
        }

        g_assert (gvdb_table_write_contents (root_table, DB_FILE, FALSE, NULL));

        g_hash_table_unref (ns_table);
        g_hash_table_unref (root_table);

        root_level = gvdb_table_new (DB_FILE, TRUE, NULL);
        g_assert (root_level);

        ns_level = gvdb_table_get_table (root_level, "namespaces");
        g_assert (ns_level);
        
        keys = gvdb_table_list (ns_level, "");
        g_assert (keys);
        g_assert_cmpint (g_strv_length (keys), ==, 3);
        for (item_id = 0; item_id < 3; item_id++) {
                key = g_strdup_printf ("ns%d", item_id);
                g_assert (gvdb_table_has_value (ns_level, key));
                value = gvdb_table_get_raw_value (ns_level, key);
                g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "http://some.cool.ns");
                g_free (key);
        }
        g_strfreev (keys);
        
        gvdb_table_unref (root_level);
        gvdb_table_unref (ns_level);
        remove_file (DB_FILE);
}

void
simple_test (const gchar *filename, gboolean use_byteswap) 
{
        GHashTable *table;
        GvdbTable  *read;
        GVariant   *value;
        GVariant   *expected;

        table = gvdb_hash_table_new (NULL, "level1");
        gvdb_hash_table_insert_string (table, "key1", "here just a flat string");
        g_assert (gvdb_table_write_contents (table, filename, use_byteswap, NULL));
        g_hash_table_unref (table);

        read = gvdb_table_new (filename, TRUE, NULL);
        g_assert (read);
        g_assert (gvdb_table_is_valid (read));

        g_assert (gvdb_table_has_value (read, "key1"));
        value = gvdb_table_get_value (read, "key1");
        expected = g_variant_new_string ("here just a flat string");
        g_assert (g_variant_equal (value, expected));

        g_variant_unref (expected);
        g_variant_unref (value);

        gvdb_table_unref (read);
}

void
test_gvdb_byteswapped ()
{
        const gchar *DB_FILE = "./test_byteswpped.gvdb";

        simple_test (DB_FILE, TRUE);

        remove_file (DB_FILE);
}

void
test_gvdb_flat_strings ()
{

        const gchar *DB_FILE = "./test_flat_strings.gvdb";
        
        simple_test (DB_FILE, FALSE);

        remove_file (DB_FILE);
}

void
test_gvdb_ref_unref ()
{
        GHashTable *table;
        GvdbTable  *read, *read_ref;
        GVariant   *value;
        GVariant   *expected;

        const gchar *DB_FILE = "./test_ref_unref.gvdb";

        /* Create a table */
        table = gvdb_hash_table_new (NULL, "level1");
        gvdb_hash_table_insert_string (table, "key1", "whatever");
        g_assert (gvdb_table_write_contents (table, DB_FILE, FALSE, NULL));
        g_hash_table_unref (table);

        /* Read the table */
        read = gvdb_table_new (DB_FILE, TRUE, NULL);
        g_assert (read && gvdb_table_is_valid (read));

        /* Run the checks with a reference */
        read_ref = gvdb_table_ref (read);
        g_assert (gvdb_table_has_value (read_ref, "key1"));
        value = gvdb_table_get_value (read_ref, "key1");
        expected = g_variant_new_string ("whatever");
        g_assert (g_variant_equal (value, expected));

        g_variant_unref (expected);
        g_variant_unref (value);

        gvdb_table_unref (read);
        gvdb_table_unref (read_ref);

        remove_file (DB_FILE);
}

void
test_gvdb_corrupted_file ()
{
        GError *error = NULL;

        g_file_set_contents ("./test_invalid.gvdb", 
                             "Just a bunch of rubbish to fill a text file and try to open it"
                             "as a gvdb and check the error is correctly reported",
                             -1, NULL);

        gvdb_table_new ("./test_invalid.gvdb", TRUE, &error);
        g_assert (error);

        remove_file ("./test_invalid.gvdb");
}


gint
main (gint argc, gchar **argv) 
{
        g_type_init ();

        g_test_init (&argc, &argv, NULL);

        g_test_add_func ("/gvdb/ref_unref", test_gvdb_ref_unref);
        g_test_add_func ("/gvdb/flat_strings", test_gvdb_flat_strings);
        g_test_add_func ("/gvdb/nested_keys", test_gvdb_nested_keys);
        g_test_add_func ("/gvdb/walk", test_gvdb_walk);
        g_test_add_func ("/gvdb/byteswapped", test_gvdb_byteswapped);
        g_test_add_func ("/gvdb/corrupted_file", test_gvdb_corrupted_file);

        return g_test_run ();
}