528f34f8617426a7f13fe276195db77312addb58
[samba.git] / source4 / torture / local / share.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of share code
5
6    Copyright (C) Jelmer Vernooij 2007
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 "param/share.h"
24 #include "torture/torture.h"
25
26 static bool test_list_empty(struct torture_context *tctx, 
27                             const void *tcase_data, 
28                             const void *test_data)
29 {
30         struct share_context *ctx = discard_const(tcase_data);
31         int count;
32         const char **names;
33
34         torture_assert_ntstatus_ok(tctx, share_list_all(tctx, ctx, &count, &names),
35                                                            "share_list_all failed");
36
37         return true;
38 }
39
40 static bool test_create(struct torture_context *tctx, 
41                         const void *tcase_data, 
42                         const void *test_data)
43 {
44         struct share_context *ctx = discard_const(tcase_data);
45         int count;
46         const char **names;
47         int i;
48         bool found = false;
49         struct share_info inf[] = { 
50                 { SHARE_INFO_STRING, SHARE_TYPE, discard_const_p(void *, "IPC$") },
51                 { SHARE_INFO_STRING, SHARE_PATH, discard_const_p(void *, "/tmp/bla") }
52         };
53         NTSTATUS status;
54
55         status = share_create(ctx, "bloe", inf, 2);
56
57         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED))
58                 torture_skip(tctx, "Not supported by backend");
59
60         torture_assert_ntstatus_ok(tctx, status, "create_share failed");
61
62         torture_assert_ntstatus_ok(tctx, share_list_all(tctx, ctx, &count, &names),
63                                                            "share_list_all failed");
64
65         torture_assert(tctx, count >= 1, "creating share failed");
66
67
68         for (i = 0; i < count; i++) {
69                 found |= strcmp(names[i], "bloe") == 0;
70         }
71
72         torture_assert(tctx, found, "created share found");
73
74         return true;
75 }
76
77
78 static bool test_create_invalid(struct torture_context *tctx, 
79                                 const void *tcase_data, 
80                                 const void *test_data)
81 {
82         struct share_context *ctx = discard_const(tcase_data);
83         NTSTATUS status;
84
85         status = share_create(ctx, "bla", NULL, 0);
86
87         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED))
88                 torture_skip(tctx, "Not supported by backend");
89
90         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
91                                       status,
92                                       "create_share failed");
93
94         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
95                                       share_create(ctx, NULL, NULL, 0),
96                                       "create_share failed");
97
98         return true;
99 }
100
101 static bool test_share_remove_invalid(struct torture_context *tctx, 
102                                       const void *tcase_data, 
103                                       const void *test_data)
104 {
105         struct share_context *ctx = discard_const(tcase_data);
106         NTSTATUS status;
107
108         status = share_remove(ctx, "nonexistant");
109
110         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED))
111                 torture_skip(tctx, "Not supported by backend");
112
113         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_UNSUCCESSFUL, "remove fails");
114
115         return true;
116 }
117
118
119
120 static bool test_share_remove(struct torture_context *tctx, 
121                               const void *tcase_data, 
122                               const void *test_data)
123 {
124         struct share_context *ctx = discard_const(tcase_data);
125         struct share_info inf[] = { 
126                 { SHARE_INFO_STRING, SHARE_TYPE, discard_const_p(void *, "IPC$") },
127                 { SHARE_INFO_STRING, SHARE_PATH, discard_const_p(void *, "/tmp/bla") }
128         };
129         NTSTATUS status;
130
131         status = share_create(ctx, "blie", inf, 2);
132
133         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED))
134                 torture_skip(tctx, "Not supported by backend");
135
136         torture_assert_ntstatus_ok(tctx, status, "create_share failed");
137
138         torture_assert_ntstatus_ok(tctx, share_remove(ctx, "blie"), "remove failed");
139
140         return true;
141 }
142
143 static bool test_double_create(struct torture_context *tctx, 
144                                const void *tcase_data, 
145                                const void *test_data)
146 {
147         struct share_context *ctx = discard_const(tcase_data);
148         struct share_info inf[] = { 
149                 { SHARE_INFO_STRING, SHARE_TYPE, discard_const_p(void *, "IPC$") },
150                 { SHARE_INFO_STRING, SHARE_PATH, discard_const_p(void *, "/tmp/bla") }
151         };
152         NTSTATUS status;
153
154         status = share_create(ctx, "bla", inf, 2);
155
156         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED))
157                 torture_skip(tctx, "Not supported by backend");
158
159         torture_assert_ntstatus_ok(tctx, status, "create_share failed");
160
161         torture_assert_ntstatus_equal(tctx, NT_STATUS_OBJECT_NAME_COLLISION,
162                                       share_create(ctx, "bla", inf, 2),
163                                       "create_share failed");
164
165         return true;
166 }
167
168 static void tcase_add_share_tests(struct torture_tcase *tcase)
169 {
170         torture_tcase_add_test(tcase, "list_empty", test_list_empty, NULL);
171         torture_tcase_add_test(tcase, "share_create", test_create, NULL);
172         torture_tcase_add_test(tcase, "share_remove", test_share_remove, NULL);
173         torture_tcase_add_test(tcase, "share_remove_invalid", test_share_remove_invalid, NULL);
174         torture_tcase_add_test(tcase, "share_create_invalid", test_create_invalid, NULL);
175         torture_tcase_add_test(tcase, "share_double_create", test_double_create, NULL);
176 }
177
178 static BOOL setup_ldb(struct torture_context *tctx, void **data)
179 {
180         return NT_STATUS_IS_OK(share_get_context_by_name(tctx, "ldb", (struct share_context **)data));
181 }
182
183 static BOOL setup_classic(struct torture_context *tctx, void **data)
184 {
185         return NT_STATUS_IS_OK(share_get_context_by_name(tctx, "classic", (struct share_context **)data));
186 }
187
188 static BOOL teardown(struct torture_context *tctx, void *data)
189 {
190         talloc_free(data);
191         return true;
192 }
193
194 struct torture_suite *torture_local_share(TALLOC_CTX *mem_ctx)
195 {
196         struct torture_suite *suite = torture_suite_create(mem_ctx, "SHARE");
197         struct torture_tcase *tcase;
198
199         share_init();
200
201         tcase = torture_suite_add_tcase(suite, "ldb");
202         torture_tcase_set_fixture(tcase, setup_ldb, teardown);
203         tcase_add_share_tests(tcase);
204
205         tcase = torture_suite_add_tcase(suite, "classic");
206         torture_tcase_set_fixture(tcase, setup_classic, teardown);
207         tcase_add_share_tests(tcase);
208
209         return suite;
210 }