r4779: demonstrate doing 50 parallel loadfile operations, with a callback for completion
[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 static void loadfile_complete(struct smbcli_composite *c)
30 {
31         int *count = c->async.private;
32         (*count)++;
33 }
34
35 /*
36   test a simple savefile/loadfile combination
37 */
38 static BOOL test_loadfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
39 {
40         const char *fname = BASEDIR "\\test.txt";
41         NTSTATUS status;
42         struct smb_composite_savefile io1;
43         struct smb_composite_loadfile io2;
44         struct smbcli_composite **c;
45         char *data;
46         size_t len = random() % 100000;
47         const int num_ops = 50;
48         int i, count=0;
49
50         data = talloc_array(mem_ctx, uint8_t, len);
51
52         generate_random_buffer(data, len);
53
54         io1.in.fname = fname;
55         io1.in.data  = data;
56         io1.in.size  = len;
57
58         printf("testing savefile\n");
59
60         status = smb_composite_savefile(cli->tree, &io1);
61         if (!NT_STATUS_IS_OK(status)) {
62                 printf("savefile failed: %s\n", nt_errstr(status));
63                 return False;
64         }
65
66         io2.in.fname = fname;
67
68         printf("testing parallel loadfile with %d ops\n", num_ops);
69
70         c = talloc_array(mem_ctx, struct smbcli_composite *, num_ops);
71
72         for (i=0;i<num_ops;i++) {
73                 c[i] = smb_composite_loadfile_send(cli->tree, &io2);
74                 c[i]->async.fn = loadfile_complete;
75                 c[i]->async.private = &count;
76         }
77
78         printf("waiting for completion\n");
79         while (count != num_ops) {
80                 event_loop_once(cli->transport->socket->event.ctx);
81                 printf("count=%d\r", count);
82                 fflush(stdout);
83         }
84         printf("count=%d\n", count);
85         
86         for (i=0;i<num_ops;i++) {
87                 status = smb_composite_loadfile_recv(c[i], mem_ctx);
88                 if (!NT_STATUS_IS_OK(status)) {
89                         printf("loadfile[%d] failed - %s\n", i, nt_errstr(status));
90                         return False;
91                 }
92
93                 if (io2.out.size != len) {
94                         printf("wrong length in returned data - %d should be %d\n",
95                                io2.out.size, len);
96                         return False;
97                 }
98                 
99                 if (memcmp(io2.out.data, data, len) != 0) {
100                         printf("wrong data in loadfile!\n");
101                         return False;
102                 }
103         }
104
105         talloc_free(data);
106
107         return True;
108 }
109
110 /* 
111    basic testing of libcli composite calls
112 */
113 BOOL torture_raw_composite(void)
114 {
115         struct smbcli_state *cli;
116         BOOL ret = True;
117         TALLOC_CTX *mem_ctx;
118
119         if (!torture_open_connection(&cli)) {
120                 return False;
121         }
122
123         mem_ctx = talloc_init("torture_raw_composite");
124
125         if (!torture_setup_dir(cli, BASEDIR)) {
126                 return False;
127         }
128
129         ret &= test_loadfile(cli, mem_ctx);
130
131         smb_raw_exit(cli->session);
132         smbcli_deltree(cli->tree, BASEDIR);
133
134         torture_close_connection(cli);
135         talloc_destroy(mem_ctx);
136         return ret;
137 }