r14720: Add torture_context argument to all torture tests
[kamenim/samba.git] / source4 / torture / libnet / libnet_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "torture/torture.h"
27
28
29 #define TEST_USERNAME  "libnetusertest"
30
31
32 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
33                          struct policy_handle *domain_handle, const char *username)
34 {
35         NTSTATUS status;
36         struct samr_LookupNames r1;
37         struct samr_OpenUser r2;
38         struct samr_DeleteUser r3;
39         struct samr_Close r4;
40         struct lsa_String names[2];
41         uint32_t rid;
42         struct policy_handle user_handle;
43
44         names[0].string = username;
45
46         r1.in.domain_handle  = domain_handle;
47         r1.in.num_names      = 1;
48         r1.in.names          = names;
49         
50         printf("user account lookup '%s'\n", username);
51
52         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("LookupNames failed - %s\n", nt_errstr(status));
55                 return False;
56         }
57
58         rid = r1.out.rids.ids[0];
59         
60         r2.in.domain_handle  = domain_handle;
61         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
62         r2.in.rid            = rid;
63         r2.out.user_handle   = &user_handle;
64
65         printf("opening user account\n");
66
67         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
68         if (!NT_STATUS_IS_OK(status)) {
69                 printf("OpenUser failed - %s\n", nt_errstr(status));
70                 return False;
71         }
72
73         r3.in.user_handle  = &user_handle;
74         r3.out.user_handle = &user_handle;
75
76         printf("deleting user account\n");
77         
78         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
79         if (!NT_STATUS_IS_OK(status)) {
80                 printf("DeleteUser failed - %s\n", nt_errstr(status));
81                 return False;
82         }
83
84         r4.in.handle = domain_handle;
85         r4.out.handle = domain_handle;
86
87         status = dcerpc_samr_Close(p, mem_ctx, &r4);
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("Close failed - %s\n", nt_errstr(status));
90                 return False;
91         }
92         
93         return True;
94 }
95
96
97 BOOL torture_createuser(struct torture_context *torture)
98 {
99         NTSTATUS status;
100         const char *binding;
101         TALLOC_CTX *mem_ctx;
102         struct libnet_context *ctx;
103         struct libnet_CreateUser req;
104
105         mem_ctx = talloc_init("test_createuser");
106         binding = lp_parm_string(-1, "torture", "binding");
107
108         ctx = libnet_context_init(NULL);
109         ctx->cred = cmdline_credentials;
110
111         req.in.user_name = TEST_USERNAME;
112         req.in.domain_name = lp_workgroup();
113
114         status = libnet_CreateUser(ctx, mem_ctx, &req);
115         if (!NT_STATUS_IS_OK(status)) {
116                 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
117                 return False;
118         }
119
120         if (!test_cleanup(ctx->pipe, mem_ctx, &ctx->domain_handle, TEST_USERNAME)) {
121                 printf("cleanup failed\n");
122                 return False;
123         }
124
125         return True;
126 }