tracker-file-utils-test.c 14.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <string.h>
21 22 23
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
24
#include <locale.h>
25 26

#include <glib.h>
27
#include <glib/gstdio.h>
28 29
#include <gio/gio.h>

30 31
#include <libtracker-miners-common/tracker-file-utils.h>
#include <libtracker-miners-common/tracker-locale.h>
32 33 34

#include <tracker-test-helpers.h>

35 36 37 38 39 40
#define TEST_FILENAME "./file-utils-test.txt"
#define TEST_HIDDEN_FILENAME "./.hidden-file.txt"

static void
ensure_file_exists (const gchar *filename)
{
41 42
        g_autoptr(GError) error = NULL;

43
        if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
44
                g_assert (g_file_set_contents (filename, "Just some stuff", -1, &error) == TRUE);
45
                g_assert_no_error (error);
46 47 48 49 50 51 52
        }
}

static void
remove_file (const gchar *filename)
{
        g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
53
        g_assert_cmpint (g_remove (filename), ==, 0);
54 55
}

56
static GSList *
57
array_as_list (const gchar **array)
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
{
	gint i;
	GSList *result = NULL;

	for (i = 0; array[i] != NULL; i++) {
		result = g_slist_prepend (result, g_strdup(array[i]));

	}

	return result;
}

static gboolean
string_in_list (GSList *list, const gchar *string)
{
	GSList *it;
	for ( it = list; it != NULL; it = it->next) {
		if (strcmp (it->data, string) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
test_path_list_filter_duplicates (void)
{
85 86 87 88 89
	const gchar *input_roots [] = {"/home/ivan",
                                       "/home",
                                       "/tmp",
                                       "/usr/",
                                       "/usr/share/local", NULL};
90 91 92 93 94 95

	GSList *input_as_list = NULL;
	GSList *result;

	input_as_list = array_as_list (input_roots);

96
	result = tracker_path_list_filter_duplicates (input_as_list, ".", TRUE);
97 98 99 100 101 102
	g_assert_cmpint (3, ==, g_slist_length (result));

	g_assert (string_in_list (result, "/home"));
	g_assert (string_in_list (result, "/tmp"));
	g_assert (string_in_list (result, "/usr"));

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
	g_slist_foreach (input_as_list, (GFunc) g_free, NULL);
	g_slist_foreach (result, (GFunc) g_free, NULL);
}

static void
test_path_list_filter_duplicates_with_exceptions ()
{
        const gchar *input_roots [] = { "/home/user/MyDocs",
                                        "/home/user/MyDocs/.sounds",
                                        "/home/user/MyDocs/visible",
                                        NULL};
        GSList *input_as_list = NULL, *result = NULL;

        input_as_list = array_as_list (input_roots);

        result = tracker_path_list_filter_duplicates (input_as_list, "/home/user/MyDocs", FALSE);
        g_assert_cmpint (g_slist_length (result), ==, 3);
	g_assert (string_in_list (result, "/home/user/MyDocs"));
	g_assert (string_in_list (result, "/home/user/MyDocs/.sounds"));
	g_assert (string_in_list (result, "/home/user/MyDocs/visible"));
	g_slist_foreach (result, (GFunc) g_free, NULL);


        result = tracker_path_list_filter_duplicates (input_as_list, "/home/user/MyDocs", TRUE);
        g_assert_cmpint (g_slist_length (result), ==, 1);
	g_assert (string_in_list (result, "/home/user/MyDocs"));
	g_slist_foreach (result, (GFunc) g_free, NULL);

131 132 133 134 135 136
	g_slist_foreach (input_as_list, (GFunc) g_free, NULL);
}

static void
test_path_evaluate_name (void)
{
137
	gchar *result, *expected, *pwd, *home;
138 139 140 141 142


	const gchar *test = "/one/two";
	gchar *parent_dir;

143 144
	home = g_strdup (g_getenv ("HOME"));
	pwd = g_get_current_dir ();
145 146 147 148
	g_setenv ("TEST_TRACKER_DIR", test, TRUE);


	result = tracker_path_evaluate_name ("/home/user/all/ok");
149
	g_assert_cmpstr (result, ==, "/home/user/all/ok");
150 151 152 153 154 155
	g_free (result);

	/* The result of this test and the next one are not consistent!
	 * Must it remove the end '/' or not?
	 */
	result = tracker_path_evaluate_name ("/home/user/all/dir/");
156
	g_assert_cmpstr (result, ==, "/home/user/all/dir");
157 158 159 160 161 162 163 164
	g_free (result);


	/*
	 * TODO: In valgrind this test shows a memory leak
	 */
	result = tracker_path_evaluate_name ("~/all/dir/");
	expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir/", NULL);
165
	g_assert_cmpstr (result, ==, expected);
166 167 168
	g_free (result);
	g_free (expected);

169 170
	result = tracker_path_evaluate_name ("just-a-filename");
	g_assert_cmpstr (result, ==, "just-a-filename");
171
	g_free (result);
172

173 174
	result = tracker_path_evaluate_name ("$HOME/all/dir/");
	expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir", NULL);
175
	g_assert_cmpstr (result, ==, expected);
176 177 178 179 180
	g_free (result);
	g_free (expected);

	result = tracker_path_evaluate_name ("${HOME}/all/dir/");
	expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir", NULL);
181
	g_assert_cmpstr (result, ==, expected);
182 183 184 185 186
	g_free (result);
	g_free (expected);

	result = tracker_path_evaluate_name ("./test/current/dir");
	expected = g_build_path (G_DIR_SEPARATOR_S, pwd, "/test/current/dir", NULL);
187
	g_assert_cmpstr (result, ==, expected);
188 189 190 191 192
	g_free (result);
	g_free (expected);

	result = tracker_path_evaluate_name ("$TEST_TRACKER_DIR/test/dir");
	expected = g_build_path (G_DIR_SEPARATOR_S, test, "/test/dir", NULL);
193
	g_assert_cmpstr (result, ==, expected);
194 195 196 197 198 199
	g_free (result);
	g_free (expected);

	result = tracker_path_evaluate_name ("../test/dir");
	parent_dir = g_path_get_dirname (pwd);
	expected = g_build_path (G_DIR_SEPARATOR_S, parent_dir, "/test/dir", NULL);
200
	g_assert_cmpstr (result, ==, expected);
201 202 203 204 205 206 207 208 209 210
	g_free (result);
	g_free (parent_dir);
	g_free (expected);

	result = tracker_path_evaluate_name ("");
	g_assert (!result);

	result = tracker_path_evaluate_name (NULL);
	g_assert (!result);

211 212 213 214 215 216 217 218

        g_setenv ("HOME", "", TRUE);
        result = tracker_path_evaluate_name ("~/but-no-home.txt");
        g_assert (!result);
        g_setenv ("HOME", home, TRUE);

        result = tracker_path_evaluate_name ("$UNDEFINED/something");
        g_assert_cmpstr (result, ==, "/something");
219
        g_free (result);
220

221
	result = tracker_path_evaluate_name (tracker_test_helpers_get_nonutf8 ());
222
	g_assert_cmpstr (result, ==, tracker_test_helpers_get_nonutf8 ());
223
	g_free (result);
224

225 226
	g_free (home);
	g_free (pwd);
227 228 229 230 231 232 233
	g_unsetenv ("TEST_TRACKER_DIR");
}


static void
test_file_get_mime_type (void)
{
234 235 236 237 238 239
	gchar *result;
	GFile *f;

        f = g_file_new_for_path (TEST_FILENAME);
        result = tracker_file_get_mime_type (f);
        g_assert_cmpstr (result, ==, "text/plain");
240

241 242
        g_object_unref (f);
        g_free (result);
243

244 245 246
        f = g_file_new_for_path ("./file-does-NOT-exist");
        result = tracker_file_get_mime_type (f);
        g_assert_cmpstr (result, ==, "unknown");
247

248 249
        g_object_unref (f);
        g_free (result);
250 251 252

}

253
#define assert_filename_match(a, b) { \
254 255
	g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, TRUE); \
	g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, TRUE); }
256 257

#define assert_no_filename_match(a, b) { \
258 259
	g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, FALSE); \
	g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, FALSE); }
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

static void
test_case_match_filename_without_extension ()
{
	assert_filename_match ("test.mp3", "test.mp3");
	assert_filename_match ("test.mp3", "test.wav");
	assert_filename_match ("test.mp3", "test.mp");
	assert_filename_match ("test.mp3", "test.");
	assert_filename_match ("test.mp3", "test");
	assert_filename_match ("01 - Song 1 (Remix).wav", "01 - Song 1 (Remix).flac");

	assert_no_filename_match ("test.mp3", "bacon.mp3");

	/* Pathological cases, mainly testing that nothing crashes */
	assert_no_filename_match (".", "\n");
	assert_no_filename_match ("as", "as..");
	assert_no_filename_match ("...as", "...as..");
	assert_no_filename_match (".", "test.");
	assert_filename_match ("", ".");
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
static void
test_file_utils_open_close ()
{
        FILE *f;

        f = tracker_file_open (TEST_FILENAME);
        g_assert (f);
        tracker_file_close (f, TRUE);

        f = tracker_file_open (TEST_FILENAME);
        g_assert (f);
        tracker_file_close (f, FALSE);

        f = tracker_file_open ("./file-does-NOT-exist");
        g_assert (!f);
}

static void
test_file_utils_get_size ()
{
        goffset size;
        struct stat st;

        size = tracker_file_get_size (TEST_FILENAME);
        g_assert_cmpint (size, >, 0);

307
        g_assert_cmpint (stat (TEST_FILENAME, &st), ==, 0);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        g_assert_cmpint (size, ==, st.st_size);

        /* File doesn't exist */
        size = tracker_file_get_size ("./file-does-NOT-exist");
        g_assert_cmpint (size, ==, 0);
}

static void
test_file_utils_get_mtime ()
{
        guint64 mtime;
        struct stat st;
        gchar *pwd, *uri;

        mtime = tracker_file_get_mtime (TEST_FILENAME);
        g_assert_cmpint (mtime, >, 0);

325
        g_assert_cmpint (stat (TEST_FILENAME, &st), ==, 0);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        // This comparison could lead a problem in 32/64 bits?
        g_assert_cmpint (mtime, ==, st.st_mtime);

        pwd = g_get_current_dir ();
        uri = g_strdup_printf ("file://%s/%s", pwd, TEST_FILENAME);
        mtime = tracker_file_get_mtime_uri (uri);
        // This comparison could lead a problem in 32/64 bits?
        g_assert_cmpint (mtime, ==, st.st_mtime);

        g_free (pwd);
        g_free (uri);

        mtime = tracker_file_get_mtime_uri ("./file-does-NOT-exist");
        g_assert_cmpint (mtime, ==, 0);
}

static void
test_file_system_get_remaining_space ()
{
        guint64 space;

        space = tracker_file_system_get_remaining_space ("/home");
        g_assert_cmpint (space, >, 0);

        // This is a critical (aborts the process)
        //space = tracker_file_system_get_remaining_space ("/unlikely/to/have/this/folder");
}

static void
test_file_system_get_remaining_space_percentage ()
{
        gdouble space;

        space = tracker_file_system_get_remaining_space_percentage ("/home");
        g_assert_cmpfloat (space, >=, 0);
        g_assert_cmpfloat (space, <=, 100);

        // This is a critical (aborts the process)
        //space = tracker_file_system_get_remaining_space_percentage ("/unlikely/to/have/this/folder");
}

static void
test_file_system_has_enough_space ()
{
        /* Hopefully we will always have 1 byte free... */
        g_assert (tracker_file_system_has_enough_space ("/home", 1, FALSE));
        g_assert (tracker_file_system_has_enough_space ("/home", 1, TRUE));

        /* gulong goes only up to 4Gb. Cannot ask for unreasonable amount of space */
        //g_assert (!tracker_file_system_has_enough_space ("/home", G_MAXULONG, FALSE));
}

static void
test_file_exists_and_writable ()
{
        const gchar *path = "./test-dir-remove-afterwards";

        if (g_file_test (path, G_FILE_TEST_EXISTS)) {
384
                g_assert_cmpint (g_remove (path), ==, 0);
385 386 387 388 389 390 391 392 393
        }

        /* This should create the directory with write access*/
        g_assert (tracker_path_has_write_access_or_was_created (path));
        g_assert (g_file_test (path, G_FILE_TEST_EXISTS));

        /* This time exists and has write access */
        g_assert (tracker_path_has_write_access_or_was_created (path));

394
        g_assert_cmpint (chmod (path, S_IRUSR & S_IRGRP), ==, 0);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

        /* Exists but is not writable */
        g_assert (!tracker_path_has_write_access_or_was_created (path));

        /* Doesn't exist and cannot be created */
        g_assert (!tracker_path_has_write_access_or_was_created ("/var/log/tracker-test"));

        g_remove (path);
}

static void
test_file_utils_is_hidden ()
{
        GFile *f;

        ensure_file_exists ("./non-hidden-test-file");

        f = g_file_new_for_path (TEST_HIDDEN_FILENAME);
        g_assert (tracker_file_is_hidden (f));
        g_object_unref (f);

        f = g_file_new_for_path ("./non-hidden-test-file");
        g_assert (!tracker_file_is_hidden (f));
        g_object_unref (f);

        remove_file ("./non-hidden-test-file");
}

static void
test_file_utils_cmp ()
{
        GFile *one, *two, *three;

        one = g_file_new_for_path (TEST_FILENAME);
        two = g_file_new_for_path (TEST_FILENAME);
        three = g_file_new_for_path (TEST_HIDDEN_FILENAME);

        g_assert (!tracker_file_cmp (one, two));
        g_assert (tracker_file_cmp (two, three));
}

436 437 438 439 440 441 442
int
main (int argc, char **argv)
{
	int result;

	g_test_init (&argc, &argv, NULL);

443 444
	setlocale (LC_ALL, "");

445 446
        ensure_file_exists (TEST_FILENAME);
        ensure_file_exists (TEST_HIDDEN_FILENAME);
447

448
	g_test_add_func ("/libtracker-common/file-utils/path_evaluate_name",
449
	                 test_path_evaluate_name);
450
	g_test_add_func ("/libtracker-common/file-utils/path_list_filter_duplicates",
451
	                 test_path_list_filter_duplicates);
452 453 454
	g_test_add_func ("/libtracker-common/file-utils/path_list_filter_duplicates_with_exceptions",
	                 test_path_list_filter_duplicates_with_exceptions);
	g_test_add_func ("/libtracker-common/file-utils/file_get_mime_type",
455
	                 test_file_get_mime_type);
456
	g_test_add_func ("/libtracker-common/file-utils/case_match_filename_without_extension",
457 458
	                 test_case_match_filename_without_extension);

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        g_test_add_func ("/libtracker-common/file-utils/open_close",
                         test_file_utils_open_close);
        g_test_add_func ("/libtracker-common/file-utils/get_size",
                         test_file_utils_get_size);
        g_test_add_func ("/libtracker-common/file-utils/get_mtime",
                         test_file_utils_get_mtime);
        g_test_add_func ("/libtracker-common/file-utils/get_remaining_space",
                         test_file_system_get_remaining_space);
        g_test_add_func ("/libtracker-common/file-utils/get_remaining_space_percentage",
                         test_file_system_get_remaining_space_percentage);
        g_test_add_func ("/libtracker-common/file-utils/has_enough_space",
                         test_file_system_has_enough_space);
        g_test_add_func ("/libtracker-common/file-utils/has_write_access_or_was_created",
                         test_file_exists_and_writable);
        g_test_add_func ("/libtracker-common/file-utils/is_hidden",
                         test_file_utils_is_hidden);
        g_test_add_func ("/libtracker-common/file-utils/cmp",
                         test_file_utils_cmp);

478 479
	result = g_test_run ();

480 481 482
        remove_file (TEST_FILENAME);
        remove_file (TEST_HIDDEN_FILENAME);

483 484
	return result;
}