1 /*
2 lib/vfs - manipulations with temp files and dirs
3
4 Copyright (C) 2012-2025
5 Free Software Foundation, Inc.
6
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2012, 2013
9
10 This file is part of the Midnight Commander.
11
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
16
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 #define TEST_SUITE_NAME "/lib/vfs"
27
28 #include "tests/mctest.h"
29
30 #include "lib/charsets.h"
31 #include "lib/strutil.h"
32 #include "lib/vfs/xdirentry.h"
33 #include "lib/vfs/path.h"
34
35 #include "src/vfs/local/local.c"
36
37 /* --------------------------------------------------------------------------------------------- */
38
39 /* @Before */
40 static void
41 setup (void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
42 {
43 // Ensure that tests behave consistently irrespectively of the environment
44 g_unsetenv ("MC_TMPDIR");
45
46 str_init_strings (NULL);
47
48 vfs_init ();
49 vfs_init_localfs ();
50 vfs_setup_work_dir ();
51 }
52
53 /* --------------------------------------------------------------------------------------------- */
54
55 /* @After */
56 static void
57 teardown (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
58 {
59 vfs_shut ();
60 str_uninit_strings ();
61 }
62
63 /* --------------------------------------------------------------------------------------------- */
64
65 /* @Test */
66 START_TEST (test_mc_tmpdir)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
67 {
68 // given
69 const char *tmpdir;
70 const char *env_tmpdir;
71
72 // when
73 tmpdir = mc_tmpdir ();
74 env_tmpdir = g_getenv ("MC_TMPDIR");
75
76 // then
77 ck_assert_msg (g_file_test (tmpdir, G_FILE_TEST_EXISTS)
78 && g_file_test (tmpdir, G_FILE_TEST_IS_DIR),
79 "\nNo such directory: %s\n", tmpdir);
80 mctest_assert_str_eq (env_tmpdir, tmpdir);
81
82 rmdir (tmpdir);
83 }
84 END_TEST
85
86 /* --------------------------------------------------------------------------------------------- */
87
88 /* @Test */
89 START_TEST (test_mc_mkstemps)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
90 {
91 // given
92 vfs_path_t *pname_vpath = NULL;
93 const char *tmpdir;
94 char *begin_pname;
95 int fd;
96
97 // when
98 fd = mc_mkstemps (&pname_vpath, "mctest-", NULL);
99 tmpdir = mc_tmpdir ();
100 begin_pname = g_build_filename (tmpdir, "mctest-", (char *) NULL);
101
102 // then
103 close (fd);
104 ck_assert_int_ne (fd, -1);
105
106 const char *pname = vfs_path_as_str (pname_vpath);
107
108 ck_assert_msg (g_file_test (pname, G_FILE_TEST_EXISTS)
109 && g_file_test (pname, G_FILE_TEST_IS_REGULAR),
110 "\nNo such file: %s\n", pname);
111 unlink (pname);
112 rmdir (tmpdir);
113 ck_assert_msg (strncmp (pname, begin_pname, strlen (begin_pname)) == 0,
114 "\nstart of %s should be equal to %s\n", pname, begin_pname);
115 vfs_path_free (pname_vpath, TRUE);
116 }
117 END_TEST
118
119 /* --------------------------------------------------------------------------------------------- */
120
121 int
122 main (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
123 {
124 TCase *tc_core;
125
126 tc_core = tcase_create ("Core");
127
128 tcase_add_checked_fixture (tc_core, setup, teardown);
129
130 // Add new tests here: ***************
131 tcase_add_test (tc_core, test_mc_tmpdir);
132 tcase_add_test (tc_core, test_mc_mkstemps);
133 // ***********************************
134
135 return mctest_run_all (tc_core);
136 }
137
138 /* --------------------------------------------------------------------------------------------- */