• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/samba-3.5.8/source3/lib/netapi/tests/
1/*
2 *  Unix SMB/CIFS implementation.
3 *  NetShare testsuite
4 *  Copyright (C) Guenther Deschner 2008
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <sys/types.h>
21#include <inttypes.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <netapi.h>
27
28#include "common.h"
29
30static NET_API_STATUS test_netshareenum(const char *hostname,
31					uint32_t level,
32					const char *sharename)
33{
34	NET_API_STATUS status;
35	uint32_t entries_read = 0;
36	uint32_t total_entries = 0;
37	uint32_t resume_handle = 0;
38	int found_share = 0;
39	const char *current_name = NULL;
40	uint8_t *buffer = NULL;
41	int i;
42
43	struct SHARE_INFO_0 *i0 = NULL;
44	struct SHARE_INFO_1 *i1 = NULL;
45	struct SHARE_INFO_2 *i2 = NULL;
46
47	printf("testing NetShareEnum level %d\n", level);
48
49	do {
50		status = NetShareEnum(hostname,
51				      level,
52				      &buffer,
53				      (uint32_t)-1,
54				      &entries_read,
55				      &total_entries,
56				      &resume_handle);
57		if (status == 0 || status == ERROR_MORE_DATA) {
58			switch (level) {
59				case 0:
60					i0 = (struct SHARE_INFO_0 *)buffer;
61					break;
62				case 1:
63					i1 = (struct SHARE_INFO_1 *)buffer;
64					break;
65				case 2:
66					i2 = (struct SHARE_INFO_2 *)buffer;
67					break;
68				default:
69					return -1;
70			}
71
72			for (i=0; i<entries_read; i++) {
73
74				switch (level) {
75					case 0:
76						current_name = i0->shi0_netname;
77						break;
78					case 1:
79						current_name = i1->shi1_netname;
80						break;
81					case 2:
82						current_name = i2->shi2_netname;
83						break;
84					default:
85						break;
86				}
87
88				if (strcasecmp(current_name, sharename) == 0) {
89					found_share = 1;
90				}
91
92				switch (level) {
93					case 0:
94						i0++;
95						break;
96					case 1:
97						i1++;
98						break;
99					case 2:
100						i2++;
101						break;
102				}
103			}
104			NetApiBufferFree(buffer);
105		}
106	} while (status == ERROR_MORE_DATA);
107
108	if (status) {
109		return status;
110	}
111
112	if (!found_share) {
113		printf("failed to get share\n");
114		return -1;
115	}
116
117	return 0;
118}
119
120NET_API_STATUS netapitest_share(struct libnetapi_ctx *ctx,
121				const char *hostname)
122{
123	NET_API_STATUS status = 0;
124	const char *sharename, *comment;
125	uint8_t *buffer = NULL;
126	struct SHARE_INFO_2 i2;
127	struct SHARE_INFO_1004 i1004;
128	struct SHARE_INFO_501 *i501 = NULL;
129	uint32_t parm_err = 0;
130	uint32_t levels[] = { 0, 1, 2, 501, 1005 };
131	uint32_t enum_levels[] = { 0, 1, 2 };
132	int i;
133
134	printf("NetShare tests\n");
135
136	sharename = "torture_test_share";
137
138	/* cleanup */
139	NetShareDel(hostname, sharename, 0);
140
141	/* add a share */
142
143	printf("testing NetShareAdd\n");
144
145	ZERO_STRUCT(i2);
146
147	i2.shi2_netname = sharename;
148	i2.shi2_path = "c:\\";
149
150	status = NetShareAdd(hostname, 2, (uint8_t *)&i2, &parm_err);
151	if (status) {
152		NETAPI_STATUS(ctx, status, "NetShareAdd");
153		goto out;
154	};
155
156	/* test enum */
157
158	for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
159
160		status = test_netshareenum(hostname, enum_levels[i], sharename);
161		if (status) {
162			NETAPI_STATUS(ctx, status, "NetShareEnum");
163			goto out;
164		}
165	}
166
167	/* basic queries */
168
169	for (i=0; i<ARRAY_SIZE(levels); i++) {
170
171		printf("testing NetShareGetInfo level %d\n", levels[i]);
172
173		status = NetShareGetInfo(hostname, sharename, levels[i], &buffer);
174		if (status && status != 124) {
175			NETAPI_STATUS(ctx, status, "NetShareGetInfo");
176			goto out;
177		}
178	}
179
180
181	comment = "NetApi generated comment";
182
183	i1004.shi1004_remark = comment;
184
185	printf("testing NetShareSetInfo level 1004\n");
186
187	status = NetShareSetInfo(hostname, sharename, 1004, (uint8_t *)&i1004, &parm_err);
188	if (status) {
189		NETAPI_STATUS(ctx, status, "NetShareSetInfo");
190		goto out;
191	}
192
193	status = NetShareGetInfo(hostname, sharename, 501, (uint8_t **)&i501);
194	if (status) {
195		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
196		goto out;
197	}
198
199	if (strcasecmp(i501->shi501_remark, comment) != 0) {
200		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
201		goto out;
202	}
203
204	/* delete */
205
206	printf("testing NetShareDel\n");
207
208	status = NetShareDel(hostname, sharename, 0);
209	if (status) {
210		NETAPI_STATUS(ctx, status, "NetShareDel");
211		goto out;
212	};
213
214	/* should not exist anymore */
215
216	status = NetShareGetInfo(hostname, sharename, 0, &buffer);
217	if (status == 0) {
218		NETAPI_STATUS(ctx, status, "NetShareGetInfo");
219		goto out;
220	};
221
222	status = 0;
223
224	printf("NetShare tests succeeded\n");
225 out:
226	if (status != 0) {
227		printf("NetShare testsuite failed with: %s\n",
228			libnetapi_get_error_string(ctx, status));
229	}
230
231	return status;
232}
233