libcli/auth: add netlogon_creds_cli_ServerGetTrustInfo*()
[Samba.git] / source3 / lib / netapi / tests / netlocalgroup.c
blob76c59c814b136d172cad7a533cd4462077d11f16
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetLocalGroup testsuite
4 * Copyright (C) Guenther Deschner 2008
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/>.
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <netapi.h>
28 #include "common.h"
30 static NET_API_STATUS test_netlocalgroupenum(const char *hostname,
31 uint32_t level,
32 const char *groupname)
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_group = 0;
39 const char *current_name = NULL;
40 uint8_t *buffer = NULL;
41 int i;
43 struct LOCALGROUP_INFO_0 *info0 = NULL;
44 struct LOCALGROUP_INFO_1 *info1 = NULL;
46 printf("testing NetLocalGroupEnum level %d\n", level);
48 do {
49 status = NetLocalGroupEnum(hostname,
50 level,
51 &buffer,
52 (uint32_t)-1,
53 &entries_read,
54 &total_entries,
55 &resume_handle);
56 if (status == 0 || status == ERROR_MORE_DATA) {
57 switch (level) {
58 case 0:
59 info0 = (struct LOCALGROUP_INFO_0 *)buffer;
60 break;
61 case 1:
62 info1 = (struct LOCALGROUP_INFO_1 *)buffer;
63 break;
64 default:
65 return -1;
68 for (i=0; i<entries_read; i++) {
70 switch (level) {
71 case 0:
72 current_name = info0->lgrpi0_name;
73 break;
74 case 1:
75 current_name = info1->lgrpi1_name;
76 break;
77 default:
78 break;
81 if (strcasecmp(current_name, groupname) == 0) {
82 found_group = 1;
85 switch (level) {
86 case 0:
87 info0++;
88 break;
89 case 1:
90 info1++;
91 break;
94 NetApiBufferFree(buffer);
96 } while (status == ERROR_MORE_DATA);
98 if (status) {
99 return status;
102 if (!found_group) {
103 printf("failed to get group\n");
104 return -1;
107 return 0;
110 NET_API_STATUS netapitest_localgroup(struct libnetapi_ctx *ctx,
111 const char *hostname)
113 NET_API_STATUS status = 0;
114 const char *groupname, *groupname2;
115 uint8_t *buffer = NULL;
116 struct LOCALGROUP_INFO_0 g0;
117 uint32_t parm_err = 0;
118 uint32_t levels[] = { 0, 1, 1002 };
119 uint32_t enum_levels[] = { 0, 1 };
120 int i;
122 printf("NetLocalgroup tests\n");
124 groupname = "torture_test_localgroup";
125 groupname2 = "torture_test_localgroup2";
127 /* cleanup */
128 NetLocalGroupDel(hostname, groupname);
129 NetLocalGroupDel(hostname, groupname2);
131 /* add a localgroup */
133 printf("testing NetLocalGroupAdd\n");
135 g0.lgrpi0_name = groupname;
137 status = NetLocalGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
138 if (status) {
139 NETAPI_STATUS(ctx, status, "NetLocalGroupAdd");
140 goto out;
143 /* test enum */
145 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
147 status = test_netlocalgroupenum(hostname, enum_levels[i], groupname);
148 if (status) {
149 NETAPI_STATUS(ctx, status, "NetLocalGroupEnum");
150 goto out;
155 /* basic queries */
157 for (i=0; i<ARRAY_SIZE(levels); i++) {
159 printf("testing NetLocalGroupGetInfo level %d\n", levels[i]);
161 status = NetLocalGroupGetInfo(hostname, groupname, levels[i], &buffer);
162 if (status && status != 124) {
163 NETAPI_STATUS(ctx, status, "NetLocalGroupGetInfo");
164 goto out;
168 /* alias rename */
170 printf("testing NetLocalGroupSetInfo level 0\n");
172 g0.lgrpi0_name = groupname2;
174 status = NetLocalGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err);
175 if (status) {
176 NETAPI_STATUS(ctx, status, "NetLocalGroupSetInfo");
177 goto out;
180 /* should not exist anymore */
182 status = NetLocalGroupDel(hostname, groupname);
183 if (status == 0) {
184 NETAPI_STATUS(ctx, status, "NetLocalGroupDel");
185 goto out;
188 /* query info */
190 for (i=0; i<ARRAY_SIZE(levels); i++) {
191 status = NetLocalGroupGetInfo(hostname, groupname2, levels[i], &buffer);
192 if (status && status != 124) {
193 NETAPI_STATUS(ctx, status, "NetLocalGroupGetInfo");
194 goto out;
198 /* delete */
200 printf("testing NetLocalGroupDel\n");
202 status = NetLocalGroupDel(hostname, groupname2);
203 if (status) {
204 NETAPI_STATUS(ctx, status, "NetLocalGroupDel");
205 goto out;
208 /* should not exist anymore */
210 status = NetLocalGroupGetInfo(hostname, groupname2, 0, &buffer);
211 if (status == 0) {
212 NETAPI_STATUS(ctx, status, "NetLocalGroupGetInfo");
213 goto out;
216 status = 0;
218 printf("NetLocalgroup tests succeeded\n");
219 out:
220 if (status != 0) {
221 printf("NetLocalGroup testsuite failed with: %s\n",
222 libnetapi_get_error_string(ctx, status));
225 return status;