49e8a5975c8a9324250a3bf7af933a72d876b5ba
[amitay/samba.git] / source4 / torture / basic / attr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    openattr tester
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25
26 extern int torture_failures;
27
28 #define CHECK_MAX_FAILURES(label) do { if (++failures >= torture_failures) goto label; } while (0)
29
30
31 static const uint32_t open_attrs_table[] = {
32                 FILE_ATTRIBUTE_NORMAL,
33                 FILE_ATTRIBUTE_ARCHIVE,
34                 FILE_ATTRIBUTE_READONLY,
35                 FILE_ATTRIBUTE_HIDDEN,
36                 FILE_ATTRIBUTE_SYSTEM,
37
38                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY,
39                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN,
40                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,
41                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
42                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
43                 FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
44
45                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN,
46                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM,
47                 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
48                 FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_SYSTEM,
49 };
50
51 struct trunc_open_results {
52         unsigned int num;
53         uint32_t init_attr;
54         uint32_t trunc_attr;
55         uint32_t result_attr;
56 };
57
58 static const struct trunc_open_results attr_results[] = {
59         { 0, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
60         { 1, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
61         { 2, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
62         { 16, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_ARCHIVE },
63         { 17, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ARCHIVE },
64         { 18, FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY },
65         { 51, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
66         { 54, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
67         { 56, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
68         { 68, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
69         { 71, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
70         { 73, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
71         { 99, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
72         { 102, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
73         { 104, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
74         { 116, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
75         { 119,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM,  FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
76         { 121, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM },
77         { 170, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN },
78         { 173, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM },
79         { 227, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
80         { 230, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN },
81         { 232, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN },
82         { 244, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
83         { 247, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_SYSTEM },
84         { 249, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM }
85 };
86
87
88 bool torture_openattrtest(struct torture_context *tctx, 
89                           struct smbcli_state *cli1)
90 {
91         const char *fname = "\\openattr.file";
92         int fnum1;
93         uint16_t attr;
94         unsigned int i, j, k, l;
95         int failures = 0;
96
97         for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32_t); i++) {
98                 smbcli_setatr(cli1->tree, fname, 0, 0);
99                 smbcli_unlink(cli1->tree, fname);
100                 fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
101                                               SEC_FILE_WRITE_DATA, 
102                                               open_attrs_table[i],
103                                               NTCREATEX_SHARE_ACCESS_NONE, NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
104                 
105                 torture_assert(tctx, fnum1 != -1, talloc_asprintf(tctx, "open %d (1) of %s failed (%s)", i, 
106                                            fname, smbcli_errstr(cli1->tree)));
107
108                 torture_assert_ntstatus_ok(tctx, 
109                                                         smbcli_close(cli1->tree, fnum1),
110                                                         talloc_asprintf(tctx, "close %d (1) of %s failed (%s)", i, fname, 
111                                                         smbcli_errstr(cli1->tree)));
112
113                 for (j = 0; j < ARRAY_SIZE(open_attrs_table); j++) {
114                         fnum1 = smbcli_nt_create_full(cli1->tree, fname, 0, 
115                                                       SEC_FILE_READ_DATA|
116                                                       SEC_FILE_WRITE_DATA, 
117                                                       open_attrs_table[j],
118                                                       NTCREATEX_SHARE_ACCESS_NONE, 
119                                                       NTCREATEX_DISP_OVERWRITE, 0, 0);
120
121                         if (fnum1 == -1) {
122                                 for (l = 0; l < ARRAY_SIZE(attr_results); l++) {
123                                         if (attr_results[l].num == k) {
124                                                 torture_comment(tctx, "[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(%s)\n",
125                                                                 k, open_attrs_table[i],
126                                                                 open_attrs_table[j],
127                                                                 fname, smbcli_errstr(cli1->tree));
128                                                 CHECK_MAX_FAILURES(error_exit);
129                                         }
130                                 }
131                                 torture_assert_ntstatus_equal(tctx, 
132                                         smbcli_nt_error(cli1->tree), NT_STATUS_ACCESS_DENIED, 
133                                         talloc_asprintf(tctx, "[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s",
134                                                         k, open_attrs_table[i], open_attrs_table[j],
135                                                         smbcli_errstr(cli1->tree)));
136                                         CHECK_MAX_FAILURES(error_exit);
137 #if 0
138                                 torture_comment(tctx, "[%d] trunc open 0x%x -> 0x%x failed\n", k, open_attrs_table[i], open_attrs_table[j]);
139 #endif
140                                 k++;
141                                 continue;
142                         }
143
144                         torture_assert_ntstatus_ok(tctx, 
145                                                                            smbcli_close(cli1->tree, fnum1),
146                                                                         talloc_asprintf(tctx, "close %d (2) of %s failed (%s)", j, 
147                                                                         fname, smbcli_errstr(cli1->tree)));
148
149                         torture_assert_ntstatus_ok(tctx, 
150                                                 smbcli_getatr(cli1->tree, fname, &attr, NULL, NULL),
151                                                 talloc_asprintf(tctx, "getatr(2) failed (%s)", smbcli_errstr(cli1->tree)));
152
153 #if 0
154                         torture_comment(tctx, "[%d] getatr check [0x%x] trunc [0x%x] got attr 0x%x\n",
155                                         k,  open_attrs_table[i],  open_attrs_table[j], attr );
156 #endif
157
158                         for (l = 0; l < ARRAY_SIZE(attr_results); l++) {
159                                 if (attr_results[l].num == k) {
160                                         if (attr != attr_results[l].result_attr ||
161                                             open_attrs_table[i] != attr_results[l].init_attr ||
162                                             open_attrs_table[j] != attr_results[l].trunc_attr) {
163                                                 torture_comment(tctx, "[%d] getatr check failed. [0x%x] trunc [0x%x] got attr 0x%x, should be 0x%x\n",
164                                                        k, open_attrs_table[i],
165                                                        open_attrs_table[j],
166                                                        (unsigned int)attr,
167                                                        attr_results[l].result_attr);
168                                                 CHECK_MAX_FAILURES(error_exit);
169                                         }
170                                         break;
171                                 }
172                         }
173                         k++;
174                 }
175         }
176 error_exit:
177         smbcli_setatr(cli1->tree, fname, 0, 0);
178         smbcli_unlink(cli1->tree, fname);
179
180         return true;
181 }
182