tracker-0.16.2/tests/libtracker-extract/tracker-test-utils.c

No issues found

  1 /*
  2  * Copyright (C) 2009, 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
  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 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 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 program; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  *
 19  * Authors: Philip Van Hoof <philip@codeminded.be>
 20  */
 21 
 22 #include "config.h"
 23 
 24 #include <time.h>
 25 #include <string.h>
 26 
 27 #include <glib-object.h>
 28 
 29 #include <libtracker-extract/tracker-extract.h>
 30 
 31 static void
 32 test_guess_date (void)
 33 {
 34 	gchar *result;
 35 
 36 	result = tracker_date_guess ("");
 37 	g_assert (result == NULL);
 38 
 39 	result = tracker_date_guess ("2008-06-14");
 40 	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00");
 41 	g_free (result);
 42 
 43 	result = tracker_date_guess ("20080614000000");
 44 	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00");
 45 	g_free (result);
 46 
 47 	result = tracker_date_guess ("20080614000000Z");
 48 	g_assert_cmpstr (result, ==, "2008-06-14T00:00:00Z");
 49 	g_free (result);
 50 
 51 	result = tracker_date_guess ("Mon Jun 14 04:20:20 2008"); /* MS Office */
 52 	g_assert_cmpstr (result, ==, "2008-06-14T04:20:20");
 53 	g_free (result);
 54 
 55 	result = tracker_date_guess ("2008:06:14 04:20:20"); /* Exif style */
 56 	g_assert_cmpstr (result, ==, "2008-06-14T04:20:20");
 57 	g_free (result);
 58 
 59         result = tracker_date_guess ("2010");
 60         g_assert_cmpstr (result, ==, "2010-01-01T00:00:00Z");
 61         g_free (result);
 62 
 63         result = tracker_date_guess ("201a");
 64         g_assert (!result);
 65 
 66         result = tracker_date_guess ("A2010");
 67         g_assert (!result);
 68 
 69         /* Guessing from the code */
 70         result = tracker_date_guess ("20100318010203-00:03Z");
 71         g_assert_cmpstr (result, ==, "2010-03-18T01:02:03-00:03");
 72         g_free (result);
 73 
 74         result = tracker_date_guess ("20100318010203+00:03Z");
 75         g_assert_cmpstr (result, ==, "2010-03-18T01:02:03+00:03");
 76         g_free (result);
 77 
 78         /* "YYYY-MM-DDThh:mm:ss.ff+zz:zz" */
 79         result = tracker_date_guess ("2010-03-18T01:02:03.10-00:03");
 80         g_assert_cmpstr (result, ==, "2010-03-18T01:02:03.10-00:03");
 81         g_free (result);
 82 
 83         result = tracker_date_guess ("2010-03-18T01:02:03.100");
 84         g_assert_cmpstr (result, ==, "2010-03-18T01:02:03.100");
 85         g_free (result);
 86 
 87 	if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 88 		result = tracker_date_guess (NULL);
 89 		g_free (result);
 90 	}
 91 
 92 	g_test_trap_assert_failed ();
 93 }
 94 
 95 static void
 96 test_text_validate_utf8 ()
 97 {
 98 	GString *s = NULL;
 99 	gsize    utf8_len = 0;
100 	gint i;
101 	gboolean result;
102 	const gchar *valid_utf8[] = {
103 		"GNU's not Unix",
104 		"abcdefghijklmnopqrstuvwxyz0123456789?!,.-_",
105 		"\xF0\x90\x80\x80",
106 		"\x41" "\xCE\xA9" "\xE8\xAA\x9E" "\xF0\x90\x8E\x84",
107 		NULL
108 	};
109 
110 	/* If text is empty, FALSE should be returned */
111 	result = tracker_text_validate_utf8 ("", 0, NULL, NULL);
112 	g_assert_cmpuint (result, ==, 0);
113 
114 	/* If a fully valid UTF-8 string is given as input, output
115 	 *  length should be equal to the input length, and the
116 	 *  output GString should contain exactly the same contents
117 	 *  as the input */
118 	for (i = 0; valid_utf8[i] != NULL; i++) {
119 		s = NULL;
120 		utf8_len = 0;
121 		result = tracker_text_validate_utf8 (valid_utf8[i],
122 		                                     strlen (valid_utf8[i]),
123 		                                     &s,
124 		                                     &utf8_len);
125 		g_assert_cmpuint (result, ==, 1);
126 		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
127 		g_assert (s);
128 		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
129 		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
130 		g_string_free (s, TRUE);
131 	}
132 
133 	/* Same as previous, passing -1 as input text length */
134 	for (i = 0; valid_utf8[i] != NULL; i++) {
135 		s = NULL;
136 		utf8_len = 0;
137 		result = tracker_text_validate_utf8 (valid_utf8[i],
138 		                                     -1,
139 		                                     &s,
140 		                                     &utf8_len);
141 		g_assert_cmpuint (result, ==, 1);
142 		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
143 		g_assert (s);
144 		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
145 		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
146 		g_string_free (s, TRUE);
147 	}
148 
149 	/* Same as previous, only wanting output text */
150 	for (i = 0; valid_utf8[i] != NULL; i++) {
151 		s = NULL;
152 		result = tracker_text_validate_utf8 (valid_utf8[i],
153 		                                     -1,
154 		                                     &s,
155 		                                     NULL);
156 		g_assert_cmpuint (result, ==, 1);
157 		g_assert (s);
158 		g_assert_cmpuint (s->len, ==, strlen (valid_utf8[i]));
159 		g_assert_cmpstr (s->str, ==, valid_utf8[i]);
160 		g_string_free (s, TRUE);
161 	}
162 
163 	/* Same as previous, only wanting number of valid UTF-8 bytes */
164 	for (i = 0; valid_utf8[i] != NULL; i++) {
165 		utf8_len = 0;
166 		result = tracker_text_validate_utf8 (valid_utf8[i],
167 		                                     -1,
168 		                                     NULL,
169 		                                     &utf8_len);
170 		g_assert_cmpuint (result, ==, 1);
171 		g_assert_cmpuint (utf8_len, ==, strlen (valid_utf8[i]));
172 	}
173 
174 	/* If the input string starts with non-valid UTF-8 already, FALSE
175 	 *  should be returned */
176 	result = tracker_text_validate_utf8 ("\xF0\x90\x80" "a", -1, NULL, NULL);
177 	g_assert_cmpuint (result, ==, 0);
178 
179 	/* If the input string suddenly has some non-valid UTF-8 bytes,
180 	 *  TRUE should be returned, and the outputs should contain only info
181 	 *  about the valid first chunk of UTF-8 bytes */
182 	s = NULL;
183 	utf8_len = 0;
184 	result = tracker_text_validate_utf8 ("abcdefghijk" "\xF0\x90\x80" "a",
185 	                                     -1,
186 	                                     &s,
187 	                                     &utf8_len);
188 	g_assert_cmpuint (result, ==, 1);
189 	g_assert_cmpuint (utf8_len, ==, strlen ("abcdefghijk"));
190 	g_assert (s);
191 	g_assert_cmpuint (s->len, ==, strlen ("abcdefghijk"));
192 	g_assert_cmpstr (s->str, ==, "abcdefghijk");
193 	g_string_free (s, TRUE);
194 }
195 
196 static void
197 test_date_to_iso8601 ()
198 {
199         /* Not much to test here because it uses strptime/strftime */
200         gchar *result;
201 
202         result = tracker_date_format_to_iso8601 ("2010:03:13 12:12:12", "%Y:%m:%d %H:%M:%S");
203         g_assert (g_str_has_prefix (result, "2010-03-13T12:12:12"));
204         g_assert_cmpint (strlen (result), <=, 25);
205 
206         /* Pattern and string don't match */
207         result = tracker_date_format_to_iso8601 ("2010:03:13 12:12", "%Y:%m:%d %H:%M:%S");
208         g_assert (result == NULL);
209 }
210 
211 static void
212 test_coalesce_strip ()
213 {
214         /* Used in other tests, but this one can try some corner cases */
215         g_assert (!tracker_coalesce_strip (0, NULL));
216 
217         /* Allocate, do not use constant strings */
218         char *e = g_strdup ("");
219         char *a = g_strdup ("a");
220         g_assert_cmpstr (tracker_coalesce_strip (2, e, a, NULL), ==, "a");
221         g_free (e);
222         g_free (a);
223 }
224 
225 static void
226 test_merge_const ()
227 {
228         gchar *result;
229 
230         result = tracker_merge_const ("*", 3, "a", "b", NULL);
231         g_assert_cmpstr (result, ==, "a*b");
232         g_free (result);
233 
234         result = tracker_merge_const ("****", 3, "a", "b", NULL);
235         g_assert_cmpstr (result, ==, "a****b");
236         g_free (result);
237 
238         result = tracker_merge_const (NULL, 3, "a", "b", NULL);
239         g_assert_cmpstr (result, ==, "ab");
240         g_free (result);
241 
242         result = tracker_merge_const ("", 3, "a", "b", NULL);
243         g_assert_cmpstr (result, ==, "ab");
244         g_free (result);
245 
246         result = tracker_merge_const ("*", 0, NULL);
247         g_assert (!result);
248 }
249 
250 static void
251 test_getline (void)
252 {
253         FILE *f;
254         gchar *line = NULL;
255         gsize  n;
256  
257         f = fopen (TOP_SRCDIR "/tests/libtracker-extract/getline-test.txt", "r");
258         g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
259         g_assert_cmpstr (line, ==, "Line 1\n");
260 
261         g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
262         g_assert_cmpstr (line, ==, "line 2\n");
263 
264         g_assert_cmpint (tracker_getline (&line, &n, f), >, 0);
265         g_assert_cmpstr (line, ==, "line 3\n");
266 }
267 
268 int
269 main (int argc, char **argv)
270 {
271 	gint result;
272 
273 	g_test_init (&argc, &argv, NULL);
274 
275 	g_test_add_func ("/libtracker-extract/tracker-utils/guess_date",
276 	                 test_guess_date);
277         g_test_add_func ("/libtracker-extract/tracker-utils/text-validate-utf8",
278                          test_text_validate_utf8);
279         g_test_add_func ("/libtracker-extract/tracker-utils/date_to_iso8601",
280                          test_date_to_iso8601);
281         g_test_add_func ("/libtracker-extract/tracker-utils/coalesce_strip",
282                          test_coalesce_strip);
283         g_test_add_func ("/libtracker-extract/tracker-utils/merge_const",
284                          test_merge_const);
285         g_test_add_func ("/libtracker-extract/tracker-utils/getline",
286                          test_getline);
287 	result = g_test_run ();
288 
289 	return result;
290 }