tracker-0.16.2/tests/libtracker-miner/tracker-file-system-test.c

No issues found

  1 /*
  2  * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program 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
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 #include <string.h>
 20 #include <unistd.h>
 21 
 22 #include <glib.h>
 23 #include <glib/gstdio.h>
 24 
 25 #include <libtracker-miner/tracker-file-system.h>
 26 
 27 /* Fixture struct */
 28 typedef struct {
 29 	/* The filesystem to test */
 30 	TrackerFileSystem *file_system;
 31 } TestCommonContext;
 32 
 33 #define test_add(path,fun)	  \
 34 	g_test_add (path, \
 35 	            TestCommonContext, \
 36 	            NULL, \
 37 	            test_common_context_setup, \
 38 	            fun, \
 39 	            test_common_context_teardown)
 40 
 41 static void
 42 test_common_context_setup (TestCommonContext *fixture,
 43                            gconstpointer      data)
 44 {
 45 	fixture->file_system = tracker_file_system_new ();
 46 }
 47 
 48 static void
 49 test_common_context_teardown (TestCommonContext *fixture,
 50                               gconstpointer      data)
 51 {
 52 	if (fixture->file_system)
 53 		g_object_unref (fixture->file_system);
 54 }
 55 
 56 static void
 57 test_file_system_insertions (TestCommonContext *fixture,
 58                              gconstpointer      data)
 59 {
 60 	GFile *file, *canonical, *other;
 61 
 62 	file = g_file_new_for_uri ("file:///aaa/");
 63 	canonical = tracker_file_system_peek_file (fixture->file_system, file);
 64 	g_assert (canonical == NULL);
 65 
 66 	canonical = tracker_file_system_get_file (fixture->file_system, file,
 67 						  G_FILE_TYPE_DIRECTORY, NULL);
 68 	g_object_unref (file);
 69 
 70 	g_assert (canonical != NULL);
 71 
 72 	file = g_file_new_for_uri ("file:///aaa/");
 73 	other = tracker_file_system_get_file (fixture->file_system, file,
 74 					      G_FILE_TYPE_DIRECTORY, NULL);
 75 	g_assert (canonical == other);
 76 
 77 	other = tracker_file_system_peek_file (fixture->file_system, file);
 78 	g_object_unref (file);
 79 	g_assert (other != NULL);
 80 }
 81 
 82 static void
 83 test_file_system_children (TestCommonContext *fixture,
 84 			   gconstpointer      data)
 85 {
 86 	GFile *file, *parent, *child, *other;
 87 
 88 	file = g_file_new_for_uri ("file:///aaa/");
 89 	parent = tracker_file_system_get_file (fixture->file_system, file,
 90 						  G_FILE_TYPE_DIRECTORY, NULL);
 91 	g_object_unref (file);
 92 
 93 	file = g_file_new_for_uri ("file:///aaa/bbb");
 94 	child = tracker_file_system_get_file (fixture->file_system, file,
 95 					      G_FILE_TYPE_REGULAR, parent);
 96 	g_assert (child != NULL);
 97 	g_object_unref (file);
 98 
 99 	file = g_file_new_for_uri ("file:///aaa/bbb");
100 	other = tracker_file_system_get_file (fixture->file_system, file,
101 					      G_FILE_TYPE_REGULAR, NULL);
102 	g_assert (other != NULL);
103 	g_assert (child == other);
104 
105 	g_object_unref (file);
106 }
107 
108 static void
109 test_file_system_indirect_children (TestCommonContext *fixture,
110 				    gconstpointer      data)
111 {
112 	GFile *file, *parent, *child, *other;
113 
114 	file = g_file_new_for_uri ("file:///aaa/");
115 	parent = tracker_file_system_get_file (fixture->file_system, file,
116 					       G_FILE_TYPE_DIRECTORY, NULL);
117 	g_object_unref (file);
118 
119 	file = g_file_new_for_uri ("file:///aaa/bbb/ccc");
120 	child = tracker_file_system_get_file (fixture->file_system, file,
121 					      G_FILE_TYPE_REGULAR, parent);
122 	g_assert (child != NULL);
123 	g_object_unref (file);
124 
125 	file = g_file_new_for_uri ("file:///aaa/bbb/ccc");
126 	other = tracker_file_system_get_file (fixture->file_system, file,
127 					      G_FILE_TYPE_REGULAR, NULL);
128 	g_assert (other != NULL);
129 	g_assert (child == other);
130 
131 	/* FIXME: check missing parent in between */
132 
133 	g_object_unref (file);
134 }
135 
136 static void
137 test_file_system_reparenting (TestCommonContext *fixture,
138 			      gconstpointer      data)
139 {
140 	GFile *file, *parent, *child, *grandchild, *other;
141 
142 	file = g_file_new_for_uri ("file:///aaa/");
143 	parent = tracker_file_system_get_file (fixture->file_system, file,
144 					       G_FILE_TYPE_DIRECTORY, NULL);
145 	g_object_unref (file);
146 
147 	file = g_file_new_for_uri ("file:///aaa/bbb/ccc");
148 	grandchild = tracker_file_system_get_file (fixture->file_system, file,
149 						   G_FILE_TYPE_REGULAR, parent);
150 	g_assert (grandchild != NULL);
151 	g_object_unref (file);
152 
153 	file = g_file_new_for_uri ("file:///aaa/bbb");
154 	child = tracker_file_system_get_file (fixture->file_system, file,
155 					      G_FILE_TYPE_REGULAR, parent);
156 	g_assert (child != NULL);
157 	g_object_unref (file);
158 
159 	file = g_file_new_for_uri ("file:///aaa/bbb/ccc");
160 	other = tracker_file_system_peek_file (fixture->file_system, file);
161 	g_assert (other != NULL);
162 	g_assert (grandchild == other);
163 	g_object_unref (file);
164 
165 	/* Delete child in between */
166 	g_object_unref (child);
167 
168 	/* Check that child doesn't exist anymore */
169 	file = g_file_new_for_uri ("file:///aaa/bbb");
170 	child = tracker_file_system_peek_file (fixture->file_system, file);
171 	g_assert (child == NULL);
172 	g_object_unref (file);
173 
174 	/* Check that grand child still exists */
175 	file = g_file_new_for_uri ("file:///aaa/bbb/ccc");
176 	other = tracker_file_system_peek_file (fixture->file_system, file);
177 	g_assert (other != NULL);
178 	g_assert (grandchild == other);
179 	g_object_unref (file);
180 }
181 
182 static void
183 test_file_system_properties (TestCommonContext *fixture,
184 			     gconstpointer      data)
185 {
186 	GQuark property1_quark, property2_quark;
187 	gchar *value = "value";
188 	gchar *ret_value;
189 	GFile *file, *f;
190 
191 	property1_quark = g_quark_from_string ("file-system-test-property1");
192 	tracker_file_system_register_property (property1_quark,
193 					       NULL);
194 	property2_quark = g_quark_from_string ("file-system-test-property2");
195 	tracker_file_system_register_property (property2_quark,
196 					       NULL);
197 
198 	f = g_file_new_for_uri ("file:///aaa/");
199 	file = tracker_file_system_get_file (fixture->file_system, f,
200 					     G_FILE_TYPE_REGULAR, NULL);
201 	g_object_unref (f);
202 
203 	/* Set both properties */
204 	tracker_file_system_set_property (fixture->file_system, file,
205 					  property1_quark, value);
206 	tracker_file_system_set_property (fixture->file_system, file,
207 					  property2_quark, value);
208 
209 	/* Check second property and remove it */
210 	ret_value = tracker_file_system_get_property (fixture->file_system,
211 						      file, property2_quark);
212 	g_assert (ret_value == value);
213 
214 	tracker_file_system_unset_property (fixture->file_system,
215 					    file, property2_quark);
216 
217 	ret_value = tracker_file_system_get_property (fixture->file_system,
218 						      file, property2_quark);
219 	g_assert (ret_value == NULL);
220 
221 	/* Check first property and remove it */
222 	ret_value = tracker_file_system_get_property (fixture->file_system,
223 						      file, property1_quark);
224 	g_assert (ret_value == value);
225 
226 	tracker_file_system_unset_property (fixture->file_system,
227 					    file, property1_quark);
228 
229 	ret_value = tracker_file_system_get_property (fixture->file_system,
230 						      file, property1_quark);
231 	g_assert (ret_value == NULL);
232 }
233 
234 gint
235 main (gint    argc,
236       gchar **argv)
237 {
238 	g_test_init (&argc, &argv, NULL);
239 
240 	g_test_message ("Testing file system abstraction");
241 
242 	test_add ("/libtracker-miner/file-system/insertions",
243 	          test_file_system_insertions);
244 	test_add ("/libtracker-miner/file-system/children",
245 	          test_file_system_children);
246 	test_add ("/libtracker-miner/file-system/indirect-children",
247 	          test_file_system_indirect_children);
248 	test_add ("/libtracker-miner/file-system/reparenting",
249 		  test_file_system_reparenting);
250 	test_add ("/libtracker-miner/file-system/file-properties",
251 	          test_file_system_properties);
252 
253 	return g_test_run ();
254 }