r4710: added a smb_composite_savefile() function, and expanded the test suite a little
[samba.git] / source4 / torture / raw / composite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libcli composite function testing
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26
27 #define BASEDIR "\\composite"
28
29 /*
30   test a simple savefile/loadfile combination
31 */
32 static BOOL test_loadfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
33 {
34         const char *fname = BASEDIR "\\test.txt";
35         NTSTATUS status;
36         struct smb_composite_savefile io1;
37         struct smb_composite_loadfile io2;
38         char *data;
39         size_t len = random() % 100000;
40
41         data = talloc_array(mem_ctx, uint8_t, len);
42
43         generate_random_buffer(data, len);
44
45         io1.in.fname = fname;
46         io1.in.data  = data;
47         io1.in.size  = len;
48
49         printf("testing savefile\n");
50
51         status = smb_composite_savefile(cli->tree, &io1);
52         if (!NT_STATUS_IS_OK(status)) {
53                 printf("savefile failed: %s\n", nt_errstr(status));
54                 return False;
55         }
56
57         io2.in.fname = fname;
58
59         printf("testing loadfile\n");
60
61         status = smb_composite_loadfile(cli->tree, mem_ctx, &io2);
62         if (!NT_STATUS_IS_OK(status)) {
63                 printf("Loadfile failed: %s\n", nt_errstr(status));
64                 return False;
65         }
66
67         if (io2.out.size != len) {
68                 printf("wrong length in returned data - %d should be %d\n",
69                        io2.out.size, len);
70                 return False;
71         }
72
73         if (memcmp(io2.out.data, data, len) != 0) {
74                 printf("wrong data in loadfile!\n");
75                 return False;
76         }
77
78         talloc_free(data);
79
80         return True;
81 }
82
83 /* 
84    basic testing of libcli composite calls
85 */
86 BOOL torture_raw_composite(void)
87 {
88         struct smbcli_state *cli;
89         BOOL ret = True;
90         TALLOC_CTX *mem_ctx;
91
92         if (!torture_open_connection(&cli)) {
93                 return False;
94         }
95
96         mem_ctx = talloc_init("torture_raw_composite");
97
98         if (!torture_setup_dir(cli, BASEDIR)) {
99                 return False;
100         }
101
102         ret &= test_loadfile(cli, mem_ctx);
103
104         smb_raw_exit(cli->session);
105         smbcli_deltree(cli->tree, BASEDIR);
106
107         torture_close_connection(cli);
108         talloc_destroy(mem_ctx);
109         return ret;
110 }