tempdir.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. lib/vfs - manipulations with temp files and dirs
  3. Copyright (C) 2012
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2012
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include <config.h>
  21. #include <check.h>
  22. #include "lib/global.c"
  23. #ifndef HAVE_CHARSET
  24. #define HAVE_CHARSET 1
  25. #endif
  26. #include "lib/charsets.h"
  27. #include "lib/strutil.h"
  28. #include "lib/vfs/xdirentry.h"
  29. #include "lib/vfs/path.h"
  30. #include "src/vfs/local/local.c"
  31. struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
  32. struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
  33. static void
  34. setup (void)
  35. {
  36. str_init_strings (NULL);
  37. vfs_init ();
  38. init_localfs ();
  39. vfs_setup_work_dir ();
  40. }
  41. static void
  42. teardown (void)
  43. {
  44. vfs_shut ();
  45. str_uninit_strings ();
  46. }
  47. /* --------------------------------------------------------------------------------------------- */
  48. START_TEST (test_mc_tmpdir)
  49. {
  50. const char *tmpdir;
  51. const char *env_tmpdir;
  52. tmpdir = mc_tmpdir();
  53. fail_unless (
  54. g_file_test (tmpdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR),
  55. "\nNo such directory: %s\n", tmpdir
  56. );
  57. env_tmpdir = g_getenv ("MC_TMPDIR");
  58. fail_unless (
  59. strcmp (env_tmpdir, tmpdir) == 0,
  60. "\nenv_tmpdir=%s\n tmpdir=%s\n", env_tmpdir, tmpdir
  61. );
  62. }
  63. END_TEST
  64. START_TEST (test_mc_mkstemps)
  65. {
  66. vfs_path_t *pname_vpath = NULL;
  67. char *pname;
  68. char *begin_pname;
  69. int fd;
  70. fd = mc_mkstemps (&pname_vpath, "mctest-", NULL);
  71. if (fd == -1)
  72. {
  73. fail ("\nerror creating temp file!\n");
  74. }
  75. pname = vfs_path_to_str (pname_vpath);
  76. vfs_path_free (pname_vpath);
  77. close (fd);
  78. fail_unless (
  79. g_file_test (pname, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR),
  80. "\nNo such file: %s\n", pname
  81. );
  82. unlink(pname);
  83. begin_pname = g_build_filename (mc_tmpdir(), "mctest-", NULL);
  84. fail_unless (
  85. strncmp(pname, begin_pname, strlen(begin_pname)) == 0,
  86. "\nstart of %s should be equal to %s\n", pname, begin_pname
  87. );
  88. g_free (pname);
  89. }
  90. END_TEST
  91. /* --------------------------------------------------------------------------------------------- */
  92. int
  93. main (void)
  94. {
  95. int number_failed;
  96. Suite *s = suite_create (TEST_SUITE_NAME);
  97. TCase *tc_core = tcase_create ("Core");
  98. SRunner *sr;
  99. tcase_add_checked_fixture (tc_core, setup, teardown);
  100. /* Add new tests here: *************** */
  101. tcase_add_test (tc_core, test_mc_tmpdir);
  102. tcase_add_test (tc_core, test_mc_mkstemps);
  103. /* *********************************** */
  104. suite_add_tcase (s, tc_core);
  105. sr = srunner_create (s);
  106. srunner_set_log (sr, "tempdir.log");
  107. srunner_run_all (sr, CK_NORMAL);
  108. number_failed = srunner_ntests_failed (sr);
  109. srunner_free (sr);
  110. return (number_failed == 0) ? 0 : 1;
  111. }
  112. /* --------------------------------------------------------------------------------------------- */