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