torture: Show that "hide new files timeout" also hides directories
[Samba.git] / source3 / torture / test_hidenewfiles.c
blob6d6811c7684448f077f443ba90a19c5976da7283
1 /*
2 * Unix SMB/CIFS implementation.
3 * Test "hide new files timeout"
4 * Copyright (C) Volker Lendecke 2018
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * 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/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "libsmb/libsmb.h"
23 #include "libcli/security/security.h"
25 static NTSTATUS servertime(
26 struct cli_state *cli, const char *fname, struct timeval *tv)
28 struct smb_create_returns cr;
29 NTSTATUS status;
30 uint16_t fnum;
32 status = cli_ntcreate(
33 cli,
34 fname,
36 FILE_GENERIC_WRITE|DELETE_ACCESS,
37 FILE_ATTRIBUTE_NORMAL,
39 FILE_CREATE,
40 FILE_DELETE_ON_CLOSE,
42 &fnum,
43 &cr);
44 if (!NT_STATUS_IS_OK(status)) {
45 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
46 return status;
49 status = cli_close(cli, fnum);
50 if (!NT_STATUS_IS_OK(status)) {
51 d_printf("cli_close failed: %s\n", nt_errstr(status));
52 return status;
55 nttime_to_timeval(tv, cr.creation_time);
57 return NT_STATUS_OK;
60 struct have_file_state {
61 bool found;
62 const char *fname;
65 static NTSTATUS have_file_fn(struct file_info *f,
66 const char *mask,
67 void *private_data)
69 struct have_file_state *state = private_data;
70 state->found |= strequal(f->name, state->fname);
71 return NT_STATUS_OK;
74 static bool have_file(struct cli_state *cli, const char *fname)
76 struct have_file_state state = { .fname = fname };
77 NTSTATUS status;
79 status = cli_list(
80 cli,
81 "*",
82 FILE_ATTRIBUTE_DIRECTORY|
83 FILE_ATTRIBUTE_SYSTEM|
84 FILE_ATTRIBUTE_HIDDEN,
85 have_file_fn,
86 &state);
87 if (!NT_STATUS_IS_OK(status)) {
88 d_printf("cli_list failed: %s\n", nt_errstr(status));
89 return false;
92 return state.found;
95 bool run_hidenewfiles(int dummy)
97 const char *tsname = "timestamp.txt";
98 const char *fname = "new_hidden.txt";
99 struct cli_state *cli;
100 struct smb_create_returns cr;
101 struct timeval create_time;
102 uint16_t fnum;
103 NTSTATUS status;
104 bool ret = false;
105 bool gotit = false;
106 bool ok;
108 /* what is configured in smb.conf */
109 unsigned hideunreadable_seconds = 5;
111 ok = torture_open_connection_flags(&cli, 0, 0);
112 if (!ok) {
113 return false;
116 cli_unlink(cli, tsname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
117 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
119 status = cli_ntcreate(
120 cli,
121 fname,
123 FILE_GENERIC_WRITE|DELETE_ACCESS,
124 FILE_ATTRIBUTE_NORMAL,
126 FILE_CREATE,
129 &fnum,
130 &cr);
131 if (!NT_STATUS_IS_OK(status)) {
132 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
133 return false;
135 nttime_to_timeval(&create_time, cr.last_write_time);
137 while (!gotit) {
138 struct timeval now;
139 double age;
141 gotit = have_file(cli, fname);
143 status = servertime(cli, tsname, &now);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_printf("servertime failed: %s\n",
146 nt_errstr(status));
147 goto fail;
149 age = timeval_elapsed2(&create_time, &now);
151 if ((age < hideunreadable_seconds) && gotit) {
152 d_printf("Found file at age of %f\n", age);
153 goto fail;
155 if ((age > (hideunreadable_seconds*10)) && !gotit) {
156 d_printf("Did not find file after %f seconds\n", age);
157 goto fail;
159 if (gotit) {
160 break;
163 smb_msleep(1000);
166 ret = true;
167 fail:
168 cli_nt_delete_on_close(cli, fnum, true);
169 cli_close(cli, fnum);
171 return ret;
174 bool run_hidenewfiles_showdirs(int dummy)
176 const char *dname = "dir";
177 const char *fname = "dir/x.txt";
178 struct cli_state *cli;
179 struct smb_create_returns cr;
180 struct timeval create_time;
181 uint16_t fnum = UINT16_MAX;
182 NTSTATUS status;
183 bool ret = false;
184 bool gotit = false;
185 bool ok;
187 ok = torture_open_connection_flags(&cli, 0, 0);
188 if (!ok) {
189 return false;
192 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
193 cli_rmdir(cli, dname);
195 status = cli_mkdir(cli, dname);
196 if (!NT_STATUS_IS_OK(status)) {
197 d_printf("mkdir(%s) failed: %s\n", dname, nt_errstr(status));
198 goto fail;
201 status = cli_ntcreate(
202 cli,
203 fname,
205 FILE_GENERIC_WRITE|DELETE_ACCESS,
206 FILE_ATTRIBUTE_NORMAL,
208 FILE_CREATE,
211 &fnum,
212 &cr);
213 if (!NT_STATUS_IS_OK(status)) {
214 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
215 goto fail;
217 nttime_to_timeval(&create_time, cr.last_write_time);
219 gotit = have_file(cli, dname);
220 if (!gotit) {
221 d_printf("%s was hidden\n", dname);
222 goto fail;
225 ret = true;
226 fail:
227 if (fnum != UINT16_MAX) {
228 cli_nt_delete_on_close(cli, fnum, true);
229 cli_close(cli, fnum);
231 cli_rmdir(cli, dname);
233 return ret;