1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
4 *
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
8 *
9 * This work 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.
12 *
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
21 */
22
23 #include "config.h"
24
25 #include <glib/glib.h>
26 #include <gio/gio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef G_OS_WIN32
30 #include <stdio.h>
31 #include <glib/gstdio.h>
32 #include <windows.h>
33 #include <shlobj.h>
34 #include <io.h> /* for _get_osfhandle */
35 #endif
36
37 #define TEST_NAME "Prilis zlutoucky kun"
38 #define TEST_DISPLAY_NAME "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"
39 #define TEST_SIZE 0xFFFFFFF0
40
41 static void
test_assigned_values(GFileInfo * info)42 test_assigned_values (GFileInfo *info)
43 {
44 const char *name, *display_name, *mistake;
45 guint64 size;
46 GFileType type;
47
48 /* Test for attributes presence */
49 g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME));
50 g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME));
51 g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
52 g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME));
53
54 /* Retrieve data back and compare */
55
56 name = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME);
57 display_name = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
58 mistake = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
59 size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
60 type = g_file_info_get_file_type (info);
61
62 g_assert_cmpstr (name, ==, TEST_NAME);
63 g_assert_cmpstr (display_name, ==, TEST_DISPLAY_NAME);
64 g_assert_null (mistake);
65 g_assert_cmpint (size, ==, TEST_SIZE);
66 g_assert_cmpstr (name, ==, g_file_info_get_name (info));
67 g_assert_cmpstr (display_name, ==, g_file_info_get_display_name (info));
68 g_assert_cmpint (size, ==, g_file_info_get_size (info) );
69 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
70 }
71
72
73
74 static void
test_g_file_info(void)75 test_g_file_info (void)
76 {
77 GFileInfo *info;
78 GFileInfo *info_dup;
79 GFileInfo *info_copy;
80 char **attr_list;
81 GFileAttributeMatcher *matcher;
82
83 info = g_file_info_new ();
84
85 /* Test for empty instance */
86 attr_list = g_file_info_list_attributes (info, NULL);
87 g_assert_nonnull (attr_list);
88 g_assert_null (*attr_list);
89 g_strfreev (attr_list);
90
91 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME, TEST_NAME);
92 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, TEST_DISPLAY_NAME);
93 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE, TEST_SIZE);
94 g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
95
96 /* The attr list should not be empty now */
97 attr_list = g_file_info_list_attributes (info, NULL);
98 g_assert_nonnull (attr_list);
99 g_assert_nonnull (*attr_list);
100 g_strfreev (attr_list);
101
102 test_assigned_values (info);
103
104 /* Test dups */
105 info_dup = g_file_info_dup (info);
106 g_assert_nonnull (info_dup);
107 test_assigned_values (info_dup);
108
109 info_copy = g_file_info_new ();
110 g_file_info_copy_into (info_dup, info_copy);
111 g_assert_nonnull (info_copy);
112 test_assigned_values (info_copy);
113
114 /* Test remove attribute */
115 g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER));
116 g_file_info_set_attribute_int32 (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER, 10);
117 g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER));
118
119 g_assert_cmpint (g_file_info_get_attribute_type (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER), ==, G_FILE_ATTRIBUTE_TYPE_INT32);
120 g_assert_cmpint (g_file_info_get_attribute_status (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER), !=, G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING);
121
122 g_file_info_remove_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
123 g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER));
124 g_assert_cmpint (g_file_info_get_attribute_type (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER), ==, G_FILE_ATTRIBUTE_TYPE_INVALID);
125
126 matcher = g_file_attribute_matcher_new (G_FILE_ATTRIBUTE_STANDARD_NAME ","
127 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
128
129 g_assert_true (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_STANDARD_NAME));
130 g_assert_false (g_file_attribute_matcher_matches_only (matcher, G_FILE_ATTRIBUTE_STANDARD_NAME));
131 g_assert_false (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_STANDARD_SIZE));
132
133 g_file_info_set_attribute_mask (info, matcher);
134 g_file_attribute_matcher_unref (matcher);
135
136 g_assert_false (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
137 g_assert_true (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME));
138
139 g_object_unref (info);
140 g_object_unref (info_dup);
141 g_object_unref (info_copy);
142 }
143
144 static void
test_g_file_info_modification_time(void)145 test_g_file_info_modification_time (void)
146 {
147 GFile *file = NULL;
148 GFileIOStream *io_stream = NULL;
149 GFileInfo *info = NULL;
150 GDateTime *dt = NULL, *dt_usecs = NULL, *dt_new = NULL, *dt_new_usecs = NULL;
151 GTimeSpan ts;
152 GError *error = NULL;
153
154 g_test_summary ("Test that getting the modification time of a file works.");
155
156 file = g_file_new_tmp ("g-file-info-test-XXXXXX", &io_stream, &error);
157 g_assert_no_error (error);
158
159 info = g_file_query_info (file,
160 G_FILE_ATTRIBUTE_TIME_MODIFIED,
161 G_FILE_QUERY_INFO_NONE,
162 NULL, &error);
163 g_assert_no_error (error);
164
165 /* Check the modification time is retrievable. */
166 dt = g_file_info_get_modification_date_time (info);
167 g_assert_nonnull (dt);
168
169 /* Try again with microsecond precision. */
170 g_clear_object (&info);
171 info = g_file_query_info (file,
172 G_FILE_ATTRIBUTE_TIME_MODIFIED "," G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
173 G_FILE_QUERY_INFO_NONE,
174 NULL, &error);
175 g_assert_no_error (error);
176
177 dt_usecs = g_file_info_get_modification_date_time (info);
178 g_assert_nonnull (dt_usecs);
179
180 ts = g_date_time_difference (dt_usecs, dt);
181 g_assert_cmpint (ts, >, 0);
182 g_assert_cmpint (ts, <, G_USEC_PER_SEC);
183
184 /* Try round-tripping the modification time. */
185 dt_new = g_date_time_add (dt_usecs, G_USEC_PER_SEC + 50);
186 g_file_info_set_modification_date_time (info, dt_new);
187
188 dt_new_usecs = g_file_info_get_modification_date_time (info);
189 ts = g_date_time_difference (dt_new_usecs, dt_new);
190 g_assert_cmpint (ts, ==, 0);
191
192 /* Clean up. */
193 g_clear_object (&io_stream);
194 g_file_delete (file, NULL, NULL);
195 g_clear_object (&file);
196
197 g_clear_object (&info);
198 g_date_time_unref (dt);
199 g_date_time_unref (dt_usecs);
200 g_date_time_unref (dt_new);
201 g_date_time_unref (dt_new_usecs);
202 }
203
204 #ifdef G_OS_WIN32
205 static void
test_internal_enhanced_stdio(void)206 test_internal_enhanced_stdio (void)
207 {
208 char *p0, *p1, *ps;
209 gboolean try_sparse;
210 gchar *tmp_dir_root;
211 wchar_t *tmp_dir_root_w;
212 gchar *c;
213 DWORD fsflags;
214 FILE *f;
215 SYSTEMTIME st;
216 FILETIME ft;
217 HANDLE h;
218 GStatBuf statbuf_p0, statbuf_p1, statbuf_ps;
219 GFile *gf_p0, *gf_p1, *gf_ps;
220 GFileInfo *fi_p0, *fi_p1, *fi_ps;
221 guint64 size_p0, alsize_p0, size_ps, alsize_ps;
222 const gchar *id_p0;
223 const gchar *id_p1;
224 volatile guint64 time_p0;
225 gchar *tmp_dir;
226 wchar_t *programdata_dir_w;
227 wchar_t *users_dir_w;
228 static const GUID folder_id_programdata =
229 { 0x62AB5D82, 0xFDC1, 0x4DC3, { 0xA9, 0xDD, 0x07, 0x0D, 0x1D, 0x49, 0x5D, 0x97 } };
230 static const GUID folder_id_users =
231 { 0x0762D272, 0xC50A, 0x4BB0, { 0xA3, 0x82, 0x69, 0x7D, 0xCD, 0x72, 0x9B, 0x80 } };
232
233 programdata_dir_w = NULL;
234 SHGetKnownFolderPath (&folder_id_programdata, 0, NULL, &programdata_dir_w);
235
236 users_dir_w = NULL;
237 SHGetKnownFolderPath (&folder_id_users, 0, NULL, &users_dir_w);
238
239 if (programdata_dir_w != NULL && users_dir_w != NULL)
240 {
241 gchar *programdata;
242 gchar *users_dir;
243 gchar *allusers;
244 gchar *commondata;
245 GFile *gf_programdata, *gf_allusers, *gf_commondata;
246 GFileInfo *fi_programdata, *fi_allusers, *fi_allusers_target, *fi_commondata, *fi_commondata_target;
247 GFileType ft_allusers;
248 GFileType ft_allusers_target;
249 GFileType ft_programdata;
250 GFileType ft_commondata;
251 gboolean allusers_is_symlink;
252 gboolean commondata_is_symlink;
253 gboolean commondata_is_mount_point;
254 guint32 allusers_reparse_tag;
255 guint32 commondata_reparse_tag;
256 const gchar *id_allusers;
257 const gchar *id_allusers_target;
258 const gchar *id_commondata_target;
259 const gchar *id_programdata;
260 const gchar *allusers_target;
261 const gchar *commondata_target;
262
263 /* C:/ProgramData */
264 programdata = g_utf16_to_utf8 (programdata_dir_w, -1, NULL, NULL, NULL);
265 g_assert_nonnull (programdata);
266 /* C:/Users */
267 users_dir = g_utf16_to_utf8 (users_dir_w, -1, NULL, NULL, NULL);
268 g_assert_nonnull (users_dir);
269 /* "C:/Users/All Users" is a known directory symlink
270 * for "C:/ProgramData".
271 */
272 allusers = g_build_filename (users_dir, "All Users", NULL);
273
274 /* "C:/Users/All Users/Application Data" is a known
275 * junction for "C:/ProgramData"
276 */
277 commondata = g_build_filename (allusers, "Application Data", NULL);
278
279 /* We don't test g_stat() and g_lstat() on these directories,
280 * because it is pointless - there's no way to tell that these
281 * functions behave correctly in this case
282 * (st_ino is useless, so we can't tell apart g_stat() and g_lstat()
283 * results; st_mode is also useless as it does not support S_ISLNK;
284 * and these directories have no interesting properties other
285 * than [not] being symlinks).
286 */
287 gf_programdata = g_file_new_for_path (programdata);
288 gf_allusers = g_file_new_for_path (allusers);
289 gf_commondata = g_file_new_for_path (commondata);
290
291 fi_programdata = g_file_query_info (gf_programdata,
292 G_FILE_ATTRIBUTE_ID_FILE ","
293 G_FILE_ATTRIBUTE_STANDARD_TYPE,
294 G_FILE_QUERY_INFO_NONE,
295 NULL, NULL);
296
297 fi_allusers_target = g_file_query_info (gf_allusers,
298 G_FILE_ATTRIBUTE_ID_FILE ","
299 G_FILE_ATTRIBUTE_STANDARD_TYPE,
300 G_FILE_QUERY_INFO_NONE,
301 NULL, NULL);
302
303 fi_allusers = g_file_query_info (gf_allusers,
304 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET ","
305 G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
306 G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG ","
307 G_FILE_ATTRIBUTE_ID_FILE ","
308 G_FILE_ATTRIBUTE_STANDARD_TYPE,
309 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
310 NULL, NULL);
311
312 fi_commondata = g_file_query_info (gf_commondata,
313 G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET ","
314 G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
315 G_FILE_ATTRIBUTE_DOS_IS_MOUNTPOINT ","
316 G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG ","
317 G_FILE_ATTRIBUTE_ID_FILE ","
318 G_FILE_ATTRIBUTE_STANDARD_TYPE,
319 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
320 NULL, NULL);
321
322 fi_commondata_target = g_file_query_info (gf_commondata,
323 G_FILE_ATTRIBUTE_ID_FILE ","
324 G_FILE_ATTRIBUTE_STANDARD_TYPE,
325 G_FILE_QUERY_INFO_NONE,
326 NULL, NULL);
327
328 g_assert_true (g_file_info_has_attribute (fi_programdata, G_FILE_ATTRIBUTE_ID_FILE));
329 g_assert_true (g_file_info_has_attribute (fi_programdata, G_FILE_ATTRIBUTE_STANDARD_TYPE));
330
331 g_assert_true (g_file_info_has_attribute (fi_allusers_target, G_FILE_ATTRIBUTE_ID_FILE));
332 g_assert_true (g_file_info_has_attribute (fi_allusers_target, G_FILE_ATTRIBUTE_STANDARD_TYPE));
333 g_assert_true (g_file_info_has_attribute (fi_commondata_target, G_FILE_ATTRIBUTE_ID_FILE));
334 g_assert_true (g_file_info_has_attribute (fi_commondata_target, G_FILE_ATTRIBUTE_STANDARD_TYPE));
335
336 g_assert_true (g_file_info_has_attribute (fi_allusers, G_FILE_ATTRIBUTE_ID_FILE));
337 g_assert_true (g_file_info_has_attribute (fi_allusers, G_FILE_ATTRIBUTE_STANDARD_TYPE));
338 g_assert_true (g_file_info_has_attribute (fi_allusers, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK));
339 g_assert_true (g_file_info_has_attribute (fi_allusers, G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG));
340 g_assert_true (g_file_info_has_attribute (fi_allusers, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET));
341
342 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_ID_FILE));
343 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_STANDARD_TYPE));
344 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK));
345 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_DOS_IS_MOUNTPOINT));
346 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG));
347 g_assert_true (g_file_info_has_attribute (fi_commondata, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET));
348
349 ft_allusers = g_file_info_get_file_type (fi_allusers);
350 ft_allusers_target = g_file_info_get_file_type (fi_allusers_target);
351 ft_programdata = g_file_info_get_file_type (fi_programdata);
352 ft_commondata = g_file_info_get_file_type (fi_commondata);
353
354 g_assert_cmpint (ft_allusers, ==, G_FILE_TYPE_DIRECTORY);
355 g_assert_cmpint (ft_allusers_target, ==, G_FILE_TYPE_DIRECTORY);
356 g_assert_cmpint (ft_programdata, ==, G_FILE_TYPE_DIRECTORY);
357 g_assert_cmpint (ft_commondata, ==, G_FILE_TYPE_DIRECTORY);
358
359 allusers_is_symlink = g_file_info_get_attribute_boolean (fi_allusers, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
360 allusers_reparse_tag = g_file_info_get_attribute_uint32 (fi_allusers, G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG);
361 commondata_is_symlink = g_file_info_get_attribute_boolean (fi_commondata, G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
362 commondata_is_mount_point = g_file_info_get_attribute_boolean (fi_commondata, G_FILE_ATTRIBUTE_DOS_IS_MOUNTPOINT);
363 commondata_reparse_tag = g_file_info_get_attribute_uint32 (fi_commondata, G_FILE_ATTRIBUTE_DOS_REPARSE_POINT_TAG);
364
365 g_assert_true (allusers_is_symlink);
366 g_assert_cmpuint (allusers_reparse_tag, ==, IO_REPARSE_TAG_SYMLINK);
367 g_assert_true (commondata_is_symlink);
368 g_assert_true (commondata_is_mount_point);
369 g_assert_cmpuint (commondata_reparse_tag, ==, IO_REPARSE_TAG_MOUNT_POINT);
370
371 id_allusers = g_file_info_get_attribute_string (fi_allusers, G_FILE_ATTRIBUTE_ID_FILE);
372 id_allusers_target = g_file_info_get_attribute_string (fi_allusers_target, G_FILE_ATTRIBUTE_ID_FILE);
373 id_commondata_target = g_file_info_get_attribute_string (fi_commondata_target, G_FILE_ATTRIBUTE_ID_FILE);
374 id_programdata = g_file_info_get_attribute_string (fi_programdata, G_FILE_ATTRIBUTE_ID_FILE);
375
376 g_assert_cmpstr (id_allusers_target, ==, id_programdata);
377 g_assert_cmpstr (id_commondata_target, ==, id_programdata);
378 g_assert_cmpstr (id_allusers, !=, id_programdata);
379
380 allusers_target = g_file_info_get_symlink_target (fi_allusers);
381
382 g_assert_true (g_str_has_suffix (allusers_target, "ProgramData"));
383
384 commondata_target = g_file_info_get_symlink_target (fi_commondata);
385
386 g_assert_true (g_str_has_suffix (commondata_target, "ProgramData"));
387
388 g_object_unref (fi_allusers);
389 g_object_unref (fi_allusers_target);
390 g_object_unref (fi_commondata);
391 g_object_unref (fi_commondata_target);
392 g_object_unref (fi_programdata);
393 g_object_unref (gf_allusers);
394 g_object_unref (gf_commondata);
395 g_object_unref (gf_programdata);
396
397 g_free (allusers);
398 g_free (commondata);
399 g_free (users_dir);
400 g_free (programdata);
401 }
402
403 if (programdata_dir_w)
404 CoTaskMemFree (programdata_dir_w);
405
406 if (users_dir_w)
407 CoTaskMemFree (users_dir_w);
408
409 tmp_dir = g_dir_make_tmp ("glib_stdio_testXXXXXX", NULL);
410 g_assert_nonnull (tmp_dir);
411
412 /* Technically, this isn't necessary - we already assume NTFS, because of
413 * symlink support, and NTFS also supports sparse files. Still, given
414 * the amount of unusual I/O APIs called in this test, checking for
415 * sparse file support of the filesystem where temp directory is
416 * doesn't seem to be out of place.
417 */
418 try_sparse = FALSE;
419 tmp_dir_root = g_strdup (tmp_dir);
420 /* We need "C:\\" or "C:/", with a trailing [back]slash */
421 for (c = tmp_dir_root; c && c[0] && c[1]; c++)
422 if (c[0] == ':')
423 {
424 c[2] = '\0';
425 break;
426 }
427 tmp_dir_root_w = g_utf8_to_utf16 (tmp_dir_root, -1, NULL, NULL, NULL);
428 g_assert_nonnull (tmp_dir_root_w);
429 g_free (tmp_dir_root);
430 g_assert_true (GetVolumeInformationW (tmp_dir_root_w, NULL, 0, NULL, NULL, &fsflags, NULL, 0));
431 g_free (tmp_dir_root_w);
432 try_sparse = (fsflags & FILE_SUPPORTS_SPARSE_FILES) == FILE_SUPPORTS_SPARSE_FILES;
433
434 p0 = g_build_filename (tmp_dir, "zool", NULL);
435 p1 = g_build_filename (tmp_dir, "looz", NULL);
436 ps = g_build_filename (tmp_dir, "sparse", NULL);
437
438 if (try_sparse)
439 {
440 FILE_SET_SPARSE_BUFFER ssb;
441 FILE_ZERO_DATA_INFORMATION zdi;
442
443 g_remove (ps);
444
445 f = g_fopen (ps, "wb");
446 g_assert_nonnull (f);
447
448 h = (HANDLE) _get_osfhandle (fileno (f));
449 g_assert_cmpuint ((guintptr) h, !=, (guintptr) INVALID_HANDLE_VALUE);
450
451 ssb.SetSparse = TRUE;
452 g_assert_true (DeviceIoControl (h,
453 FSCTL_SET_SPARSE,
454 &ssb, sizeof (ssb),
455 NULL, 0, NULL, NULL));
456
457 /* Make it a sparse file that starts with 4GBs of zeros */
458 zdi.FileOffset.QuadPart = 0;
459 zdi.BeyondFinalZero.QuadPart = 0xFFFFFFFFULL + 1;
460 g_assert_true (DeviceIoControl (h,
461 FSCTL_SET_ZERO_DATA,
462 &zdi, sizeof (zdi),
463 NULL, 0, NULL, NULL));
464
465 /* Let's not keep this seemingly 4GB monster around
466 * longer than we absolutely need to. Do all operations
467 * without assertions, then remove the file immediately.
468 */
469 _fseeki64 (f, 0xFFFFFFFFULL, SEEK_SET);
470 fprintf (f, "Hello 4GB World!");
471 fflush (f);
472 fclose (f);
473
474 memset (&statbuf_ps, 0, sizeof (statbuf_ps));
475
476 g_stat (ps, &statbuf_ps);
477
478 gf_ps = g_file_new_for_path (ps);
479
480 fi_ps = g_file_query_info (gf_ps,
481 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
482 G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE,
483 G_FILE_QUERY_INFO_NONE,
484 NULL, NULL);
485
486 g_remove (ps);
487
488 g_assert_true (g_file_info_has_attribute (fi_ps, G_FILE_ATTRIBUTE_STANDARD_SIZE));
489 g_assert_true (g_file_info_has_attribute (fi_ps, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE));
490
491 size_ps = g_file_info_get_attribute_uint64 (fi_ps, G_FILE_ATTRIBUTE_STANDARD_SIZE);
492 alsize_ps = g_file_info_get_attribute_uint64 (fi_ps, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE);
493
494 /* allocated size should small (usually - size of the FS cluster,
495 * let's assume it's less than 1 gigabyte),
496 * size should be more than 4 gigabytes,
497 * st_size should not exceed its 0xFFFFFFFF 32-bit limit,
498 * and should be nonzero (this also detects a failed g_stat() earlier).
499 */
500 g_assert_cmpuint (alsize_ps, <, 0x40000000);
501 g_assert_cmpuint (size_ps, >, G_GUINT64_CONSTANT (0xFFFFFFFF));
502 g_assert_cmpuint (statbuf_ps.st_size, >, 0);
503 g_assert_cmpuint (statbuf_ps.st_size, <=, 0xFFFFFFFF);
504
505 g_object_unref (fi_ps);
506 g_object_unref (gf_ps);
507 }
508
509 /* Wa-a-ay past 02/07/2106 @ 6:28am (UTC),
510 * which is the date corresponding to 0xFFFFFFFF + 1.
511 * This is easier to check than Y2038 (0x80000000 + 1),
512 * since there's no need to worry about signedness this way.
513 */
514 st.wYear = 2106;
515 st.wMonth = 2;
516 st.wDay = 9;
517 st.wHour = 0;
518 st.wMinute = 0;
519 st.wSecond = 0;
520 st.wMilliseconds = 0;
521
522 g_assert_true (SystemTimeToFileTime (&st, &ft));
523
524 f = g_fopen (p0, "w");
525 g_assert_nonnull (f);
526
527 h = (HANDLE) _get_osfhandle (fileno (f));
528 g_assert_cmpuint ((guintptr) h, !=, (guintptr) INVALID_HANDLE_VALUE);
529
530 fprintf (f, "1");
531 fflush (f);
532
533 g_assert_true (SetFileTime (h, &ft, &ft, &ft));
534
535 fclose (f);
536
537 f = g_fopen (p1, "w");
538 g_assert_nonnull (f);
539
540 fclose (f);
541
542 memset (&statbuf_p0, 0, sizeof (statbuf_p0));
543 memset (&statbuf_p1, 0, sizeof (statbuf_p1));
544
545 g_assert_cmpint (g_stat (p0, &statbuf_p0), ==, 0);
546 g_assert_cmpint (g_stat (p1, &statbuf_p1), ==, 0);
547
548 gf_p0 = g_file_new_for_path (p0);
549 gf_p1 = g_file_new_for_path (p1);
550
551 fi_p0 = g_file_query_info (gf_p0,
552 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
553 G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE ","
554 G_FILE_ATTRIBUTE_ID_FILE ","
555 G_FILE_ATTRIBUTE_TIME_MODIFIED,
556 G_FILE_QUERY_INFO_NONE,
557 NULL, NULL);
558
559 fi_p1 = g_file_query_info (gf_p1,
560 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
561 G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE ","
562 G_FILE_ATTRIBUTE_ID_FILE ","
563 G_FILE_ATTRIBUTE_TIME_MODIFIED,
564 G_FILE_QUERY_INFO_NONE,
565 NULL, NULL);
566
567 g_assert_true (g_file_info_has_attribute (fi_p0, G_FILE_ATTRIBUTE_STANDARD_SIZE));
568 g_assert_true (g_file_info_has_attribute (fi_p0, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE));
569 g_assert_true (g_file_info_has_attribute (fi_p0, G_FILE_ATTRIBUTE_ID_FILE));
570 g_assert_true (g_file_info_has_attribute (fi_p0, G_FILE_ATTRIBUTE_TIME_MODIFIED));
571
572 g_assert_true (g_file_info_has_attribute (fi_p1, G_FILE_ATTRIBUTE_STANDARD_SIZE));
573 g_assert_true (g_file_info_has_attribute (fi_p1, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE));
574 g_assert_true (g_file_info_has_attribute (fi_p1, G_FILE_ATTRIBUTE_ID_FILE));
575 g_assert_true (g_file_info_has_attribute (fi_p1, G_FILE_ATTRIBUTE_TIME_MODIFIED));
576
577 size_p0 = g_file_info_get_attribute_uint64 (fi_p0, G_FILE_ATTRIBUTE_STANDARD_SIZE);
578 alsize_p0 = g_file_info_get_attribute_uint64 (fi_p0, G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE);
579
580 /* size should be 1, allocated size should be something else
581 * (could be 0 or the size of the FS cluster, but never 1).
582 */
583 g_assert_cmpuint (size_p0, ==, statbuf_p0.st_size);
584 g_assert_cmpuint (size_p0, ==, 1);
585 g_assert_cmpuint (alsize_p0, !=, size_p0);
586
587 id_p0 = g_file_info_get_attribute_string (fi_p0, G_FILE_ATTRIBUTE_ID_FILE);
588 id_p1 = g_file_info_get_attribute_string (fi_p1, G_FILE_ATTRIBUTE_ID_FILE);
589
590 /* st_ino from W32 stat() is useless for file identification.
591 * It will be either 0, or it will be the same for both files.
592 */
593 g_assert_cmpint (statbuf_p0.st_ino, ==, statbuf_p1.st_ino);
594 g_assert_cmpstr (id_p0, !=, id_p1);
595
596 time_p0 = g_file_info_get_attribute_uint64 (fi_p0, G_FILE_ATTRIBUTE_TIME_MODIFIED);
597
598 /* Check that GFileInfo doesn't suffer from Y2106 problem.
599 * Don't check stat(), as its contents may vary depending on
600 * the host platform architecture
601 * (time fields are 32-bit on 32-bit Windows,
602 * and 64-bit on 64-bit Windows, usually),
603 * so it *can* pass this test in some cases.
604 */
605 g_assert_cmpuint (time_p0, >, G_GUINT64_CONSTANT (0xFFFFFFFF));
606
607 g_object_unref (fi_p0);
608 g_object_unref (fi_p1);
609 g_object_unref (gf_p0);
610 g_object_unref (gf_p1);
611 g_remove (p0);
612 g_remove (p1);
613 g_free (p0);
614 g_free (p1);
615 g_rmdir (tmp_dir);
616 }
617 #endif
618
619
620 int
main(int argc,char * argv[])621 main (int argc,
622 char *argv[])
623 {
624 g_test_init (&argc, &argv, NULL);
625
626 g_test_add_func ("/g-file-info/test_g_file_info", test_g_file_info);
627 g_test_add_func ("/g-file-info/test_g_file_info/modification-time", test_g_file_info_modification_time);
628 #ifdef G_OS_WIN32
629 g_test_add_func ("/g-file-info/internal-enhanced-stdio", test_internal_enhanced_stdio);
630 #endif
631
632 return g_test_run();
633 }
634