dsdb: Align integer types
[Samba.git] / source3 / torture / test_hidenewfiles.c
blob2f1638a6ae7faba7f4b440c0abe801d8b470a377
1 /*
2 * Unix SMB/CIFS implementation.
3 * Test pthreadpool_tevent
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(const char *mntpoint,
66 struct file_info *f,
67 const char *mask,
68 void *private_data)
70 struct have_file_state *state = private_data;
71 state->found |= strequal(f->name, state->fname);
72 return NT_STATUS_OK;
75 static bool have_file(struct cli_state *cli, const char *fname)
77 struct have_file_state state = { .fname = fname };
78 NTSTATUS status;
80 status = cli_list(
81 cli,
82 "*.*",
83 FILE_ATTRIBUTE_DIRECTORY|
84 FILE_ATTRIBUTE_SYSTEM|
85 FILE_ATTRIBUTE_HIDDEN,
86 have_file_fn,
87 &state);
88 if (!NT_STATUS_IS_OK(status)) {
89 d_printf("cli_list failed: %s\n", nt_errstr(status));
90 return false;
93 return state.found;
96 bool run_hidenewfiles(int dummy)
98 const char *tsname = "timestamp.txt";
99 const char *fname = "new_hidden.txt";
100 struct cli_state *cli;
101 struct smb_create_returns cr;
102 struct timeval create_time;
103 uint16_t fnum;
104 NTSTATUS status;
105 bool ret = false;
106 bool gotit = false;
107 bool ok;
109 /* what is configure in smb.conf */
110 unsigned hideunreadable_seconds = 5;
112 ok = torture_open_connection(&cli, 0);
113 if (!ok) {
114 return false;
117 cli_unlink(cli, tsname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
118 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
120 status = cli_ntcreate(
121 cli,
122 fname,
124 FILE_GENERIC_WRITE|DELETE_ACCESS,
125 FILE_ATTRIBUTE_NORMAL,
127 FILE_CREATE,
130 &fnum,
131 &cr);
132 if (!NT_STATUS_IS_OK(status)) {
133 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
134 return false;
136 nttime_to_timeval(&create_time, cr.last_write_time);
138 while (!gotit) {
139 struct timeval now;
140 double age;
142 gotit = have_file(cli, fname);
144 status = servertime(cli, tsname, &now);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("servertime failed: %s\n",
147 nt_errstr(status));
148 goto fail;
150 age = timeval_elapsed2(&create_time, &now);
152 if ((age < hideunreadable_seconds) && gotit) {
153 d_printf("Found file at age of %f\n", age);
154 goto fail;
156 if ((age > (hideunreadable_seconds*10)) && !gotit) {
157 d_printf("Did not find file after %f seconds\n", age);
158 goto fail;
160 if (gotit) {
161 break;
164 smb_msleep(1000);
167 ret = true;
168 fail:
169 cli_nt_delete_on_close(cli, fnum, true);
170 cli_close(cli, fnum);
172 return ret;