source: trunk/libtransmission/file-test.c @ 14314

Last change on this file since 14314 was 14314, checked in by jordan, 8 years ago

(trunk, libT) mike.dld's 4160-02a-path.patch: portability wrapper around file paths.

File size: 21.1 KB
Line 
1/*
2 * This file Copyright (C) 2013-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id$
8 */
9
10#include <string.h>
11
12#ifndef WIN32
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16#else
17 #include <windows.h>
18#endif
19
20#include "transmission.h"
21#include "error.h"
22#include "file.h"
23
24#include "libtransmission-test.h"
25
26#ifndef WIN32
27 #define NATIVE_PATH_SEP "/"
28#else
29 #define NATIVE_PATH_SEP "\\"
30#endif
31
32static tr_session * session;
33
34static char *
35create_test_dir (const char * name)
36{
37  char * const test_dir = tr_buildPath (tr_sessionGetConfigDir (session), name, NULL);
38  tr_mkdirp (test_dir, 0777);
39  return test_dir;
40}
41
42static bool
43create_symlink (const char * dst_path, const char * src_path, bool dst_is_dir)
44{
45#ifndef WIN32
46
47  (void) dst_is_dir;
48
49  return symlink (src_path, dst_path) != -1;
50
51#else
52
53  wchar_t * wide_src_path;
54  wchar_t * wide_dst_path;
55  bool ret = false;
56
57  wide_src_path = tr_win32_utf8_to_native (src_path, -1);
58  wide_dst_path = tr_win32_utf8_to_native (dst_path, -1);
59
60  ret = CreateSymbolicLinkW (wide_dst_path, wide_src_path,
61                             dst_is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0);
62
63  tr_free (wide_dst_path);
64  tr_free (wide_src_path);
65
66  return ret;
67
68#endif
69}
70
71static bool
72create_hardlink (const char * dst_path, const char * src_path)
73{
74#ifndef WIN32
75
76  return link (src_path, dst_path) != -1;
77
78#else
79
80  wchar_t * wide_src_path = tr_win32_utf8_to_native (src_path, -1);
81  wchar_t * wide_dst_path = tr_win32_utf8_to_native (dst_path, -1);
82
83  bool ret = CreateHardLinkW (wide_dst_path, wide_src_path, NULL);
84
85  tr_free (wide_dst_path);
86  tr_free (wide_src_path);
87
88  return ret;
89
90#endif
91}
92
93static void
94clear_path_info (tr_sys_path_info * info)
95{
96  info->type = (tr_sys_path_type_t)-1;
97  info->size = (uint64_t)-1;
98  info->last_modified_at = (time_t)-1;
99}
100
101static bool
102path_contains_no_symlinks (const char * path)
103{
104  const char * p = path;
105
106  while (*p != '\0')
107    {
108      tr_sys_path_info info;
109      char * pathPart;
110      const char * slashPos = strchr (p, '/');
111
112#ifdef WIN32
113
114      const char * backslashPos = strchr (p, '\\');
115      if (slashPos == NULL || (backslashPos != NULL && backslashPos < slashPos))
116        slashPos = backslashPos;
117
118#endif
119
120      if (slashPos == NULL)
121        slashPos = p + strlen (p) - 1;
122
123      pathPart = tr_strndup (path, slashPos - path + 1);
124
125      if (!tr_sys_path_get_info (pathPart, TR_SYS_PATH_NO_FOLLOW, &info, NULL) ||
126          (info.type != TR_SYS_PATH_IS_FILE && info.type != TR_SYS_PATH_IS_DIRECTORY))
127        {
128          tr_free (pathPart);
129          return false;
130        }
131
132      tr_free (pathPart);
133
134      p = slashPos + 1;
135    }
136
137  return true;
138}
139
140static int
141test_get_info (void)
142{
143  char * const test_dir = create_test_dir (__FUNCTION__);
144  tr_sys_path_info info;
145  tr_error * err = NULL;
146  char * path1, * path2;
147
148  path1 = tr_buildPath (test_dir, "a", NULL);
149  path2 = tr_buildPath (test_dir, "b", NULL);
150
151  /* Can't get info of non-existent file/directory */
152  check (!tr_sys_path_get_info (path1, 0, &info, &err));
153  check (err != NULL);
154  tr_error_clear (&err);
155
156  libtest_create_file_with_string_contents (path1, "test");
157
158  /* Good file info */
159  clear_path_info (&info);
160  check (tr_sys_path_get_info (path1, 0, &info, &err));
161  check (err == NULL);
162  check_int_eq (TR_SYS_PATH_IS_FILE, info.type);
163  check_int_eq (4, info.size);
164  check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
165
166  tr_sys_path_remove (path1, NULL);
167
168  /* Good directory info */
169  tr_mkdirp (path1, 0777);
170  clear_path_info (&info);
171  check (tr_sys_path_get_info (path1, 0, &info, &err));
172  check (err == NULL);
173  check_int_eq (TR_SYS_PATH_IS_DIRECTORY, info.type);
174  check (info.size != (uint64_t)-1);
175  check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
176  tr_sys_path_remove (path1, NULL);
177
178  if (create_symlink (path1, path2, false))
179    {
180      /* Can't get info of non-existent file/directory */
181      check (!tr_sys_path_get_info (path1, 0, &info, &err));
182      check (err != NULL);
183      tr_error_clear (&err);
184
185      libtest_create_file_with_string_contents (path2, "test");
186
187      /* Good file info */
188      clear_path_info (&info);
189      check (tr_sys_path_get_info (path1, 0, &info, &err));
190      check (err == NULL);
191      check_int_eq (TR_SYS_PATH_IS_FILE, info.type);
192      check_int_eq (4, info.size);
193      check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
194
195      tr_sys_path_remove (path2, NULL);
196
197      /* Good directory info */
198      tr_mkdirp (path2, 0777);
199      clear_path_info (&info);
200      check (tr_sys_path_get_info (path1, 0, &info, &err));
201      check (err == NULL);
202      check_int_eq (TR_SYS_PATH_IS_DIRECTORY, info.type);
203      check (info.size != (uint64_t)-1);
204      check (info.last_modified_at >= time(0) - 1 && info.last_modified_at <= time(0));
205
206      tr_sys_path_remove (path2, NULL);
207      tr_sys_path_remove (path1, NULL);
208    }
209  else
210    {
211      fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__);
212    }
213
214  tr_free (path2);
215  tr_free (path1);
216
217  tr_free (test_dir);
218  return 0;
219}
220
221static int
222test_path_exists (void)
223{
224  char * const test_dir = create_test_dir (__FUNCTION__);
225  tr_error * err = NULL;
226  char * path1, * path2;
227
228  path1 = tr_buildPath (test_dir, "a", NULL);
229  path2 = tr_buildPath (test_dir, "b", NULL);
230
231  /* Non-existent file does not exist */
232  check (!tr_sys_path_exists (path1, &err));
233  check (err == NULL);
234
235  /* Create file and see that it exists */
236  libtest_create_file_with_string_contents (path1, "test");
237  check (tr_sys_path_exists (path1, &err));
238  check (err == NULL);
239
240  tr_sys_path_remove (path1, NULL);
241
242  /* Create directory and see that it exists */
243  tr_mkdirp (path1, 0777);
244  check (tr_sys_path_exists (path1, &err));
245  check (err == NULL);
246
247  tr_sys_path_remove (path1, NULL);
248
249  if (create_symlink (path1, path2, false))
250    {
251      /* Non-existent file does not exist (via symlink) */
252      check (!tr_sys_path_exists (path1, &err));
253      check (err == NULL);
254
255      /* Create file and see that it exists (via symlink) */
256      libtest_create_file_with_string_contents (path2, "test");
257      check (tr_sys_path_exists (path1, &err));
258      check (err == NULL);
259
260      tr_sys_path_remove (path2, NULL);
261
262      /* Create directory and see that it exists (via symlink) */
263      tr_mkdirp (path2, 0777);
264      check (tr_sys_path_exists (path1, &err));
265      check (err == NULL);
266
267      tr_sys_path_remove (path2, NULL);
268      tr_sys_path_remove (path1, NULL);
269    }
270  else
271    {
272      fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__);
273    }
274
275  tr_free (path2);
276  tr_free (path1);
277
278  tr_free (test_dir);
279  return 0;
280}
281
282static int
283test_path_is_same (void)
284{
285  char * const test_dir = create_test_dir (__FUNCTION__);
286  tr_error * err = NULL;
287  char * path1, * path2, * path3;
288
289  path1 = tr_buildPath (test_dir, "a", NULL);
290  path2 = tr_buildPath (test_dir, "b", NULL);
291  path3 = tr_buildPath (path2, "c", NULL);
292
293  /* Two non-existent files are not the same */
294  check (!tr_sys_path_is_same (path1, path1, &err));
295  check (err == NULL);
296  check (!tr_sys_path_is_same (path1, path2, &err));
297  check (err == NULL);
298
299  /* Two same files are the same */
300  libtest_create_file_with_string_contents (path1, "test");
301  check (tr_sys_path_is_same (path1, path1, &err));
302  check (err == NULL);
303
304  /* Existent and non-existent files are not the same */
305  check (!tr_sys_path_is_same (path1, path2, &err));
306  check (err == NULL);
307  check (!tr_sys_path_is_same (path2, path1, &err));
308  check (err == NULL);
309
310  /* Two separate files (even with same content) are not the same */
311  libtest_create_file_with_string_contents (path2, "test");
312  check (!tr_sys_path_is_same (path1, path2, &err));
313  check (err == NULL);
314
315  tr_sys_path_remove (path1, NULL);
316
317  /* Two same directories are the same */
318  tr_mkdirp (path1, 0777);
319  check (tr_sys_path_is_same (path1, path1, &err));
320  check (err == NULL);
321
322  /* File and directory are not the same */
323  check (!tr_sys_path_is_same (path1, path2, &err));
324  check (err == NULL);
325  check (!tr_sys_path_is_same (path2, path1, &err));
326  check (err == NULL);
327
328  tr_sys_path_remove (path2, NULL);
329
330  /* Two separate directories are not the same */
331  tr_mkdirp (path2, 0777);
332  check (!tr_sys_path_is_same (path1, path2, &err));
333  check (err == NULL);
334
335  tr_sys_path_remove (path1, NULL);
336  tr_sys_path_remove (path2, NULL);
337
338  if (create_symlink (path1, ".", true))
339    {
340      /* Directory and symlink pointing to it are the same */
341      check (tr_sys_path_is_same (path1, test_dir, &err));
342      check (err == NULL);
343      check (tr_sys_path_is_same (test_dir, path1, &err));
344      check (err == NULL);
345
346      /* Non-existent file and symlink are not the same */
347      check (!tr_sys_path_is_same (path1, path2, &err));
348      check (err == NULL);
349      check (!tr_sys_path_is_same (path2, path1, &err));
350      check (err == NULL);
351
352      /* Symlinks pointing to different directories are not the same */
353      create_symlink (path2, "..", true);
354      check (!tr_sys_path_is_same (path1, path2, &err));
355      check (err == NULL);
356      check (!tr_sys_path_is_same (path2, path1, &err));
357      check (err == NULL);
358
359      tr_sys_path_remove (path2, NULL);
360
361      /* Symlinks pointing to same directory are the same */
362      create_symlink (path2, ".", true);
363      check (tr_sys_path_is_same (path1, path2, &err));
364      check (err == NULL);
365
366      tr_sys_path_remove (path2, NULL);
367
368      /* Directory and symlink pointing to another directory are not the same */
369      tr_mkdirp (path2, 0777);
370      check (!tr_sys_path_is_same (path1, path2, &err));
371      check (err == NULL);
372      check (!tr_sys_path_is_same (path2, path1, &err));
373      check (err == NULL);
374
375      /* Symlinks pointing to same directory are the same */
376      create_symlink (path3, "..", true);
377      check (tr_sys_path_is_same (path1, path3, &err));
378      check (err == NULL);
379
380      tr_sys_path_remove (path1, NULL);
381
382      /* File and symlink pointing to directory are not the same */
383      libtest_create_file_with_string_contents (path1, "test");
384      check (!tr_sys_path_is_same (path1, path3, &err));
385      check (err == NULL);
386      check (!tr_sys_path_is_same (path3, path1, &err));
387      check (err == NULL);
388
389      tr_sys_path_remove (path3, NULL);
390
391      /* File and symlink pointing to same file are the same */
392      create_symlink (path3, path1, false);
393      check (tr_sys_path_is_same (path1, path3, &err));
394      check (err == NULL);
395      check (tr_sys_path_is_same (path3, path1, &err));
396      check (err == NULL);
397
398      /* Symlinks pointing to non-existent files are not the same */
399      tr_sys_path_remove (path1, NULL);
400      create_symlink (path1, "missing", false);
401      tr_sys_path_remove (path3, NULL);
402      create_symlink (path3, "missing", false);
403      check (!tr_sys_path_is_same (path1, path3, &err));
404      check (err == NULL);
405      check (!tr_sys_path_is_same (path3, path1, &err));
406      check (err == NULL);
407
408      tr_sys_path_remove (path3, NULL);
409
410      /* Symlinks pointing to same non-existent file are not the same */
411      create_symlink (path3, ".." NATIVE_PATH_SEP "missing", false);
412      check (!tr_sys_path_is_same (path1, path3, &err));
413      check (err == NULL);
414      check (!tr_sys_path_is_same (path3, path1, &err));
415      check (err == NULL);
416
417      /* Non-existent file and symlink pointing to non-existent file are not the same */
418      tr_sys_path_remove (path3, NULL);
419      check (!tr_sys_path_is_same (path1, path3, &err));
420      check (err == NULL);
421      check (!tr_sys_path_is_same (path3, path1, &err));
422      check (err == NULL);
423
424      tr_sys_path_remove (path2, NULL);
425      tr_sys_path_remove (path1, NULL);
426    }
427  else
428    {
429      fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__);
430    }
431
432  tr_free (path3);
433  path3 = tr_buildPath (test_dir, "c", NULL);
434
435  libtest_create_file_with_string_contents (path1, "test");
436
437  if (create_hardlink (path2, path1))
438    {
439      /* File and hardlink to it are the same */
440      check (tr_sys_path_is_same (path1, path2, &err));
441      check (err == NULL);
442
443      /* Two hardlinks to the same file are the same */
444      create_hardlink (path3, path2);
445      check (tr_sys_path_is_same (path2, path3, &err));
446      check (err == NULL);
447      check (tr_sys_path_is_same (path1, path3, &err));
448      check (err == NULL);
449
450      tr_sys_path_remove (path2, NULL);
451
452      check (tr_sys_path_is_same (path1, path3, &err));
453      check (err == NULL);
454
455      tr_sys_path_remove (path3, NULL);
456
457      /* File and hardlink to another file are not the same */
458      libtest_create_file_with_string_contents (path3, "test");
459      create_hardlink (path2, path3);
460      check (!tr_sys_path_is_same (path1, path2, &err));
461      check (err == NULL);
462      check (!tr_sys_path_is_same (path2, path1, &err));
463      check (err == NULL);
464
465      tr_sys_path_remove (path3, NULL);
466      tr_sys_path_remove (path2, NULL);
467    }
468  else
469    {
470      fprintf (stderr, "WARNING: [%s] unable to run hardlink tests\n", __FUNCTION__);
471    }
472
473  if (create_symlink (path2, path1, false) && create_hardlink (path3, path1))
474    {
475      check (tr_sys_path_is_same (path2, path3, &err));
476      check (err == NULL);
477    }
478  else
479    {
480      fprintf (stderr, "WARNING: [%s] unable to run combined symlink and hardlink tests\n", __FUNCTION__);
481    }
482
483  tr_sys_path_remove (path3, NULL);
484  tr_sys_path_remove (path2, NULL);
485  tr_sys_path_remove (path1, NULL);
486
487  tr_free (path3);
488  tr_free (path2);
489  tr_free (path1);
490
491  tr_free (test_dir);
492  return 0;
493}
494
495static int
496test_path_resolve (void)
497{
498  char * const test_dir = create_test_dir (__FUNCTION__);
499  tr_error * err = NULL;
500  char * path1, * path2;
501
502  path1 = tr_buildPath (test_dir, "a", NULL);
503  path2 = tr_buildPath (test_dir, "b", NULL);
504
505  libtest_create_file_with_string_contents (path1, "test");
506  if (create_symlink (path2, path1, false))
507    {
508      char * tmp;
509
510      tmp = tr_sys_path_resolve (path2, &err);
511      check (tmp != NULL);
512      check (err == NULL);
513      check (path_contains_no_symlinks (tmp));
514      tr_free (tmp);
515
516      tr_sys_path_remove (path1, NULL);
517      tr_mkdirp (path1, 0755);
518
519      tmp = tr_sys_path_resolve (path2, &err);
520      check (tmp != NULL);
521      check (err == NULL);
522      check (path_contains_no_symlinks (tmp));
523      tr_free (tmp);
524    }
525  else
526    {
527      fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__);
528    }
529
530  tr_sys_path_remove (path2, NULL);
531  tr_sys_path_remove (path1, NULL);
532
533  tr_free (path2);
534  tr_free (path1);
535
536  tr_free (test_dir);
537  return 0;
538}
539
540static int
541test_path_basename_dirname (void)
542{
543  tr_error * err = NULL;
544  char * name;
545
546  name = tr_sys_path_basename ("/a/b/c", &err);
547  check (name != NULL);
548  check (err == NULL);
549  check_streq ("c", name);
550  tr_free (name);
551
552  name = tr_sys_path_basename ("", &err);
553  check (name != NULL);
554  check (err == NULL);
555  check_streq (".", name);
556  tr_free (name);
557
558  name = tr_sys_path_dirname ("/a/b/c", &err);
559  check (name != NULL);
560  check (err == NULL);
561  check_streq ("/a/b", name);
562  tr_free (name);
563
564  name = tr_sys_path_dirname ("a/b/c", &err);
565  check (name != NULL);
566  check (err == NULL);
567  check_streq ("a/b", name);
568  tr_free (name);
569
570  name = tr_sys_path_dirname ("a", &err);
571  check (name != NULL);
572  check (err == NULL);
573  check_streq (".", name);
574  tr_free (name);
575
576  name = tr_sys_path_dirname ("", &err);
577  check (name != NULL);
578  check (err == NULL);
579  check_streq (".", name);
580  tr_free (name);
581
582#ifdef WIN32
583
584  name = tr_sys_path_basename ("c:\\a\\b\\c", &err);
585  check (name != NULL);
586  check (err == NULL);
587  check_streq ("c", name);
588  tr_free (name);
589
590  name = tr_sys_path_dirname ("C:\\a/b\\c", &err);
591  check (name != NULL);
592  check (err == NULL);
593  check_streq ("C:\\a/b", name);
594  tr_free (name);
595
596  name = tr_sys_path_dirname ("a/b\\c", &err);
597  check (name != NULL);
598  check (err == NULL);
599  check_streq ("a/b", name);
600  tr_free (name);
601
602#endif
603
604  return 0;
605}
606
607static int
608test_path_rename (void)
609{
610  char * const test_dir = create_test_dir (__FUNCTION__);
611  tr_error * err = NULL;
612  char * path1, * path2, * path3;
613
614  path1 = tr_buildPath (test_dir, "a", NULL);
615  path2 = tr_buildPath (test_dir, "b", NULL);
616  path3 = tr_buildPath (path2, "c", NULL);
617
618  libtest_create_file_with_string_contents (path1, "test");
619
620  /* Preconditions */
621  check (tr_sys_path_exists (path1, NULL));
622  check (!tr_sys_path_exists (path2, NULL));
623
624  /* Forward rename works */
625  check (tr_sys_path_rename (path1, path2, &err));
626  check (!tr_sys_path_exists (path1, NULL));
627  check (tr_sys_path_exists (path2, NULL));
628  check (err == NULL);
629
630  /* Backward rename works */
631  check (tr_sys_path_rename (path2, path1, &err));
632  check (tr_sys_path_exists (path1, NULL));
633  check (!tr_sys_path_exists (path2, NULL));
634  check (err == NULL);
635
636  /* Another backward rename [of non-existent file] does not work */
637  check (!tr_sys_path_rename (path2, path1, &err));
638  check (err != NULL);
639  tr_error_clear (&err);
640
641  /* Rename to file which couldn't be created does not work */
642  check (!tr_sys_path_rename (path1, path3, &err));
643  check (err != NULL);
644  tr_error_clear (&err);
645
646  /* Rename of non-existent file does not work */
647  check (!tr_sys_path_rename (path3, path2, &err));
648  check (err != NULL);
649  tr_error_clear (&err);
650
651  libtest_create_file_with_string_contents (path2, "test");
652
653  /* Renaming file does overwrite existing file */
654  check (tr_sys_path_rename (path2, path1, &err));
655  check (err == NULL);
656
657  tr_mkdirp (path2, 0777);
658
659  /* Renaming file does not overwrite existing directory, and vice versa */
660  check (!tr_sys_path_rename (path1, path2, &err));
661  check (err != NULL);
662  tr_error_clear (&err);
663  check (!tr_sys_path_rename (path2, path1, &err));
664  check (err != NULL);
665  tr_error_clear (&err);
666
667  tr_sys_path_remove (path2, NULL);
668
669  tr_free (path3);
670  path3 = tr_buildPath (test_dir, "c", NULL);
671
672  if (create_symlink (path2, path1, false))
673    {
674      /* Preconditions */
675      check (tr_sys_path_exists (path2, NULL));
676      check (!tr_sys_path_exists (path3, NULL));
677      check (tr_sys_path_is_same (path1, path2, NULL));
678
679      /* Rename of symlink works, files stay the same */
680      check (tr_sys_path_rename (path2, path3, &err));
681      check (err == NULL);
682      check (!tr_sys_path_exists (path2, NULL));
683      check (tr_sys_path_exists (path3, NULL));
684      check (tr_sys_path_is_same (path1, path3, NULL));
685
686      tr_sys_path_remove (path3, NULL);
687    }
688  else
689    {
690      fprintf (stderr, "WARNING: [%s] unable to run symlink tests\n", __FUNCTION__);
691    }
692
693  if (create_hardlink (path2, path1))
694    {
695      /* Preconditions */
696      check (tr_sys_path_exists (path2, NULL));
697      check (!tr_sys_path_exists (path3, NULL));
698      check (tr_sys_path_is_same (path1, path2, NULL));
699
700      /* Rename of hardlink works, files stay the same */
701      check (tr_sys_path_rename (path2, path3, &err));
702      check (err == NULL);
703      check (!tr_sys_path_exists (path2, NULL));
704      check (tr_sys_path_exists (path3, NULL));
705      check (tr_sys_path_is_same (path1, path3, NULL));
706
707      tr_sys_path_remove (path3, NULL);
708    }
709  else
710    {
711      fprintf (stderr, "WARNING: [%s] unable to run hardlink tests\n", __FUNCTION__);
712    }
713
714  tr_sys_path_remove (path1, NULL);
715
716  tr_free (path3);
717  tr_free (path2);
718  tr_free (path1);
719
720  tr_free (test_dir);
721  return 0;
722}
723
724static int
725test_path_remove (void)
726{
727  char * const test_dir = create_test_dir (__FUNCTION__);
728  tr_error * err = NULL;
729  char * path1, * path2, * path3;
730
731  path1 = tr_buildPath (test_dir, "a", NULL);
732  path2 = tr_buildPath (test_dir, "b", NULL);
733  path3 = tr_buildPath (path2, "c", NULL);
734
735  /* Can't remove non-existent file/directory */
736  check (!tr_sys_path_exists (path1, NULL));
737  check (!tr_sys_path_remove (path1, &err));
738  check (err != NULL);
739  check (!tr_sys_path_exists (path1, NULL));
740  tr_error_clear (&err);
741
742  /* Removing file works */
743  libtest_create_file_with_string_contents (path1, "test");
744  check (tr_sys_path_exists (path1, NULL));
745  check (tr_sys_path_remove (path1, &err));
746  check (err == NULL);
747  check (!tr_sys_path_exists (path1, NULL));
748
749  /* Removing empty directory works */
750  tr_mkdirp (path1, 0777);
751  check (tr_sys_path_exists (path1, NULL));
752  check (tr_sys_path_remove (path1, &err));
753  check (err == NULL);
754  check (!tr_sys_path_exists (path1, NULL));
755
756  /* Removing non-empty directory fails */
757  tr_mkdirp (path2, 0777);
758  libtest_create_file_with_string_contents (path3, "test");
759  check (tr_sys_path_exists (path2, NULL));
760  check (tr_sys_path_exists (path3, NULL));
761  check (!tr_sys_path_remove (path2, &err));
762  check (err != NULL);
763  check (tr_sys_path_exists (path2, NULL));
764  check (tr_sys_path_exists (path3, NULL));
765  tr_error_clear (&err);
766
767  tr_sys_path_remove (path3, NULL);
768  tr_sys_path_remove (path2, NULL);
769
770  tr_free (path3);
771  tr_free (path2);
772  tr_free (path1);
773
774  tr_free (test_dir);
775  return 0;
776}
777
778int
779main (void)
780{
781  const testFunc tests[] =
782    {
783      test_get_info,
784      test_path_exists,
785      test_path_is_same,
786      test_path_resolve,
787      test_path_basename_dirname,
788      test_path_rename,
789      test_path_remove
790    };
791  int ret;
792
793  /* init the session */
794  session = libttest_session_init (NULL);
795
796  ret = runTests (tests, NUM_TESTS (tests));
797
798  if (ret == 0)
799    libttest_session_close (session);
800
801  return ret;
802}
Note: See TracBrowser for help on using the repository browser.