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"
27 #define TORTURE_TEST_USER "testuser"
29 #define NETAPI_STATUS(tctx, x,y,fn) \
30 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d)\n", \
31 __LINE__, fn, libnetapi_get_error_string(x,y), y);
33 #define NETAPI_STATUS_MSG(tctx, x,y,fn,z) \
34 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d), %s\n", \
35 __LINE__, fn, libnetapi_get_error_string(x,y), y, z);
37 static NET_API_STATUS
test_netgroupenum(struct torture_context
*tctx
,
40 const char *groupname
)
42 NET_API_STATUS status
;
43 uint32_t entries_read
= 0;
44 uint32_t total_entries
= 0;
45 uint32_t resume_handle
= 0;
47 const char *current_name
= NULL
;
48 uint8_t *buffer
= NULL
;
51 struct GROUP_INFO_0
*info0
= NULL
;
52 struct GROUP_INFO_1
*info1
= NULL
;
53 struct GROUP_INFO_2
*info2
= NULL
;
54 struct GROUP_INFO_3
*info3
= NULL
;
56 torture_comment(tctx
, "Testing NetGroupEnum level %d\n", level
);
59 status
= NetGroupEnum(hostname
,
66 if (status
== 0 || status
== ERROR_MORE_DATA
) {
69 info0
= (struct GROUP_INFO_0
*)buffer
;
72 info1
= (struct GROUP_INFO_1
*)buffer
;
75 info2
= (struct GROUP_INFO_2
*)buffer
;
78 info3
= (struct GROUP_INFO_3
*)buffer
;
84 for (i
=0; i
<entries_read
; i
++) {
88 current_name
= info0
->grpi0_name
;
91 current_name
= info1
->grpi1_name
;
94 current_name
= info2
->grpi2_name
;
97 current_name
= info3
->grpi3_name
;
103 if (strcasecmp(current_name
, groupname
) == 0) {
122 NetApiBufferFree(buffer
);
124 } while (status
== ERROR_MORE_DATA
);
131 torture_comment(tctx
, "failed to get group\n");
138 static NET_API_STATUS
test_netgroupgetusers(struct torture_context
*tctx
,
139 const char *hostname
,
141 const char *groupname
,
142 const char *username
)
144 NET_API_STATUS status
;
145 uint32_t entries_read
= 0;
146 uint32_t total_entries
= 0;
147 uint32_t resume_handle
= 0;
149 const char *current_name
= NULL
;
150 uint8_t *buffer
= NULL
;
153 struct GROUP_USERS_INFO_0
*info0
= NULL
;
154 struct GROUP_USERS_INFO_1
*info1
= NULL
;
156 torture_comment(tctx
, "Testing NetGroupGetUsers level %d\n", level
);
159 status
= NetGroupGetUsers(hostname
,
167 if (status
== 0 || status
== ERROR_MORE_DATA
) {
171 info0
= (struct GROUP_USERS_INFO_0
*)buffer
;
174 info1
= (struct GROUP_USERS_INFO_1
*)buffer
;
179 for (i
=0; i
<entries_read
; i
++) {
182 current_name
= info0
->grui0_name
;
185 current_name
= info1
->grui1_name
;
191 if (username
&& strcasecmp(current_name
, username
) == 0) {
204 NetApiBufferFree(buffer
);
206 } while (status
== ERROR_MORE_DATA
);
212 if (username
&& !found_user
) {
213 torture_comment(tctx
, "failed to get user\n");
220 static NET_API_STATUS
test_netgroupsetusers(struct torture_context
*tctx
,
221 const char *hostname
,
222 const char *groupname
,
227 NET_API_STATUS status
;
228 uint8_t *buffer
= NULL
;
232 struct GROUP_USERS_INFO_0
*g0
= NULL
;
233 struct GROUP_USERS_INFO_1
*g1
= NULL
;
235 torture_comment(tctx
, "Testing NetGroupSetUsers level %d\n", level
);
239 buf_size
= sizeof(struct GROUP_USERS_INFO_0
) * num_entries
;
241 status
= NetApiBufferAllocate(buf_size
, (void **)&g0
);
246 for (i
=0; i
<num_entries
; i
++) {
247 g0
[i
].grui0_name
= names
[i
];
250 buffer
= (uint8_t *)g0
;
253 buf_size
= sizeof(struct GROUP_USERS_INFO_1
) * num_entries
;
255 status
= NetApiBufferAllocate(buf_size
, (void **)&g1
);
260 for (i
=0; i
<num_entries
; i
++) {
261 g1
[i
].grui1_name
= names
[i
];
264 buffer
= (uint8_t *)g1
;
270 /* NetGroupSetUsers */
272 status
= NetGroupSetUsers(hostname
,
282 NetApiBufferFree(buffer
);
286 bool torture_libnetapi_group(struct torture_context
*tctx
)
288 NET_API_STATUS status
= 0;
289 const char *username
, *groupname
, *groupname2
;
290 uint8_t *buffer
= NULL
;
291 struct GROUP_INFO_0 g0
;
292 uint32_t parm_err
= 0;
293 uint32_t levels
[] = { 0, 1, 2, 3};
294 uint32_t enum_levels
[] = { 0, 1, 2, 3};
295 uint32_t getmem_levels
[] = { 0, 1};
297 const char *hostname
= torture_setting_string(tctx
, "host", NULL
);
298 struct libnetapi_ctx
*ctx
;
300 torture_assert(tctx
, torture_libnetapi_init_context(tctx
, &ctx
),
301 "failed to initialize libnetapi");
303 torture_comment(tctx
, "NetGroup tests\n");
305 username
= "torture_test_user";
306 groupname
= "torture_test_group";
307 groupname2
= "torture_test_group2";
310 NetGroupDel(hostname
, groupname
);
311 NetGroupDel(hostname
, groupname2
);
312 NetUserDel(hostname
, username
);
316 g0
.grpi0_name
= groupname
;
318 torture_comment(tctx
, "Testing NetGroupAdd\n");
320 status
= NetGroupAdd(hostname
, 0, (uint8_t *)&g0
, &parm_err
);
322 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupAdd");
326 /* 2nd add must fail */
328 status
= NetGroupAdd(hostname
, 0, (uint8_t *)&g0
, &parm_err
);
330 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupAdd");
337 for (i
=0; i
<ARRAY_SIZE(enum_levels
); i
++) {
339 status
= test_netgroupenum(tctx
, hostname
, enum_levels
[i
], groupname
);
341 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupEnum");
348 for (i
=0; i
<ARRAY_SIZE(levels
); i
++) {
350 torture_comment(tctx
, "Testing NetGroupGetInfo level %d\n", levels
[i
]);
352 status
= NetGroupGetInfo(hostname
, groupname
, levels
[i
], &buffer
);
353 if (status
&& status
!= 124) {
354 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetInfo");
361 g0
.grpi0_name
= groupname2
;
363 torture_comment(tctx
, "Testing NetGroupSetInfo level 0\n");
365 status
= NetGroupSetInfo(hostname
, groupname
, 0, (uint8_t *)&g0
, &parm_err
);
366 switch ((uint32_t)status
) {
369 case 50: /* not supported */
370 case 124: /* not implemented */
371 groupname2
= groupname
;
374 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupSetInfo");
378 /* should not exist anymore */
380 status
= NetGroupDel(hostname
, groupname
);
382 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupDel");
389 for (i
=0; i
<ARRAY_SIZE(levels
); i
++) {
391 status
= NetGroupGetInfo(hostname
, groupname2
, levels
[i
], &buffer
);
392 if (status
&& status
!= 124) {
393 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetInfo");
398 /* add user to group */
400 status
= test_netuseradd(tctx
, hostname
, username
);
402 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserAdd");
406 /* should not be member */
408 for (i
=0; i
<ARRAY_SIZE(getmem_levels
); i
++) {
410 status
= test_netgroupgetusers(tctx
, hostname
, getmem_levels
[i
], groupname2
, NULL
);
412 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetUsers");
417 torture_comment(tctx
, "Testing NetGroupAddUser\n");
419 status
= NetGroupAddUser(hostname
, groupname2
, username
);
421 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupAddUser");
425 /* should be member */
427 for (i
=0; i
<ARRAY_SIZE(getmem_levels
); i
++) {
429 status
= test_netgroupgetusers(tctx
, hostname
, getmem_levels
[i
], groupname2
, username
);
431 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetUsers");
436 torture_comment(tctx
, "Testing NetGroupDelUser\n");
438 status
= NetGroupDelUser(hostname
, groupname2
, username
);
440 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupDelUser");
444 /* should not be member */
446 status
= test_netgroupgetusers(tctx
, hostname
, 0, groupname2
, NULL
);
448 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetUsers");
452 /* set it again via exlicit member set */
454 status
= test_netgroupsetusers(tctx
, hostname
, groupname2
, 0, 1, &username
);
456 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupSetUsers");
460 /* should be member */
462 status
= test_netgroupgetusers(tctx
, hostname
, 0, groupname2
, username
);
464 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetUsers");
468 /* wipe out member list */
470 status
= test_netgroupsetusers(hostname
, groupname2
, 0, 0, NULL
);
472 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupSetUsers");
476 /* should not be member */
478 status
= test_netgroupgetusers(hostname
, 0, groupname2
, NULL
);
480 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupGetUsers");
484 status
= NetUserDel(hostname
, username
);
486 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserDel");
492 torture_comment(tctx
, "Testing NetGroupDel\n");
494 status
= NetGroupDel(hostname
, groupname2
);
496 NETAPI_STATUS(tctx
, ctx
, status
, "NetGroupDel");
500 /* should not exist anymore */
502 status
= NetGroupGetInfo(hostname
, groupname2
, 0, &buffer
);
504 NETAPI_STATUS_MSG(tctx
, ctx
, status
, "NetGroupGetInfo", "expected failure and error code");
511 torture_comment(tctx
, "NetGroup tests succeeded\n");
514 torture_comment(tctx
, "NetGroup testsuite failed with: %s\n",
515 libnetapi_get_error_string(ctx
, status
));