r20456: Rename variables to avoid shadowing global function names.
[Samba.git] / source / torture / libnet / libnet_share.c
blobd60a9c538fc754d18b881fdb7963144ce2db22df
1 /*
2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Gregory LEOCADIE <gleocadie@idealx.com> 2005
6 Copyright (C) Rafal Szczesniak 2005
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "torture/rpc/rpc.h"
25 #include "libnet/libnet.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
30 #define TEST_SHARENAME "libnetsharetest"
33 static void test_displayshares(struct libnet_ListShares s)
35 int i, j;
37 struct share_type {
38 enum srvsvc_ShareType type;
39 const char *desc;
40 } share_types[] = {
41 { STYPE_DISKTREE, "STYPE_DISKTREE" },
42 { STYPE_DISKTREE_TEMPORARY, "STYPE_DISKTREE_TEMPORARY" },
43 { STYPE_DISKTREE_HIDDEN, "STYPE_DISKTREE_HIDDEN" },
44 { STYPE_PRINTQ, "STYPE_PRINTQ" },
45 { STYPE_PRINTQ_TEMPORARY, "STYPE_PRINTQ_TEMPORARY" },
46 { STYPE_PRINTQ_HIDDEN, "STYPE_PRINTQ_HIDDEN" },
47 { STYPE_DEVICE, "STYPE_DEVICE" },
48 { STYPE_DEVICE_TEMPORARY, "STYPE_DEVICE_TEMPORARY" },
49 { STYPE_DEVICE_HIDDEN, "STYPE_DEVICE_HIDDEN" },
50 { STYPE_IPC, "STYPE_IPC" },
51 { STYPE_IPC_TEMPORARY, "STYPE_IPC_TEMPORARY" },
52 { STYPE_IPC_HIDDEN, "STYPE_IPC_HIDDEN" }
55 switch (s.in.level) {
56 case 0:
57 for (i = 0; i < s.out.ctr.ctr0->count; i++) {
58 struct srvsvc_NetShareInfo0 *info = &s.out.ctr.ctr0->array[i];
59 d_printf("\t[%d] %s\n", i, info->name);
61 break;
63 case 1:
64 for (i = 0; i < s.out.ctr.ctr1->count; i++) {
65 struct srvsvc_NetShareInfo1 *info = &s.out.ctr.ctr1->array[i];
66 for (j = 0; j < ARRAY_SIZE(share_types); j++) {
67 if (share_types[j].type == info->type) break;
69 d_printf("\t[%d] %s (%s)\t%s\n", i, info->name,
70 info->comment, share_types[j].desc);
72 break;
74 case 2:
75 for (i = 0; i < s.out.ctr.ctr2->count; i++) {
76 struct srvsvc_NetShareInfo2 *info = &s.out.ctr.ctr2->array[i];
77 for (j = 0; j < ARRAY_SIZE(share_types); j++) {
78 if (share_types[j].type == info->type) break;
80 d_printf("\t[%d] %s\t%s\n\t %s\n\t [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s]\n",
81 i, info->name, share_types[j].desc, info->comment,
82 info->permissions, info->max_users,
83 info->current_users, info->path,
84 info->password);
86 break;
88 case 501:
89 for (i = 0; i < s.out.ctr.ctr501->count; i++) {
90 struct srvsvc_NetShareInfo501 *info = &s.out.ctr.ctr501->array[i];
91 for (j = 0; j < ARRAY_SIZE(share_types); j++) {
92 if (share_types[j].type == info->type) break;
94 d_printf("\t[%d] %s\t%s [csc_policy=0x%08x]\n\t %s\n", i, info->name,
95 share_types[j].desc, info->csc_policy,
96 info->comment);
98 break;
100 case 502:
101 for (i = 0; i < s.out.ctr.ctr502->count; i++) {
102 struct srvsvc_NetShareInfo502 *info = &s.out.ctr.ctr502->array[i];
103 for (j = 0; j < ARRAY_SIZE(share_types); j++) {
104 if (share_types[j].type == info->type) break;
106 d_printf("\t[%d] %s\t%s\n\t %s\n\t [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s, unknown=0x%08x]\n",
107 i, info->name, share_types[j].desc, info->comment,
108 info->permissions, info->max_users,
109 info->current_users, info->path,
110 info->password, info->unknown);
112 break;
117 BOOL torture_listshares(struct torture_context *torture)
119 struct libnet_ListShares share;
120 NTSTATUS status;
121 uint32_t levels[] = { 0, 1, 2, 501, 502 };
122 int i;
123 BOOL ret = True;
124 struct libnet_context* libnetctx;
125 const char *binding;
126 struct dcerpc_binding *bind;
127 TALLOC_CTX *mem_ctx;
129 mem_ctx = talloc_init("test_listshares");
130 binding = torture_setting_string(torture, "binding", NULL);
131 status = dcerpc_parse_binding(mem_ctx, binding, &bind);
132 if (!NT_STATUS_IS_OK(status)) {
133 printf("Error while parsing the binding string\n");
134 ret = False;
135 goto done;
138 libnetctx = libnet_context_init(NULL);
139 if (!libnetctx) {
140 printf("Couldn't allocate libnet context\n");
141 ret = False;
142 goto done;
145 libnetctx->cred = cmdline_credentials;
147 printf("Testing libnet_ListShare\n");
149 share.in.server_name = talloc_asprintf(mem_ctx, "%s", bind->host);
151 for (i = 0; i < ARRAY_SIZE(levels); i++) {
152 share.in.level = levels[i];
153 printf("testing libnet_ListShare level %u\n", share.in.level);
155 status = libnet_ListShares(libnetctx, mem_ctx, &share);
156 if (!NT_STATUS_IS_OK(status)) {
157 printf("libnet_ListShare level %u failed - %s\n", share.in.level, share.out.error_string);
158 ret = False;
159 goto done;
162 printf("listing shares:\n");
163 test_displayshares(share);
166 done:
167 talloc_free(mem_ctx);
168 return ret;
172 static BOOL test_addshare(struct dcerpc_pipe *svc_pipe, TALLOC_CTX *mem_ctx, const char *host,
173 const char* share)
175 NTSTATUS status;
176 struct srvsvc_NetShareAdd add;
177 struct srvsvc_NetShareInfo2 i;
179 i.name = share;
180 i.type = STYPE_DISKTREE;
181 i.path = "C:\\WINDOWS\\TEMP";
182 i.max_users = 5;
183 i.comment = "Comment to the test share";
184 i.password = NULL;
185 i.permissions = 0x0;
187 add.in.server_unc = host;
188 add.in.level = 2;
189 add.in.info.info2 = &i;
191 status = dcerpc_srvsvc_NetShareAdd(svc_pipe, mem_ctx, &add);
192 if (!NT_STATUS_IS_OK(status)) {
193 printf("Failed to add a new share\n");
194 return False;
197 printf("share added\n");
198 return True;
202 BOOL torture_delshare(struct torture_context *torture)
204 struct dcerpc_pipe *p;
205 struct dcerpc_binding *bind;
206 struct libnet_context* libnetctx;
207 const char *host, *binding;
208 TALLOC_CTX *mem_ctx;
209 NTSTATUS status;
210 BOOL ret = True;
211 struct libnet_DelShare share;
213 mem_ctx = talloc_init("test_listshares");
214 host = torture_setting_string(torture, "host", NULL);
215 binding = torture_setting_string(torture, "binding", NULL);
216 status = dcerpc_parse_binding(mem_ctx, binding, &bind);
217 if (!NT_STATUS_IS_OK(status)) {
218 printf("Error while parsing the binding string\n");
219 ret = False;
220 goto done;
223 libnetctx = libnet_context_init(NULL);
224 libnetctx->cred = cmdline_credentials;
226 status = torture_rpc_connection(mem_ctx,
228 &dcerpc_table_srvsvc);
230 if (!test_addshare(p, mem_ctx, host, TEST_SHARENAME)) {
231 ret = False;
232 goto done;
235 share.in.server_name = bind->host;
236 share.in.share_name = TEST_SHARENAME;
238 status = libnet_DelShare(libnetctx, mem_ctx, &share);
239 if (!NT_STATUS_IS_OK(status)) {
240 ret = False;
241 goto done;
245 done:
246 talloc_free(mem_ctx);
247 return ret;