tracker-0.16.2/tests/libtracker-data/tracker-db-journal.c

No issues found

  1 /*
  2  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include <config.h>
 21 
 22 #include <glib/gstdio.h>
 23 
 24 #include <libtracker-common/tracker-crc32.h>
 25 
 26 #include <libtracker-data/tracker-db-journal.h>
 27 
 28 #ifndef DISABLE_JOURNAL
 29 
 30 static void
 31 test_init_and_shutdown (void)
 32 {
 33 	GError *error = NULL;
 34 	gboolean result;
 35 
 36 	/* check double init/shutdown */
 37 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 38 	result = tracker_db_journal_init (NULL, FALSE, &error);
 39 	g_assert_no_error (error);
 40 	g_assert (result == TRUE);
 41 
 42 	result = tracker_db_journal_shutdown (&error);
 43 	g_assert_no_error (error);
 44 	g_assert (result == TRUE);
 45 
 46 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 47 	result = tracker_db_journal_init (NULL, FALSE, &error);
 48 	g_assert_no_error (error);
 49 	g_assert (result == TRUE);
 50 
 51 	result = tracker_db_journal_shutdown (&error);
 52 	g_assert_no_error (error);
 53 	g_assert (result == TRUE);
 54 }
 55 
 56 static void
 57 test_write_functions (void)
 58 {
 59 	gchar *path;
 60 	const gchar *filename;
 61 	gsize initial_size, actual_size;
 62 	gboolean result;
 63 	GError *error = NULL;
 64 
 65 	path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
 66 	g_unlink (path);
 67 
 68 	tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
 69 	tracker_db_journal_init (path, FALSE, &error);
 70 	g_assert_no_error (error);
 71 
 72 	filename = tracker_db_journal_get_filename ();
 73 	g_assert (filename != NULL);
 74 	g_assert_cmpstr (filename, ==, path);
 75 
 76 	/* Size is 8 due to header */
 77 	actual_size = tracker_db_journal_get_size ();
 78 	g_assert_cmpint (actual_size, ==, 8);
 79 
 80 	/* Check with rollback, nothing is added */
 81 	initial_size = tracker_db_journal_get_size ();
 82 	result = tracker_db_journal_start_transaction (time (NULL));
 83 	g_assert_cmpint (result, ==, TRUE);
 84 	result = tracker_db_journal_append_resource (10, "http://resource");
 85 	g_assert_cmpint (result, ==, TRUE);
 86 	result = tracker_db_journal_append_resource (11, "http://predicate");
 87 	g_assert_cmpint (result, ==, TRUE);
 88 	result = tracker_db_journal_append_delete_statement (0, 10, 11, "test");
 89 	g_assert_cmpint (result, ==, TRUE);
 90 	result = tracker_db_journal_rollback_transaction (&error);
 91 	g_assert_no_error (error);
 92 	g_assert_cmpint (result, ==, TRUE);
 93 	actual_size = tracker_db_journal_get_size ();
 94 	g_assert_cmpint (initial_size, ==, actual_size);
 95 
 96 	/* Check with commit, somethign is added */
 97 	result = tracker_db_journal_start_transaction (time (NULL));
 98 	g_assert_cmpint (result, ==, TRUE);
 99 	result = tracker_db_journal_append_resource (12, "http://resource");
100 	g_assert_cmpint (result, ==, TRUE);
101 	result = tracker_db_journal_append_resource (13, "http://predicate");
102 	g_assert_cmpint (result, ==, TRUE);
103 	result = tracker_db_journal_append_resource (14, "http://resource");
104 	g_assert_cmpint (result, ==, TRUE);
105 	result = tracker_db_journal_append_delete_statement_id (0, 12, 13, 14);
106 	g_assert_cmpint (result, ==, TRUE);
107 	result = tracker_db_journal_commit_db_transaction (&error);
108 	g_assert_no_error (error);
109 	g_assert_cmpint (result, ==, TRUE);
110 	actual_size = tracker_db_journal_get_size ();
111 	g_assert_cmpint (initial_size, !=, actual_size);
112 
113 	/* Test insert statement */
114 	result = tracker_db_journal_start_transaction (time (NULL));
115 	g_assert_cmpint (result, ==, TRUE);
116 	result = tracker_db_journal_append_resource (15, "http://resource");
117 	g_assert_cmpint (result, ==, TRUE);
118 	result = tracker_db_journal_append_resource (16, "http://predicate");
119 	g_assert_cmpint (result, ==, TRUE);
120 	result = tracker_db_journal_append_insert_statement (0, 15, 16, "test");
121 	g_assert_cmpint (result, ==, TRUE);
122 	result = tracker_db_journal_commit_db_transaction (&error);
123 	g_assert_no_error (error);
124 	g_assert_cmpint (result, ==, TRUE);
125 
126 	/* Test insert id */
127 	result = tracker_db_journal_start_transaction (time (NULL));
128 	g_assert_cmpint (result, ==, TRUE);
129 	result = tracker_db_journal_append_resource (17, "http://resource");
130 	g_assert_cmpint (result, ==, TRUE);
131 	result = tracker_db_journal_append_resource (18, "http://predicate");
132 	g_assert_cmpint (result, ==, TRUE);
133 	result = tracker_db_journal_append_resource (19, "http://resource");
134 	g_assert_cmpint (result, ==, TRUE);
135 	result = tracker_db_journal_append_insert_statement_id (0, 17, 18, 19);
136 	g_assert_cmpint (result, ==, TRUE);
137 	result = tracker_db_journal_commit_db_transaction (&error);
138 	g_assert_no_error (error);
139 	g_assert_cmpint (result, ==, TRUE);
140 
141 	/* Test fsync */
142 	result = tracker_db_journal_fsync ();
143 	g_assert_cmpint (result, ==, TRUE);
144 
145 	tracker_db_journal_shutdown (&error);
146 	g_assert_no_error (error);
147 
148 	g_free (path);
149 }
150 
151 static void
152 test_read_functions (void)
153 {
154 	GError *error = NULL;
155 	gchar *path;
156 	gboolean result;
157 	TrackerDBJournalEntryType type;
158 	gint id, s_id, p_id, o_id;
159 	const gchar *uri, *str;
160 
161 	path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
162 
163 	/* NOTE: we don't unlink here so we can use the data from the write tests */
164 
165 	/* Create an iterator */
166 	result = tracker_db_journal_reader_init (path, &error);
167 	g_assert_no_error (error);
168 	g_assert_cmpint (result, ==, TRUE);
169 
170 	type = tracker_db_journal_reader_get_type ();
171 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START);
172 
173 	/* First transaction */
174 	result = tracker_db_journal_reader_next (&error);
175 	g_assert_no_error (error);
176 	g_assert_cmpint (result, ==, TRUE);
177 
178 	type = tracker_db_journal_reader_get_type ();
179 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
180 
181 	result = tracker_db_journal_reader_next (&error);
182 	g_assert_no_error (error);
183 	g_assert_cmpint (result, ==, TRUE);
184 
185 	type = tracker_db_journal_reader_get_type ();
186 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
187 
188 	result = tracker_db_journal_reader_get_resource (&id, &uri);
189 	g_assert_cmpint (result, ==, TRUE);
190 	g_assert_cmpint (id, ==, 12);
191 	g_assert_cmpstr (uri, ==, "http://resource");
192 
193 	result = tracker_db_journal_reader_next (&error);
194 	g_assert_no_error (error);
195 	g_assert_cmpint (result, ==, TRUE);
196 
197 	type = tracker_db_journal_reader_get_type ();
198 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
199 
200 	result = tracker_db_journal_reader_get_resource (&id, &uri);
201 	g_assert_cmpint (result, ==, TRUE);
202 	g_assert_cmpint (id, ==, 13);
203 	g_assert_cmpstr (uri, ==, "http://predicate");
204 
205 	result = tracker_db_journal_reader_next (&error);
206 	g_assert_no_error (error);
207 	g_assert_cmpint (result, ==, TRUE);
208 
209 	type = tracker_db_journal_reader_get_type ();
210 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
211 
212 	result = tracker_db_journal_reader_get_resource (&id, &uri);
213 	g_assert_cmpint (result, ==, TRUE);
214 	g_assert_cmpint (id, ==, 14);
215 	g_assert_cmpstr (uri, ==, "http://resource");
216 
217 	result = tracker_db_journal_reader_next (&error);
218 	g_assert_no_error (error);
219 	g_assert_cmpint (result, ==, TRUE);
220 
221 	type = tracker_db_journal_reader_get_type ();
222 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_DELETE_STATEMENT_ID);
223 
224 	result = tracker_db_journal_reader_get_statement_id (NULL, &s_id, &p_id, &o_id);
225 	g_assert_cmpint (result, ==, TRUE);
226 	g_assert_cmpint (s_id, ==, 12);
227 	g_assert_cmpint (p_id, ==, 13);
228 	g_assert_cmpint (o_id, ==, 14);
229 
230 	result = tracker_db_journal_reader_next (&error);
231 	g_assert_no_error (error);
232 	g_assert_cmpint (result, ==, TRUE);
233 
234 	type = tracker_db_journal_reader_get_type ();
235 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
236 
237 	/* Second transaction */
238 	result = tracker_db_journal_reader_next (&error);
239 	g_assert_no_error (error);
240 	g_assert_cmpint (result, ==, TRUE);
241 
242 	type = tracker_db_journal_reader_get_type ();
243 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
244 
245 	result = tracker_db_journal_reader_next (&error);
246 	g_assert_no_error (error);
247 	g_assert_cmpint (result, ==, TRUE);
248 
249 	type = tracker_db_journal_reader_get_type ();
250 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
251 
252 	result = tracker_db_journal_reader_get_resource (&id, &uri);
253 	g_assert_cmpint (result, ==, TRUE);
254 	g_assert_cmpint (id, ==, 15);
255 	g_assert_cmpstr (uri, ==, "http://resource");
256 
257 	result = tracker_db_journal_reader_next (&error);
258 	g_assert_no_error (error);
259 	g_assert_cmpint (result, ==, TRUE);
260 
261 	type = tracker_db_journal_reader_get_type ();
262 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
263 
264 	result = tracker_db_journal_reader_get_resource (&id, &uri);
265 	g_assert_cmpint (result, ==, TRUE);
266 	g_assert_cmpint (id, ==, 16);
267 	g_assert_cmpstr (uri, ==, "http://predicate");
268 
269 	result = tracker_db_journal_reader_next (&error);
270 	g_assert_no_error (error);
271 	g_assert_cmpint (result, ==, TRUE);
272 
273 	type = tracker_db_journal_reader_get_type ();
274 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT);
275 
276 	result = tracker_db_journal_reader_get_statement (NULL, &s_id, &p_id, &str);
277 	g_assert_cmpint (result, ==, TRUE);
278 	g_assert_cmpint (s_id, ==, 15);
279 	g_assert_cmpint (p_id, ==, 16);
280 	g_assert_cmpstr (str, ==, "test");
281 
282 	result = tracker_db_journal_reader_next (&error);
283 	g_assert_no_error (error);
284 	g_assert_cmpint (result, ==, TRUE);
285 
286 	type = tracker_db_journal_reader_get_type ();
287 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
288 
289 	/* Third transaction */
290 	result = tracker_db_journal_reader_next (&error);
291 	g_assert_no_error (error);
292 	g_assert_cmpint (result, ==, TRUE);
293 
294 	type = tracker_db_journal_reader_get_type ();
295 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_START_TRANSACTION);
296 
297 	result = tracker_db_journal_reader_next (&error);
298 	g_assert_no_error (error);
299 	g_assert_cmpint (result, ==, TRUE);
300 
301 	type = tracker_db_journal_reader_get_type ();
302 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
303 
304 	result = tracker_db_journal_reader_get_resource (&id, &uri);
305 	g_assert_cmpint (result, ==, TRUE);
306 	g_assert_cmpint (id, ==, 17);
307 	g_assert_cmpstr (uri, ==, "http://resource");
308 
309 	result = tracker_db_journal_reader_next (&error);
310 	g_assert_no_error (error);
311 	g_assert_cmpint (result, ==, TRUE);
312 
313 	type = tracker_db_journal_reader_get_type ();
314 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
315 
316 	result = tracker_db_journal_reader_get_resource (&id, &uri);
317 	g_assert_cmpint (result, ==, TRUE);
318 	g_assert_cmpint (id, ==, 18);
319 	g_assert_cmpstr (uri, ==, "http://predicate");
320 
321 	result = tracker_db_journal_reader_next (&error);
322 	g_assert_no_error (error);
323 	g_assert_cmpint (result, ==, TRUE);
324 
325 	type = tracker_db_journal_reader_get_type ();
326 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_RESOURCE);
327 
328 	result = tracker_db_journal_reader_get_resource (&id, &uri);
329 	g_assert_cmpint (result, ==, TRUE);
330 	g_assert_cmpint (id, ==, 19);
331 	g_assert_cmpstr (uri, ==, "http://resource");
332 
333 	result = tracker_db_journal_reader_next (&error);
334 	g_assert_no_error (error);
335 	g_assert_cmpint (result, ==, TRUE);
336 
337 	type = tracker_db_journal_reader_get_type ();
338 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_INSERT_STATEMENT_ID);
339 
340 	result = tracker_db_journal_reader_get_statement_id (NULL, &s_id, &p_id, &o_id);
341 	g_assert_cmpint (result, ==, TRUE);
342 	g_assert_cmpint (s_id, ==, 17);
343 	g_assert_cmpint (p_id, ==, 18);
344 	g_assert_cmpint (o_id, ==, 19);
345 
346 	result = tracker_db_journal_reader_next (&error);
347 	g_assert_no_error (error);
348 	g_assert_cmpint (result, ==, TRUE);
349 
350 	type = tracker_db_journal_reader_get_type ();
351 	g_assert_cmpint (type, ==, TRACKER_DB_JOURNAL_END_TRANSACTION);
352 
353 	/* Shutdown */
354 	result = tracker_db_journal_reader_shutdown ();
355 	g_assert_cmpint (result, ==, TRUE);
356 
357 	g_free (path);
358 }
359 
360 #endif /* DISABLE_JOURNAL */
361 
362 int
363 main (int argc, char **argv) 
364 {
365 	gchar *path;
366 	int result;
367 
368 	g_test_init (&argc, &argv, NULL);
369 
370 #ifndef DISABLE_JOURNAL
371 	/* None of these tests make sense in case of disabled journal */
372 	g_test_add_func ("/libtracker-db/tracker-db-journal/init-and-shutdown",
373 	                 test_init_and_shutdown);
374 	g_test_add_func ("/libtracker-db/tracker-db-journal/write-functions",
375 	                 test_write_functions);
376 	g_test_add_func ("/libtracker-db/tracker-db-journal/read-functions",
377 	                 test_read_functions);
378 #endif /* DISABLE_JOURNAL */
379 
380 	result = g_test_run ();
381 
382 	/* Clean up */
383 	path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", "tracker-store.journal", NULL);
384 	g_unlink (path);
385 	g_free (path);
386 
387 	path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-data", "dconf", "user", NULL);
388 	g_unlink (path);
389 	g_free (path);
390 
391 	return result;
392 }