tracker-utils-test.c 9.79 KB
Newer Older
1
/*
2
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program 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 program 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 program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Authors: Philip Van Hoof <philip@codeminded.be>
 */

22 23
#include "config.h"

24 25 26 27 28
#include <time.h>
#include <string.h>

#include <glib-object.h>

29
#include <libtracker-extract/tracker-extract.h>
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
static void
test_guess_date_failures_subprocess ()
{
	gchar *result;

	result = tracker_date_guess (NULL);
	g_free (result);
}

static void
test_guess_date_failures ()
{
	g_test_trap_subprocess ("/libtracker-extract/tracker-utils/guess_date_failures/subprocess", 0, G_TEST_SUBPROCESS_INHERIT_STDERR);
	g_test_trap_assert_passed ();

	/* Should be NO output when using NULL with tracker_date_guess() */
}

49 50 51 52 53
static void
test_guess_date (void)
{
	gchar *result;

54
	result = tracker_date_guess ("");
55 56
	g_assert (result == NULL);

57
	result = tracker_date_guess ("2008-06-14");
58
	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00");
59 60
	g_free (result);

61
	result = tracker_date_guess ("20080614000000");
62
	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00");
63 64
	g_free (result);

65
	result = tracker_date_guess ("20080614000000Z");
66
	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00Z");
67 68
	g_free (result);

69
	result = tracker_date_guess ("Mon Jun 14 04:20:20 2008"); /* MS Office */
70
	g_assert_cmpstr (result, ==, "2008-06-14T04:20:20");
71 72
	g_free (result);

73
	result = tracker_date_guess ("2008:06:14 04:20:20"); /* Exif style */
74
	g_assert_cmpstr (result, ==, "2008-06-14T04:20:20");
75 76
	g_free (result);

77
        result = tracker_date_guess ("2010");
78
        g_assert_cmpstr (result, ==, "2010-01-01T00:00:00Z");
79 80
        g_free (result);

81 82 83
        result = tracker_date_guess ("201a");
        g_assert (!result);

84 85 86 87 88
        result = tracker_date_guess ("A2010");
        g_assert (!result);

        /* Guessing from the code */
        result = tracker_date_guess ("20100318010203-00:03Z");
89
        g_assert_cmpstr (result, ==, "2010-03-18T01:02:03-00:03");
90 91 92
        g_free (result);

        result = tracker_date_guess ("20100318010203+00:03Z");
93
        g_assert_cmpstr (result, ==, "2010-03-18T01:02:03+00:03");
94 95 96 97
        g_free (result);

        /* "YYYY-MM-DDThh:mm:ss.ff+zz:zz" */
        result = tracker_date_guess ("2010-03-18T01:02:03.10-00:03");
98
        g_assert_cmpstr (result, ==, "2010-03-18T01:02:03.10-00:03");
99 100
        g_free (result);

101 102 103
        result = tracker_date_guess ("2010-03-18T01:02:03.100");
        g_assert_cmpstr (result, ==, "2010-03-18T01:02:03.100");
        g_free (result);
104 105
}

106
static void
107
test_text_validate_utf8 ()
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
	GString *s = NULL;
	gsize    utf8_len = 0;
	gint i;
	gboolean result;
	const gchar *valid_utf8[] = {
		"GNU's not Unix",
		"abcdefghijklmnopqrstuvwxyz0123456789?!,.-_",
		"\xF0\x90\x80\x80",
		"\x41" "\xCE\xA9" "\xE8\xAA\x9E" "\xF0\x90\x8E\x84",
		NULL
	};

	/* If text is empty, FALSE should be returned */
	result = tracker_text_validate_utf8 ("", 0, NULL, NULL);
	g_assert_cmpuint (result, ==, 0);

	/* If a fully valid UTF-8 string is given as input, output
	 *  length should be equal to the input length, and the
	 *  output GString should contain exactly the same contents
	 *  as the input */
	for (i = 0; valid_utf8[i] != NULL; i++) {
		s = NULL;
		utf8_len = 0;
		result = tracker_text_validate_utf8 (valid_utf8[i],
		                                     strlen (valid_utf8[i]),
		                                     &s,
		                                     &utf8_len);
		g_assert_cmpuint (result, ==, 1);
		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
		g_assert (s);
		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
		g_string_free (s, TRUE);
	}

	/* Same as previous, passing -1 as input text length */
	for (i = 0; valid_utf8[i] != NULL; i++) {
		s = NULL;
		utf8_len = 0;
		result = tracker_text_validate_utf8 (valid_utf8[i],
		                                     -1,
		                                     &s,
		                                     &utf8_len);
		g_assert_cmpuint (result, ==, 1);
		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
		g_assert (s);
		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
		g_string_free (s, TRUE);
	}

	/* Same as previous, only wanting output text */
	for (i = 0; valid_utf8[i] != NULL; i++) {
		s = NULL;
		result = tracker_text_validate_utf8 (valid_utf8[i],
		                                     -1,
		                                     &s,
		                                     NULL);
		g_assert_cmpuint (result, ==, 1);
		g_assert (s);
		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
		g_string_free (s, TRUE);
	}

	/* Same as previous, only wanting number of valid UTF-8 bytes */
	for (i = 0; valid_utf8[i] != NULL; i++) {
		utf8_len = 0;
		result = tracker_text_validate_utf8 (valid_utf8[i],
		                                     -1,
		                                     NULL,
		                                     &utf8_len);
		g_assert_cmpuint (result, ==, 1);
		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
	}

	/* If the input string starts with non-valid UTF-8 already, FALSE
	 *  should be returned */
	result = tracker_text_validate_utf8 ("\xF0\x90\x80" "a", -1, NULL, NULL);
	g_assert_cmpuint (result, ==, 0);

	/* If the input string suddenly has some non-valid UTF-8 bytes,
	 *  TRUE should be returned, and the outputs should contain only info
	 *  about the valid first chunk of UTF-8 bytes */
	s = NULL;
	utf8_len = 0;
	result = tracker_text_validate_utf8 ("abcdefghijk" "\xF0\x90\x80" "a",
	                                     -1,
	                                     &s,
	                                     &utf8_len);
	g_assert_cmpuint (result, ==, 1);
	g_assert_cmpuint (utf8_len, ==, strlen ("abcdefghijk"));
	g_assert (s);
	g_assert_cmpuint (s->len, ==, strlen ("abcdefghijk"));
	g_assert_cmpstr (s->str, ==, "abcdefghijk");
	g_string_free (s, TRUE);
205 206 207 208 209 210 211 212 213 214 215
}

static void
test_date_to_iso8601 ()
{
        /* Not much to test here because it uses strptime/strftime */
        gchar *result;

        result = tracker_date_format_to_iso8601 ("2010:03:13 12:12:12", "%Y:%m:%d %H:%M:%S");
        g_assert (g_str_has_prefix (result, "2010-03-13T12:12:12"));
        g_assert_cmpint (strlen (result), <=, 25);
216
        g_free (result);
217 218 219 220 221 222

        /* Pattern and string don't match */
        result = tracker_date_format_to_iso8601 ("2010:03:13 12:12", "%Y:%m:%d %H:%M:%S");
        g_assert (result == NULL);
}

223 224 225 226 227
static void
test_coalesce_strip ()
{
        /* Used in other tests, but this one can try some corner cases */
        g_assert (!tracker_coalesce_strip (0, NULL));
228 229 230 231 232 233 234

        /* Allocate, do not use constant strings */
        char *e = g_strdup ("");
        char *a = g_strdup ("a");
        g_assert_cmpstr (tracker_coalesce_strip (2, e, a, NULL), ==, "a");
        g_free (e);
        g_free (a);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
}

static void
test_merge_const ()
{
        gchar *result;

        result = tracker_merge_const ("*", 3, "a", "b", NULL);
        g_assert_cmpstr (result, ==, "a*b");
        g_free (result);

        result = tracker_merge_const ("****", 3, "a", "b", NULL);
        g_assert_cmpstr (result, ==, "a****b");
        g_free (result);

        result = tracker_merge_const (NULL, 3, "a", "b", NULL);
        g_assert_cmpstr (result, ==, "ab");
        g_free (result);

        result = tracker_merge_const ("", 3, "a", "b", NULL);
        g_assert_cmpstr (result, ==, "ab");
        g_free (result);

        result = tracker_merge_const ("*", 0, NULL);
        g_assert (!result);
}

262 263
static void
test_getline (void)
264 265 266
{
        FILE *f;
        gchar *line = NULL;
267
        gsize  n = 0;
268
 
269
        f = fopen (TOP_SRCDIR "/tests/libtracker-extract/getline-test.txt", "r");
270 271
        g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
        g_assert_cmpstr (line, ==, "Line 1\n");
272 273
        g_free (line);
        line = NULL;
274 275 276

        g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
        g_assert_cmpstr (line, ==, "line 2\n");
277 278
        g_free (line);
        line = NULL;
279 280 281

        g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
        g_assert_cmpstr (line, ==, "line 3\n");
282
        g_free (line);
283 284
}

285 286 287 288 289 290 291 292 293
int
main (int argc, char **argv)
{
	gint result;

	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/libtracker-extract/tracker-utils/guess_date",
	                 test_guess_date);
294 295 296 297
	g_test_add_func ("/libtracker-extract/tracker-utils/guess_date_failures",
	                 test_guess_date_failures);
	g_test_add_func ("/libtracker-extract/tracker-utils/guess_date_failures/subprocess",
	                 test_guess_date_failures_subprocess);
298 299
        g_test_add_func ("/libtracker-extract/tracker-utils/text-validate-utf8",
                         test_text_validate_utf8);
300 301
        g_test_add_func ("/libtracker-extract/tracker-utils/date_to_iso8601",
                         test_date_to_iso8601);
302 303 304 305 306 307
        g_test_add_func ("/libtracker-extract/tracker-utils/coalesce_strip",
                         test_coalesce_strip);
        g_test_add_func ("/libtracker-extract/tracker-utils/merge_const",
                         test_merge_const);
        g_test_add_func ("/libtracker-extract/tracker-utils/getline",
                         test_getline);
308 309 310 311
	result = g_test_run ();

	return result;
}