2 Unix SMB/CIFS implementation.
4 Copyright (C) Guenther Deschner 2009
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/>.
21 #include "torture/smbtorture.h"
23 #include "torture/libnetapi/proto.h"
25 #define TORTURE_TEST_USER "testuser"
27 #define NETAPI_STATUS(tctx, x,y,fn) \
28 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d)\n", \
29 __LINE__, fn, libnetapi_get_error_string(x,y), y);
31 #define NETAPI_STATUS_MSG(tctx, x,y,fn,z) \
32 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d), %s\n", \
33 __LINE__, fn, libnetapi_get_error_string(x,y), y, z);
35 static NET_API_STATUS test_netgroupenum(struct torture_context *tctx,
38 const char *groupname)
40 NET_API_STATUS status;
41 uint32_t entries_read = 0;
42 uint32_t total_entries = 0;
43 uint32_t resume_handle = 0;
45 const char *current_name = NULL;
46 uint8_t *buffer = NULL;
49 struct GROUP_INFO_0 *info0 = NULL;
50 struct GROUP_INFO_1 *info1 = NULL;
51 struct GROUP_INFO_2 *info2 = NULL;
52 struct GROUP_INFO_3 *info3 = NULL;
54 torture_comment(tctx, "testing NetGroupEnum level %d\n", level);
57 status = NetGroupEnum(hostname,
64 if (status == 0 || status == ERROR_MORE_DATA) {
67 info0 = (struct GROUP_INFO_0 *)buffer;
70 info1 = (struct GROUP_INFO_1 *)buffer;
73 info2 = (struct GROUP_INFO_2 *)buffer;
76 info3 = (struct GROUP_INFO_3 *)buffer;
82 for (i=0; i<entries_read; i++) {
86 current_name = info0->grpi0_name;
89 current_name = info1->grpi1_name;
92 current_name = info2->grpi2_name;
95 current_name = info3->grpi3_name;
101 if (strcasecmp(current_name, groupname) == 0) {
120 NetApiBufferFree(buffer);
122 } while (status == ERROR_MORE_DATA);
129 torture_comment(tctx, "failed to get group\n");
136 static NET_API_STATUS test_netgroupgetusers(struct torture_context *tctx,
137 const char *hostname,
139 const char *groupname,
140 const char *username)
142 NET_API_STATUS status;
143 uint32_t entries_read = 0;
144 uint32_t total_entries = 0;
145 uint32_t resume_handle = 0;
147 const char *current_name = NULL;
148 uint8_t *buffer = NULL;
151 struct GROUP_USERS_INFO_0 *info0 = NULL;
152 struct GROUP_USERS_INFO_1 *info1 = NULL;
154 torture_comment(tctx, "testing NetGroupGetUsers level %d\n", level);
157 status = NetGroupGetUsers(hostname,
165 if (status == 0 || status == ERROR_MORE_DATA) {
169 info0 = (struct GROUP_USERS_INFO_0 *)buffer;
172 info1 = (struct GROUP_USERS_INFO_1 *)buffer;
177 for (i=0; i<entries_read; i++) {
180 current_name = info0->grui0_name;
183 current_name = info1->grui1_name;
189 if (username && strcasecmp(current_name, username) == 0) {
202 NetApiBufferFree(buffer);
204 } while (status == ERROR_MORE_DATA);
210 if (username && !found_user) {
211 torture_comment(tctx, "failed to get user\n");
218 static NET_API_STATUS test_netgroupsetusers(struct torture_context *tctx,
219 const char *hostname,
220 const char *groupname,
225 NET_API_STATUS status;
226 uint8_t *buffer = NULL;
230 struct GROUP_USERS_INFO_0 *g0 = NULL;
231 struct GROUP_USERS_INFO_1 *g1 = NULL;
233 torture_comment(tctx, "testing NetGroupSetUsers level %d\n", level);
237 buf_size = sizeof(struct GROUP_USERS_INFO_0) * num_entries;
239 status = NetApiBufferAllocate(buf_size, (void **)&g0);
244 for (i=0; i<num_entries; i++) {
245 g0[i].grui0_name = names[i];
248 buffer = (uint8_t *)g0;
251 buf_size = sizeof(struct GROUP_USERS_INFO_1) * num_entries;
253 status = NetApiBufferAllocate(buf_size, (void **)&g1);
258 for (i=0; i<num_entries; i++) {
259 g1[i].grui1_name = names[i];
262 buffer = (uint8_t *)g1;
268 /* NetGroupSetUsers */
270 status = NetGroupSetUsers(hostname,
280 NetApiBufferFree(buffer);
284 bool torture_libnetapi_group(struct torture_context *tctx)
286 NET_API_STATUS status = 0;
287 const char *username, *groupname, *groupname2;
288 uint8_t *buffer = NULL;
289 struct GROUP_INFO_0 g0;
290 uint32_t parm_err = 0;
291 uint32_t levels[] = { 0, 1, 2, 3};
292 uint32_t enum_levels[] = { 0, 1, 2, 3};
293 uint32_t getmem_levels[] = { 0, 1};
295 const char *hostname = torture_setting_string(tctx, "host", NULL);
296 struct libnetapi_ctx *ctx;
298 torture_assert(tctx, torture_libnetapi_init_context(tctx, &ctx),
299 "failed to initialize libnetapi");
301 torture_comment(tctx, "NetGroup tests\n");
303 username = "torture_test_user";
304 groupname = "torture_test_group";
305 groupname2 = "torture_test_group2";
308 NetGroupDel(hostname, groupname);
309 NetGroupDel(hostname, groupname2);
310 NetUserDel(hostname, username);
314 g0.grpi0_name = groupname;
316 torture_comment(tctx, "testing NetGroupAdd\n");
318 status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
320 NETAPI_STATUS(tctx, ctx, status, "NetGroupAdd");
324 /* 2nd add must fail */
326 status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
328 NETAPI_STATUS(tctx, ctx, status, "NetGroupAdd");
335 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
337 status = test_netgroupenum(tctx, hostname, enum_levels[i], groupname);
339 NETAPI_STATUS(tctx, ctx, status, "NetGroupEnum");
346 for (i=0; i<ARRAY_SIZE(levels); i++) {
348 torture_comment(tctx, "testing NetGroupGetInfo level %d\n", levels[i]);
350 status = NetGroupGetInfo(hostname, groupname, levels[i], &buffer);
351 if (status && status != 124) {
352 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetInfo");
359 g0.grpi0_name = groupname2;
361 torture_comment(tctx, "testing NetGroupSetInfo level 0\n");
363 status = NetGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err);
367 case 50: /* not supported */
368 case 124: /* not implemented */
369 groupname2 = groupname;
372 NETAPI_STATUS(tctx, ctx, status, "NetGroupSetInfo");
376 /* should not exist anymore */
378 status = NetGroupDel(hostname, groupname);
380 NETAPI_STATUS(tctx, ctx, status, "NetGroupDel");
387 for (i=0; i<ARRAY_SIZE(levels); i++) {
389 status = NetGroupGetInfo(hostname, groupname2, levels[i], &buffer);
390 if (status && status != 124) {
391 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetInfo");
396 /* add user to group */
398 status = test_netuseradd(tctx, hostname, username);
400 NETAPI_STATUS(tctx, ctx, status, "NetUserAdd");
404 /* should not be member */
406 for (i=0; i<ARRAY_SIZE(getmem_levels); i++) {
408 status = test_netgroupgetusers(tctx, hostname, getmem_levels[i], groupname2, NULL);
410 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetUsers");
415 torture_comment(tctx, "testing NetGroupAddUser\n");
417 status = NetGroupAddUser(hostname, groupname2, username);
419 NETAPI_STATUS(tctx, ctx, status, "NetGroupAddUser");
423 /* should be member */
425 for (i=0; i<ARRAY_SIZE(getmem_levels); i++) {
427 status = test_netgroupgetusers(tctx, hostname, getmem_levels[i], groupname2, username);
429 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetUsers");
434 torture_comment(tctx, "testing NetGroupDelUser\n");
436 status = NetGroupDelUser(hostname, groupname2, username);
438 NETAPI_STATUS(tctx, ctx, status, "NetGroupDelUser");
442 /* should not be member */
444 status = test_netgroupgetusers(tctx, hostname, 0, groupname2, NULL);
446 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetUsers");
450 /* set it again via exlicit member set */
452 status = test_netgroupsetusers(tctx, hostname, groupname2, 0, 1, &username);
454 NETAPI_STATUS(tctx, ctx, status, "NetGroupSetUsers");
458 /* should be member */
460 status = test_netgroupgetusers(tctx, hostname, 0, groupname2, username);
462 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetUsers");
466 /* wipe out member list */
468 status = test_netgroupsetusers(hostname, groupname2, 0, 0, NULL);
470 NETAPI_STATUS(tctx, ctx, status, "NetGroupSetUsers");
474 /* should not be member */
476 status = test_netgroupgetusers(hostname, 0, groupname2, NULL);
478 NETAPI_STATUS(tctx, ctx, status, "NetGroupGetUsers");
482 status = NetUserDel(hostname, username);
484 NETAPI_STATUS(tctx, ctx, status, "NetUserDel");
490 torture_comment(tctx, "testing NetGroupDel\n");
492 status = NetGroupDel(hostname, groupname2);
494 NETAPI_STATUS(tctx, ctx, status, "NetGroupDel");
498 /* should not exist anymore */
500 status = NetGroupGetInfo(hostname, groupname2, 0, &buffer);
502 NETAPI_STATUS_MSG(tctx, ctx, status, "NetGroupGetInfo", "expected failure and error code");
509 torture_comment(tctx, "NetGroup tests succeeded\n");
512 torture_comment(tctx, "NetGroup testsuite failed with: %s\n",
513 libnetapi_get_error_string(ctx, status));