55e4bc977fc90374264172e8ccb0880c06218d7c
[samba.git] / source4 / torture / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester utility functions
4    Copyright (C) Jelmer Vernooij 2006
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "torture/torture.h"
25 #include "libcli/raw/interfaces.h"
26 #include "libcli/raw/libcliraw.h"
27
28 /**
29  create a temporary directory.
30 */
31 _PUBLIC_ NTSTATUS torture_temp_dir(TALLOC_CTX *mem_ctx, const char *prefix, 
32                                                                    char **tempdir)
33 {
34         const char *basedir = lp_parm_string(-1, "torture", "basedir");
35         if (basedir == NULL) basedir = ".";
36         *tempdir = talloc_asprintf(mem_ctx, "%s/%s.XXXXXX", 
37                                                            basedir, prefix);
38
39         if (mkdtemp(*tempdir) == NULL)
40                 return NT_STATUS_UNSUCCESSFUL;
41
42         return NT_STATUS_OK;
43 }
44
45 /**
46   check if 2 NTTIMEs are equal.
47 */
48 BOOL nt_time_equal(NTTIME *t1, NTTIME *t2)
49 {
50         return *t1 == *t2;
51 }
52
53 NTSTATUS torture_second_tcon(TALLOC_CTX *mem_ctx,
54                              struct smbcli_session *session,
55                              const char *sharename,
56                              struct smbcli_tree **res)
57 {
58         union smb_tcon tcon;
59         struct smbcli_tree *result;
60         TALLOC_CTX *tmp_ctx;
61         NTSTATUS status;
62
63         if ((tmp_ctx = talloc_new(mem_ctx)) == NULL) {
64                 return NT_STATUS_NO_MEMORY;
65         }
66
67         result = smbcli_tree_init(session, tmp_ctx, False);
68         if (result == NULL) {
69                 talloc_free(tmp_ctx);
70                 return NT_STATUS_NO_MEMORY;
71         }
72
73         tcon.generic.level = RAW_TCON_TCONX;
74         tcon.tconx.in.flags = 0;
75
76         /* Ignore share mode security here */
77         tcon.tconx.in.password = data_blob(NULL, 0);
78         tcon.tconx.in.path = sharename;
79         tcon.tconx.in.device = "?????";
80
81         status = smb_raw_tcon(result, tmp_ctx, &tcon);
82         if (!NT_STATUS_IS_OK(status)) {
83                 talloc_free(tmp_ctx);
84                 return status;
85         }
86
87         result->tid = tcon.tconx.out.tid;
88         *res = talloc_steal(mem_ctx, result);
89         talloc_free(tmp_ctx);
90         return NT_STATUS_OK;
91 }