source: branches/samba-3.5.x/source3/lib/netapi/tests/netlocalgroup.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 4.9 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * NetLocalGroup 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_netlocalgroupenum(const char *hostname,
31 uint32_t level,
32 const char *groupname)
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_group = 0;
39 const char *current_name = NULL;
40 uint8_t *buffer = NULL;
41 int i;
42
43 struct LOCALGROUP_INFO_0 *info0 = NULL;
44 struct LOCALGROUP_INFO_1 *info1 = NULL;
45
46 printf("testing NetLocalGroupEnum level %d\n", level);
47
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;
66 }
67
68 for (i=0; i<entries_read; i++) {
69
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;
79 }
80
81 if (strcasecmp(current_name, groupname) == 0) {
82 found_group = 1;
83 }
84
85 switch (level) {
86 case 0:
87 info0++;
88 break;
89 case 1:
90 info1++;
91 break;
92 }
93 }
94 NetApiBufferFree(buffer);
95 }
96 } while (status == ERROR_MORE_DATA);
97
98 if (status) {
99 return status;
100 }
101
102 if (!found_group) {
103 printf("failed to get group\n");
104 return -1;
105 }
106
107 return 0;
108}
109
110NET_API_STATUS netapitest_localgroup(struct libnetapi_ctx *ctx,
111 const char *hostname)
112{
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;
121
122 printf("NetLocalgroup tests\n");
123
124 groupname = "torture_test_localgroup";
125 groupname2 = "torture_test_localgroup2";
126
127 /* cleanup */
128 NetLocalGroupDel(hostname, groupname);
129 NetLocalGroupDel(hostname, groupname2);
130
131 /* add a localgroup */
132
133 printf("testing NetLocalGroupAdd\n");
134
135 g0.lgrpi0_name = groupname;
136
137 status = NetLocalGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
138 if (status) {
139 NETAPI_STATUS(ctx, status, "NetLocalGroupAdd");
140 goto out;
141 };
142
143 /* test enum */
144
145 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
146
147 status = test_netlocalgroupenum(hostname, enum_levels[i], groupname);
148 if (status) {
149 NETAPI_STATUS(ctx, status, "NetLocalGroupEnum");
150 goto out;
151 }
152 }
153
154
155 /* basic queries */
156
157 for (i=0; i<ARRAY_SIZE(levels); i++) {
158
159 printf("testing NetLocalGroupGetInfo level %d\n", levels[i]);
160
161 status = NetLocalGroupGetInfo(hostname, groupname, levels[i], &buffer);
162 if (status && status != 124) {
163 NETAPI_STATUS(ctx, status, "NetLocalGroupGetInfo");
164 goto out;
165 };
166 }
167
168 /* alias rename */
169
170 printf("testing NetLocalGroupSetInfo level 0\n");
171
172 g0.lgrpi0_name = groupname2;
173
174 status = NetLocalGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err);
175 if (status) {
176 NETAPI_STATUS(ctx, status, "NetLocalGroupSetInfo");
177 goto out;
178 };
179
180 /* should not exist anymore */
181
182 status = NetLocalGroupDel(hostname, groupname);
183 if (status == 0) {
184 NETAPI_STATUS(ctx, status, "NetLocalGroupDel");
185 goto out;
186 };
187
188 /* query info */
189
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;
195 };
196 }
197
198 /* delete */
199
200 printf("testing NetLocalGroupDel\n");
201
202 status = NetLocalGroupDel(hostname, groupname2);
203 if (status) {
204 NETAPI_STATUS(ctx, status, "NetLocalGroupDel");
205 goto out;
206 };
207
208 /* should not exist anymore */
209
210 status = NetLocalGroupGetInfo(hostname, groupname2, 0, &buffer);
211 if (status == 0) {
212 NETAPI_STATUS(ctx, status, "NetLocalGroupGetInfo");
213 goto out;
214 };
215
216 status = 0;
217
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));
223 }
224
225 return status;
226}
Note: See TracBrowser for help on using the repository browser.